github.com/mithrandie/csvq@v1.18.1/lib/parser/parser.y (about)

     1  %{
     2  package parser
     3  
     4  import (
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/mithrandie/csvq/lib/value"
     9  )
    10  %}
    11  
    12  %union{
    13      program     []Statement
    14      statement   Statement
    15      queryexpr   QueryExpression
    16      queryexprs  []QueryExpression
    17      expression  Expression
    18      expressions []Expression
    19      identifier  Identifier
    20      table       Table
    21      variable    Variable
    22      variables   []Variable
    23      varassign   VariableAssignment
    24      varassigns  []VariableAssignment
    25      envvar      EnvironmentVariable
    26      flag        Flag
    27      updateset   UpdateSet
    28      updatesets  []UpdateSet
    29      columndef   ColumnDefault
    30      columndefs  []ColumnDefault
    31      elseif      []ElseIf
    32      elseexpr    Else
    33      casewhen    []CaseWhen
    34      caseelse    CaseElse
    35      fetchpos    FetchPosition
    36      replaceval  ReplaceValue
    37      replacevals []ReplaceValue
    38      token       Token
    39      bool        bool
    40  }
    41  
    42  %type<program>     program
    43  %type<program>     loop_program
    44  %type<program>     function_program
    45  %type<program>     function_loop_program
    46  %type<statement>   common_statement
    47  %type<statement>   common_loop_flow_control_statement
    48  %type<statement>   procedure_statement
    49  %type<statement>   while_statement
    50  %type<token>       while_variable_declaration
    51  %type<statement>   exit_statement
    52  %type<statement>   flow_control_statement
    53  %type<statement>   loop_statement
    54  %type<statement>   loop_flow_control_statement
    55  %type<statement>   function_statement
    56  %type<statement>   function_while_statement
    57  %type<statement>   function_exit_statement
    58  %type<statement>   function_loop_statement
    59  %type<statement>   function_flow_control_statement
    60  %type<statement>   function_loop_flow_control_statement
    61  %type<statement>   variable_statement
    62  %type<statement>   environment_variable_statement
    63  %type<statement>   transaction_statement
    64  %type<statement>   table_operation_statement
    65  %type<columndef>   column_default
    66  %type<columndefs>  column_defaults
    67  %type<expression>  column_position
    68  %type<statement>   cursor_statement
    69  %type<statement>   temporary_table_statement
    70  %type<replaceval>  replace_value
    71  %type<replacevals> replace_values
    72  %type<statement>   prepared_statement
    73  %type<varassign>   parameter
    74  %type<varassigns>  parameters
    75  %type<varassign>   optional_parameter
    76  %type<varassigns>  optional_parameters
    77  %type<varassigns>  function_parameters
    78  %type<statement>   user_defined_function_statement
    79  %type<fetchpos>    fetch_position
    80  %type<queryexpr>   cursor_status
    81  %type<statement>   command_statement
    82  %type<statement>   trigger_statement
    83  %type<queryexpr>   select_query
    84  %type<queryexpr>   select_into_query
    85  %type<queryexpr>   select_entity
    86  %type<queryexpr>   select_set_entity
    87  %type<queryexpr>   select_clause
    88  %type<queryexpr>   into_clause
    89  %type<queryexpr>   from_clause
    90  %type<queryexpr>   where_clause
    91  %type<queryexpr>   group_by_clause
    92  %type<queryexpr>   having_clause
    93  %type<queryexpr>   order_by_clause
    94  %type<queryexpr>   limit_clause
    95  %type<token>       limit_restriction
    96  %type<token>       limit_fetch_position
    97  %type<token>       limit_unit
    98  %type<token>       limit_fetch_unit
    99  %type<token>       offset_unit
   100  %type<queryexpr>   offset_clause
   101  %type<queryexpr>   with_clause
   102  %type<queryexpr>   inline_table
   103  %type<queryexprs>  inline_tables
   104  %type<queryexpr>   primitive_type
   105  %type<queryexpr>   ternary
   106  %type<queryexpr>   null
   107  %type<queryexpr>   field_reference
   108  %type<queryexpr>   value
   109  %type<queryexpr>   substantial_value
   110  %type<queryexpr>   wildcard
   111  %type<queryexpr>   row_value
   112  %type<queryexprs>  row_values
   113  %type<queryexprs>  order_items
   114  %type<queryexpr>   order_item
   115  %type<token>       order_direction
   116  %type<token>       order_null_position
   117  %type<queryexpr>   subquery
   118  %type<queryexpr>   string_operation
   119  %type<queryexpr>   matrix_value
   120  %type<queryexpr>   comparison
   121  %type<queryexpr>   arithmetic
   122  %type<queryexpr>   logic
   123  %type<queryexprs>  arguments
   124  %type<queryexpr>   function
   125  %type<queryexpr>   aggregate_function
   126  %type<queryexpr>   list_function
   127  %type<queryexpr>   analytic_function
   128  %type<queryexpr>   analytic_clause
   129  %type<queryexpr>   analytic_clause_with_windowing
   130  %type<queryexpr>   partition_clause
   131  %type<queryexpr>   windowing_clause
   132  %type<queryexpr>   window_position
   133  %type<queryexpr>   window_relative_position
   134  %type<queryexpr>   window_frame_low
   135  %type<queryexpr>   window_frame_high
   136  %type<queryexpr>   table_identifier
   137  %type<token>       table_format
   138  %type<queryexpr>   format_specified_function
   139  %type<token>       inline_table_format
   140  %type<queryexpr>   inline_format_specified_function
   141  %type<queryexpr>   updatable_table_identifier
   142  %type<queryexprs>  identified_tables
   143  %type<queryexprs>  updatable_tables
   144  %type<queryexpr>   table_object
   145  %type<table>       laterable_query_table
   146  %type<queryexprs>  joinable_tables
   147  %type<queryexpr>   table
   148  %type<queryexpr>   join
   149  %type<queryexpr>   join_condition
   150  %type<queryexpr>   field
   151  %type<queryexpr>   case_expr
   152  %type<queryexpr>   case_value
   153  %type<queryexprs>  case_expr_when
   154  %type<queryexpr>   case_expr_else
   155  %type<queryexprs>  field_references
   156  %type<queryexprs>  values
   157  %type<queryexprs>  substantial_values
   158  %type<queryexprs>  tables
   159  %type<queryexprs>  identifiers
   160  %type<queryexprs>  fields
   161  %type<expression>  insert_query
   162  %type<expression>  update_query
   163  %type<updateset>   update_set
   164  %type<updatesets>  update_set_list
   165  %type<expression>  replace_query
   166  %type<expression>  delete_query
   167  %type<elseif>      elseif
   168  %type<elseexpr>    else
   169  %type<elseif>      in_loop_elseif
   170  %type<elseexpr>    in_loop_else
   171  %type<elseif>      in_function_elseif
   172  %type<elseexpr>    in_function_else
   173  %type<elseif>      in_function_in_loop_elseif
   174  %type<elseexpr>    in_function_in_loop_else
   175  %type<casewhen>    case_when
   176  %type<caseelse>    case_else
   177  %type<casewhen>    in_loop_case_when
   178  %type<caseelse>    in_loop_case_else
   179  %type<casewhen>    in_function_case_when
   180  %type<caseelse>    in_function_case_else
   181  %type<casewhen>    in_function_in_loop_case_when
   182  %type<caseelse>    in_function_in_loop_case_else
   183  %type<identifier>  identifier
   184  %type<variable>    variable
   185  %type<variables>   variables
   186  %type<queryexpr>   variable_substitution
   187  %type<varassign>   variable_assignment
   188  %type<varassigns>  variable_assignments
   189  %type<envvar>      environment_variable
   190  %type<queryexpr>   runtime_information
   191  %type<queryexpr>   constant
   192  %type<flag>        flag
   193  %type<token>       distinct
   194  %type<token>       negation
   195  %type<token>       join_type_inner
   196  %type<token>       join_type_outer
   197  %type<token>       join_outer_direction
   198  %type<token>       all
   199  %type<token>       recursive
   200  %type<token>       as
   201  %type<token>       comparison_operator
   202  %type<bool>        if_not_exists
   203  
   204  %token<token> IDENTIFIER STRING INTEGER FLOAT BOOLEAN TERNARY DATETIME
   205  %token<token> VARIABLE FLAG ENVIRONMENT_VARIABLE RUNTIME_INFORMATION EXTERNAL_COMMAND PLACEHOLDER
   206  %token<token> CONSTANT TABLE_FUNCTION URL
   207  %token<token> SELECT FROM UPDATE SET UNSET DELETE WHERE INSERT INTO VALUES REPLACE AS DUAL STDIN
   208  %token<token> RECURSIVE
   209  %token<token> CREATE ADD DROP ALTER TABLE FIRST LAST AFTER BEFORE DEFAULT RENAME TO VIEW
   210  %token<token> ORDER GROUP HAVING BY ASC DESC LIMIT OFFSET PERCENT
   211  %token<token> JOIN INNER OUTER LEFT RIGHT FULL CROSS ON USING NATURAL LATERAL
   212  %token<token> UNION INTERSECT EXCEPT
   213  %token<token> ALL ANY EXISTS IN
   214  %token<token> AND OR NOT BETWEEN LIKE IS NULL
   215  %token<token> DISTINCT WITH
   216  %token<token> RANGE UNBOUNDED PRECEDING FOLLOWING CURRENT ROW
   217  %token<token> CASE IF ELSEIF WHILE WHEN THEN ELSE DO END
   218  %token<token> DECLARE CURSOR FOR FETCH OPEN CLOSE DISPOSE PREPARE
   219  %token<token> NEXT PRIOR ABSOLUTE RELATIVE
   220  %token<token> SEPARATOR PARTITION OVER
   221  %token<token> COMMIT ROLLBACK
   222  %token<token> CONTINUE BREAK EXIT
   223  %token<token> ECHO PRINT PRINTF SOURCE EXECUTE CHDIR PWD RELOAD REMOVE SYNTAX TRIGGER
   224  %token<token> FUNCTION AGGREGATE BEGIN RETURN
   225  %token<token> IGNORE WITHIN
   226  %token<token> VAR SHOW
   227  %token<token> TIES NULLS ROWS ONLY
   228  %token<token> CSV JSON JSONL FIXED LTSV
   229  %token<token> CSV_INLINE JSON_INLINE JSON_TABLE
   230  %token<token> JSON_ROW
   231  %token<token> SUBSTRING COUNT JSON_OBJECT
   232  %token<token> AGGREGATE_FUNCTION LIST_FUNCTION ANALYTIC_FUNCTION FUNCTION_NTH FUNCTION_WITH_INS
   233  %token<token> COMPARISON_OP STRING_OP SUBSTITUTION_OP
   234  %token<token> UMINUS UPLUS
   235  %token<token> ';' '=' '-' '+' '*' '/' '%' '!' '(' ')'
   236  
   237  %right SUBSTITUTION_OP
   238  %left UNION EXCEPT
   239  %left INTERSECT
   240  %left CROSS FULL NATURAL JOIN
   241  %left OR
   242  %left AND
   243  %right NOT
   244  %nonassoc '=' COMPARISON_OP IS BETWEEN IN LIKE
   245  %left STRING_OP
   246  %left '+' '-'
   247  %left '*' '/' '%'
   248  %right UMINUS UPLUS '!'
   249  
   250  %%
   251  
   252  program
   253      :
   254      {
   255          $$ = nil
   256          yylex.(*Lexer).program = $$
   257      }
   258      | procedure_statement
   259      {
   260          $$ = []Statement{$1}
   261          yylex.(*Lexer).program = $$
   262      }
   263      | procedure_statement ';' program
   264      {
   265          $$ = append([]Statement{$1}, $3...)
   266          yylex.(*Lexer).program = $$
   267      }
   268  
   269  loop_program
   270      :
   271      {
   272          $$ = nil
   273      }
   274      | loop_statement ';' loop_program
   275      {
   276          $$ = append([]Statement{$1}, $3...)
   277      }
   278  
   279  function_program
   280      :
   281      {
   282          $$ = nil
   283      }
   284      | function_statement ';' function_program
   285      {
   286          $$ = append([]Statement{$1}, $3...)
   287      }
   288  
   289  function_loop_program
   290      :
   291      {
   292          $$ = nil
   293      }
   294      | function_loop_statement ';' function_loop_program
   295      {
   296          $$ = append([]Statement{$1}, $3...)
   297      }
   298  
   299  common_statement
   300      : select_query
   301      {
   302          $$ = $1
   303      }
   304      | select_into_query
   305      {
   306          $$ = $1
   307      }
   308      | insert_query
   309      {
   310          $$ = $1
   311      }
   312      | update_query
   313      {
   314          $$ = $1
   315      }
   316      | replace_query
   317      {
   318          $$ = $1
   319      }
   320      | delete_query
   321      {
   322          $$ = $1
   323      }
   324      | table_operation_statement
   325      {
   326          $$ = $1
   327      }
   328      | variable_statement
   329      {
   330          $$ = $1
   331      }
   332      | environment_variable_statement
   333      {
   334          $$ = $1
   335      }
   336      | cursor_statement
   337      {
   338          $$ = $1
   339      }
   340      | temporary_table_statement
   341      {
   342          $$ = $1
   343      }
   344      | prepared_statement
   345      {
   346          $$ = $1
   347      }
   348      | user_defined_function_statement
   349      {
   350          $$ = $1
   351      }
   352      | transaction_statement
   353      {
   354          $$ = $1
   355      }
   356      | command_statement
   357      {
   358          $$ = $1
   359      }
   360      | trigger_statement
   361      {
   362          $$ = $1
   363      }
   364      | substantial_value
   365      {
   366          $$ = $1
   367      }
   368      | EXTERNAL_COMMAND
   369      {
   370          $$ = ExternalCommand{BaseExpr: NewBaseExpr($1), Command: $1.Literal}
   371      }
   372  
   373  common_loop_flow_control_statement
   374      : CONTINUE
   375      {
   376          $$ = FlowControl{Token: $1.Token}
   377      }
   378      | BREAK
   379      {
   380          $$ = FlowControl{Token: $1.Token}
   381      }
   382  
   383  procedure_statement
   384      : common_statement
   385      {
   386          $$ = $1
   387      }
   388      | flow_control_statement
   389      {
   390          $$ = $1
   391      }
   392  
   393  while_statement
   394      : WHILE substantial_value DO loop_program END WHILE
   395      {
   396          $$ = While{Condition: $2, Statements: $4}
   397      }
   398      | WHILE variable IN identifier DO loop_program END WHILE
   399      {
   400          $$ = WhileInCursor{Variables: []Variable{$2}, Cursor: $4, Statements: $6}
   401      }
   402      | WHILE variables IN identifier DO loop_program END WHILE
   403      {
   404          $$ = WhileInCursor{Variables: $2, Cursor: $4, Statements: $6}
   405      }
   406      | WHILE while_variable_declaration variable IN identifier DO loop_program END WHILE
   407      {
   408          $$ = WhileInCursor{WithDeclaration: true, Variables: []Variable{$3}, Cursor: $5, Statements: $7}
   409      }
   410      | WHILE while_variable_declaration variables IN identifier DO loop_program END WHILE
   411      {
   412          $$ = WhileInCursor{WithDeclaration: true, Variables: $3, Cursor: $5, Statements: $7}
   413      }
   414  
   415  while_variable_declaration
   416      : VAR
   417      {
   418          $$ = $1
   419      }
   420      | DECLARE
   421      {
   422          $$ = $1
   423      }
   424  
   425  exit_statement
   426      : EXIT
   427      {
   428          $$ = Exit{}
   429      }
   430      | EXIT INTEGER
   431      {
   432          $$ = Exit{Code: value.NewIntegerFromString($2.Literal)}
   433      }
   434  
   435  loop_statement
   436      : common_statement
   437      {
   438          $$ = $1
   439      }
   440      | loop_flow_control_statement
   441      {
   442          $$ = $1
   443      }
   444  
   445  flow_control_statement
   446      : IF substantial_value THEN program else END IF
   447      {
   448          $$ = If{Condition: $2, Statements: $4, Else: $5}
   449      }
   450      | IF substantial_value THEN program elseif else END IF
   451      {
   452          $$ = If{Condition: $2, Statements: $4, ElseIf: $5, Else: $6}
   453      }
   454      | CASE case_value case_when case_else END CASE
   455      {
   456          $$ = Case{Value: $2, When: $3, Else: $4}
   457      }
   458      | while_statement
   459      {
   460          $$ = $1
   461      }
   462      | exit_statement
   463      {
   464          $$ = $1
   465      }
   466  
   467  loop_flow_control_statement
   468      : IF substantial_value THEN loop_program in_loop_else END IF
   469      {
   470          $$ = If{Condition: $2, Statements: $4, Else: $5}
   471      }
   472      | IF substantial_value THEN loop_program in_loop_elseif in_loop_else END IF
   473      {
   474          $$ = If{Condition: $2, Statements: $4, ElseIf: $5, Else: $6}
   475      }
   476      | CASE case_value in_loop_case_when in_loop_case_else END CASE
   477      {
   478          $$ = Case{Value: $2, When: $3, Else: $4}
   479      }
   480      | while_statement
   481      {
   482          $$ = $1
   483      }
   484      | exit_statement
   485      {
   486          $$ = $1
   487      }
   488      | common_loop_flow_control_statement
   489      {
   490          $$ = $1
   491      }
   492  
   493  function_statement
   494      : common_statement
   495      {
   496          $$ = $1
   497      }
   498      | function_flow_control_statement
   499      {
   500          $$ = $1
   501      }
   502  
   503  function_while_statement
   504      : WHILE substantial_value DO function_loop_program END WHILE
   505      {
   506          $$ = While{Condition: $2, Statements: $4}
   507      }
   508      | WHILE variable IN identifier DO function_loop_program END WHILE
   509      {
   510          $$ = WhileInCursor{Variables: []Variable{$2}, Cursor: $4, Statements: $6}
   511      }
   512      | WHILE variables IN identifier DO function_loop_program END WHILE
   513      {
   514          $$ = WhileInCursor{Variables: $2, Cursor: $4, Statements: $6}
   515      }
   516      | WHILE while_variable_declaration variable IN identifier DO function_loop_program END WHILE
   517      {
   518          $$ = WhileInCursor{WithDeclaration: true, Variables: []Variable{$3}, Cursor: $5, Statements: $7}
   519      }
   520      | WHILE while_variable_declaration variables IN identifier DO function_loop_program END WHILE
   521      {
   522          $$ = WhileInCursor{WithDeclaration: true, Variables: $3, Cursor: $5, Statements: $7}
   523      }
   524  
   525  function_exit_statement
   526      : RETURN
   527      {
   528          $$ = Return{Value: NewNullValue()}
   529      }
   530      | RETURN substantial_value
   531      {
   532          $$ = Return{Value: $2}
   533      }
   534  
   535  function_loop_statement
   536      : common_statement
   537      {
   538          $$ = $1
   539      }
   540      | function_loop_flow_control_statement
   541      {
   542          $$ = $1
   543      }
   544  
   545  function_flow_control_statement
   546      : IF substantial_value THEN function_program in_function_else END IF
   547      {
   548          $$ = If{Condition: $2, Statements: $4, Else: $5}
   549      }
   550      | IF substantial_value THEN function_program in_function_elseif in_function_else END IF
   551      {
   552          $$ = If{Condition: $2, Statements: $4, ElseIf: $5, Else: $6}
   553      }
   554      | CASE case_value in_function_case_when in_function_case_else END CASE
   555      {
   556          $$ = Case{Value: $2, When: $3, Else: $4}
   557      }
   558      | function_while_statement
   559      {
   560          $$ = $1
   561      }
   562      | function_exit_statement
   563      {
   564          $$ = $1
   565      }
   566  
   567  function_loop_flow_control_statement
   568      : IF substantial_value THEN function_loop_program in_function_in_loop_else END IF
   569      {
   570          $$ = If{Condition: $2, Statements: $4, Else: $5}
   571      }
   572      | IF substantial_value THEN function_loop_program in_function_in_loop_elseif in_function_in_loop_else END IF
   573      {
   574          $$ = If{Condition: $2, Statements: $4, ElseIf: $5, Else: $6}
   575      }
   576      | CASE case_value in_function_in_loop_case_when in_function_in_loop_case_else END CASE
   577      {
   578          $$ = Case{Value: $2, When: $3, Else: $4}
   579      }
   580      | function_while_statement
   581      {
   582          $$ = $1
   583      }
   584      | function_exit_statement
   585      {
   586          $$ = $1
   587      }
   588      | common_loop_flow_control_statement
   589      {
   590          $$ = $1
   591      }
   592  
   593  variable_statement
   594      : VAR variable_assignments
   595      {
   596          $$ = VariableDeclaration{Assignments:$2}
   597      }
   598      | DECLARE variable_assignments
   599      {
   600          $$ = VariableDeclaration{Assignments:$2}
   601      }
   602      | variable_substitution
   603      {
   604          $$ = $1
   605      }
   606      | DISPOSE variable
   607      {
   608          $$ = DisposeVariable{Variable:$2}
   609      }
   610  
   611  environment_variable_statement
   612      : SET environment_variable '=' substantial_value
   613      {
   614          $$ = SetEnvVar{EnvVar:$2, Value:$4}
   615      }
   616      | SET environment_variable '=' identifier
   617      {
   618          $$ = SetEnvVar{EnvVar:$2, Value:$4}
   619      }
   620      | SET environment_variable TO substantial_value
   621      {
   622          $$ = SetEnvVar{EnvVar:$2, Value:$4}
   623      }
   624      | SET environment_variable TO identifier
   625      {
   626          $$ = SetEnvVar{EnvVar:$2, Value:$4}
   627      }
   628      | UNSET environment_variable
   629      {
   630          $$ = UnsetEnvVar{EnvVar:$2}
   631      }
   632  
   633  transaction_statement
   634      : COMMIT
   635      {
   636          $$ = TransactionControl{BaseExpr: NewBaseExpr($1), Token: $1.Token}
   637      }
   638      | ROLLBACK
   639      {
   640          $$ = TransactionControl{BaseExpr: NewBaseExpr($1), Token: $1.Token}
   641      }
   642  
   643  table_operation_statement
   644      : CREATE TABLE if_not_exists identifier '(' identifiers ')'
   645      {
   646          $$ = CreateTable{Table: $4, Fields: $6, IfNotExists: $3}
   647      }
   648      | CREATE TABLE if_not_exists identifier '(' identifiers ')' as select_query
   649      {
   650          $$ = CreateTable{Table: $4, Fields: $6, Query: $9, IfNotExists: $3}
   651      }
   652      | CREATE TABLE if_not_exists identifier as select_query
   653      {
   654          $$ = CreateTable{Table: $4, Query: $6, IfNotExists: $3}
   655      }
   656      | ALTER TABLE updatable_table_identifier ADD column_default column_position
   657      {
   658          $$ = AddColumns{Table: $3, Columns: []ColumnDefault{$5}, Position: $6}
   659      }
   660      | ALTER TABLE updatable_table_identifier ADD '(' column_defaults ')' column_position
   661      {
   662          $$ = AddColumns{Table: $3, Columns: $6, Position: $8}
   663      }
   664      | ALTER TABLE updatable_table_identifier DROP field_reference
   665      {
   666          $$ = DropColumns{Table: $3, Columns: []QueryExpression{$5}}
   667      }
   668      | ALTER TABLE updatable_table_identifier DROP '(' field_references ')'
   669      {
   670          $$ = DropColumns{Table: $3, Columns: $6}
   671      }
   672      | ALTER TABLE updatable_table_identifier RENAME field_reference TO identifier
   673      {
   674          $$ = RenameColumn{Table: $3, Old: $5, New: $7}
   675      }
   676      | ALTER TABLE updatable_table_identifier SET identifier TO identifier
   677      {
   678          $$ = SetTableAttribute{BaseExpr: NewBaseExpr($1), Table: $3, Attribute: $5, Value: $7}
   679      }
   680      | ALTER TABLE updatable_table_identifier SET identifier TO substantial_value
   681      {
   682          $$ = SetTableAttribute{BaseExpr: NewBaseExpr($1), Table: $3, Attribute: $5, Value: $7}
   683      }
   684  
   685  column_default
   686      : identifier
   687      {
   688          $$ = ColumnDefault{Column: $1}
   689      }
   690      | identifier DEFAULT value
   691      {
   692          $$ = ColumnDefault{Column: $1, Value: $3}
   693      }
   694  
   695  column_defaults
   696      : column_default
   697      {
   698          $$ = []ColumnDefault{$1}
   699      }
   700      | column_default ',' column_defaults
   701      {
   702          $$ = append([]ColumnDefault{$1}, $3...)
   703      }
   704  
   705  column_position
   706      :
   707      {
   708          $$ = nil
   709      }
   710      | FIRST
   711      {
   712          $$ = ColumnPosition{Position: $1}
   713      }
   714      | LAST
   715      {
   716          $$ = ColumnPosition{Position: $1}
   717      }
   718      | AFTER field_reference
   719      {
   720          $$ = ColumnPosition{Position: $1, Column: $2}
   721      }
   722      | BEFORE field_reference
   723      {
   724          $$ = ColumnPosition{Position: $1, Column: $2}
   725      }
   726  
   727  cursor_statement
   728      : DECLARE identifier CURSOR FOR select_query
   729      {
   730          $$ = CursorDeclaration{Cursor:$2, Query: $5.(SelectQuery)}
   731      }
   732      | DECLARE identifier CURSOR FOR identifier
   733      {
   734          $$ = CursorDeclaration{Cursor:$2, Statement: $5}
   735      }
   736      | OPEN identifier
   737      {
   738          $$ = OpenCursor{Cursor: $2}
   739      }
   740      | OPEN identifier USING replace_values
   741      {
   742          $$ = OpenCursor{Cursor: $2, Values: $4}
   743      }
   744      | CLOSE identifier
   745      {
   746          $$ = CloseCursor{Cursor: $2}
   747      }
   748      | DISPOSE CURSOR identifier
   749      {
   750          $$ = DisposeCursor{Cursor: $3}
   751      }
   752      | FETCH fetch_position identifier INTO variables
   753      {
   754          $$ = FetchCursor{Position: $2, Cursor: $3, Variables: $5}
   755      }
   756  
   757  temporary_table_statement
   758      : DECLARE identifier VIEW '(' identifiers ')'
   759      {
   760          $$ = ViewDeclaration{View: $2, Fields: $5}
   761      }
   762      | DECLARE identifier VIEW '(' identifiers ')' AS select_query
   763      {
   764          $$ = ViewDeclaration{View: $2, Fields: $5, Query: $8}
   765      }
   766      | DECLARE identifier VIEW AS select_query
   767      {
   768          $$ = ViewDeclaration{View: $2, Query: $5}
   769      }
   770      | DISPOSE VIEW identifier
   771      {
   772          $$ = DisposeView{View: $3}
   773      }
   774      | DISPOSE VIEW STDIN
   775      {
   776          $$ = DisposeView{View: Stdin{BaseExpr: NewBaseExpr($3)}}
   777      }
   778  
   779  replace_value
   780      : substantial_value
   781      {
   782          $$ = ReplaceValue{Value: $1}
   783      }
   784      | substantial_value AS identifier
   785      {
   786          $$ = ReplaceValue{Value: $1, Name: $3}
   787      }
   788  
   789  replace_values
   790      : replace_value
   791      {
   792          $$ = []ReplaceValue{$1}
   793      }
   794      | replace_value ',' replace_values
   795      {
   796          $$ = append([]ReplaceValue{$1}, $3...)
   797      }
   798  
   799  prepared_statement
   800      : PREPARE identifier FROM STRING
   801      {
   802          $$ = StatementPreparation{Name: $2, Statement: value.NewString($4.Literal)}
   803      }
   804      | EXECUTE identifier
   805      {
   806          $$ = ExecuteStatement{BaseExpr: NewBaseExpr($1), Name: $2}
   807      }
   808      | EXECUTE identifier USING replace_values
   809      {
   810          $$ = ExecuteStatement{BaseExpr: NewBaseExpr($1), Name: $2, Values: $4}
   811      }
   812      | DISPOSE PREPARE identifier
   813      {
   814          $$ = DisposeStatement{Name: $3}
   815      }
   816  
   817  parameter
   818      : variable
   819      {
   820          $$ = VariableAssignment{Variable:$1}
   821      }
   822  
   823  parameters
   824      : parameter
   825      {
   826          $$ = []VariableAssignment{$1}
   827      }
   828      | parameters ',' parameter
   829      {
   830          $$ = append($1, $3)
   831      }
   832  
   833  optional_parameter
   834      : variable DEFAULT substantial_value
   835      {
   836          $$ = VariableAssignment{Variable: $1, Value: $3}
   837      }
   838  
   839  optional_parameters
   840      : optional_parameter
   841      {
   842          $$ = []VariableAssignment{$1}
   843      }
   844      | optional_parameter ',' optional_parameters
   845      {
   846          $$ = append([]VariableAssignment{$1}, $3...)
   847      }
   848  
   849  function_parameters
   850      : parameters
   851      {
   852          $$ = $1
   853      }
   854      | optional_parameters
   855      {
   856          $$ = $1
   857      }
   858      | parameters ',' optional_parameters
   859      {
   860          $$ = append($1, $3...)
   861      }
   862  
   863  user_defined_function_statement
   864      : DECLARE identifier FUNCTION '(' ')' AS BEGIN function_program END
   865      {
   866          $$ = FunctionDeclaration{Name: $2, Statements: $8}
   867      }
   868      | DECLARE identifier FUNCTION '(' function_parameters ')' AS BEGIN function_program END
   869      {
   870          $$ = FunctionDeclaration{Name: $2, Parameters: $5, Statements: $9}
   871      }
   872      | DECLARE identifier AGGREGATE '(' identifier ')' AS BEGIN function_program END
   873      {
   874          $$ = AggregateDeclaration{Name: $2, Cursor: $5, Statements: $9}
   875      }
   876      | DECLARE identifier AGGREGATE '(' identifier ',' function_parameters ')' AS BEGIN function_program END
   877      {
   878          $$ = AggregateDeclaration{Name: $2, Cursor: $5, Parameters: $7, Statements: $11}
   879      }
   880      | DISPOSE FUNCTION identifier
   881      {
   882          $$ = DisposeFunction{Name: $3}
   883      }
   884  
   885  fetch_position
   886      :
   887      {
   888          $$ = FetchPosition{}
   889      }
   890      | NEXT
   891      {
   892          $$ = FetchPosition{Position: $1}
   893      }
   894      | PRIOR
   895      {
   896          $$ = FetchPosition{Position: $1}
   897      }
   898      | FIRST
   899      {
   900          $$ = FetchPosition{Position: $1}
   901      }
   902      | LAST
   903      {
   904          $$ = FetchPosition{Position: $1}
   905      }
   906      | ABSOLUTE substantial_value
   907      {
   908          $$ = FetchPosition{BaseExpr: NewBaseExpr($1), Position: $1, Number: $2}
   909      }
   910      | RELATIVE substantial_value
   911      {
   912          $$ = FetchPosition{BaseExpr: NewBaseExpr($1), Position: $1, Number: $2}
   913      }
   914  
   915  cursor_status
   916      : CURSOR identifier IS negation OPEN
   917      {
   918          $$ = CursorStatus{Cursor: $2, Negation: $4, Type: $5}
   919      }
   920      | CURSOR identifier IS negation IN RANGE
   921      {
   922          $$ = CursorStatus{Cursor: $2, Negation: $4, Type: $6}
   923      }
   924      | CURSOR identifier COUNT
   925      {
   926          $$ = CursorAttrebute{Cursor: $2, Attrebute: $3}
   927      }
   928  
   929  command_statement
   930      : SET flag '=' identifier
   931      {
   932          $$ = SetFlag{BaseExpr: NewBaseExpr($1), Flag: $2, Value: $4}
   933      }
   934      | SET flag '=' substantial_value
   935      {
   936          $$ = SetFlag{BaseExpr: NewBaseExpr($1), Flag: $2, Value: $4}
   937      }
   938      | SET flag TO identifier
   939      {
   940          $$ = SetFlag{BaseExpr: NewBaseExpr($1), Flag: $2, Value: $4}
   941      }
   942      | SET flag TO substantial_value
   943      {
   944          $$ = SetFlag{BaseExpr: NewBaseExpr($1), Flag: $2, Value: $4}
   945      }
   946      | ADD substantial_value TO flag
   947      {
   948          $$ = AddFlagElement{BaseExpr: NewBaseExpr($1), Flag: $4, Value: $2}
   949      }
   950      | REMOVE substantial_value FROM flag
   951      {
   952          $$ = RemoveFlagElement{BaseExpr: NewBaseExpr($1), Flag: $4, Value: $2}
   953      }
   954      | SHOW flag
   955      {
   956          $$ = ShowFlag{BaseExpr: NewBaseExpr($1), Flag: $2}
   957      }
   958      | ECHO substantial_value
   959      {
   960          $$ = Echo{Value: $2}
   961      }
   962      | PRINT substantial_value
   963      {
   964          $$ = Print{Value: $2}
   965      }
   966      | PRINTF substantial_value
   967      {
   968          $$ = Printf{BaseExpr: NewBaseExpr($1), Format: $2}
   969      }
   970      | PRINTF substantial_value ',' substantial_values
   971      {
   972          $$ = Printf{BaseExpr: NewBaseExpr($1), Format: $2, Values: $4}
   973      }
   974      | PRINTF substantial_value USING substantial_values
   975      {
   976          $$ = Printf{BaseExpr: NewBaseExpr($1), Format: $2, Values: $4}
   977      }
   978      | SOURCE identifier
   979      {
   980          $$ = Source{BaseExpr: NewBaseExpr($1), FilePath: $2}
   981      }
   982      | SOURCE substantial_value
   983      {
   984          $$ = Source{BaseExpr: NewBaseExpr($1), FilePath: $2}
   985      }
   986      | EXECUTE substantial_value
   987      {
   988          $$ = Execute{BaseExpr: NewBaseExpr($1), Statements: $2}
   989      }
   990      | EXECUTE substantial_value USING substantial_values
   991      {
   992          $$ = Execute{BaseExpr: NewBaseExpr($1), Statements: $2, Values: $4}
   993      }
   994      | SYNTAX
   995      {
   996          $$ = Syntax{BaseExpr: NewBaseExpr($1)}
   997      }
   998      | SYNTAX values
   999      {
  1000          $$ = Syntax{BaseExpr: NewBaseExpr($1), Keywords: $2}
  1001      }
  1002      | SHOW identifier
  1003      {
  1004          $$ = ShowObjects{BaseExpr: NewBaseExpr($1), Type: $2}
  1005      }
  1006      | SHOW identifier FROM updatable_table_identifier
  1007      {
  1008          $$ = ShowFields{BaseExpr: NewBaseExpr($1), Type: $2, Table: $4}
  1009      }
  1010      | CHDIR identifier
  1011      {
  1012          $$ = Chdir{BaseExpr: NewBaseExpr($1), DirPath: $2}
  1013      }
  1014      | CHDIR substantial_value
  1015      {
  1016          $$ = Chdir{BaseExpr: NewBaseExpr($1), DirPath: $2}
  1017      }
  1018      | PWD
  1019      {
  1020          $$ = Pwd{BaseExpr: NewBaseExpr($1)}
  1021      }
  1022      | RELOAD identifier
  1023      {
  1024          $$ = Reload{BaseExpr: NewBaseExpr($1), Type: $2}
  1025      }
  1026  
  1027  trigger_statement
  1028      : TRIGGER identifier
  1029      {
  1030          $$ = Trigger{BaseExpr: NewBaseExpr($1), Event: $2}
  1031      }
  1032      | TRIGGER identifier substantial_value
  1033      {
  1034          $$ = Trigger{BaseExpr: NewBaseExpr($1), Event: $2, Message: $3}
  1035      }
  1036      | TRIGGER identifier INTEGER substantial_value
  1037      {
  1038          $$ = Trigger{BaseExpr: NewBaseExpr($1), Event: $2, Message: $4, Code: value.NewIntegerFromString($3.Literal)}
  1039      }
  1040  
  1041  select_query
  1042      : select_entity order_by_clause limit_clause
  1043      {
  1044          $$ = SelectQuery{
  1045              SelectEntity:  $1,
  1046              OrderByClause: $2,
  1047              LimitClause:   $3,
  1048          }
  1049      }
  1050      | select_entity order_by_clause limit_clause FOR UPDATE
  1051      {
  1052          $$ = SelectQuery{
  1053              SelectEntity:  $1,
  1054              OrderByClause: $2,
  1055              LimitClause:   $3,
  1056              Context:       $5,
  1057          }
  1058      }
  1059      | with_clause select_entity order_by_clause limit_clause
  1060      {
  1061          $$ = SelectQuery{
  1062              WithClause:    $1,
  1063              SelectEntity:  $2,
  1064              OrderByClause: $3,
  1065              LimitClause:   $4,
  1066          }
  1067      }
  1068      | with_clause select_entity order_by_clause limit_clause FOR UPDATE
  1069      {
  1070          $$ = SelectQuery{
  1071              WithClause:    $1,
  1072              SelectEntity:  $2,
  1073              OrderByClause: $3,
  1074              LimitClause:   $4,
  1075              Context:       $6,
  1076          }
  1077      }
  1078  
  1079  select_into_query
  1080      : select_clause into_clause from_clause where_clause group_by_clause having_clause order_by_clause limit_clause
  1081      {
  1082          $$ = SelectQuery{
  1083              SelectEntity:  SelectEntity{
  1084                  SelectClause:  $1,
  1085                  IntoClause:    $2,
  1086                  FromClause:    $3,
  1087                  WhereClause:   $4,
  1088                  GroupByClause: $5,
  1089                  HavingClause:  $6,
  1090              },
  1091              OrderByClause: $7,
  1092              LimitClause:   $8,
  1093          }
  1094      }
  1095      | select_clause into_clause from_clause where_clause group_by_clause having_clause order_by_clause limit_clause FOR UPDATE
  1096      {
  1097          $$ = SelectQuery{
  1098              SelectEntity:  SelectEntity{
  1099                  SelectClause:  $1,
  1100                  IntoClause:    $2,
  1101                  FromClause:    $3,
  1102                  WhereClause:   $4,
  1103                  GroupByClause: $5,
  1104                  HavingClause:  $6,
  1105              },
  1106              OrderByClause: $7,
  1107              LimitClause:   $8,
  1108              Context:       $10,
  1109          }
  1110      }
  1111      | with_clause select_clause into_clause from_clause where_clause group_by_clause having_clause order_by_clause limit_clause
  1112      {
  1113          $$ = SelectQuery{
  1114              WithClause:    $1,
  1115              SelectEntity:  SelectEntity{
  1116                  SelectClause:  $2,
  1117                  IntoClause:    $3,
  1118                  FromClause:    $4,
  1119                  WhereClause:   $5,
  1120                  GroupByClause: $6,
  1121                  HavingClause:  $7,
  1122              },
  1123              OrderByClause: $8,
  1124              LimitClause:   $9,
  1125          }
  1126      }
  1127      | with_clause select_clause into_clause from_clause where_clause group_by_clause having_clause order_by_clause limit_clause FOR UPDATE
  1128      {
  1129          $$ = SelectQuery{
  1130              WithClause:    $1,
  1131              SelectEntity:  SelectEntity{
  1132                  SelectClause:  $2,
  1133                  IntoClause:    $3,
  1134                  FromClause:    $4,
  1135                  WhereClause:   $5,
  1136                  GroupByClause: $6,
  1137                  HavingClause:  $7,
  1138              },
  1139              OrderByClause: $8,
  1140              LimitClause:   $9,
  1141              Context:       $11,
  1142          }
  1143      }
  1144  
  1145  select_entity
  1146      : select_clause from_clause where_clause group_by_clause having_clause
  1147      {
  1148          $$ = SelectEntity{
  1149              SelectClause:  $1,
  1150              FromClause:    $2,
  1151              WhereClause:   $3,
  1152              GroupByClause: $4,
  1153              HavingClause:  $5,
  1154          }
  1155      }
  1156      | select_set_entity UNION all select_set_entity
  1157      {
  1158          $$ = SelectSet{
  1159              LHS:      $1,
  1160              Operator: $2,
  1161              All:      $3,
  1162              RHS:      $4,
  1163          }
  1164      }
  1165      | select_set_entity INTERSECT all select_set_entity
  1166      {
  1167          $$ = SelectSet{
  1168              LHS:      $1,
  1169              Operator: $2,
  1170              All:      $3,
  1171              RHS:      $4,
  1172          }
  1173      }
  1174      | select_set_entity EXCEPT all select_set_entity
  1175      {
  1176          $$ = SelectSet{
  1177              LHS:      $1,
  1178              Operator: $2,
  1179              All:      $3,
  1180              RHS:      $4,
  1181          }
  1182      }
  1183  
  1184  select_set_entity
  1185      : select_entity
  1186      {
  1187          $$ = $1
  1188      }
  1189      | subquery
  1190      {
  1191          $$ = $1
  1192      }
  1193  
  1194  select_clause
  1195      : SELECT distinct fields
  1196      {
  1197          $$ = SelectClause{BaseExpr: NewBaseExpr($1), Distinct: $2, Fields: $3}
  1198      }
  1199  
  1200  into_clause
  1201      : INTO variables
  1202      {
  1203          $$ = IntoClause{Variables: $2}
  1204      }
  1205  
  1206  from_clause
  1207      :
  1208      {
  1209          $$ = nil
  1210      }
  1211      | FROM tables
  1212      {
  1213          $$ = FromClause{Tables: $2}
  1214      }
  1215  
  1216  where_clause
  1217      :
  1218      {
  1219          $$ = nil
  1220      }
  1221      | WHERE value
  1222      {
  1223          $$ = WhereClause{Filter: $2}
  1224      }
  1225  
  1226  group_by_clause
  1227      :
  1228      {
  1229          $$ = nil
  1230      }
  1231      | GROUP BY values
  1232      {
  1233          $$ = GroupByClause{Items: $3}
  1234      }
  1235  
  1236  having_clause
  1237      :
  1238      {
  1239          $$ = nil
  1240      }
  1241      | HAVING value
  1242      {
  1243          $$ = HavingClause{Filter: $2}
  1244      }
  1245  
  1246  order_by_clause
  1247      :
  1248      {
  1249          $$ = nil
  1250      }
  1251      | ORDER BY order_items
  1252      {
  1253          $$ = OrderByClause{Items: $3}
  1254      }
  1255  
  1256  limit_clause
  1257      : offset_clause
  1258      {
  1259          if $1 == nil {
  1260              $$ = $1
  1261          } else {
  1262              $$ = LimitClause{BaseExpr: $1.(OffsetClause).BaseExpr, OffsetClause: $1}
  1263          }
  1264      }
  1265      | offset_clause FETCH limit_fetch_position substantial_value limit_fetch_unit limit_restriction
  1266      {
  1267          var base *BaseExpr
  1268          if $1 == nil {
  1269              base = NewBaseExpr($2)
  1270          } else {
  1271              base = $1.(OffsetClause).BaseExpr
  1272          }
  1273          $$ = LimitClause{BaseExpr: base, Type: $2, Position: $3, Value: $4, Unit: $5, Restriction: $6, OffsetClause: $1}
  1274      }
  1275      | LIMIT substantial_value limit_unit limit_restriction offset_clause
  1276      {
  1277          $$ = LimitClause{BaseExpr: NewBaseExpr($1), Type: $1, Value: $2, Unit: $3, Restriction: $4, OffsetClause: $5}
  1278      }
  1279  
  1280  limit_restriction
  1281      :
  1282      {
  1283          $$ = Token{}
  1284      }
  1285      | ONLY
  1286      {
  1287          $$ = $1
  1288      }
  1289      | WITH TIES
  1290      {
  1291          $$ = $2
  1292      }
  1293  
  1294  limit_fetch_position
  1295      : FIRST
  1296      {
  1297          $$ = $1
  1298      }
  1299      | NEXT
  1300      {
  1301          $$ = $1
  1302      }
  1303  
  1304  limit_unit
  1305      :
  1306      {
  1307          $$ = Token{}
  1308      }
  1309      | limit_fetch_unit
  1310      {
  1311          $$ = $1
  1312      }
  1313  
  1314  limit_fetch_unit
  1315      : PERCENT
  1316      {
  1317          $$ = $1
  1318      }
  1319      | ROW
  1320      {
  1321          $$ = $1
  1322      }
  1323      | ROWS
  1324      {
  1325          $$ = $1
  1326      }
  1327  
  1328  offset_unit
  1329      :
  1330      {
  1331          $$ = Token{}
  1332      }
  1333      | ROW
  1334      {
  1335          $$ = $1
  1336      }
  1337      | ROWS
  1338      {
  1339          $$ = $1
  1340      }
  1341  
  1342  offset_clause
  1343      :
  1344      {
  1345          $$ = nil
  1346      }
  1347      | OFFSET substantial_value offset_unit
  1348      {
  1349          $$ = OffsetClause{BaseExpr: NewBaseExpr($1), Value: $2, Unit: $3}
  1350      }
  1351  
  1352  with_clause
  1353      :
  1354      {
  1355          $$ = nil
  1356      }
  1357      | WITH inline_tables
  1358      {
  1359          $$ = WithClause{InlineTables: $2}
  1360      }
  1361  
  1362  inline_table
  1363      : recursive identifier AS '(' select_query ')'
  1364      {
  1365          $$ = InlineTable{Recursive: $1, Name: $2, Query: $5.(SelectQuery)}
  1366      }
  1367      | recursive identifier '(' identifiers ')' AS '(' select_query ')'
  1368      {
  1369          $$ = InlineTable{Recursive: $1, Name: $2, Fields: $4, Query: $8.(SelectQuery)}
  1370      }
  1371  
  1372  inline_tables
  1373      : inline_table
  1374      {
  1375          $$ = []QueryExpression{$1}
  1376      }
  1377      | inline_table ',' inline_tables
  1378      {
  1379          $$ = append([]QueryExpression{$1}, $3...)
  1380      }
  1381  
  1382  primitive_type
  1383      : STRING
  1384      {
  1385          $$ = NewStringValue($1.Literal)
  1386      }
  1387      | INTEGER
  1388      {
  1389          i, err := strconv.ParseInt($1.Literal, 10, 64)
  1390          if err != nil {
  1391            $$ = NewFloatValueFromString($1.Literal)
  1392          } else {
  1393            iv := NewIntegerValue(i)
  1394            iv.Literal = $1.Literal
  1395            $$ = iv
  1396          }
  1397      }
  1398      | FLOAT
  1399      {
  1400          $$ = NewFloatValueFromString($1.Literal)
  1401      }
  1402      | ternary
  1403      {
  1404          $$ = $1
  1405      }
  1406      | null
  1407      {
  1408          $$ = $1
  1409      }
  1410  
  1411  ternary
  1412      : TERNARY
  1413      {
  1414          $$ = NewTernaryValueFromString($1.Literal)
  1415      }
  1416  
  1417  null
  1418      : NULL
  1419      {
  1420          $$ = NewNullValue()
  1421      }
  1422  
  1423  field_reference
  1424      : identifier
  1425      {
  1426          $$ = FieldReference{BaseExpr: $1.BaseExpr, Column: $1}
  1427      }
  1428      | identifier '.' identifier
  1429      {
  1430          $$ = FieldReference{BaseExpr: $1.BaseExpr, View: $1, Column: $3}
  1431      }
  1432      | STDIN '.' identifier
  1433      {
  1434          $$ = FieldReference{BaseExpr: NewBaseExpr($1), View: Identifier{BaseExpr: NewBaseExpr($1), Literal: $1.Literal}, Column: $3}
  1435      }
  1436      | identifier '.' INTEGER
  1437      {
  1438          $$ = ColumnNumber{BaseExpr: $1.BaseExpr, View: $1, Number: value.NewIntegerFromString($3.Literal)}
  1439      }
  1440      | STDIN '.' INTEGER
  1441      {
  1442          $$ = ColumnNumber{BaseExpr: NewBaseExpr($1), View: Identifier{BaseExpr: NewBaseExpr($1), Literal: $1.Literal}, Number: value.NewIntegerFromString($3.Literal)}
  1443      }
  1444  
  1445  value
  1446      : field_reference
  1447      {
  1448          $$ = $1
  1449      }
  1450      | substantial_value
  1451      {
  1452          $$ = $1
  1453      }
  1454      | '(' value ')'
  1455      {
  1456          $$ = Parentheses{Expr: $2}
  1457      }
  1458  
  1459  substantial_value
  1460      : primitive_type
  1461      {
  1462          $$ = $1
  1463      }
  1464      | arithmetic
  1465      {
  1466          $$ = $1
  1467      }
  1468      | string_operation
  1469      {
  1470          $$ = $1
  1471      }
  1472      | subquery
  1473      {
  1474          $$ = $1
  1475      }
  1476      | function
  1477      {
  1478          $$ = $1
  1479      }
  1480      | aggregate_function
  1481      {
  1482          $$ = $1
  1483      }
  1484      | analytic_function
  1485      {
  1486          $$ = $1
  1487      }
  1488      | case_expr
  1489      {
  1490          $$ = $1
  1491      }
  1492      | comparison
  1493      {
  1494          $$ = $1
  1495      }
  1496      | logic
  1497      {
  1498          $$ = $1
  1499      }
  1500      | variable
  1501      {
  1502          $$ = $1
  1503      }
  1504      | variable_substitution
  1505      {
  1506          $$ = $1
  1507      }
  1508      | environment_variable
  1509      {
  1510          $$ = $1
  1511      }
  1512      | runtime_information
  1513      {
  1514          $$ = $1
  1515      }
  1516      | constant
  1517      {
  1518          $$ = $1
  1519      }
  1520      | flag
  1521      {
  1522          $$ = $1
  1523      }
  1524      | cursor_status
  1525      {
  1526          $$ = $1
  1527      }
  1528      | '(' substantial_value ')'
  1529      {
  1530          $$ = Parentheses{Expr: $2}
  1531      }
  1532      | PLACEHOLDER
  1533      {
  1534          name := ""
  1535          if $1.Literal[0] == ':' {
  1536              name = $1.Literal[1:]
  1537          }
  1538          $$ = Placeholder{BaseExpr: NewBaseExpr($1), Literal: $1.Literal, Ordinal: $1.HolderOrdinal, Name: name}
  1539      }
  1540  
  1541  wildcard
  1542      : '*'
  1543      {
  1544          $$ = AllColumns{BaseExpr: NewBaseExpr($1)}
  1545      }
  1546  
  1547  row_value
  1548      : '(' values ')'
  1549      {
  1550          $$ = RowValue{BaseExpr: NewBaseExpr($1), Value: ValueList{Values: $2}}
  1551      }
  1552      | subquery
  1553      {
  1554          $$ = RowValue{BaseExpr: $1.GetBaseExpr(), Value: $1}
  1555      }
  1556      | JSON_ROW '(' value ',' value ')'
  1557      {
  1558          $$ = RowValue{BaseExpr: NewBaseExpr($1), Value: JsonQuery{JsonQuery: $1, Query: $3, JsonText: $5}}
  1559      }
  1560  
  1561  row_values
  1562      : row_value
  1563      {
  1564          $$ = []QueryExpression{$1}
  1565      }
  1566      | row_value ',' row_values
  1567      {
  1568          $$ = append([]QueryExpression{$1}, $3...)
  1569      }
  1570  
  1571  order_items
  1572      : order_item
  1573      {
  1574          $$ = []QueryExpression{$1}
  1575      }
  1576      | order_item ',' order_items
  1577      {
  1578          $$ = append([]QueryExpression{$1}, $3...)
  1579      }
  1580  
  1581  order_item
  1582      : value order_direction
  1583      {
  1584          $$ = OrderItem{Value: $1, Direction: $2}
  1585      }
  1586      | value order_direction NULLS order_null_position
  1587      {
  1588          $$ = OrderItem{Value: $1, Direction: $2, NullsPosition: $4}
  1589      }
  1590  
  1591  order_direction
  1592      :
  1593      {
  1594          $$ = Token{}
  1595      }
  1596      | ASC
  1597      {
  1598          $$ = $1
  1599      }
  1600      | DESC
  1601      {
  1602          $$ = $1
  1603      }
  1604  
  1605  order_null_position
  1606      : FIRST
  1607      {
  1608          $$ = $1
  1609      }
  1610      | LAST
  1611      {
  1612          $$ = $1
  1613      }
  1614  
  1615  subquery
  1616      : '(' select_query ')'
  1617      {
  1618          $$ = Subquery{BaseExpr: NewBaseExpr($1), Query: $2.(SelectQuery)}
  1619      }
  1620  
  1621  string_operation
  1622      : value STRING_OP value
  1623      {
  1624          var item1 []QueryExpression
  1625          var item2 []QueryExpression
  1626  
  1627          c1, ok := $1.(Concat)
  1628          if ok {
  1629              item1 = c1.Items
  1630          } else {
  1631              item1 = []QueryExpression{$1}
  1632          }
  1633  
  1634          c2, ok := $3.(Concat)
  1635          if ok {
  1636              item2 = c2.Items
  1637          } else {
  1638              item2 = []QueryExpression{$3}
  1639          }
  1640  
  1641          $$ = Concat{Items: append(item1, item2...)}
  1642      }
  1643  
  1644  matrix_value
  1645      : '(' row_values ')'
  1646      {
  1647          $$ = RowValueList{RowValues: $2}
  1648      }
  1649      | subquery
  1650      {
  1651          $$ = $1
  1652      }
  1653      | JSON_ROW '(' value ',' value ')'
  1654      {
  1655          $$ = JsonQuery{BaseExpr: NewBaseExpr($1), JsonQuery: $1, Query: $3, JsonText: $5}
  1656      }
  1657  
  1658  comparison
  1659      : value COMPARISON_OP value
  1660      {
  1661          $$ = Comparison{LHS: $1, Operator: $2, RHS: $3}
  1662      }
  1663      | row_value COMPARISON_OP row_value
  1664      {
  1665          $$ = Comparison{LHS: $1, Operator: $2, RHS: $3}
  1666      }
  1667      | value '=' value
  1668      {
  1669          $$ = Comparison{LHS: $1, Operator: $2, RHS: $3}
  1670      }
  1671      | row_value '=' row_value
  1672      {
  1673          $$ = Comparison{LHS: $1, Operator: $2, RHS: $3}
  1674      }
  1675      | value IS negation ternary
  1676      {
  1677          $$ = Is{LHS: $1, RHS: $4, Negation: $3}
  1678      }
  1679      | value IS negation null
  1680      {
  1681          $$ = Is{LHS: $1, RHS: $4, Negation: $3}
  1682      }
  1683      | value BETWEEN value AND value
  1684      {
  1685          $$ = Between{LHS: $1, Low: $3, High: $5}
  1686      }
  1687      | value NOT BETWEEN value AND value
  1688      {
  1689          $$ = Between{LHS: $1, Low: $4, High: $6, Negation: $2}
  1690      }
  1691      | row_value negation BETWEEN row_value AND row_value
  1692      {
  1693          $$ = Between{LHS: $1, Low: $4, High: $6, Negation: $2}
  1694      }
  1695      | value IN row_value
  1696      {
  1697          $$ = In{LHS: $1, Values: $3}
  1698      }
  1699      | value NOT IN row_value
  1700      {
  1701          $$ = In{LHS: $1, Values: $4, Negation: $2}
  1702      }
  1703      | row_value negation IN matrix_value
  1704      {
  1705          $$ = In{LHS: $1, Values: $4, Negation: $2}
  1706      }
  1707      | value LIKE value
  1708      {
  1709          $$ = Like{LHS: $1, Pattern: $3}
  1710      }
  1711      | value NOT LIKE value
  1712      {
  1713          $$ = Like{LHS: $1, Pattern: $4, Negation: $2}
  1714      }
  1715      | value comparison_operator ANY row_value
  1716      {
  1717          $$ = Any{LHS: $1, Operator: $2, Values: $4}
  1718      }
  1719      | row_value comparison_operator ANY matrix_value
  1720      {
  1721          $$ = Any{LHS: $1, Operator: $2, Values: $4}
  1722      }
  1723      | value comparison_operator ALL row_value
  1724      {
  1725          $$ = All{LHS: $1, Operator: $2, Values: $4}
  1726      }
  1727      | row_value comparison_operator ALL matrix_value
  1728      {
  1729          $$ = All{LHS: $1, Operator: $2, Values: $4}
  1730      }
  1731      | EXISTS subquery
  1732      {
  1733          $$ = Exists{Query: $2.(Subquery)}
  1734      }
  1735  
  1736  arithmetic
  1737      : value '+' value
  1738      {
  1739          $$ = Arithmetic{BaseExpr: NewBaseExpr($2), LHS: $1, Operator: $2, RHS: $3}
  1740      }
  1741      | value '-' value
  1742      {
  1743          $$ = Arithmetic{BaseExpr: NewBaseExpr($2), LHS: $1, Operator: $2, RHS: $3}
  1744      }
  1745      | value '*' value
  1746      {
  1747          $$ = Arithmetic{BaseExpr: NewBaseExpr($2), LHS: $1, Operator: $2, RHS: $3}
  1748      }
  1749      | value '/' value
  1750      {
  1751          $$ = Arithmetic{BaseExpr: NewBaseExpr($2), LHS: $1, Operator: $2, RHS: $3}
  1752      }
  1753      | value '%' value
  1754      {
  1755          $$ = Arithmetic{BaseExpr: NewBaseExpr($2), LHS: $1, Operator: $2, RHS: $3}
  1756      }
  1757      | '-' value %prec UMINUS
  1758      {
  1759          $$ = UnaryArithmetic{Operand: $2, Operator: $1}
  1760      }
  1761      | '+' value %prec UPLUS
  1762      {
  1763          $$ = UnaryArithmetic{Operand: $2, Operator: $1}
  1764      }
  1765  
  1766  logic
  1767      : value OR value
  1768      {
  1769          $$ = Logic{LHS: $1, Operator: $2, RHS: $3}
  1770      }
  1771      | value AND value
  1772      {
  1773          $$ = Logic{LHS: $1, Operator: $2, RHS: $3}
  1774      }
  1775      | NOT value
  1776      {
  1777          $$ = UnaryLogic{Operand: $2, Operator: $1}
  1778      }
  1779      | '!' value
  1780      {
  1781          $$ = UnaryLogic{Operand: $2, Operator: $1}
  1782      }
  1783  
  1784  arguments
  1785      :
  1786      {
  1787          $$ = nil
  1788      }
  1789      | values
  1790      {
  1791          $$ = $1
  1792      }
  1793  
  1794  function
  1795      : identifier '(' arguments ')'
  1796      {
  1797          $$ = Function{BaseExpr: $1.BaseExpr, Name: $1.Literal, Args: $3}
  1798      }
  1799      | SUBSTRING '(' arguments ')'
  1800      {
  1801          $$ = Function{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Args: $3}
  1802      }
  1803      | SUBSTRING '(' value FROM value ')'
  1804      {
  1805          $$ = Function{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Args: []QueryExpression{$3, $5}, From: $4}
  1806      }
  1807      | SUBSTRING '(' value FROM value FOR value ')'
  1808      {
  1809          $$ = Function{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Args: []QueryExpression{$3, $5, $7}, From: $4, For: $6}
  1810      }
  1811      | JSON_OBJECT '(' ')'
  1812      {
  1813          $$ = Function{BaseExpr: NewBaseExpr($1), Name: $1.Literal}
  1814      }
  1815      | JSON_OBJECT '(' fields ')'
  1816      {
  1817          $$ = Function{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Args: $3}
  1818      }
  1819      | IF '(' arguments ')'
  1820      {
  1821          $$ = Function{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Args: $3}
  1822      }
  1823      | REPLACE '(' arguments ')'
  1824      {
  1825          $$ = Function{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Args: $3}
  1826      }
  1827  
  1828  
  1829  aggregate_function
  1830      : identifier '(' distinct arguments ')'
  1831      {
  1832          $$ = AggregateFunction{BaseExpr: $1.BaseExpr, Name: $1.Literal, Distinct: $3, Args: $4}
  1833      }
  1834      | AGGREGATE_FUNCTION '(' distinct arguments ')'
  1835      {
  1836          $$ = AggregateFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Distinct: $3, Args: $4}
  1837      }
  1838      | VAR '(' distinct arguments ')'
  1839      {
  1840          $$ = AggregateFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Distinct: $3, Args: $4}
  1841      }
  1842      | COUNT '(' distinct arguments ')'
  1843      {
  1844          $$ = AggregateFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Distinct: $3, Args: $4}
  1845      }
  1846      | COUNT '(' distinct wildcard ')'
  1847      {
  1848          $$ = AggregateFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Distinct: $3, Args: []QueryExpression{$4}}
  1849      }
  1850      | list_function
  1851      {
  1852          $$ = $1
  1853      }
  1854  
  1855  list_function
  1856      : LIST_FUNCTION '(' distinct arguments ')'
  1857      {
  1858          $$ = ListFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Distinct: $3, Args: $4}
  1859      }
  1860      | LIST_FUNCTION '(' distinct arguments ')' WITHIN GROUP '(' order_by_clause ')'
  1861      {
  1862          $$ = ListFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Distinct: $3, Args: $4, OrderBy: $9}
  1863      }
  1864  
  1865  analytic_function
  1866      : identifier '(' arguments ')' OVER '(' analytic_clause_with_windowing ')'
  1867      {
  1868          $$ = AnalyticFunction{BaseExpr: $1.BaseExpr, Name: $1.Literal, Args: $3, AnalyticClause: $7.(AnalyticClause)}
  1869      }
  1870      | identifier '(' distinct arguments ')' OVER '(' analytic_clause_with_windowing ')'
  1871      {
  1872          $$ = AnalyticFunction{BaseExpr: $1.BaseExpr, Name: $1.Literal, Distinct: $3, Args: $4, AnalyticClause: $8.(AnalyticClause)}
  1873      }
  1874      | AGGREGATE_FUNCTION '(' distinct arguments ')' OVER '(' analytic_clause_with_windowing ')'
  1875      {
  1876          $$ = AnalyticFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Distinct: $3, Args: $4, AnalyticClause: $8.(AnalyticClause)}
  1877      }
  1878      | VAR '(' distinct arguments ')' OVER '(' analytic_clause_with_windowing ')'
  1879      {
  1880          $$ = AnalyticFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Distinct: $3, Args: $4, AnalyticClause: $8.(AnalyticClause)}
  1881      }
  1882      | COUNT '(' distinct arguments ')' OVER '(' analytic_clause_with_windowing ')'
  1883      {
  1884          $$ = AnalyticFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Distinct: $3, Args: $4, AnalyticClause: $8.(AnalyticClause)}
  1885      }
  1886      | COUNT '(' distinct wildcard ')' OVER '(' analytic_clause_with_windowing ')'
  1887      {
  1888          $$ = AnalyticFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Distinct: $3, Args: []QueryExpression{$4}, AnalyticClause: $8.(AnalyticClause)}
  1889      }
  1890      | LIST_FUNCTION '(' distinct arguments ')' OVER '(' analytic_clause ')'
  1891      {
  1892          $$ = AnalyticFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Distinct: $3, Args: $4, AnalyticClause: $8.(AnalyticClause)}
  1893      }
  1894      | ANALYTIC_FUNCTION '(' arguments ')' OVER '(' analytic_clause ')'
  1895      {
  1896          $$ = AnalyticFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Args: $3, AnalyticClause: $7.(AnalyticClause)}
  1897      }
  1898      | FUNCTION_NTH '(' arguments ')' OVER '(' analytic_clause_with_windowing ')'
  1899      {
  1900          $$ = AnalyticFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Args: $3, AnalyticClause: $7.(AnalyticClause)}
  1901      }
  1902      | FUNCTION_NTH '(' arguments ')' IGNORE NULLS OVER '(' analytic_clause_with_windowing ')'
  1903      {
  1904          $$ = AnalyticFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Args: $3, IgnoreType: $6, AnalyticClause: $9.(AnalyticClause)}
  1905      }
  1906      | FUNCTION_WITH_INS '(' arguments ')' OVER '(' analytic_clause ')'
  1907      {
  1908          $$ = AnalyticFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Args: $3, AnalyticClause: $7.(AnalyticClause)}
  1909      }
  1910      | FUNCTION_WITH_INS '(' arguments ')' IGNORE NULLS OVER '(' analytic_clause ')'
  1911      {
  1912          $$ = AnalyticFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Args: $3, IgnoreType: $6, AnalyticClause: $9.(AnalyticClause)}
  1913      }
  1914  
  1915  analytic_clause
  1916      : partition_clause order_by_clause
  1917      {
  1918          $$ = AnalyticClause{PartitionClause: $1, OrderByClause: $2}
  1919      }
  1920  
  1921  analytic_clause_with_windowing
  1922      : analytic_clause
  1923      {
  1924          $$ = $1
  1925      }
  1926      | partition_clause ORDER BY order_items windowing_clause
  1927      {
  1928          $$ = AnalyticClause{PartitionClause: $1, OrderByClause: OrderByClause{Items: $4}, WindowingClause: $5}
  1929      }
  1930  
  1931  partition_clause
  1932      :
  1933      {
  1934          $$ = nil
  1935      }
  1936      | PARTITION BY values
  1937      {
  1938          $$ = PartitionClause{Values: $3}
  1939      }
  1940  
  1941  windowing_clause
  1942      : ROWS window_position
  1943      {
  1944          $$ = WindowingClause{FrameLow: $2}
  1945      }
  1946      | ROWS BETWEEN window_frame_low AND window_frame_high
  1947      {
  1948          $$ = WindowingClause{FrameLow: $3, FrameHigh: $5}
  1949      }
  1950  
  1951  window_position
  1952      : UNBOUNDED PRECEDING
  1953      {
  1954          $$ = WindowFramePosition{Direction: $2, Unbounded: $1}
  1955      }
  1956      | INTEGER PRECEDING
  1957      {
  1958          i, _ := strconv.Atoi($1.Literal)
  1959          $$ = WindowFramePosition{Direction: $2, Offset: i}
  1960      }
  1961      | CURRENT ROW
  1962      {
  1963          $$ = WindowFramePosition{Direction: $1}
  1964      }
  1965  
  1966  window_relative_position
  1967      : INTEGER PRECEDING
  1968      {
  1969          i, _ := strconv.Atoi($1.Literal)
  1970          $$ = WindowFramePosition{Direction: $2, Offset: i}
  1971      }
  1972      | INTEGER FOLLOWING
  1973      {
  1974          i, _ := strconv.Atoi($1.Literal)
  1975          $$ = WindowFramePosition{Direction: $2, Offset: i}
  1976      }
  1977      | CURRENT ROW
  1978      {
  1979          $$ = WindowFramePosition{Direction: $1}
  1980      }
  1981  
  1982  window_frame_low
  1983      : UNBOUNDED PRECEDING
  1984      {
  1985          $$ = WindowFramePosition{Direction: $2, Unbounded: $1}
  1986      }
  1987      | window_relative_position
  1988      {
  1989          $$ = $1
  1990      }
  1991  
  1992  window_frame_high
  1993      : UNBOUNDED FOLLOWING
  1994      {
  1995          $$ = WindowFramePosition{Direction: $2, Unbounded: $1}
  1996      }
  1997      | window_relative_position
  1998      {
  1999          $$ = $1
  2000      }
  2001  
  2002  table_identifier
  2003      : identifier
  2004      {
  2005          $$ = $1
  2006      }
  2007      | URL
  2008      {
  2009          $$ = Url{BaseExpr: NewBaseExpr($1), Raw: $1.Literal}
  2010      }
  2011      | TABLE_FUNCTION '(' arguments ')'
  2012      {
  2013          $$ = TableFunction{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Args: $3}
  2014      }
  2015      | STDIN
  2016      {
  2017          $$ = Stdin{BaseExpr: NewBaseExpr($1)}
  2018      }
  2019  
  2020  table_format
  2021      : CSV
  2022      {
  2023          $$ = $1
  2024      }
  2025      | JSON
  2026      {
  2027          $$ = $1
  2028      }
  2029      | JSONL
  2030      {
  2031          $$ = $1
  2032      }
  2033      | FIXED
  2034      {
  2035          $$ = $1
  2036      }
  2037      | LTSV
  2038      {
  2039          $$ = $1
  2040      }
  2041  
  2042  inline_table_format
  2043      : CSV_INLINE
  2044      {
  2045          $$ = $1
  2046      }
  2047      | JSON_INLINE
  2048      {
  2049          $$ = $1
  2050      }
  2051      | JSON_TABLE
  2052      {
  2053          $$ = $1
  2054      }
  2055  
  2056  format_specified_function
  2057      : table_format '(' table_identifier ')'
  2058      {
  2059          $$ = FormatSpecifiedFunction{BaseExpr: NewBaseExpr($1), Type: $1, Path: $3, Args: nil}
  2060      }
  2061      | table_format '(' table_identifier ',' arguments ')'
  2062      {
  2063          $$ = FormatSpecifiedFunction{BaseExpr: NewBaseExpr($1), Type: $1, Path: $3, Args: $5}
  2064      }
  2065      | table_format '(' substantial_value ',' table_identifier ')'
  2066      {
  2067          $$ = FormatSpecifiedFunction{BaseExpr: NewBaseExpr($1), Type: $1, FormatElement: $3, Path: $5, Args: nil}
  2068      }
  2069      | table_format '(' substantial_value ',' table_identifier ',' arguments ')'
  2070      {
  2071          $$ = FormatSpecifiedFunction{BaseExpr: NewBaseExpr($1), Type: $1, FormatElement: $3, Path: $5, Args: $7}
  2072      }
  2073  
  2074  inline_format_specified_function
  2075      : inline_table_format '(' substantial_value ',' identifier ')'
  2076      {
  2077          $$ = FormatSpecifiedFunction{BaseExpr: NewBaseExpr($1), Type: $1, FormatElement: $3, Path: $5, Args: nil}
  2078      }
  2079      | inline_table_format '(' substantial_value ',' identifier ',' arguments ')'
  2080      {
  2081          $$ = FormatSpecifiedFunction{BaseExpr: NewBaseExpr($1), Type: $1, FormatElement: $3, Path: $5, Args: $7}
  2082      }
  2083      | inline_table_format '(' substantial_value ',' substantial_value ')'
  2084      {
  2085          $$ = FormatSpecifiedFunction{BaseExpr: NewBaseExpr($1), Type: $1, FormatElement: $3, Path: $5, Args: nil}
  2086      }
  2087      | inline_table_format '(' substantial_value ',' substantial_value ',' arguments ')'
  2088      {
  2089          $$ = FormatSpecifiedFunction{BaseExpr: NewBaseExpr($1), Type: $1, FormatElement: $3, Path: $5, Args: $7}
  2090      }
  2091  
  2092  updatable_table_identifier
  2093      : table_identifier
  2094      {
  2095          $$ = $1
  2096      }
  2097      | format_specified_function
  2098      {
  2099          $$ = $1
  2100      }
  2101  
  2102  table_object
  2103      : updatable_table_identifier
  2104      {
  2105          $$ = $1
  2106      }
  2107      | inline_format_specified_function
  2108      {
  2109          $$ = $1
  2110      }
  2111  
  2112  laterable_query_table
  2113      : subquery
  2114      {
  2115          $$ = Table{Object: $1}
  2116      }
  2117      | subquery identifier
  2118      {
  2119          $$ = Table{Object: $1, Alias: $2}
  2120      }
  2121      | subquery AS identifier
  2122      {
  2123          $$ = Table{Object: $1, As: $2, Alias: $3}
  2124      }
  2125  
  2126  joinable_tables
  2127      : table
  2128      {
  2129          $$ = []QueryExpression{$1}
  2130      }
  2131      | LATERAL laterable_query_table
  2132      {
  2133          $2.Lateral = $1
  2134          $2.BaseExpr = NewBaseExpr($1)
  2135          $$ = []QueryExpression{$2}
  2136      }
  2137      | laterable_query_table ',' joinable_tables
  2138      {
  2139          $$ = append([]QueryExpression{$1}, $3...)
  2140      }
  2141      | LATERAL laterable_query_table ',' joinable_tables
  2142      {
  2143          $2.Lateral = $1
  2144          $2.BaseExpr = NewBaseExpr($1)
  2145          $$ = append([]QueryExpression{$2}, $4...)
  2146      }
  2147  
  2148  table
  2149      : table_object
  2150      {
  2151          $$ = Table{Object: $1}
  2152      }
  2153      | table_object identifier
  2154      {
  2155          $$ = Table{Object: $1, Alias: $2}
  2156      }
  2157      | table_object AS identifier
  2158      {
  2159          $$ = Table{Object: $1, As: $2, Alias: $3}
  2160      }
  2161      | join
  2162      {
  2163          $$ = Table{Object: $1}
  2164      }
  2165      | DUAL
  2166      {
  2167          $$ = Table{Object: Dual{}}
  2168      }
  2169      | laterable_query_table
  2170      {
  2171          $$ = $1
  2172      }
  2173      | '(' table ')'
  2174      {
  2175          $$ = Parentheses{Expr: $2}
  2176      }
  2177  
  2178  join
  2179      : table CROSS JOIN table
  2180      {
  2181          $$ = Join{Table: $1, JoinTable: $4, JoinType: $2, Condition: nil}
  2182      }
  2183      | table join_type_inner JOIN table join_condition
  2184      {
  2185          $$ = Join{Table: $1, JoinTable: $4, JoinType: $2, Condition: $5}
  2186      }
  2187      | table join_outer_direction join_type_outer JOIN table join_condition
  2188      {
  2189          $$ = Join{Table: $1, JoinTable: $5, JoinType: $3, Direction: $2, Condition: $6}
  2190      }
  2191      | table NATURAL join_type_inner JOIN table
  2192      {
  2193          $$ = Join{Table: $1, JoinTable: $5, JoinType: $3, Natural: $2}
  2194      }
  2195      | table NATURAL join_outer_direction join_type_outer JOIN table
  2196      {
  2197          $$ = Join{Table: $1, JoinTable: $6, JoinType: $4, Direction: $3, Natural: $2}
  2198      }
  2199      | table CROSS JOIN LATERAL laterable_query_table
  2200      {
  2201          $5.Lateral = $4
  2202          $5.BaseExpr = NewBaseExpr($4)
  2203          $$ = Join{Table: $1, JoinTable: $5, JoinType: $2, Condition: nil}
  2204      }
  2205      | table join_type_inner JOIN LATERAL laterable_query_table join_condition
  2206      {
  2207          $5.Lateral = $4
  2208          $5.BaseExpr = NewBaseExpr($4)
  2209          $$ = Join{Table: $1, JoinTable: $5, JoinType: $2, Condition: $6}
  2210      }
  2211      | table join_outer_direction join_type_outer JOIN LATERAL laterable_query_table join_condition
  2212      {
  2213          $6.Lateral = $5
  2214          $6.BaseExpr = NewBaseExpr($5)
  2215          $$ = Join{Table: $1, JoinTable: $6, JoinType: $3, Direction: $2, Condition: $7}
  2216      }
  2217      | table NATURAL join_type_inner JOIN LATERAL laterable_query_table
  2218      {
  2219          $6.Lateral = $5
  2220          $6.BaseExpr = NewBaseExpr($5)
  2221          $$ = Join{Table: $1, JoinTable: $6, JoinType: $3, Natural: $2}
  2222      }
  2223      | table NATURAL join_outer_direction join_type_outer JOIN LATERAL laterable_query_table
  2224      {
  2225          $7.Lateral = $6
  2226          $7.BaseExpr = NewBaseExpr($6)
  2227          $$ = Join{Table: $1, JoinTable: $7, JoinType: $4, Direction: $3, Natural: $2}
  2228      }
  2229  
  2230  join_condition
  2231      : ON value
  2232      {
  2233          $$ = JoinCondition{On: $2}
  2234      }
  2235      | USING '(' identifiers ')'
  2236      {
  2237          $$ = JoinCondition{Using: $3}
  2238      }
  2239  
  2240  field
  2241      : value
  2242      {
  2243          $$ = Field{Object: $1}
  2244      }
  2245      | value AS identifier
  2246      {
  2247          $$ = Field{Object: $1, As: $2, Alias: $3}
  2248      }
  2249      | wildcard
  2250      {
  2251          $$ = Field{Object: $1}
  2252      }
  2253      | identifier '.' wildcard
  2254      {
  2255          $$ = Field{Object: FieldReference{BaseExpr: $1.BaseExpr, View: $1, Column: $3}}
  2256      }
  2257  
  2258  case_expr
  2259      : CASE case_value case_expr_when case_expr_else END
  2260      {
  2261          $$ = CaseExpr{Value: $2, When: $3, Else: $4}
  2262      }
  2263  
  2264  case_value
  2265      :
  2266      {
  2267          $$ = nil
  2268      }
  2269      | value
  2270      {
  2271          $$ = $1
  2272      }
  2273  
  2274  case_expr_when
  2275      : WHEN value THEN value
  2276      {
  2277          $$ = []QueryExpression{CaseExprWhen{Condition: $2, Result: $4}}
  2278      }
  2279      | WHEN value THEN value case_expr_when
  2280      {
  2281          $$ = append([]QueryExpression{CaseExprWhen{Condition: $2, Result: $4}}, $5...)
  2282      }
  2283  
  2284  case_expr_else
  2285      :
  2286      {
  2287          $$ = nil
  2288      }
  2289      | ELSE value
  2290      {
  2291          $$ = CaseExprElse{Result: $2}
  2292      }
  2293  
  2294  field_references
  2295      : field_reference
  2296      {
  2297          $$ = []QueryExpression{$1}
  2298      }
  2299      | field_reference ',' field_references
  2300      {
  2301          $$ = append([]QueryExpression{$1}, $3...)
  2302      }
  2303  
  2304  values
  2305      : value
  2306      {
  2307          $$ = []QueryExpression{$1}
  2308      }
  2309      | value ',' values
  2310      {
  2311          $$ = append([]QueryExpression{$1}, $3...)
  2312      }
  2313  
  2314  substantial_values
  2315      : substantial_value
  2316      {
  2317          $$ = []QueryExpression{$1}
  2318      }
  2319      | substantial_value ',' substantial_values
  2320      {
  2321          $$ = append([]QueryExpression{$1}, $3...)
  2322      }
  2323  
  2324  tables
  2325      : table
  2326      {
  2327          $$ = []QueryExpression{$1}
  2328      }
  2329      | table ',' joinable_tables
  2330      {
  2331          $$ = append([]QueryExpression{$1}, $3...)
  2332      }
  2333  
  2334  identified_tables
  2335      : table_identifier
  2336      {
  2337          $$ = []QueryExpression{Table{Object: $1}}
  2338      }
  2339      | table_identifier ',' identified_tables
  2340      {
  2341          $$ = append([]QueryExpression{Table{Object: $1}}, $3...)
  2342      }
  2343  
  2344  updatable_tables
  2345      : updatable_table_identifier
  2346      {
  2347          $$ = []QueryExpression{Table{Object: $1}}
  2348      }
  2349      | updatable_table_identifier ',' updatable_tables
  2350      {
  2351          $$ = append([]QueryExpression{Table{Object: $1}}, $3...)
  2352      }
  2353  
  2354  identifiers
  2355      : identifier
  2356      {
  2357          $$ = []QueryExpression{$1}
  2358      }
  2359      | identifier ',' identifiers
  2360      {
  2361          $$ = append([]QueryExpression{$1}, $3...)
  2362      }
  2363  
  2364  fields
  2365      : field
  2366      {
  2367          $$ = []QueryExpression{$1}
  2368      }
  2369      | field ',' fields
  2370      {
  2371          $$ = append([]QueryExpression{$1}, $3...)
  2372      }
  2373  
  2374  insert_query
  2375      : with_clause INSERT INTO updatable_table_identifier VALUES row_values
  2376      {
  2377          $$ = InsertQuery{WithClause: $1, Table: Table{Object: $4}, ValuesList: $6}
  2378      }
  2379      | with_clause INSERT INTO updatable_table_identifier '(' field_references ')' VALUES row_values
  2380      {
  2381          $$ = InsertQuery{WithClause: $1, Table: Table{Object: $4}, Fields: $6, ValuesList: $9}
  2382      }
  2383      | with_clause INSERT INTO updatable_table_identifier select_query
  2384      {
  2385          $$ = InsertQuery{WithClause: $1, Table: Table{Object: $4}, Query: $5.(SelectQuery)}
  2386      }
  2387      | with_clause INSERT INTO updatable_table_identifier '(' field_references ')' select_query
  2388      {
  2389          $$ = InsertQuery{WithClause: $1, Table: Table{Object: $4}, Fields: $6, Query: $8.(SelectQuery)}
  2390      }
  2391  
  2392  update_query
  2393      : with_clause UPDATE updatable_tables SET update_set_list from_clause where_clause
  2394      {
  2395          $$ = UpdateQuery{WithClause: $1, Tables: $3, SetList: $5, FromClause: $6, WhereClause: $7}
  2396      }
  2397  
  2398  update_set
  2399      : field_reference '=' value
  2400      {
  2401          $$ = UpdateSet{Field: $1, Value: $3}
  2402      }
  2403  
  2404  update_set_list
  2405      : update_set
  2406      {
  2407          $$ = []UpdateSet{$1}
  2408      }
  2409      | update_set ',' update_set_list
  2410      {
  2411          $$ = append([]UpdateSet{$1}, $3...)
  2412      }
  2413  
  2414  replace_query
  2415      : with_clause REPLACE INTO updatable_table_identifier USING '(' field_references ')' VALUES row_values
  2416      {
  2417          $$ = ReplaceQuery{WithClause: $1, Table: Table{Object: $4}, Keys: $7, ValuesList: $10}
  2418      }
  2419      | with_clause REPLACE INTO updatable_table_identifier '(' field_references ')' USING '(' field_references ')' VALUES row_values
  2420      {
  2421          $$ = ReplaceQuery{WithClause: $1, Table: Table{Object: $4}, Fields: $6, Keys: $10, ValuesList: $13}
  2422      }
  2423      | with_clause REPLACE INTO updatable_table_identifier USING '(' field_references ')' select_query
  2424      {
  2425          $$ = ReplaceQuery{WithClause: $1, Table: Table{Object: $4}, Keys: $7, Query: $9.(SelectQuery)}
  2426      }
  2427      | with_clause REPLACE INTO updatable_table_identifier '(' field_references ')' USING '(' field_references ')' select_query
  2428      {
  2429          $$ = ReplaceQuery{WithClause: $1, Table: Table{Object: $4}, Fields: $6, Keys: $10, Query: $12.(SelectQuery)}
  2430      }
  2431      | REPLACE INTO updatable_table_identifier USING '(' field_references ')' VALUES row_values
  2432      {
  2433          $$ = ReplaceQuery{Table: Table{Object: $3}, Keys: $6, ValuesList: $9}
  2434      }
  2435      | REPLACE INTO updatable_table_identifier '(' field_references ')' USING '(' field_references ')' VALUES row_values
  2436      {
  2437          $$ = ReplaceQuery{Table: Table{Object: $3}, Fields: $5, Keys: $9, ValuesList: $12}
  2438      }
  2439      | REPLACE INTO updatable_table_identifier USING '(' field_references ')' select_query
  2440      {
  2441          $$ = ReplaceQuery{Table: Table{Object: $3}, Keys: $6, Query: $8.(SelectQuery)}
  2442      }
  2443      | REPLACE INTO updatable_table_identifier '(' field_references ')' USING '(' field_references ')' select_query
  2444      {
  2445          $$ = ReplaceQuery{Table: Table{Object: $3}, Fields: $5, Keys: $9, Query: $11.(SelectQuery)}
  2446      }
  2447  
  2448  delete_query
  2449      : with_clause DELETE FROM tables where_clause
  2450      {
  2451          $$ = DeleteQuery{BaseExpr: NewBaseExpr($2), WithClause: $1, FromClause: FromClause{Tables: $4}, WhereClause: $5}
  2452      }
  2453      | with_clause DELETE identified_tables FROM tables where_clause
  2454      {
  2455          $$ = DeleteQuery{BaseExpr: NewBaseExpr($2), WithClause: $1, Tables: $3, FromClause: FromClause{Tables: $5}, WhereClause: $6}
  2456      }
  2457  
  2458  elseif
  2459      : ELSEIF substantial_value THEN program
  2460      {
  2461          $$ = []ElseIf{{Condition: $2, Statements: $4}}
  2462      }
  2463      | ELSEIF substantial_value THEN program elseif
  2464      {
  2465          $$ = append([]ElseIf{{Condition: $2, Statements: $4}}, $5...)
  2466      }
  2467  
  2468  else
  2469      :
  2470      {
  2471          $$ = Else{}
  2472      }
  2473      | ELSE program
  2474      {
  2475          $$ = Else{Statements: $2}
  2476      }
  2477  
  2478  in_loop_elseif
  2479      : ELSEIF substantial_value THEN loop_program
  2480      {
  2481          $$ = []ElseIf{{Condition: $2, Statements: $4}}
  2482      }
  2483      | ELSEIF substantial_value THEN loop_program in_loop_elseif
  2484      {
  2485          $$ = append([]ElseIf{{Condition: $2, Statements: $4}}, $5...)
  2486      }
  2487  
  2488  in_loop_else
  2489      :
  2490      {
  2491          $$ = Else{}
  2492      }
  2493      | ELSE loop_program
  2494      {
  2495          $$ = Else{Statements: $2}
  2496      }
  2497  
  2498  in_function_elseif
  2499      : ELSEIF substantial_value THEN function_program
  2500      {
  2501          $$ = []ElseIf{{Condition: $2, Statements: $4}}
  2502      }
  2503      | ELSEIF substantial_value THEN function_program in_function_elseif
  2504      {
  2505          $$ = append([]ElseIf{{Condition: $2, Statements: $4}}, $5...)
  2506      }
  2507  
  2508  in_function_else
  2509      :
  2510      {
  2511          $$ = Else{}
  2512      }
  2513      | ELSE function_program
  2514      {
  2515          $$ = Else{Statements: $2}
  2516      }
  2517  
  2518  in_function_in_loop_elseif
  2519      : ELSEIF substantial_value THEN function_loop_program
  2520      {
  2521          $$ = []ElseIf{{Condition: $2, Statements: $4}}
  2522      }
  2523      | ELSEIF substantial_value THEN function_loop_program in_function_in_loop_elseif
  2524      {
  2525          $$ = append([]ElseIf{{Condition: $2, Statements: $4}}, $5...)
  2526      }
  2527  
  2528  in_function_in_loop_else
  2529      :
  2530      {
  2531          $$ = Else{}
  2532      }
  2533      | ELSE function_loop_program
  2534      {
  2535          $$ = Else{Statements: $2}
  2536      }
  2537  
  2538  case_when
  2539      : WHEN substantial_value THEN program
  2540      {
  2541          $$ = []CaseWhen{{Condition: $2, Statements: $4}}
  2542      }
  2543      | WHEN substantial_value THEN program case_when
  2544      {
  2545          $$ = append([]CaseWhen{{Condition: $2, Statements: $4}}, $5...)
  2546      }
  2547  
  2548  case_else
  2549      :
  2550      {
  2551          $$ = CaseElse{}
  2552      }
  2553      | ELSE program
  2554      {
  2555          $$ = CaseElse{Statements: $2}
  2556      }
  2557  
  2558  in_loop_case_when
  2559      : WHEN substantial_value THEN loop_program
  2560      {
  2561          $$ = []CaseWhen{{Condition: $2, Statements: $4}}
  2562      }
  2563      | WHEN substantial_value THEN loop_program in_loop_case_when
  2564      {
  2565          $$ = append([]CaseWhen{{Condition: $2, Statements: $4}}, $5...)
  2566      }
  2567  
  2568  in_loop_case_else
  2569      :
  2570      {
  2571          $$ = CaseElse{}
  2572      }
  2573      | ELSE loop_program
  2574      {
  2575          $$ = CaseElse{Statements: $2}
  2576      }
  2577  
  2578  in_function_case_when
  2579      : WHEN substantial_value THEN function_program
  2580      {
  2581          $$ = []CaseWhen{{Condition: $2, Statements: $4}}
  2582      }
  2583      | WHEN substantial_value THEN function_program in_function_case_when
  2584      {
  2585          $$ = append([]CaseWhen{{Condition: $2, Statements: $4}}, $5...)
  2586      }
  2587  
  2588  in_function_case_else
  2589      :
  2590      {
  2591          $$ = CaseElse{}
  2592      }
  2593      | ELSE function_program
  2594      {
  2595          $$ = CaseElse{Statements: $2}
  2596      }
  2597  
  2598  in_function_in_loop_case_when
  2599      : WHEN substantial_value THEN function_loop_program
  2600      {
  2601          $$ = []CaseWhen{{Condition: $2, Statements: $4}}
  2602      }
  2603      | WHEN substantial_value THEN function_loop_program in_function_in_loop_case_when
  2604      {
  2605          $$ = append([]CaseWhen{{Condition: $2, Statements: $4}}, $5...)
  2606      }
  2607  
  2608  in_function_in_loop_case_else
  2609      :
  2610      {
  2611          $$ = CaseElse{}
  2612      }
  2613      | ELSE function_loop_program
  2614      {
  2615          $$ = CaseElse{Statements: $2}
  2616      }
  2617  
  2618  identifier
  2619      : IDENTIFIER
  2620      {
  2621          $$ = Identifier{BaseExpr: NewBaseExpr($1), Literal: $1.Literal, Quoted: $1.Quoted}
  2622      }
  2623      | TIES
  2624      {
  2625          $$ = Identifier{BaseExpr: NewBaseExpr($1), Literal: $1.Literal, Quoted: $1.Quoted}
  2626      }
  2627      | NULLS
  2628      {
  2629          $$ = Identifier{BaseExpr: NewBaseExpr($1), Literal: $1.Literal, Quoted: $1.Quoted}
  2630      }
  2631      | ROWS
  2632      {
  2633          $$ = Identifier{BaseExpr: NewBaseExpr($1), Literal: $1.Literal, Quoted: $1.Quoted}
  2634      }
  2635      | CSV
  2636      {
  2637          $$ = Identifier{BaseExpr: NewBaseExpr($1), Literal: $1.Literal, Quoted: $1.Quoted}
  2638      }
  2639      | JSON
  2640      {
  2641          $$ = Identifier{BaseExpr: NewBaseExpr($1), Literal: $1.Literal, Quoted: $1.Quoted}
  2642      }
  2643      | JSONL
  2644      {
  2645          $$ = Identifier{BaseExpr: NewBaseExpr($1), Literal: $1.Literal, Quoted: $1.Quoted}
  2646      }
  2647      | FIXED
  2648      {
  2649          $$ = Identifier{BaseExpr: NewBaseExpr($1), Literal: $1.Literal, Quoted: $1.Quoted}
  2650      }
  2651      | LTSV
  2652      {
  2653          $$ = Identifier{BaseExpr: NewBaseExpr($1), Literal: $1.Literal, Quoted: $1.Quoted}
  2654      }
  2655  
  2656  variable
  2657      : VARIABLE
  2658      {
  2659          $$ = Variable{BaseExpr: NewBaseExpr($1), Name:$1.Literal}
  2660      }
  2661  
  2662  variables
  2663      : variable
  2664      {
  2665          $$ = []Variable{$1}
  2666      }
  2667      | variable ',' variables
  2668      {
  2669          $$ = append([]Variable{$1}, $3...)
  2670      }
  2671  
  2672  variable_substitution
  2673      : variable SUBSTITUTION_OP value
  2674      {
  2675          $$ = VariableSubstitution{Variable:$1, Value:$3}
  2676      }
  2677  
  2678  variable_assignment
  2679      : variable
  2680      {
  2681          $$ = VariableAssignment{Variable:$1}
  2682      }
  2683      | variable SUBSTITUTION_OP value
  2684      {
  2685          $$ = VariableAssignment{Variable: $1, Value: $3}
  2686      }
  2687  
  2688  variable_assignments
  2689      : variable_assignment
  2690      {
  2691          $$ = []VariableAssignment{$1}
  2692      }
  2693      | variable_assignment ',' variable_assignments
  2694      {
  2695          $$ = append([]VariableAssignment{$1}, $3...)
  2696      }
  2697  
  2698  environment_variable
  2699      : ENVIRONMENT_VARIABLE
  2700      {
  2701          $$ = EnvironmentVariable{BaseExpr: NewBaseExpr($1), Name: $1.Literal, Quoted: $1.Quoted}
  2702      }
  2703  
  2704  runtime_information
  2705      : RUNTIME_INFORMATION
  2706      {
  2707          $$ = RuntimeInformation{BaseExpr: NewBaseExpr($1), Name: $1.Literal}
  2708      }
  2709  
  2710  constant
  2711      : CONSTANT
  2712      {
  2713          items := strings.Split($1.Literal, ConstantDelimiter)
  2714          space := ""
  2715          if 0 < len(items) {
  2716              space = items[0]
  2717          }
  2718          name := ""
  2719          if 1 < len(items) {
  2720              name = items[1]
  2721          }
  2722  
  2723          $$ = Constant{BaseExpr: NewBaseExpr($1), Space: space, Name: name}
  2724      }
  2725  
  2726  flag
  2727      : FLAG
  2728      {
  2729          $$ = Flag{BaseExpr: NewBaseExpr($1), Name: $1.Literal}
  2730      }
  2731  
  2732  distinct
  2733      :
  2734      {
  2735          $$ = Token{}
  2736      }
  2737      | DISTINCT
  2738      {
  2739          $$ = $1
  2740      }
  2741  
  2742  negation
  2743      :
  2744      {
  2745          $$ = Token{}
  2746      }
  2747      | NOT
  2748      {
  2749          $$ = $1
  2750      }
  2751  
  2752  join_type_inner
  2753      :
  2754      {
  2755          $$ = Token{}
  2756      }
  2757      | INNER
  2758      {
  2759          $$ = $1
  2760      }
  2761  
  2762  join_type_outer
  2763      :
  2764      {
  2765          $$ = Token{}
  2766      }
  2767      | OUTER
  2768      {
  2769          $$ = $1
  2770      }
  2771  
  2772  join_outer_direction
  2773      : LEFT
  2774      {
  2775          $$ = $1
  2776      }
  2777      | RIGHT
  2778      {
  2779          $$ = $1
  2780      }
  2781      | FULL
  2782      {
  2783          $$ = $1
  2784      }
  2785  
  2786  all
  2787      :
  2788      {
  2789          $$ = Token{}
  2790      }
  2791      | ALL
  2792      {
  2793          $$ = $1
  2794      }
  2795  
  2796  recursive
  2797      :
  2798      {
  2799          $$ = Token{}
  2800      }
  2801      | RECURSIVE
  2802      {
  2803          $$ = $1
  2804      }
  2805  
  2806  as
  2807      :
  2808      {
  2809          $$ = Token{}
  2810      }
  2811      | AS
  2812      {
  2813          $$ = $1
  2814      }
  2815  
  2816  comparison_operator
  2817      : COMPARISON_OP
  2818      {
  2819          $$ = $1
  2820      }
  2821      | '='
  2822      {
  2823          $1.Token = COMPARISON_OP
  2824          $$ = $1
  2825      }
  2826  
  2827  if_not_exists
  2828      :
  2829      {
  2830          $$ = false
  2831      }
  2832      | IF NOT EXISTS
  2833      {
  2834          $$ = true
  2835      }
  2836  
  2837  %%
  2838  
  2839  func SetDebugLevel(level int, verbose bool) {
  2840  	yyDebug        = level
  2841  	yyErrorVerbose = verbose
  2842  }
  2843  
  2844  func Parse(s string, sourceFile string, forPrepared bool, ansiQuotes bool) ([]Statement, int, error) {
  2845      l := new(Lexer)
  2846      l.Init(s, sourceFile, forPrepared, ansiQuotes)
  2847      yyParse(l)
  2848      return l.program, l.HolderNumber(), l.err
  2849  }