@@ -70,6 +70,7 @@ class MindsDBParser(Parser):
7070 'drop_dataset' ,
7171 'select' ,
7272 'insert' ,
73+ 'union' ,
7374 'update' ,
7475 'delete' ,
7576 'evaluate' ,
@@ -614,10 +615,13 @@ def update(self, p):
614615
615616 # INSERT
616617 @_ ('INSERT INTO identifier LPAREN column_list RPAREN select' ,
617- 'INSERT INTO identifier select' )
618+ 'INSERT INTO identifier LPAREN column_list RPAREN union' ,
619+ 'INSERT INTO identifier select' ,
620+ 'INSERT INTO identifier union' )
618621 def insert (self , p ):
619622 columns = getattr (p , 'column_list' , None )
620- return Insert (table = p .identifier , columns = columns , from_select = p .select )
623+ query = p .select if hasattr (p , 'select' ) else p .union
624+ return Insert (table = p .identifier , columns = columns , from_select = query )
621625
622626 @_ ('INSERT INTO identifier LPAREN column_list RPAREN VALUES expr_list_set' ,
623627 'INSERT INTO identifier VALUES expr_list_set' )
@@ -998,19 +1002,35 @@ def database_engine(self, p):
9981002 engine = p .string
9991003 return {'identifier' :p .identifier , 'engine' :engine , 'if_not_exists' :p .if_not_exists_or_empty }
10001004
1001- # UNION / UNION ALL
1002- @_ ('select UNION select' )
1003- def select (self , p ):
1004- return Union (left = p [0 ], right = p [2 ], unique = True )
1005-
1006- @_ ('select UNION ALL select' )
1007- def select (self , p ):
1008- return Union (left = p [0 ], right = p [3 ], unique = False )
1005+ # Combining
1006+ @_ ('select UNION select' ,
1007+ 'union UNION select' ,
1008+ 'select UNION ALL select' ,
1009+ 'union UNION ALL select' )
1010+ def union (self , p ):
1011+ unique = not hasattr (p , 'ALL' )
1012+ return Union (left = p [0 ], right = p [2 ] if unique else p [3 ], unique = unique )
1013+
1014+ @_ ('select INTERSECT select' ,
1015+ 'union INTERSECT select' ,
1016+ 'select INTERSECT ALL select' ,
1017+ 'union INTERSECT ALL select' )
1018+ def union (self , p ):
1019+ unique = not hasattr (p , 'ALL' )
1020+ return Intersect (left = p [0 ], right = p [2 ] if unique else p [3 ], unique = unique )
1021+ @_ ('select EXCEPT select' ,
1022+ 'union EXCEPT select' ,
1023+ 'select EXCEPT ALL select' ,
1024+ 'union EXCEPT ALL select' )
1025+ def union (self , p ):
1026+ unique = not hasattr (p , 'ALL' )
1027+ return Except (left = p [0 ], right = p [2 ] if unique else p [3 ], unique = unique )
10091028
10101029 # tableau
10111030 @_ ('LPAREN select RPAREN' )
1031+ @_ ('LPAREN union RPAREN' )
10121032 def select (self , p ):
1013- return p . select
1033+ return p [ 1 ]
10141034
10151035 # WITH
10161036 @_ ('ctes select' )
@@ -1030,13 +1050,14 @@ def ctes(self, p):
10301050 ]
10311051 return ctes
10321052
1033- @_ ('WITH identifier cte_columns_or_nothing AS LPAREN select RPAREN' )
1053+ @_ ('WITH identifier cte_columns_or_nothing AS LPAREN select RPAREN' ,
1054+ 'WITH identifier cte_columns_or_nothing AS LPAREN union RPAREN' )
10341055 def ctes (self , p ):
10351056 return [
10361057 CommonTableExpression (
10371058 name = p .identifier ,
10381059 columns = p .cte_columns_or_nothing ,
1039- query = p . select )
1060+ query = p [ 5 ] )
10401061 ]
10411062
10421063 @_ ('empty' )
@@ -1331,6 +1352,15 @@ def column_list(self, p):
13311352 def case (self , p ):
13321353 return Case (rules = p .case_conditions , default = getattr (p , 'expr' , None ))
13331354
1355+ @_ ('CASE expr case_conditions ELSE expr END' ,
1356+ 'CASE expr case_conditions END' )
1357+ def case (self , p ):
1358+ if hasattr (p , 'expr' ):
1359+ arg , default = p .expr , None
1360+ else :
1361+ arg , default = p .expr0 , p .expr1
1362+ return Case (rules = p .case_conditions , default = default , arg = arg )
1363+
13341364 @_ ('case_condition' ,
13351365 'case_conditions case_condition' )
13361366 def case_conditions (self , p ):
@@ -1343,13 +1373,18 @@ def case_condition(self, p):
13431373 return [p .expr0 , p .expr1 ]
13441374
13451375 # Window function
1346- @_ ('function OVER LPAREN window RPAREN' )
1376+ @_ ('expr OVER LPAREN window RPAREN' ,
1377+ 'expr OVER LPAREN window id BETWEEN id id AND id id RPAREN' )
13471378 def window_function (self , p ):
13481379
1380+ modifier = None
1381+ if hasattr (p , 'BETWEEN' ):
1382+ modifier = f'{ p .id0 } BETWEEN { p .id1 } { p .id2 } AND { p .id3 } { p .id4 } '
13491383 return WindowFunction (
1350- function = p .function ,
1384+ function = p .expr ,
13511385 order_by = p .window .get ('order_by' ),
13521386 partition = p .window .get ('partition' ),
1387+ modifier = modifier ,
13531388 )
13541389
13551390 @_ ('window PARTITION_BY expr_list' )
0 commit comments