github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/generatedexpr/generated_expr.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package generatedexpr
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/whtcorpsinc/errors"
    20  	"github.com/whtcorpsinc/BerolinaSQL"
    21  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    22  	"github.com/whtcorpsinc/BerolinaSQL/charset"
    23  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    24  	"github.com/whtcorpsinc/milevadb/soliton"
    25  )
    26  
    27  // nameResolver is the visitor to resolve causet name and defCausumn name.
    28  // it combines BlockInfo and DeferredCausetInfo to a generation memex.
    29  type nameResolver struct {
    30  	blockInfo *perceptron.BlockInfo
    31  	err       error
    32  }
    33  
    34  // Enter implements ast.Visitor interface.
    35  func (nr *nameResolver) Enter(inNode ast.Node) (ast.Node, bool) {
    36  	return inNode, false
    37  }
    38  
    39  // Leave implements ast.Visitor interface.
    40  func (nr *nameResolver) Leave(inNode ast.Node) (node ast.Node, ok bool) {
    41  	switch v := inNode.(type) {
    42  	case *ast.DeferredCausetNameExpr:
    43  		for _, defCaus := range nr.blockInfo.DeferredCausets {
    44  			if defCaus.Name.L == v.Name.Name.L {
    45  				v.Refer = &ast.ResultField{
    46  					DeferredCauset: defCaus,
    47  					Block:  nr.blockInfo,
    48  				}
    49  				return inNode, true
    50  			}
    51  		}
    52  		nr.err = errors.Errorf("can't find defCausumn %s in %s", v.Name.Name.O, nr.blockInfo.Name.O)
    53  		return inNode, false
    54  	}
    55  	return inNode, true
    56  }
    57  
    58  // ParseExpression parses an ExprNode from a string.
    59  // When MilevaDB loads schemareplicant from EinsteinDB, `GeneratedExprString`
    60  // of `DeferredCausetInfo` is a string field, so we need to parse
    61  // it into ast.ExprNode. This function is for that.
    62  func ParseExpression(expr string) (node ast.ExprNode, err error) {
    63  	expr = fmt.Sprintf("select %s", expr)
    64  	charset, defCauslation := charset.GetDefaultCharsetAndDefCauslate()
    65  	stmts, _, err := BerolinaSQL.New().Parse(expr, charset, defCauslation)
    66  	if err == nil {
    67  		node = stmts[0].(*ast.SelectStmt).Fields.Fields[0].Expr
    68  	}
    69  	return node, soliton.SyntaxError(err)
    70  }
    71  
    72  // SimpleResolveName resolves all defCausumn names in the memex node.
    73  func SimpleResolveName(node ast.ExprNode, tblInfo *perceptron.BlockInfo) (ast.ExprNode, error) {
    74  	nr := nameResolver{tblInfo, nil}
    75  	if _, ok := node.Accept(&nr); !ok {
    76  		return nil, errors.Trace(nr.err)
    77  	}
    78  	return node, nil
    79  }