本文整理汇总了Golang中github.com/pingcap/tidb/expression.NewFunction函数的典型用法代码示例。如果您正苦于以下问题:Golang NewFunction函数的具体用法?Golang NewFunction怎么用?Golang NewFunction使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewFunction函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: betweenToScalarFunc
func (er *expressionRewriter) betweenToScalarFunc(v *ast.BetweenExpr) {
stkLen := len(er.ctxStack)
var op string
var l, r *expression.ScalarFunction
if v.Not {
l, er.err = expression.NewFunction(ast.LT, v.Type, er.ctxStack[stkLen-3], er.ctxStack[stkLen-2])
if er.err == nil {
r, er.err = expression.NewFunction(ast.GT, v.Type, er.ctxStack[stkLen-3].DeepCopy(), er.ctxStack[stkLen-1])
}
op = ast.OrOr
} else {
l, er.err = expression.NewFunction(ast.GE, v.Type, er.ctxStack[stkLen-3], er.ctxStack[stkLen-2])
if er.err == nil {
r, er.err = expression.NewFunction(ast.LE, v.Type, er.ctxStack[stkLen-3].DeepCopy(), er.ctxStack[stkLen-1])
}
op = ast.AndAnd
}
if er.err != nil {
er.err = errors.Trace(er.err)
return
}
function, err := expression.NewFunction(op, v.Type, l, r)
if err != nil {
er.err = errors.Trace(err)
return
}
er.ctxStack = er.ctxStack[:stkLen-3]
er.ctxStack = append(er.ctxStack, function)
}
开发者ID:yuyongwei,项目名称:tidb,代码行数:29,代码来源:expression_rewriter.go
示例2: betweenToExpression
func (er *expressionRewriter) betweenToExpression(v *ast.BetweenExpr) {
stkLen := len(er.ctxStack)
er.checkArgsOneColumn(er.ctxStack[stkLen-3:]...)
if er.err != nil {
return
}
var op string
var l, r expression.Expression
l, er.err = expression.NewFunction(ast.GE, &v.Type, er.ctxStack[stkLen-3], er.ctxStack[stkLen-2])
if er.err == nil {
r, er.err = expression.NewFunction(ast.LE, &v.Type, er.ctxStack[stkLen-3].Clone(), er.ctxStack[stkLen-1])
}
op = ast.AndAnd
if er.err != nil {
er.err = errors.Trace(er.err)
return
}
function, err := expression.NewFunction(op, &v.Type, l, r)
if err != nil {
er.err = errors.Trace(err)
return
}
if v.Not {
function, err = expression.NewFunction(ast.UnaryNot, &v.Type, function)
if err != nil {
er.err = errors.Trace(err)
return
}
}
er.ctxStack = er.ctxStack[:stkLen-3]
er.ctxStack = append(er.ctxStack, function)
}
开发者ID:pingcap,项目名称:tidb,代码行数:32,代码来源:expression_rewriter.go
示例3: notToScalarFunc
func (er *expressionRewriter) notToScalarFunc(b bool, op string, tp *types.FieldType,
args ...expression.Expression) *expression.ScalarFunction {
opFunc := expression.NewFunction(op, tp, args...)
if !b {
return opFunc
}
return expression.NewFunction(ast.UnaryNot, tp, opFunc)
}
开发者ID:duzhanyuan,项目名称:tidb,代码行数:8,代码来源:expression_rewriter.go
示例4: handleScalarSubquery
func (er *expressionRewriter) handleScalarSubquery(v *ast.SubqueryExpr) (ast.Node, bool) {
np := er.buildSubquery(v)
if er.err != nil {
return v, true
}
np = er.b.buildMaxOneRow(np)
if np.IsCorrelated() {
er.p = er.b.buildApply(er.p, np, nil)
if len(np.GetSchema()) > 1 {
newCols := make([]expression.Expression, 0, len(np.GetSchema()))
for _, col := range np.GetSchema() {
newCols = append(newCols, col.Clone())
}
expr, err := expression.NewFunction(ast.RowFunc, nil, newCols...)
if err != nil {
er.err = errors.Trace(err)
return v, true
}
er.ctxStack = append(er.ctxStack, expr)
} else {
er.ctxStack = append(er.ctxStack, er.p.GetSchema()[len(er.p.GetSchema())-1])
}
return v, true
}
physicalPlan, err := doOptimize(np, er.b.ctx, er.b.allocator)
if err != nil {
er.err = errors.Trace(err)
return v, true
}
d, err := EvalSubquery(physicalPlan, er.b.is, er.b.ctx)
if err != nil {
er.err = errors.Trace(err)
return v, true
}
if len(np.GetSchema()) > 1 {
newCols := make([]expression.Expression, 0, len(np.GetSchema()))
for i, data := range d {
newCols = append(newCols, &expression.Constant{
Value: data,
RetType: np.GetSchema()[i].GetType()})
}
expr, err1 := expression.NewFunction(ast.RowFunc, nil, newCols...)
if err1 != nil {
er.err = errors.Trace(err1)
return v, true
}
er.ctxStack = append(er.ctxStack, expr)
} else {
er.ctxStack = append(er.ctxStack, &expression.Constant{
Value: d[0],
RetType: np.GetSchema()[0].GetType(),
})
}
return v, true
}
开发者ID:pingcap,项目名称:tidb,代码行数:55,代码来源:expression_rewriter.go
示例5: caseToExpression
func (er *expressionRewriter) caseToExpression(v *ast.CaseExpr) {
stkLen := len(er.ctxStack)
argsLen := 2 * len(v.WhenClauses)
if v.ElseClause != nil {
argsLen++
}
er.checkArgsOneColumn(er.ctxStack[stkLen-argsLen:]...)
if er.err != nil {
return
}
// value -> ctxStack[stkLen-argsLen-1]
// when clause(condition, result) -> ctxStack[stkLen-argsLen:stkLen-1];
// else clause -> ctxStack[stkLen-1]
var args []expression.Expression
if v.Value != nil {
// args: eq scalar func(args: value, condition1), result1,
// eq scalar func(args: value, condition2), result2,
// ...
// else clause
value := er.ctxStack[stkLen-argsLen-1]
args = make([]expression.Expression, 0, argsLen)
for i := stkLen - argsLen; i < stkLen-1; i += 2 {
arg, err := expression.NewFunction(ast.EQ, types.NewFieldType(mysql.TypeTiny), value.Clone(), er.ctxStack[i])
if err != nil {
er.err = errors.Trace(err)
return
}
args = append(args, arg)
args = append(args, er.ctxStack[i+1])
}
if v.ElseClause != nil {
args = append(args, er.ctxStack[stkLen-1])
}
argsLen++ // for trimming the value element later
} else {
// args: condition1, result1,
// condition2, result2,
// ...
// else clause
args = er.ctxStack[stkLen-argsLen:]
}
function, err := expression.NewFunction(ast.Case, &v.Type, args...)
if err != nil {
er.err = errors.Trace(err)
return
}
er.ctxStack = er.ctxStack[:stkLen-argsLen]
er.ctxStack = append(er.ctxStack, function)
}
开发者ID:pingcap,项目名称:tidb,代码行数:50,代码来源:expression_rewriter.go
示例6: funcCallToScalarFunc
func (er *expressionRewriter) funcCallToScalarFunc(v *ast.FuncCallExpr) {
stackLen := len(er.ctxStack)
var function *expression.ScalarFunction
function, er.err = expression.NewFunction(v.FnName.L, v.Type, er.ctxStack[stackLen-len(v.Args):]...)
er.ctxStack = er.ctxStack[:stackLen-len(v.Args)]
er.ctxStack = append(er.ctxStack, function)
}
开发者ID:yuyongwei,项目名称:tidb,代码行数:7,代码来源:expression_rewriter.go
示例7: binaryOpToExpression
func (er *expressionRewriter) binaryOpToExpression(v *ast.BinaryOperationExpr) {
stkLen := len(er.ctxStack)
var function expression.Expression
switch v.Op {
case opcode.EQ, opcode.NE, opcode.NullEQ:
function, er.err = er.constructBinaryOpFunction(er.ctxStack[stkLen-2], er.ctxStack[stkLen-1],
opcode.Ops[v.Op])
default:
lLen := getRowLen(er.ctxStack[stkLen-2])
rLen := getRowLen(er.ctxStack[stkLen-1])
switch v.Op {
case opcode.GT, opcode.GE, opcode.LT, opcode.LE:
if lLen != rLen {
er.err = ErrOperandColumns.GenByArgs(lLen)
}
default:
if lLen != 1 || rLen != 1 {
er.err = ErrOperandColumns.GenByArgs(1)
}
}
if er.err != nil {
return
}
function, er.err = expression.NewFunction(opcode.Ops[v.Op], &v.Type, er.ctxStack[stkLen-2:]...)
}
if er.err != nil {
er.err = errors.Trace(er.err)
return
}
er.ctxStack = er.ctxStack[:stkLen-2]
er.ctxStack = append(er.ctxStack, function)
}
开发者ID:pingcap,项目名称:tidb,代码行数:32,代码来源:expression_rewriter.go
示例8: pushDownNot
func pushDownNot(expr expression.Expression, not bool) expression.Expression {
if f, ok := expr.(*expression.ScalarFunction); ok {
switch f.FuncName.L {
case ast.UnaryNot:
return pushDownNot(f.Args[0], !not)
case ast.LT, ast.GE, ast.GT, ast.LE, ast.EQ, ast.NE:
if not {
nf, _ := expression.NewFunction(oppositeOp[f.FuncName.L], f.GetType(), f.Args...)
return nf
}
for i, arg := range f.Args {
f.Args[i] = pushDownNot(arg, false)
}
return f
case ast.AndAnd:
if not {
args := f.Args
for i, a := range args {
args[i] = pushDownNot(a, true)
}
nf, _ := expression.NewFunction(ast.OrOr, f.GetType(), args...)
return nf
}
for i, arg := range f.Args {
f.Args[i] = pushDownNot(arg, false)
}
return f
case ast.OrOr:
if not {
args := f.Args
for i, a := range args {
args[i] = pushDownNot(a, true)
}
nf, _ := expression.NewFunction(ast.AndAnd, f.GetType(), args...)
return nf
}
for i, arg := range f.Args {
f.Args[i] = pushDownNot(arg, false)
}
return f
}
}
if not {
expr, _ = expression.NewFunction(ast.UnaryNot, types.NewFieldType(mysql.TypeTiny), expr)
}
return expr
}
开发者ID:yangxuanjia,项目名称:tidb,代码行数:47,代码来源:refiner.go
示例9: betweenToScalarFunc
func (er *expressionRewriter) betweenToScalarFunc(v *ast.BetweenExpr) {
stkLen := len(er.ctxStack)
var op string
var l, r *expression.ScalarFunction
if v.Not {
l = expression.NewFunction(ast.LT, v.Type, er.ctxStack[stkLen-3], er.ctxStack[stkLen-2])
r = expression.NewFunction(ast.GT, v.Type, er.ctxStack[stkLen-3], er.ctxStack[stkLen-1])
op = ast.OrOr
} else {
l = expression.NewFunction(ast.GE, v.Type, er.ctxStack[stkLen-3], er.ctxStack[stkLen-2])
r = expression.NewFunction(ast.LE, v.Type, er.ctxStack[stkLen-3], er.ctxStack[stkLen-1])
op = ast.AndAnd
}
function := expression.NewFunction(op, v.Type, l, r)
er.ctxStack = er.ctxStack[:stkLen-3]
er.ctxStack = append(er.ctxStack, function)
}
开发者ID:duzhanyuan,项目名称:tidb,代码行数:17,代码来源:expression_rewriter.go
示例10: notToScalarFunc
func (er *expressionRewriter) notToScalarFunc(hasNot bool, op string, tp *types.FieldType,
args ...expression.Expression) *expression.ScalarFunction {
opFunc, err := expression.NewFunction(op, tp, args...)
if err != nil {
er.err = errors.Trace(err)
return nil
}
if !hasNot {
return opFunc
}
opFunc, err = expression.NewFunction(ast.UnaryNot, tp, opFunc)
if err != nil {
er.err = errors.Trace(err)
return nil
}
return opFunc
}
开发者ID:yuyongwei,项目名称:tidb,代码行数:18,代码来源:expression_rewriter.go
示例11: funcCallToExpression
func (er *expressionRewriter) funcCallToExpression(v *ast.FuncCallExpr) {
stackLen := len(er.ctxStack)
args := er.ctxStack[stackLen-len(v.Args):]
er.checkArgsOneColumn(args...)
if er.err != nil {
return
}
var function expression.Expression
function, er.err = expression.NewFunction(v.FnName.L, &v.Type, args...)
er.ctxStack = er.ctxStack[:stackLen-len(v.Args)]
er.ctxStack = append(er.ctxStack, function)
}
开发者ID:pingcap,项目名称:tidb,代码行数:12,代码来源:expression_rewriter.go
示例12: composeCondition
// compose CNF items into a balance deep CNF tree, which benefits a lot for pb decoder/encoder.
func composeCondition(conditions []expression.Expression) expression.Expression {
length := len(conditions)
if length == 0 {
return nil
}
if length == 1 {
return conditions[0]
}
return expression.NewFunction(model.NewCIStr(ast.AndAnd),
[]expression.Expression{composeCondition(conditions[0 : length/2]),
composeCondition(conditions[length/2:])})
}
开发者ID:tangfeixiong,项目名称:tidb,代码行数:13,代码来源:new_builder.go
示例13: handleInSubquery
func (er *expressionRewriter) handleInSubquery(v *ast.PatternInExpr) (ast.Node, bool) {
v.Expr.Accept(er)
if er.err != nil {
return v, true
}
lexpr := er.ctxStack[len(er.ctxStack)-1]
subq, ok := v.Sel.(*ast.SubqueryExpr)
if !ok {
er.err = errors.Errorf("Unknown compare type %T.", v.Sel)
return v, true
}
np, outerSchema := er.buildSubquery(subq)
if er.err != nil {
return v, true
}
if getRowLen(lexpr) != len(np.GetSchema()) {
er.err = errors.Errorf("Operand should contain %d column(s)", getRowLen(lexpr))
return v, true
}
var rexpr expression.Expression
if len(np.GetSchema()) == 1 {
rexpr = np.GetSchema()[0].DeepCopy()
} else {
args := make([]expression.Expression, 0, len(np.GetSchema()))
for _, col := range np.GetSchema() {
args = append(args, col.DeepCopy())
}
rexpr, er.err = expression.NewFunction(ast.RowFunc, nil, args...)
if er.err != nil {
er.err = errors.Trace(er.err)
return v, true
}
}
// a in (subq) will be rewrited as a = any(subq).
// a not in (subq) will be rewrited as a != all(subq).
op, all := ast.EQ, false
if v.Not {
op, all = ast.NE, true
}
checkCondition, err := constructBinaryOpFunction(lexpr, rexpr, op)
if err != nil {
er.err = errors.Trace(err)
return v, true
}
er.p = er.b.buildApply(er.p, np, outerSchema, &ApplyConditionChecker{Condition: checkCondition, All: all})
if er.p.IsCorrelated() {
er.correlated = true
}
// The parent expression only use the last column in schema, which represents whether the condition is matched.
er.ctxStack[len(er.ctxStack)-1] = er.p.GetSchema()[len(er.p.GetSchema())-1]
return v, true
}
开发者ID:yubobo,项目名称:tidb,代码行数:53,代码来源:expression_rewriter.go
示例14: rowToScalarFunc
func (er *expressionRewriter) rowToScalarFunc(v *ast.RowExpr) {
stkLen := len(er.ctxStack)
length := len(v.Values)
rows := make([]expression.Expression, 0, length)
for i := stkLen - length; i < stkLen; i++ {
rows = append(rows, er.ctxStack[i])
}
er.ctxStack = er.ctxStack[:stkLen-length]
function, err := expression.NewFunction(ast.RowFunc, nil, rows...)
if err != nil {
er.err = errors.Trace(err)
return
}
er.ctxStack = append(er.ctxStack, function)
}
开发者ID:yuyongwei,项目名称:tidb,代码行数:15,代码来源:expression_rewriter.go
示例15: constructBinaryOpFunction
// constructBinaryOpFunctions converts (a0,a1,a2) op (b0,b1,b2) to (a0 op b0) and (a1 op b1) and (a2 op b2).
func constructBinaryOpFunction(l expression.Expression, r expression.Expression, op string) (expression.Expression, error) {
lLen, rLen := getRowLen(l), getRowLen(r)
if lLen == 1 && rLen == 1 {
return expression.NewFunction(op, types.NewFieldType(mysql.TypeTiny), l, r)
} else if rLen != lLen {
return nil, errors.Errorf("Operand should contain %d column(s)", lLen)
}
funcs := make([]expression.Expression, lLen)
for i := 0; i < lLen; i++ {
var err error
funcs[i], err = constructBinaryOpFunction(getRowArg(l, i), getRowArg(r, i), op)
if err != nil {
return nil, err
}
}
return expression.ComposeCNFCondition(funcs), nil
}
开发者ID:yuyongwei,项目名称:tidb,代码行数:18,代码来源:expression_rewriter.go
示例16: constructBinaryOpFunction
// constructBinaryOpFunctions converts (a0,a1,a2) op (b0,b1,b2) to (a0 op b0) and (a1 op b1) and (a2 op b2).
func (er *expressionRewriter) constructBinaryOpFunction(l expression.Expression, r expression.Expression, op string) (expression.Expression, error) {
lLen, rLen := getRowLen(l), getRowLen(r)
if lLen == 1 && rLen == 1 {
return expression.NewFunction(op, types.NewFieldType(mysql.TypeTiny), l, r)
} else if rLen != lLen {
return nil, ErrOperandColumns.GenByArgs(lLen)
}
funcs := make([]expression.Expression, lLen)
for i := 0; i < lLen; i++ {
var err error
funcs[i], err = er.constructBinaryOpFunction(getRowArg(l, i), getRowArg(r, i), op)
if err != nil {
return nil, errors.Trace(err)
}
}
return expression.ComposeCNFCondition(funcs), nil
}
开发者ID:pingcap,项目名称:tidb,代码行数:18,代码来源:expression_rewriter.go
示例17: constantSubstitute
// constantSubstitute substitute column expression in a condition by an equivalent constant.
func constantSubstitute(equalities map[string]*expression.Constant, condition expression.Expression) expression.Expression {
switch expr := condition.(type) {
case *expression.Column:
if v, ok := equalities[string(expr.HashCode())]; ok {
return v
}
case *expression.ScalarFunction:
for i, arg := range expr.Args {
expr.Args[i] = constantSubstitute(equalities, arg)
}
if _, ok := evaluator.Funcs[expr.FuncName.L]; ok {
condition, _ = expression.NewFunction(expr.FuncName.L, expr.RetType, expr.Args...)
}
return condition
}
return condition
}
开发者ID:yangxuanjia,项目名称:tidb,代码行数:18,代码来源:predicate_push_down.go
示例18: binaryOpToScalarFunc
func (er *expressionRewriter) binaryOpToScalarFunc(v *ast.BinaryOperationExpr) {
stkLen := len(er.ctxStack)
var function expression.Expression
switch v.Op {
case opcode.EQ, opcode.NE, opcode.NullEQ:
function, er.err = constructBinaryOpFunction(er.ctxStack[stkLen-2], er.ctxStack[stkLen-1],
opcode.Ops[v.Op])
default:
function, er.err = expression.NewFunction(opcode.Ops[v.Op], v.Type, er.ctxStack[stkLen-2:]...)
}
if er.err != nil {
er.err = errors.Trace(er.err)
return
}
er.ctxStack = er.ctxStack[:stkLen-2]
er.ctxStack = append(er.ctxStack, function)
}
开发者ID:yuyongwei,项目名称:tidb,代码行数:17,代码来源:expression_rewriter.go
示例19: unaryOpToScalarFunc
func (er *expressionRewriter) unaryOpToScalarFunc(v *ast.UnaryOperationExpr) {
stkLen := len(er.ctxStack)
if getRowLen(er.ctxStack[stkLen-1]) != 1 {
er.err = errors.New("Operand should contain 1 column(s)")
}
var op string
switch v.Op {
case opcode.Plus:
op = ast.UnaryPlus
case opcode.Minus:
op = ast.UnaryMinus
case opcode.BitNeg:
op = ast.BitNeg
case opcode.Not:
op = ast.UnaryNot
default:
er.err = errors.Errorf("Unknown Unary Op %T", v.Op)
return
}
er.ctxStack[stkLen-1], er.err = expression.NewFunction(op, v.Type, er.ctxStack[stkLen-1])
}
开发者ID:yuyongwei,项目名称:tidb,代码行数:21,代码来源:expression_rewriter.go
示例20: extractOnCondition
func extractOnCondition(conditions []expression.Expression, left LogicalPlan, right LogicalPlan) (
eqCond []*expression.ScalarFunction, leftCond []expression.Expression, rightCond []expression.Expression,
otherCond []expression.Expression) {
for _, expr := range conditions {
binop, ok := expr.(*expression.ScalarFunction)
if ok && binop.FuncName.L == ast.EQ {
ln, lOK := binop.Args[0].(*expression.Column)
rn, rOK := binop.Args[1].(*expression.Column)
if lOK && rOK {
if left.GetSchema().GetIndex(ln) != -1 && right.GetSchema().GetIndex(rn) != -1 {
eqCond = append(eqCond, binop)
continue
}
if left.GetSchema().GetIndex(rn) != -1 && right.GetSchema().GetIndex(ln) != -1 {
cond, _ := expression.NewFunction(ast.EQ, types.NewFieldType(mysql.TypeTiny), rn, ln)
eqCond = append(eqCond, cond.(*expression.ScalarFunction))
continue
}
}
}
columns, _ := extractColumn(expr, nil, nil)
allFromLeft, allFromRight := true, true
for _, col := range columns {
if left.GetSchema().GetIndex(col) == -1 {
allFromLeft = false
}
if right.GetSchema().GetIndex(col) == -1 {
allFromRight = false
}
}
if allFromRight {
rightCond = append(rightCond, expr)
} else if allFromLeft {
leftCond = append(leftCond, expr)
} else {
otherCond = append(otherCond, expr)
}
}
return
}
开发者ID:yangxuanjia,项目名称:tidb,代码行数:40,代码来源:logical_plan_builder.go
注:本文中的github.com/pingcap/tidb/expression.NewFunction函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论