Expression Syntax

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:

The following rules are the expression syntax, which is based on the parser.y rules of TiDB parser. For the navigable version of the following syntax diagram, refer to TiDB SQL Syntax Diagram.

Expression

Expression Syntax - 图1

PredicateExpr

Expression Syntax - 图2

BitExpr

Expression Syntax - 图3

SimpleExpr

Expression Syntax - 图4

  1. Expression ::=
  2. ( singleAtIdentifier assignmentEq | 'NOT' | Expression ( logOr | 'XOR' | logAnd ) ) Expression
  3. | 'MATCH' '(' ColumnNameList ')' 'AGAINST' '(' BitExpr FulltextSearchModifierOpt ')'
  4. | PredicateExpr ( IsOrNotOp 'NULL' | CompareOp ( ( singleAtIdentifier assignmentEq )? PredicateExpr | AnyOrAll SubSelect ) )* ( IsOrNotOp ( trueKwd | falseKwd | 'UNKNOWN' ) )?
  5. PredicateExpr ::=
  6. BitExpr ( BetweenOrNotOp BitExpr 'AND' BitExpr )* ( InOrNotOp ( '(' ExpressionList ')' | SubSelect ) | LikeOrNotOp SimpleExpr LikeEscapeOpt | RegexpOrNotOp SimpleExpr )?
  7. BitExpr ::=
  8. BitExpr ( ( '|' | '&' | '<<' | '>>' | '*' | '/' | '%' | 'DIV' | 'MOD' | '^' ) BitExpr | ( '+' | '-' ) ( BitExpr | "INTERVAL" Expression TimeUnit ) )
  9. | SimpleExpr
  10. SimpleExpr ::=
  11. SimpleIdent ( ( '->' | '->>' ) stringLit )?
  12. | FunctionCallKeyword
  13. | FunctionCallNonKeyword
  14. | FunctionCallGeneric
  15. | SimpleExpr ( 'COLLATE' CollationName | pipes SimpleExpr )
  16. | WindowFuncCall
  17. | Literal
  18. | paramMarker
  19. | Variable
  20. | SumExpr
  21. | ( '!' | '~' | '-' | '+' | 'NOT' | 'BINARY' ) SimpleExpr
  22. | 'EXISTS'? SubSelect
  23. | ( ( '(' ( ExpressionList ',' )? | 'ROW' '(' ExpressionList ',' ) Expression | builtinCast '(' Expression 'AS' CastType | ( 'DEFAULT' | 'VALUES' ) '(' SimpleIdent | 'CONVERT' '(' Expression ( ',' CastType | 'USING' CharsetName ) ) ')'
  24. | 'CASE' ExpressionOpt WhenClause+ ElseOpt 'END'