Skip to content

Commit bf42753

Browse files
Merge pull request taozhi8833998#1514 from taozhi8833998/refactor-slow-parser-pg
refactor: fix slow parser in pg
2 parents 25d9c39 + 9dd31ca commit bf42753

14 files changed

Lines changed: 179 additions & 147 deletions

ast/postgresql.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,9 @@ export type multiplicative_expr = binary_expr;
910910

911911
export type multiplicative_operator = "*" | "/" | "%" | "||";
912912

913-
export type primary = cast_expr | literal | aggr_func | window_func | func_call | case_expr | interval_expr | column_ref | param | or_and_where_expr | var_decl | { type: 'origin'; value: string; };
913+
export type column_ref_array_index = column_ref;
914+
915+
export type primary = cast_expr | or_and_where_expr | var_decl | { type: 'origin'; value: string; };
914916

915917
export type string_constants_escape = { type: 'origin'; value: string; };
916918

@@ -1027,7 +1029,7 @@ export type trim_rem = expr_list;
10271029

10281030
export type trim_func_clause = { type: 'function'; name: string; args: expr_list; };
10291031

1030-
export type func_call = trim_func_clause | { type: 'function'; name: string; args: expr_list; suffix: literal_string; } | { type: 'function'; name: string; args: expr_list; over?: over_partition; } | { type: 'function'; name: string; args: expr_list; } | extract_func | { type: 'function'; name: string; over?: on_update_current_timestamp; };
1032+
export type func_call = trim_func_clause | { type: 'function'; name: string; args: expr_list; suffix: literal_string; } | { type: 'function'; name: string; args: expr_list; over?: over_partition; } | extract_func | { type: 'function'; name: string; over?: on_update_current_timestamp; } | { type: 'function'; name: string; args: expr_list; };
10311033

10321034
export type extract_filed = 'string';
10331035

@@ -1039,18 +1041,24 @@ export type scalar_func = scalar_time_func | KW_CURRENT_USER | KW_USER | KW_SESS
10391041

10401042

10411043

1044+
export type cast_double_colon = {
1045+
as?: alias_clause,
1046+
symbol: '::' | 'as',
1047+
target: data_type;
1048+
arrows?: ('->>' | '->')[];
1049+
property?: (literal_string | literal_numeric)[];
1050+
};
1051+
1052+
1053+
10421054

10431055

10441056
export type cast_expr = {
1045-
as?: alias_clause,
10461057
type: 'cast';
10471058
expr: literal | aggr_func | func_call | case_expr | interval_expr | column_ref | param
10481059
| expr;
1049-
symbol: '::' | 'as',
10501060
keyword: 'cast';
1051-
target: data_type;
1052-
arrows?: ('->>' | '->')[];
1053-
property?: (literal_string | literal_numeric)[];
1061+
...cast_double_colon;
10541062
};
10551063

10561064
export type signedness = KW_SIGNED | KW_UNSIGNED;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-sql-parser",
3-
"version": "4.8.0",
3+
"version": "4.9.0",
44
"description": "simple node sql parser",
55
"main": "index.js",
66
"types": "types.d.ts",

pegjs/bigquery.pegjs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,15 +2407,6 @@ star_expr
24072407
func_call
24082408
= extract_func
24092409
/ any_value_func
2410-
/ name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
2411-
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
2412-
return {
2413-
type: 'function',
2414-
name: name,
2415-
args: l ? l: { type: 'expr_list', value: [] },
2416-
over: bc
2417-
};
2418-
}
24192410
/ name:scalar_func __ LPAREN __ l:expr_list? __ RPAREN __ bc:over_partition? {
24202411
return {
24212412
type: 'function',
@@ -2431,6 +2422,15 @@ func_call
24312422
over: up
24322423
}
24332424
}
2425+
/ name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
2426+
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
2427+
return {
2428+
type: 'function',
2429+
name: name,
2430+
args: l ? l: { type: 'expr_list', value: [] },
2431+
over: bc
2432+
};
2433+
}
24342434

24352435
proc_func_name
24362436
= dt:ident_name tail:(__ DOT __ ident_name)* {

pegjs/db2.pegjs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,16 +1912,7 @@ star_expr
19121912
= "*" { return { type: 'star', value: '*' }; }
19131913

19141914
func_call
1915-
= name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
1916-
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
1917-
return {
1918-
type: 'function',
1919-
name: name,
1920-
args: l ? l: { type: 'expr_list', value: [] },
1921-
over: bc,
1922-
};
1923-
}
1924-
/ name:scalar_func __ LPAREN __ l:expr_list? __ RPAREN __ bc:over_partition? {
1915+
= name:scalar_func __ LPAREN __ l:expr_list? __ RPAREN __ bc:over_partition? {
19251916
return {
19261917
type: 'function',
19271918
name: name,
@@ -1936,6 +1927,15 @@ func_call
19361927
over: up
19371928
}
19381929
}
1930+
/ name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
1931+
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
1932+
return {
1933+
type: 'function',
1934+
name: name,
1935+
args: l ? l: { type: 'expr_list', value: [] },
1936+
over: bc,
1937+
};
1938+
}
19391939
scalar_time_func
19401940
= KW_CURRENT_DATE
19411941
/ KW_CURRENT_TIME

pegjs/flinksql.pegjs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,16 +2948,6 @@ func_call
29482948
/ trim_func_clause
29492949
/ substring_func_clause
29502950
/ overlay_func_clause
2951-
/ name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
2952-
// => { type: 'function'; name: string; args: expr_list; }
2953-
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
2954-
return {
2955-
type: 'function',
2956-
name: name,
2957-
args: l ? l: { type: 'expr_list', value: [] },
2958-
over: bc,
2959-
};
2960-
}
29612951
/ name:scalar_func __ LPAREN __ l:expr_list? __ RPAREN __ bc:over_partition? {
29622952
return {
29632953
type: 'function',
@@ -2975,6 +2965,16 @@ func_call
29752965
over: up
29762966
}
29772967
}
2968+
/ name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
2969+
// => { type: 'function'; name: string; args: expr_list; }
2970+
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
2971+
return {
2972+
type: 'function',
2973+
name: name,
2974+
args: l ? l: { type: 'expr_list', value: [] },
2975+
over: bc,
2976+
};
2977+
}
29782978

29792979
extract_filed
29802980
= f:('CENTURY'i / 'DAY'i / 'DATE'i / 'DECADE'i / 'DOW'i / 'DOY'i / 'EPOCH'i / 'HOUR'i / 'ISODOW'i / 'ISOYEAR'i / 'MICROSECONDS'i / 'MILLENNIUM'i / 'MILLISECONDS'i / 'MINUTE'i / 'MONTH'i / 'QUARTER'i / 'SECOND'i / 'TIMEZONE'i / 'TIMEZONE_HOUR'i / 'TIMEZONE_MINUTE'i / 'WEEK'i / 'YEAR'i) {

pegjs/hive.pegjs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,16 +1920,7 @@ star_expr
19201920
= "*" { return { type: 'star', value: '*' }; }
19211921

19221922
func_call
1923-
= name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
1924-
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
1925-
return {
1926-
type: 'function',
1927-
name: name,
1928-
args: l ? l: { type: 'expr_list', value: [] },
1929-
over: bc
1930-
};
1931-
}
1932-
/ name:scalar_func __ LPAREN __ l:expr_list? __ RPAREN __ bc:over_partition? {
1923+
= name:scalar_func __ LPAREN __ l:expr_list? __ RPAREN __ bc:over_partition? {
19331924
return {
19341925
type: 'function',
19351926
name: name,
@@ -1953,6 +1944,15 @@ func_call
19531944
over: bc,
19541945
args_parentheses: false,
19551946
};
1947+
}
1948+
/ name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
1949+
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
1950+
return {
1951+
type: 'function',
1952+
name: name,
1953+
args: l ? l: { type: 'expr_list', value: [] },
1954+
over: bc
1955+
};
19561956
}
19571957
scalar_time_func
19581958
= KW_CURRENT_DATE

pegjs/mariadb.pegjs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,16 +2708,6 @@ func_call
27082708
collate: ca,
27092709
};
27102710
}
2711-
/ name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
2712-
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
2713-
if ((name.toUpperCase() === 'TIMESTAMPDIFF' || name.toUpperCase() === 'TIMESTAMPADD') && l.value && l.value[0]) l.value[0] = { type: 'origin', value: l.value[0].column }
2714-
return {
2715-
type: 'function',
2716-
name: name,
2717-
args: l ? l: { type: 'expr_list', value: [] },
2718-
over: bc,
2719-
};
2720-
}
27212711
/ name:scalar_func __ LPAREN __ l:expr_list? __ RPAREN __ bc:over_partition? {
27222712
return {
27232713
type: 'function',
@@ -2733,6 +2723,16 @@ func_call
27332723
over: up
27342724
}
27352725
}
2726+
/ name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
2727+
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
2728+
if ((name.toUpperCase() === 'TIMESTAMPDIFF' || name.toUpperCase() === 'TIMESTAMPADD') && l.value && l.value[0]) l.value[0] = { type: 'origin', value: l.value[0].column }
2729+
return {
2730+
type: 'function',
2731+
name: name,
2732+
args: l ? l: { type: 'expr_list', value: [] },
2733+
over: bc,
2734+
};
2735+
}
27362736
scalar_time_func
27372737
= KW_CURRENT_DATE
27382738
/ KW_CURRENT_TIME

pegjs/mysql.pegjs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,16 +2999,6 @@ func_call
29992999
collate: ca,
30003000
};
30013001
}
3002-
/ name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
3003-
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
3004-
if ((name.toUpperCase() === 'TIMESTAMPDIFF' || name.toUpperCase() === 'TIMESTAMPADD') && l.value && l.value[0]) l.value[0] = { type: 'origin', value: l.value[0].column }
3005-
return {
3006-
type: 'function',
3007-
name: name,
3008-
args: l ? l: { type: 'expr_list', value: [] },
3009-
over: bc
3010-
};
3011-
}
30123002
/ name:scalar_func __ LPAREN __ l:expr_list? __ RPAREN __ bc:over_partition? {
30133003
return {
30143004
type: 'function',
@@ -3024,6 +3014,16 @@ func_call
30243014
over: up
30253015
}
30263016
}
3017+
/ name:proc_func_name __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
3018+
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
3019+
if ((name.toUpperCase() === 'TIMESTAMPDIFF' || name.toUpperCase() === 'TIMESTAMPADD') && l.value && l.value[0]) l.value[0] = { type: 'origin', value: l.value[0].column }
3020+
return {
3021+
type: 'function',
3022+
name: name,
3023+
args: l ? l: { type: 'expr_list', value: [] },
3024+
over: bc
3025+
};
3026+
}
30273027
scalar_time_func
30283028
= KW_CURRENT_DATE
30293029
/ KW_CURRENT_TIME

0 commit comments

Comments
 (0)