From f75cdaea9a4a4a8b3f7135f6bb33d56797dcd905 Mon Sep 17 00:00:00 2001 From: ireneontheway <48651140+ireneontheway@users.noreply.github.com> Date: Mon, 20 Jul 2020 15:17:08 +0800 Subject: [PATCH] cherry pick #3288 to release-4.0 Signed-off-by: ti-srebot --- expression-syntax.md | 89 +++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 60 deletions(-) diff --git a/expression-syntax.md b/expression-syntax.md index 9d3cdb03bfefb..0222b5de36a5e 100644 --- a/expression-syntax.md +++ b/expression-syntax.md @@ -6,63 +6,32 @@ aliases: ['/docs/stable/expression-syntax/','/docs/v4.0/expression-syntax/','/do # Expression Syntax -The following rules define the expression syntax in TiDB. You can find the definition in `parser/parser.y`. The syntax parsing in TiDB is based on Yacc. - -``` -Expression: - singleAtIdentifier assignmentEq Expression - | Expression logOr Expression - | Expression "XOR" Expression - | Expression logAnd Expression - | "NOT" Expression - | Factor IsOrNotOp trueKwd - | Factor IsOrNotOp falseKwd - | Factor IsOrNotOp "UNKNOWN" - | Factor - -Factor: - Factor IsOrNotOp "NULL" - | Factor CompareOp PredicateExpr - | Factor CompareOp singleAtIdentifier assignmentEq PredicateExpr - | Factor CompareOp AnyOrAll SubSelect - | PredicateExpr - -PredicateExpr: - PrimaryFactor InOrNotOp '(' ExpressionList ')' - | PrimaryFactor InOrNotOp SubSelect - | PrimaryFactor BetweenOrNotOp PrimaryFactor "AND" PredicateExpr - | PrimaryFactor LikeOrNotOp PrimaryExpression LikeEscapeOpt - | PrimaryFactor RegexpOrNotOp PrimaryExpression - | PrimaryFactor - -PrimaryFactor: - PrimaryFactor '|' PrimaryFactor - | PrimaryFactor '&' PrimaryFactor - | PrimaryFactor "<<" PrimaryFactor - | PrimaryFactor ">>" PrimaryFactor - | PrimaryFactor '+' PrimaryFactor - | PrimaryFactor '-' PrimaryFactor - | PrimaryFactor '*' PrimaryFactor - | PrimaryFactor '/' PrimaryFactor - | PrimaryFactor '%' PrimaryFactor - | PrimaryFactor "DIV" PrimaryFactor - | PrimaryFactor "MOD" PrimaryFactor - | PrimaryFactor '^' PrimaryFactor - | PrimaryExpression - -PrimaryExpression: - Operand - | FunctionCallKeyword - | FunctionCallNonKeyword - | FunctionCallAgg - | FunctionCallGeneric - | Identifier jss stringLit - | Identifier juss stringLit - | SubSelect - | '!' PrimaryExpression - | '~' PrimaryExpression - | '-' PrimaryExpression - | '+' PrimaryExpression - | "BINARY" PrimaryExpression - | PrimaryExpression "COLLATE" StringName -``` +An expression is a combination of one or more values, operators, or functions. In TiDB, expressions are mainly used in various clauses of the `SELECT` statement, including Group by clause, Where clause, Having clause, Join condition and window function. In addition, some DDL statements also use expressions, such as the setting of the default values, columns, and partition rules when creating tables. + +The expressions can be divided into the following types: + +- Identifier. For reference, see [Schema object names](/schema-object-names.md). + +- Predicates, numeric values, strings, date expressions. The [Literal values](/literal-values.md) of these types are also expressions. + +- Function calls and window functions. For reference, see [Functions and operators overview](/functions-and-operators/functions-and-operators-overview.md) and [Window functions](/functions-and-operators/window-functions.md) + +- ParamMarker (`?`), system variables, user variables and CASE expressions. + +The following rules are the expression syntax, which is based on the [parser.y](https://github.com/pingcap/parser/blob/master/parser.y) rules of TiDB parser. For the navigable version of the following syntax diagram, refer to [TiDB SQL Syntax Diagram](https://pingcap.github.io/sqlgram/#Expression). + +**Expression:** + +![Expression](/media/sqlgram/Expression.png) + +**PredicateExpr:** + +![PredicateExpr](/media/sqlgram/PredicateExpr.png) + +**BitExpr:** + +![BitExpr](/media/sqlgram/BitExpr.png) + +**SimpleExpr:** + +![SimpleExpr](/media/sqlgram/SimpleExpr.png)