本文整理汇总了Golang中github.com/pingcap/tidb/expression.NewFunction函数的典型用法代码示例。如果您正苦于以下问题:Golang NewFunction函数的具体用法?Golang NewFunction怎么用?Golang NewFunction使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewFunction函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
}
示例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)
}
示例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)
}
示例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
}
示例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)
}
示例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)
}
示例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)
}
示例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
}
示例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)
}
示例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
}
示例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)
}
示例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:])})
}
示例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
}
示例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)
}
示例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
}