当前位置: 首页>>代码示例>>Golang>>正文


Golang parser.EvalExpr函数代码示例

本文整理汇总了Golang中github.com/cockroachdb/cockroach/sql/parser.EvalExpr函数的典型用法代码示例。如果您正苦于以下问题:Golang EvalExpr函数的具体用法?Golang EvalExpr怎么用?Golang EvalExpr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了EvalExpr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Set

// Set sets session variables.
// Privileges: None.
//   Notes: postgres/mysql do not require privileges for session variables (some exceptions).
func (p *planner) Set(n *parser.Set) (planNode, error) {
	// By using QualifiedName.String() here any variables that are keywords will
	// be double quoted.
	name := strings.ToLower(n.Name.String())
	switch name {
	case `"database"`: // Quoted: database is a reserved word
		if len(n.Values) != 1 {
			return nil, fmt.Errorf("database: requires a single string value")
		}
		val, err := parser.EvalExpr(n.Values[0])
		if err != nil {
			return nil, err
		}

		s, ok := val.(parser.DString)
		if !ok {
			return nil, fmt.Errorf("database: requires a single string value: %s is a %s",
				n.Values[0], val.Type())
		}

		dbName := string(s)
		if len(dbName) != 0 {
			// Verify database descriptor exists.
			if _, err := p.getDatabaseDesc(dbName); err != nil {
				return nil, err
			}
		}
		p.session.Database = dbName

	default:
		return nil, util.Errorf("unknown variable: %s", name)
	}
	return &valuesNode{}, nil
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:37,代码来源:set.go

示例2: Values

// Values constructs a valuesNode from a VALUES expression.
func (p *planner) Values(n parser.Values) (planNode, error) {
	v := &valuesNode{
		rows: make([]parser.DTuple, 0, len(n)),
	}

	nCols := 0
	for _, tuple := range n {
		data, err := parser.EvalExpr(tuple)
		if err != nil {
			return nil, err
		}
		vals, ok := data.(parser.DTuple)
		if !ok {
			return nil, fmt.Errorf("expected a tuple, but found %T", data)
		}
		if len(v.rows) == 0 {
			nCols = len(vals)
		} else if nCols != len(vals) {
			return nil, errors.New("VALUES lists must all be the same length")
		}
		v.rows = append(v.rows, vals)
	}

	v.columns = make([]string, nCols)
	for i := 0; i < nCols; i++ {
		v.columns[i] = fmt.Sprintf("column%d", i+1)
	}
	return v, nil
}
开发者ID:husttom,项目名称:cockroach,代码行数:30,代码来源:values.go

示例3: filterRow

// filterRow checks to see if the current row matches the filter (i.e. the
// where-clause). May set n.err if an error occurs during expression
// evaluation.
func (n *scanNode) filterRow() bool {
	if n.desc != nil {
		for _, col := range n.visibleCols {
			if !col.Nullable {
				break
			}
			if qval, ok := n.qvals[col.ID]; ok && qval.datum == nil {
				qval.datum = parser.DNull
				continue
			}
		}
	}

	if n.filter == nil {
		return true
	}

	var d parser.Datum
	d, n.err = parser.EvalExpr(n.filter)
	if n.err != nil {
		return false
	}

	v, ok := d.(parser.DBool)
	if !ok {
		n.err = fmt.Errorf("WHERE clause did not evaluate to a boolean")
		return false
	}
	return bool(v)
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:33,代码来源:scan.go

示例4: filterRow

// filterRow checks to see if the current row matches the filter (i.e. the
// where-clause). May set n.err if an error occurs during expression
// evaluation.
func (n *scanNode) filterRow() bool {
	if n.desc != nil {
		for _, col := range n.visibleCols {
			if !col.Nullable {
				break
			}
			if qval, ok := n.qvals[col.ID]; ok && qval.datum == nil {
				qval.datum = parser.DNull
				continue
			}
		}
	}

	if n.filter == nil {
		return true
	}

	var d parser.Datum
	d, n.err = parser.EvalExpr(n.filter)
	if n.err != nil {
		return false
	}

	return d != parser.DNull && bool(d.(parser.DBool))
}
开发者ID:harryyeh,项目名称:cockroach,代码行数:28,代码来源:scan.go

示例5: fuzzSingle

func fuzzSingle(stmt parser.Statement) (interestingness int) {
	var lastExpr parser.Expr
	rcvr := func() {
		if r := recover(); r != nil {
			if !expected(fmt.Sprintf("%v", r)) {
				fmt.Printf("Stmt: %s\n%s", stmt, spew.Sdump(stmt))
				if lastExpr != nil {
					fmt.Printf("Expr: %s", spew.Sdump(lastExpr))
				}
				panic(r)
			}
			// Anything that has expected errors in it is fine, but not as
			// interesting as things that go through.
			interestingness = 1
		}
	}
	defer rcvr()

	data0 := stmt.String()
	// TODO(tschottdorf): again, this is since we're ignoring stuff in the
	// grammar instead of erroring out on unsupported language. See:
	// https://github.com/cockroachdb/cockroach/issues/1949
	if strings.Contains(data0, "%!s(<nil>)") {
		return 0
	}
	stmt1, err := parser.Parse(data0)
	if err != nil {
		fmt.Printf("AST: %s", spew.Sdump(stmt))
		fmt.Printf("data0: %q\n", data0)
		panic(err)
	}
	interestingness = 2

	data1 := stmt1.String()
	// TODO(tschottdorf): due to the ignoring issue again.
	// if !reflect.DeepEqual(stmt, stmt1) {
	if data1 != data0 {
		fmt.Printf("data0: %q\n", data0)
		fmt.Printf("AST: %s", spew.Sdump(stmt))
		fmt.Printf("data1: %q\n", data1)
		fmt.Printf("AST: %s", spew.Sdump(stmt1))
		panic("not equal")
	}

	var v visitorFunc = func(e parser.Expr) parser.Expr {
		lastExpr = e
		if _, err := parser.EvalExpr(e); err != nil {
			panic(err)
		}
		return e
	}
	parser.WalkStmt(v, stmt)
	return
}
开发者ID:cockroachdb,项目名称:go-fuzz,代码行数:54,代码来源:main.go

示例6: initWhere

func (n *scanNode) initWhere(where *parser.Where) error {
	if where == nil {
		return nil
	}
	n.filter, n.err = n.resolveQNames(where.Expr)
	if n.err == nil {
		// Evaluate the expression once to memoize operators and functions.
		_, n.err = parser.EvalExpr(n.filter)
	}
	return n.err
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:11,代码来源:scan.go

示例7: checkEquivExpr

func checkEquivExpr(a, b parser.Expr, qvals qvalMap) error {
	// The expressions above only use the values 1 and 2. Verify that the
	// simplified expressions evaluate to the same value as the original
	// expression for interesting values.
	for _, v := range []parser.DInt{0, 1, 2, 3} {
		for _, q := range qvals {
			q.datum = v
		}
		da, err := parser.EvalExpr(a)
		if err != nil {
			return fmt.Errorf("%s: %v", a, err)
		}
		db, err := parser.EvalExpr(b)
		if err != nil {
			return fmt.Errorf("%s: %v", b, err)
		}
		if da != db {
			return fmt.Errorf("%s: %d: expected %s, but found %s", a, v, da, db)
		}
	}
	return nil
}
开发者ID:harryyeh,项目名称:cockroach,代码行数:22,代码来源:analyze_test.go

示例8: renderRow

func (n *scanNode) renderRow() error {
	if n.row == nil {
		n.row = make([]parser.Datum, len(n.render))
	}
	for i, e := range n.render {
		var err error
		n.row[i], err = parser.EvalExpr(e, n.vals)
		if err != nil {
			return err
		}
	}
	return nil
}
开发者ID:routhcr,项目名称:cockroach,代码行数:13,代码来源:scan.go

示例9: shouldOutputRow

func shouldOutputRow(where *parser.Where, vals valMap) (bool, error) {
	if where == nil {
		return true, nil
	}
	d, err := parser.EvalExpr(where.Expr, vals)
	if err != nil {
		return false, err
	}
	v, ok := d.(parser.DBool)
	if !ok {
		return false, fmt.Errorf("WHERE clause did not evaluate to a boolean")
	}
	return bool(v), nil
}
开发者ID:zmoon111,项目名称:cockroach,代码行数:14,代码来源:server.go

示例10: filterRow

func (n *scanNode) filterRow() (bool, error) {
	if n.filter == nil {
		return true, nil
	}
	d, err := parser.EvalExpr(n.filter, n.vals)
	if err != nil {
		return false, err
	}
	v, ok := d.(parser.DBool)
	if !ok {
		return false, fmt.Errorf("WHERE clause did not evaluate to a boolean")
	}
	return bool(v), nil
}
开发者ID:elkingtoncode,项目名称:LampDB,代码行数:14,代码来源:scan.go

示例11: limit

// limit constructs a limitNode based on the LIMIT and OFFSET clauses.
func (*planner) limit(n *parser.Select, p planNode) (planNode, error) {
	if n.Limit == nil {
		return p, nil
	}

	var count, offset int64

	data := []struct {
		name       string
		src        parser.Expr
		dst        *int64
		defaultVal int64
	}{
		{"LIMIT", n.Limit.Count, &count, math.MaxInt64},
		{"OFFSET", n.Limit.Offset, &offset, 0},
	}

	for _, datum := range data {
		if datum.src == nil {
			*datum.dst = datum.defaultVal
		} else {
			if parser.ContainsVars(datum.src) {
				return nil, fmt.Errorf("argument of %s must not contain variables", datum.name)
			}

			normalized, err := parser.NormalizeExpr(datum.src)
			if err != nil {
				return nil, err
			}
			dstDatum, err := parser.EvalExpr(normalized)
			if err != nil {
				return nil, err
			}

			if dstDatum == parser.DNull {
				*datum.dst = datum.defaultVal
				continue
			}

			if dstDInt, ok := dstDatum.(parser.DInt); ok {
				*datum.dst = int64(dstDInt)
				continue
			}

			return nil, fmt.Errorf("argument of %s must be type %s, not type %s", datum.name, parser.DummyInt.Type(), dstDatum.Type())
		}
	}

	return &limitNode{planNode: p, count: count, offset: offset}, nil
}
开发者ID:husttom,项目名称:cockroach,代码行数:51,代码来源:limit.go

示例12: getStringVal

func getStringVal(name string, values parser.Exprs) (string, error) {
	if len(values) != 1 {
		return "", fmt.Errorf("%s: requires a single string value", name)
	}
	val, err := parser.EvalExpr(values[0])
	if err != nil {
		return "", err
	}
	s, ok := val.(parser.DString)
	if !ok {
		return "", fmt.Errorf("%s: requires a single string value: %s is a %s",
			name, values[0], val.Type())
	}
	return string(s), nil
}
开发者ID:husttom,项目名称:cockroach,代码行数:15,代码来源:set.go

示例13: renderRow

// renderRow renders the row by evaluating the render expressions. May set
// n.err if an error occurs during expression evaluation.
func (n *scanNode) renderRow() {
	if n.explain == explainDebug {
		n.explainDebug(true, true)
		return
	}

	if n.row == nil {
		n.row = make([]parser.Datum, len(n.render))
	}
	for i, e := range n.render {
		n.row[i], n.err = parser.EvalExpr(e)
		if n.err != nil {
			return
		}
	}
	n.rowIndex++
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:19,代码来源:scan.go

示例14: Values

// Values constructs a valuesNode from a VALUES expression.
func (p *planner) Values(n parser.Values) (planNode, error) {
	v := &valuesNode{
		rows: make([]parser.DTuple, 0, len(n)),
	}
	for _, tuple := range n {
		data, err := parser.EvalExpr(tuple, nil)
		if err != nil {
			return nil, err
		}
		vals, ok := data.(parser.DTuple)
		if !ok {
			return nil, fmt.Errorf("expected a tuple, but found %T", data)
		}
		v.rows = append(v.rows, vals)
	}
	return v, nil
}
开发者ID:routhcr,项目名称:cockroach,代码行数:18,代码来源:values.go

示例15: Set

// Set sets session variables.
func (s *Server) Set(session *Session, p *parser.Set, args []sqlwire.Datum,
	resp *sqlwire.Response) error {
	// By using QualifiedName.String() here any variables that are keywords will
	// be double quoted.
	name := strings.ToLower(p.Name.String())
	switch name {
	case `"database"`: // Quoted: database is a reserved word
		if len(p.Values) != 1 {
			return fmt.Errorf("database: requires a single string value")
		}
		val, err := parser.EvalExpr(p.Values[0], nil)
		if err != nil {
			return err
		}
		session.Database = val.String()
	default:
		return util.Errorf("unknown variable: %s", name)
	}
	return nil
}
开发者ID:zmoon111,项目名称:cockroach,代码行数:21,代码来源:server.go


注:本文中的github.com/cockroachdb/cockroach/sql/parser.EvalExpr函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。