forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserImpls.ftl
More file actions
516 lines (482 loc) · 10.4 KB
/
Copy pathparserImpls.ftl
File metadata and controls
516 lines (482 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<#-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for additional
information regarding copyright ownership. The ASF licenses this file to
You under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License. -->
boolean IfNotExistsOpt() :
{
}
{
<IF> <NOT> <EXISTS> { return true; }
|
{ return false; }
}
boolean IfExistsOpt() :
{
}
{
<IF> <EXISTS> { return true; }
|
{ return false; }
}
SqlNodeList Options() :
{
final Span s;
final List<SqlNode> list = new ArrayList<SqlNode>();
}
{
<OPTIONS> { s = span(); } <LPAREN>
[
Option(list)
(
<COMMA>
Option(list)
)*
]
<RPAREN> {
return new SqlNodeList(list, s.end(this));
}
}
void Option(List<SqlNode> list) :
{
final SqlIdentifier id;
final SqlNode value;
}
{
id = SimpleIdentifier()
value = Literal() {
list.add(id);
list.add(value);
}
}
List<Schema.Field> FieldListParens() :
{
final List<Schema.Field> fields;
}
{
<LPAREN>
fields = FieldListBody()
<RPAREN>
{
return fields;
}
}
List<Schema.Field> FieldListAngular() :
{
final List<Schema.Field> fields;
}
{
<LT>
fields = FieldListBody()
<GT>
{
return fields;
}
}
List<Schema.Field> FieldListBody() :
{
final List<Schema.Field> fields = new ArrayList<Schema.Field>();
Schema.Field field = null;
}
{
field = Field() { fields.add(field); }
(
<COMMA> field = Field() { fields.add(field); }
)*
{
return fields;
}
}
Schema.Field Field() :
{
final String name;
final Schema.FieldType type;
final boolean nullable;
Schema.Field field = null;
SqlNode comment = null;
}
{
name = Identifier()
type = FieldType()
{
field = Schema.Field.of(name, type);
}
(
<NULL> { field = field.withNullable(true); }
|
<NOT> <NULL> { field = field.withNullable(false); }
|
{ field = field.withNullable(true); }
)
[
<COMMENT> comment = StringLiteral()
{
if (comment != null) {
String commentString =
((NlsString) SqlLiteral.value(comment)).getValue();
field = field.withDescription(commentString);
}
}
]
{
return field;
}
}
SqlNodeList PropertyList() :
{
SqlNodeList list = new SqlNodeList(getPos());
SqlNode property;
}
{
property = Property() { list.add(property); }
(
<COMMA> property = Property() { list.add(property); }
)*
{
return list;
}
}
SqlNode Property() :
{
SqlNode key;
SqlNode value;
}
{
key = StringLiteral()
<EQ>
value = StringLiteral()
{
SqlNodeList pair = new SqlNodeList(getPos());
pair.add(key);
pair.add(value);
return pair;
}
}
/**
* CREATE CATALOG ( IF NOT EXISTS )? catalog_name
* TYPE type_name
* ( PROPERTIES '(' key = value ( ',' key = value )* ')' )?
*/
SqlCreate SqlCreateCatalog(Span s, boolean replace) :
{
final boolean ifNotExists;
final SqlNode catalogName;
final SqlNode type;
SqlNodeList properties = null;
}
{
<CATALOG> {
s.add(this);
}
ifNotExists = IfNotExistsOpt()
(
catalogName = StringLiteral()
|
catalogName = SimpleIdentifier()
)
<TYPE>
(
type = StringLiteral()
|
type = SimpleIdentifier()
)
[ <PROPERTIES> <LPAREN> properties = PropertyList() <RPAREN> ]
{
return new SqlCreateCatalog(
s.end(this),
replace,
ifNotExists,
catalogName,
type,
properties);
}
}
/**
* SET CATALOG catalog_name
*/
SqlCall SqlSetCatalog(Span s, String scope) :
{
final SqlNode catalogName;
}
{
<SET> {
s.add(this);
}
<CATALOG>
(
catalogName = StringLiteral()
|
catalogName = SimpleIdentifier()
)
{
return new SqlSetCatalog(
s.end(this),
scope,
catalogName);
}
}
SqlDrop SqlDropCatalog(Span s, boolean replace) :
{
final boolean ifExists;
final SqlNode catalogName;
}
{
<CATALOG> ifExists = IfExistsOpt()
(
catalogName = StringLiteral()
|
catalogName = SimpleIdentifier()
)
{
return new SqlDropCatalog(s.end(this), ifExists, catalogName);
}
}
SqlNodeList PartitionFieldList() :
{
final List<SqlNode> list = new ArrayList<SqlNode>();
SqlNode field;
}
{
field = StringLiteral() { list.add(field); }
(
<COMMA> field = StringLiteral() { list.add(field); }
)*
{
return new SqlNodeList(list, getPos());
}
}
/**
* Note: This example is probably out of sync with the code.
*
* CREATE EXTERNAL TABLE ( IF NOT EXISTS )?
* ( database_name '.' )? table_name '(' column_def ( ',' column_def )* ')'
* TYPE type_name
* ( PARTITIONED BY '(' partition_field ( ',' partition_field )* ')' )?
* ( COMMENT comment_string )?
* ( LOCATION location_string )?
* ( TBLPROPERTIES tbl_properties )?
*/
SqlCreate SqlCreateExternalTable(Span s, boolean replace) :
{
final boolean ifNotExists;
final SqlIdentifier id;
List<Schema.Field> fieldList = null;
final SqlNode type;
SqlNodeList partitionFields = null;
SqlNode comment = null;
SqlNode location = null;
SqlNode tblProperties = null;
}
{
<EXTERNAL> <TABLE> {
s.add(this);
}
ifNotExists = IfNotExistsOpt()
id = CompoundIdentifier()
fieldList = FieldListParens()
<TYPE>
(
type = StringLiteral()
|
type = SimpleIdentifier()
)
[ <PARTITIONED> <BY> <LPAREN> partitionFields = PartitionFieldList() <RPAREN> ]
[ <COMMENT> comment = StringLiteral() ]
[ <LOCATION> location = StringLiteral() ]
[ <TBLPROPERTIES> tblProperties = StringLiteral() ]
{
return
new SqlCreateExternalTable(
s.end(this),
replace,
ifNotExists,
id,
fieldList,
type,
partitionFields,
comment,
location,
tblProperties);
}
}
SqlCreate SqlCreateFunction(Span s, boolean replace) :
{
boolean isAggregate = false;
final SqlIdentifier name;
final SqlNode jarName;
}
{
(
<AGGREGATE> {
isAggregate = true;
}
)?
<FUNCTION> {
s.add(this);
}
name = CompoundIdentifier()
<USING> <JAR>
jarName = StringLiteral()
{
return
new SqlCreateFunction(
s.end(this),
replace,
name,
jarName,
isAggregate);
}
}
SqlCreate SqlCreateTableNotSupportedMessage(Span s, boolean replace) :
{
}
{
<TABLE>
{
throw new ParseException("'CREATE TABLE' is not supported in SQL. You can use "
+ "'CREATE EXTERNAL TABLE' to register an external data source to SQL. For more details, "
+ "please check: https://beam.apache.org/documentation/dsls/sql/create-external-table");
}
}
SqlDrop SqlDropTable(Span s, boolean replace) :
{
final boolean ifExists;
final SqlIdentifier id;
}
{
<TABLE> ifExists = IfExistsOpt() id = CompoundIdentifier() {
return SqlDdlNodes.dropTable(s.end(this), ifExists, id);
}
}
Schema.FieldType FieldType() :
{
final SqlTypeName collectionTypeName;
Schema.FieldType fieldType;
final Span s = Span.of();
}
{
(
fieldType = Map()
|
fieldType = Array()
|
fieldType = Row()
|
fieldType = SimpleType()
)
{
return fieldType;
}
}
Schema.FieldType Array() :
{
final Schema.FieldType arrayElementType;
}
{
<ARRAY> <LT> arrayElementType = FieldType() <GT>
{
return Schema.FieldType.array(arrayElementType);
}
}
Schema.FieldType Map() :
{
final Schema.FieldType mapKeyType;
final Schema.FieldType mapValueType;
}
{
<MAP>
<LT>
mapKeyType = SimpleType()
<COMMA>
mapValueType = FieldType()
<GT>
{
return Schema.FieldType.map(mapKeyType, mapValueType);
}
}
Schema.FieldType Row() :
{
final List<Schema.Field> fields;
}
{
<ROW> fields = RowFields()
{
Schema rowSchema = Schema.builder().addFields(fields).build();
return Schema.FieldType.row(rowSchema);
}
}
List<Schema.Field> RowFields() :
{
final List<Schema.Field> fields;
}
{
(
fields = FieldListParens()
|
fields = FieldListAngular()
)
{
return fields;
}
}
Schema.FieldType SimpleType() :
{
final Span s = Span.of();
final SqlTypeNameSpec simpleTypeName;
}
{
simpleTypeName = SqlTypeName(s)
{
s.end(this);
return CalciteUtils.toFieldType(simpleTypeName);
}
}
SqlSetOptionBeam SqlSetOptionBeam(Span s, String scope) :
{
SqlIdentifier name;
final SqlNode val;
}
{
(
<SET> {
s.add(this);
}
name = CompoundIdentifier()
<EQ>
(
val = Literal()
|
val = SimpleIdentifier()
|
<ON> {
// OFF is handled by SimpleIdentifier, ON handled here.
val = new SqlIdentifier(token.image.toUpperCase(Locale.ROOT),
getPos());
}
)
{
return new SqlSetOptionBeam(s.end(val), scope, name, val);
}
|
<RESET> {
s.add(this);
}
(
name = CompoundIdentifier()
|
<ALL> {
name = new SqlIdentifier(token.image.toUpperCase(Locale.ROOT),
getPos());
}
)
{
return new SqlSetOptionBeam(s.end(name), scope, name, null);
}
)
}
// End parserImpls.ftl