Skip to content

Commit f8afbe2

Browse files
authored
fix(flink): #398 fix flinksql built-in function's using (#399)
* fix(flink): #398 fix flinksql built-in function's using * fix(flink): #398 optimize flink g4 rule and sql --------- Co-authored-by: zhaoge <>
1 parent 881192c commit f8afbe2

File tree

7 files changed

+2707
-2151
lines changed

7 files changed

+2707
-2151
lines changed

src/grammar/flink/FlinkSqlParser.g4

+46-13
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,8 @@ selectClause
487487

488488
projectItemDefinition
489489
: overWindowItem
490-
| columnName (KW_AS? expression)?
491490
| expression (KW_AS? columnName)?
491+
| columnName (KW_AS? expression)?
492492
;
493493

494494
overWindowItem
@@ -515,7 +515,7 @@ tableReference
515515
tablePrimary
516516
: KW_TABLE? tablePath systemTimePeriod?
517517
| viewPath systemTimePeriod?
518-
| KW_LATERAL KW_TABLE LR_BRACKET functionName LR_BRACKET functionParam (COMMA functionParam)* RR_BRACKET RR_BRACKET
518+
| KW_LATERAL KW_TABLE LR_BRACKET functionCallExpression RR_BRACKET
519519
| KW_LATERAL? LR_BRACKET queryStatement RR_BRACKET
520520
| KW_UNNEST LR_BRACKET expression RR_BRACKET
521521
;
@@ -747,6 +747,12 @@ valueExpression
747747
| left=valueExpression comparisonOperator right=valueExpression # comparison
748748
;
749749

750+
functionCallExpression
751+
: reservedKeywordsNoParamsUsedAsFuncName
752+
| functionNameAndParams
753+
| functionNameWithParams LR_BRACKET (setQuantifier? functionParam (COMMA functionParam)*)? RR_BRACKET
754+
;
755+
750756
primaryExpression
751757
: KW_CASE whenClause+ (KW_ELSE elseExpression=expression)? KW_END # searchedCase
752758
| KW_CASE value=expression whenClause+ (KW_ELSE elseExpression=expression)? KW_END # simpleCase
@@ -759,15 +765,14 @@ primaryExpression
759765
| ASTERISK_SIGN # star
760766
| uid DOT ASTERISK_SIGN # star
761767
// | LR_BRACKET namedExpression (COMMA namedExpression)+ RR_BRACKET #rowConstructor
762-
| LR_BRACKET queryStatement RR_BRACKET # subqueryExpression
763-
| functionName LR_BRACKET (setQuantifier? functionParam (COMMA functionParam)*)? RR_BRACKET # functionCall
768+
| LR_BRACKET queryStatement RR_BRACKET # subqueryExpression
769+
| functionCallExpression # functionCall
764770
// | identifier '->' expression #lambda
765771
// | '(' identifier (',' identifier)+ ')' '->' expression #lambda
766772
| value=primaryExpression LS_BRACKET index=valueExpression RS_BRACKET # subscript
767773
| identifier # columnReference
768774
| dereferenceDefinition # dereference
769775
| LR_BRACKET expression RR_BRACKET # parenthesizedExpression
770-
| KW_CURRENT_TIMESTAMP # dateFunctionExpression
771776
// | EXTRACT LR_BRACKET field=identifier KW_FROM source=valueExpression RR_BRACKET #extract
772777
// | (SUBSTR | SUBSTRING) LR_BRACKET str=valueExpression (KW_FROM | COMMA) pos=valueExpression
773778
// ((KW_FOR | COMMA) len=valueExpression)? RR_BRACKET #substring
@@ -782,6 +787,25 @@ functionNameCreate
782787
;
783788

784789
functionName
790+
: reservedKeywordsUsedAsFuncName
791+
| reservedKeywordsNoParamsUsedAsFuncName
792+
| reservedKeywordsFollowParamsUsedAsFuncName
793+
| uid
794+
;
795+
796+
/**
797+
* Built-in function name that is following with params directly without parentheses
798+
* Reference Link:https://nightlies.apache.org/flink/flink-docs-release-1.16/zh/docs/dev/table/functions/systemfunctions/#%E6%97%B6%E9%97%B4%E5%87%BD%E6%95%B0
799+
*/
800+
functionNameAndParams
801+
: reservedKeywordsFollowParamsUsedAsFuncName STRING_LITERAL
802+
| timeIntervalExpression
803+
;
804+
805+
/**
806+
* Function name that is need to follow with parentheses and params
807+
*/
808+
functionNameWithParams
785809
: reservedKeywordsUsedAsFuncName
786810
| uid
787811
;
@@ -1077,6 +1101,23 @@ reservedKeywordsUsedAsFuncParam
10771101
| ASTERISK_SIGN
10781102
;
10791103

1104+
/**
1105+
* Built-in function name without parentheses and params
1106+
*/
1107+
reservedKeywordsNoParamsUsedAsFuncName
1108+
: KW_LOCALTIME
1109+
| KW_LOCALTIMESTAMP
1110+
| KW_CURRENT_TIME
1111+
| KW_CURRENT_DATE
1112+
| KW_CURRENT_TIMESTAMP
1113+
;
1114+
1115+
reservedKeywordsFollowParamsUsedAsFuncName
1116+
: KW_DATE
1117+
| KW_TIME
1118+
| KW_TIMESTAMP
1119+
;
1120+
10801121
reservedKeywordsUsedAsFuncName
10811122
: KW_ABS
10821123
| KW_ARRAY
@@ -1089,10 +1130,6 @@ reservedKeywordsUsedAsFuncName
10891130
| KW_COLLECT
10901131
| KW_COUNT
10911132
| KW_CUME_DIST
1092-
| KW_CURRENT_DATE
1093-
| KW_CURRENT_TIME
1094-
| KW_CURRENT_TIMESTAMP
1095-
| KW_DATE
10961133
| KW_DAYOFWEEK
10971134
| KW_DAYOFYEAR
10981135
| KW_DENSE_RANK
@@ -1109,8 +1146,6 @@ reservedKeywordsUsedAsFuncName
11091146
| KW_LEAD
11101147
| KW_LEFT
11111148
| KW_LN
1112-
| KW_LOCALTIME
1113-
| KW_LOCALTIMESTAMP
11141149
| KW_LOWER
11151150
| KW_MAP
11161151
| KW_MAX
@@ -1135,8 +1170,6 @@ reservedKeywordsUsedAsFuncName
11351170
| KW_STDDEV_SAMP
11361171
| KW_SUBSTRING
11371172
| KW_SUM
1138-
| KW_TIME
1139-
| KW_TIMESTAMP
11401173
| KW_TIMESTAMP_DIFF
11411174
| KW_TRIM
11421175
| KW_TRUNCATE

src/lib/flink/FlinkSqlParser.interp

+6-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)