本文整理汇总了Golang中github.com/cockroachdb/cockroach/sql/parser.EvalExpr函数的典型用法代码示例。如果您正苦于以下问题:Golang EvalExpr函数的具体用法?Golang EvalExpr怎么用?Golang EvalExpr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EvalExpr函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
示例16: processSelect
func (s *Server) processSelect(node parser.SelectStatement) (rows []sqlwire.Result_Row, _ error) {
switch nt := node.(type) {
// case *parser.Select:
// case *parser.Union:
// TODO(vivek): return s.query(nt.stmt, nil)
case parser.Values:
for _, tuple := range nt {
data, err := parser.EvalExpr(tuple, nil)
if err != nil {
return rows, err
}
dTuple, ok := data.(parser.DTuple)
if !ok {
// A one-element DTuple is currently turned into whatever its
// underlying element is, so we have to massage here.
// See #1741.
dTuple = parser.DTuple([]parser.Datum{data})
}
var vals []sqlwire.Datum
for _, val := range dTuple {
switch vt := val.(type) {
case parser.DBool:
vals = append(vals, sqlwire.Datum{BoolVal: (*bool)(&vt)})
case parser.DInt:
vals = append(vals, sqlwire.Datum{IntVal: (*int64)(&vt)})
case parser.DFloat:
vals = append(vals, sqlwire.Datum{FloatVal: (*float64)(&vt)})
case parser.DString:
vals = append(vals, sqlwire.Datum{StringVal: (*string)(&vt)})
case parser.DNull:
vals = append(vals, sqlwire.Datum{})
default:
return rows, util.Errorf("unsupported node: %T", val)
}
}
rows = append(rows, sqlwire.Result_Row{Values: vals})
}
return rows, nil
}
return nil, util.Errorf("TODO(pmattis): unsupported node: %T", node)
}
开发者ID:zmoon111,项目名称:cockroach,代码行数:41,代码来源:server.go
示例17: Next
func (n *groupNode) Next() bool {
if !n.needGroup {
return false
}
n.needGroup = false
// Loop over the rows passing the values into the corresponding aggregation
// functions.
for n.plan.Next() {
values := n.plan.Values()
for i, f := range n.funcs {
if n.err = f.impl.Add(values[i]); n.err != nil {
return false
}
}
}
n.err = n.plan.Err()
if n.err != nil {
return false
}
// Fill in the aggregate function result value.
for _, f := range n.funcs {
if f.val.datum, n.err = f.impl.Result(); n.err != nil {
return false
}
}
// Render the results.
n.row = make([]parser.Datum, len(n.render))
for i, r := range n.render {
n.row[i], n.err = parser.EvalExpr(r)
if n.err != nil {
return false
}
}
return n.err == nil
}
开发者ID:husttom,项目名称:cockroach,代码行数:40,代码来源:group.go
示例18: processSelect
func (s *Server) processSelect(node parser.SelectStatement) (rows []sqlwire.Result_Row, _ error) {
switch nt := node.(type) {
// case *parser.Select:
// case *parser.Union:
// TODO(vivek): return s.query(nt.stmt, nil)
case parser.Values:
for _, tuple := range nt {
data, err := parser.EvalExpr(tuple, nil)
if err != nil {
return rows, err
}
dTuple, ok := data.(parser.DTuple)
if !ok {
return nil, fmt.Errorf("expected a tuple, but found %T", data)
}
var vals []sqlwire.Datum
for _, val := range dTuple {
switch vt := val.(type) {
case parser.DBool:
vals = append(vals, sqlwire.Datum{BoolVal: (*bool)(&vt)})
case parser.DInt:
vals = append(vals, sqlwire.Datum{IntVal: (*int64)(&vt)})
case parser.DFloat:
vals = append(vals, sqlwire.Datum{FloatVal: (*float64)(&vt)})
case parser.DString:
vals = append(vals, sqlwire.Datum{StringVal: (*string)(&vt)})
case parser.DNull:
vals = append(vals, sqlwire.Datum{})
default:
return rows, util.Errorf("unsupported node: %T", val)
}
}
rows = append(rows, sqlwire.Result_Row{Values: vals})
}
return rows, nil
}
return nil, util.Errorf("TODO(pmattis): unsupported node: %T", node)
}
开发者ID:zhengchen1208,项目名称:cockroach,代码行数:38,代码来源:server.go
示例19: renderRow
func (n *scanNode) renderRow() error {
if n.row == nil {
n.row = make([]parser.Datum, len(n.render))
}
if n.desc != nil {
for _, col := range n.desc.Columns {
if _, ok := n.vals[col.Name]; ok {
continue
}
if col.Nullable {
n.vals[col.Name] = parser.DNull
}
}
}
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:elkingtoncode,项目名称:LampDB,代码行数:23,代码来源:scan.go
示例20: Insert
// Insert inserts rows into the database.
// Privileges: INSERT on table
// Notes: postgres requires INSERT. No "on duplicate key update" option.
// mysql requires INSERT. Also requires UPDATE on "ON DUPLICATE KEY UPDATE".
func (p *planner) Insert(n *parser.Insert) (planNode, error) {
tableDesc, err := p.getTableDesc(n.Table)
if err != nil {
return nil, err
}
if err := p.checkPrivilege(tableDesc, privilege.INSERT); err != nil {
return nil, err
}
// Determine which columns we're inserting into.
cols, err := p.processColumns(tableDesc, n.Columns)
if err != nil {
return nil, err
}
// Construct a map from column ID to the index the value appears at within a
// row.
colIDtoRowIndex := map[ColumnID]int{}
for i, c := range cols {
colIDtoRowIndex[c.ID] = i
}
// Add any column not already present that has a DEFAULT expression.
for _, col := range tableDesc.Columns {
if _, ok := colIDtoRowIndex[col.ID]; ok {
continue
}
if col.DefaultExpr != nil {
colIDtoRowIndex[col.ID] = len(cols)
cols = append(cols, col)
}
}
// Verify we have at least the columns that are part of the primary key.
primaryKeyCols := map[ColumnID]struct{}{}
for i, id := range tableDesc.PrimaryIndex.ColumnIDs {
if _, ok := colIDtoRowIndex[id]; !ok {
return nil, fmt.Errorf("missing %q primary key column", tableDesc.PrimaryIndex.ColumnNames[i])
}
primaryKeyCols[id] = struct{}{}
}
// Construct the default expressions. The returned slice will be nil if no
// column in the table has a default expression.
defaultExprs, err := makeDefaultExprs(cols)
if err != nil {
return nil, err
}
// Replace any DEFAULT markers with the corresponding default expressions.
if n.Rows, err = p.fillDefaults(defaultExprs, cols, n.Rows); err != nil {
return nil, err
}
// Transform the values into a rows object. This expands SELECT statements or
// generates rows from the values contained within the query.
rows, err := p.makePlan(n.Rows)
if err != nil {
return nil, err
}
primaryIndex := tableDesc.PrimaryIndex
primaryIndexKeyPrefix := MakeIndexKeyPrefix(tableDesc.ID, primaryIndex.ID)
var b client.Batch
for rows.Next() {
rowVals := rows.Values()
// The values for the row may be shorter than the number of columns being
// inserted into. Generate default values for those columns using the
// default expressions.
for i := len(rowVals); i < len(cols); i++ {
if defaultExprs == nil {
rowVals = append(rowVals, parser.DNull)
continue
}
d, err := parser.EvalExpr(defaultExprs[i])
if err != nil {
return nil, err
}
rowVals = append(rowVals, d)
}
// Check to see if NULL is being inserted into any non-nullable column.
for _, col := range tableDesc.Columns {
if !col.Nullable {
if i, ok := colIDtoRowIndex[col.ID]; !ok || rowVals[i] == parser.DNull {
return nil, fmt.Errorf("null value in column %q violates not-null constraint", col.Name)
}
}
}
primaryIndexKey, _, err := encodeIndexKey(
primaryIndex.ColumnIDs, colIDtoRowIndex, rowVals, primaryIndexKeyPrefix)
if err != nil {
//.........这里部分代码省略.........
开发者ID:harryyeh,项目名称:cockroach,代码行数:101,代码来源:insert.go
注:本文中的github.com/cockroachdb/cockroach/sql/parser.EvalExpr函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论