Skip to content

Commit bbd60de

Browse files
committed
parser gaps, operator precedence, ttl fixes, missing keywords
1 parent 1247bda commit bbd60de

File tree

11 files changed

+796
-54
lines changed

11 files changed

+796
-54
lines changed

src/grammar/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ export const constants: string[] = [
33
"brotli",
44
"century",
55
"complete",
6+
"datestyle",
67
"day",
78
"days",
89
"decade",
10+
"default_transaction_read_only",
911
"desc",
1012
"dow",
1113
"doy",
@@ -26,6 +28,7 @@ export const constants: string[] = [
2628
"lz4_raw",
2729
"lzo",
2830
"manual",
31+
"max_identifier_length",
2932
"microsecond",
3033
"microseconds",
3134
"millennium",
@@ -47,11 +50,16 @@ export const constants: string[] = [
4750
"prev",
4851
"quarter",
4952
"rest",
53+
"search_path",
5054
"second",
5155
"seconds",
56+
"server_version",
57+
"server_version_num",
5258
"skip_column",
5359
"skip_row",
5460
"snappy",
61+
"standard_conforming_strings",
62+
"transaction_isolation",
5563
"true",
5664
"uncompressed",
5765
"week",

src/grammar/keywords.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const keywords: string[] = [
9393
"is",
9494
"isolation",
9595
"join",
96+
"keep",
9697
"key",
9798
"keys",
9899
"latest",
@@ -104,6 +105,7 @@ export const keywords: string[] = [
104105
"list",
105106
"lock",
106107
"lt",
108+
"maps",
107109
"materialized",
108110
"maxUncommittedRows",
109111
"no",
@@ -155,7 +157,6 @@ export const keywords: string[] = [
155157
"row_group_size",
156158
"rows",
157159
"sample",
158-
"server_version",
159160
"select",
160161
"service",
161162
"set",

src/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,17 @@ export function parseToAst(sql: string): ParseResult {
8787
}
8888

8989
let ast: Statement[] = []
90-
if (errors.length > 0) {
91-
try {
92-
ast = visitor.visit(cst) as Statement[]
93-
} catch {
94-
// The visitor may throw on incomplete CST nodes produced by error recovery
95-
// (e.g. "Unknown primary expression", null dereferences on missing children).
96-
// Since we already have parse errors, return them with whatever AST was built.
97-
}
98-
} else {
90+
try {
9991
ast = visitor.visit(cst) as Statement[]
92+
} catch (e) {
93+
if (errors.length === 0) {
94+
// Semantic error from the visitor (e.g. invalid TTL duration unit).
95+
errors.push({
96+
message: e instanceof Error ? e.message : String(e),
97+
})
98+
}
99+
// When there are already lex/parse errors, the visitor may throw on
100+
// incomplete CST nodes produced by error recovery — that is expected.
100101
}
101102

102103
return { ast, errors }

src/parser/ast.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ export interface AlterUserCreateTokenAction {
349349
tokenType: "JWK" | "REST"
350350
ttl?: string
351351
refresh?: boolean
352+
transient?: boolean
353+
publicKeyX?: string
354+
publicKeyY?: string
352355
}
353356

354357
export interface AlterUserDropTokenAction {
@@ -535,8 +538,10 @@ export interface DropServiceAccountStatement extends AstNode {
535538

536539
export interface TruncateTableStatement extends AstNode {
537540
type: "truncateTable"
538-
table: QualifiedName
541+
tables: QualifiedName[]
539542
ifExists?: boolean
543+
only?: boolean
544+
keepSymbolMaps?: boolean
540545
}
541546

542547
export interface RenameTableStatement extends AstNode {
@@ -589,6 +594,14 @@ export interface ShowStatement extends AstNode {
589594
| "permissions"
590595
| "serverVersion"
591596
| "parameters"
597+
| "transactionIsolationLevel"
598+
| "maxIdentifierLength"
599+
| "standardConformingStrings"
600+
| "searchPath"
601+
| "dateStyle"
602+
| "timeZone"
603+
| "serverVersionNum"
604+
| "defaultTransactionReadOnly"
592605
table?: QualifiedName
593606
name?: QualifiedName
594607
}
@@ -942,6 +955,8 @@ export interface FunctionCall extends AstNode {
942955
fromSeparator?: boolean
943956
/** IGNORE NULLS modifier (e.g., first_value(x) IGNORE NULLS) */
944957
ignoreNulls?: boolean
958+
/** RESPECT NULLS modifier (e.g., first_value(x) RESPECT NULLS) */
959+
respectNulls?: boolean
945960
/** Subquery as function argument (e.g., touch(SELECT * FROM t)) */
946961
subquery?: SelectStatement
947962
over?: WindowSpecification

src/parser/cst-types.d.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,12 +994,15 @@ export type AlterUserActionCstChildren = {
994994
Token?: (IToken)[];
995995
Type?: (IToken)[];
996996
Jwk?: (IToken)[];
997+
Public?: IToken[];
998+
Key?: IToken[];
999+
Identifier?: (IToken)[];
9971000
Rest?: (IToken)[];
9981001
Ttl?: IToken[];
9991002
DurationLiteral?: IToken[];
10001003
Refresh?: IToken[];
1004+
Transient?: IToken[];
10011005
Drop?: IToken[];
1002-
Identifier?: IToken[];
10031006
};
10041007

10051008
export interface AlterTableStatementCstNode extends CstNode {
@@ -1234,7 +1237,12 @@ export type TruncateTableStatementCstChildren = {
12341237
Table: IToken[];
12351238
If?: IToken[];
12361239
Exists?: IToken[];
1237-
qualifiedName: QualifiedNameCstNode[];
1240+
Only?: IToken[];
1241+
qualifiedName: (QualifiedNameCstNode)[];
1242+
Comma?: IToken[];
1243+
Keep?: IToken[];
1244+
Symbol?: IToken[];
1245+
Maps?: IToken[];
12381246
};
12391247

12401248
export interface RenameTableStatementCstNode extends CstNode {
@@ -1336,6 +1344,18 @@ export type ShowStatementCstChildren = {
13361344
Accounts?: IToken[];
13371345
Permissions?: IToken[];
13381346
ServerVersion?: IToken[];
1347+
Transaction?: IToken[];
1348+
Isolation?: IToken[];
1349+
Level?: IToken[];
1350+
TransactionIsolation?: IToken[];
1351+
MaxIdentifierLength?: IToken[];
1352+
StandardConformingStrings?: IToken[];
1353+
SearchPath?: IToken[];
1354+
Datestyle?: IToken[];
1355+
Time?: IToken[];
1356+
Zone?: IToken[];
1357+
ServerVersionNum?: IToken[];
1358+
DefaultTransactionReadOnly?: IToken[];
13391359
Parameters?: IToken[];
13401360
};
13411361

@@ -1839,11 +1859,15 @@ export type SetExpressionCstChildren = {
18391859
expression?: (ExpressionCstNode)[];
18401860
Comma?: (IToken)[];
18411861
RParen?: (IToken)[];
1862+
inValue?: BitOrExpressionCstNode[];
18421863
Between?: IToken[];
18431864
betweenLow?: BitOrExpressionCstNode[];
18441865
And?: IToken[];
18451866
betweenHigh?: BitOrExpressionCstNode[];
18461867
Within?: IToken[];
1868+
Like?: IToken[];
1869+
Ilike?: IToken[];
1870+
notLikeRight?: BitOrExpressionCstNode[];
18471871
};
18481872

18491873
export interface BitOrExpressionCstNode extends CstNode {
@@ -2103,6 +2127,7 @@ export type FunctionCallCstChildren = {
21032127
From?: IToken[];
21042128
RParen: IToken[];
21052129
Ignore?: IToken[];
2130+
Respect?: IToken[];
21062131
Nulls?: IToken[];
21072132
overClause?: OverClauseCstNode[];
21082133
};
@@ -2123,6 +2148,7 @@ export type IdentifierExpressionCstChildren = {
21232148
From?: IToken[];
21242149
RParen?: IToken[];
21252150
Ignore?: IToken[];
2151+
Respect?: IToken[];
21262152
Nulls?: IToken[];
21272153
overClause?: OverClauseCstNode[];
21282154
};

src/parser/lexer.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ import {
5454
Current,
5555
Database,
5656
DataPageSize,
57+
Datestyle,
5758
Date,
5859
Day,
5960
Days,
6061
Decimal,
6162
Declare,
6263
Dedup,
6364
Default,
65+
DefaultTransactionReadOnly,
6466
Deferred,
6567
Delay,
6668
Delete,
@@ -122,6 +124,7 @@ import {
122124
Isolation,
123125
Jwk,
124126
Join,
127+
Keep,
125128
Key,
126129
Keys,
127130
Latest,
@@ -138,7 +141,9 @@ import {
138141
Long256,
139142
Lt,
140143
Manual,
144+
Maps,
141145
Materialized,
146+
MaxIdentifierLength,
142147
MaxUncommittedRows,
143148
Microsecond,
144149
Microseconds,
@@ -207,10 +212,12 @@ import {
207212
RowGroupSize,
208213
Rows,
209214
Sample,
215+
SearchPath,
210216
Second,
211217
Seconds,
212218
Select,
213219
ServerVersion,
220+
ServerVersionNum,
214221
Service,
215222
Set,
216223
Short,
@@ -221,6 +228,7 @@ import {
221228
Snapshot,
222229
Splice,
223230
Squash,
231+
StandardConformingStrings,
224232
Start,
225233
StatisticsEnabled,
226234
String,
@@ -237,6 +245,7 @@ import {
237245
Token,
238246
Tolerance,
239247
Transaction,
248+
TransactionIsolation,
240249
Transient,
241250
True,
242251
Truncate,
@@ -333,13 +342,15 @@ export {
333342
Current,
334343
Database,
335344
DataPageSize,
345+
Datestyle,
336346
Date,
337347
Day,
338348
Days,
339349
Decimal,
340350
Declare,
341351
Dedup,
342352
Default,
353+
DefaultTransactionReadOnly,
343354
Deferred,
344355
Delay,
345356
Delete,
@@ -401,6 +412,7 @@ export {
401412
Isolation,
402413
Jwk,
403414
Join,
415+
Keep,
404416
Key,
405417
Keys,
406418
Latest,
@@ -417,7 +429,9 @@ export {
417429
Long256,
418430
Lt,
419431
Manual,
432+
Maps,
420433
Materialized,
434+
MaxIdentifierLength,
421435
MaxUncommittedRows,
422436
Microsecond,
423437
Microseconds,
@@ -486,10 +500,12 @@ export {
486500
RowGroupSize,
487501
Rows,
488502
Sample,
503+
SearchPath,
489504
Second,
490505
Seconds,
491506
Select,
492507
ServerVersion,
508+
ServerVersionNum,
493509
Service,
494510
Set,
495511
Short,
@@ -500,6 +516,7 @@ export {
500516
Snapshot,
501517
Splice,
502518
Squash,
519+
StandardConformingStrings,
503520
Start,
504521
StatisticsEnabled,
505522
String,
@@ -516,6 +533,7 @@ export {
516533
Token,
517534
Tolerance,
518535
Transaction,
536+
TransactionIsolation,
519537
Transient,
520538
True,
521539
Truncate,

0 commit comments

Comments
 (0)