ParseExpression parses an ExprNode from a string. When TiDB loads infoschema from TiKV, `GeneratedExprString` of `ColumnInfo` is a string field, so we need to parse it into ast.ExprNode. This function is for that.
(expr string)
| 58 | // of `ColumnInfo` is a string field, so we need to parse |
| 59 | // it into ast.ExprNode. This function is for that. |
| 60 | func ParseExpression(expr string) (node ast.ExprNode, err error) { |
| 61 | expr = fmt.Sprintf("select %s", expr) |
| 62 | charset, collation := charset.GetDefaultCharsetAndCollate() |
| 63 | stmts, _, err := parser.New().ParseSQL(expr, |
| 64 | parser.CharsetConnection(charset), |
| 65 | parser.CollationConnection(collation)) |
| 66 | if err == nil { |
| 67 | node = stmts[0].(*ast.SelectStmt).Fields.Fields[0].Expr |
| 68 | } |
| 69 | return node, util.SyntaxError(err) |
| 70 | } |
| 71 | |
| 72 | // SimpleResolveName resolves all column names in the expression node. |
| 73 | func SimpleResolveName(node ast.ExprNode, tblInfo *model.TableInfo) (ast.ExprNode, error) { |