Skip to content

Commit 65b6f18

Browse files
committed
feat: add insert overwrite from spark sql
1 parent ef3df9c commit 65b6f18

File tree

2 files changed

+157
-20
lines changed

2 files changed

+157
-20
lines changed

grammar.js

+36-20
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = grammar({
4444
keyword_replace: _ => make_keyword("replace"),
4545
keyword_update: _ => make_keyword("update"),
4646
keyword_into: _ => make_keyword("into"),
47+
keyword_overwrite: _ => make_keyword("overwrite"),
4748
keyword_values: _ => make_keyword("values"),
4849
keyword_set: _ => make_keyword("set"),
4950
keyword_from: _ => make_keyword("from"),
@@ -1270,8 +1271,14 @@ module.exports = grammar({
12701271
),
12711272
),
12721273
optional($.keyword_ignore),
1273-
optional($.keyword_into),
1274+
optional(
1275+
choice(
1276+
$.keyword_into,
1277+
$.keyword_overwrite, // Spark SQL
1278+
),
1279+
),
12741280
$.table_reference,
1281+
optional($.table_partition), // Spark SQL
12751282
optional(
12761283
seq(
12771284
$.keyword_as,
@@ -1418,26 +1425,35 @@ module.exports = grammar({
14181425
),
14191426

14201427
table_partition: $ => seq(
1421-
choice(
1422-
// Postgres/MySQL style
1423-
seq(
1424-
$.keyword_partition,
1425-
$.keyword_by,
1426-
choice(
1427-
$.keyword_range,
1428-
$.keyword_hash,
1429-
)
1430-
),
1431-
// Hive style
1432-
seq(
1433-
$.keyword_partitioned,
1434-
$.keyword_by,
1435-
),
1428+
choice(
1429+
// Postgres/MySQL style
1430+
seq(
1431+
$.keyword_partition,
1432+
$.keyword_by,
1433+
choice(
1434+
$.keyword_range,
1435+
$.keyword_hash,
1436+
)
14361437
),
1437-
choice(
1438-
seq('(', $.identifier, ')'), // postgres
1439-
$.column_definitions // impala/hive
1440-
)
1438+
// Hive style
1439+
seq(
1440+
$.keyword_partitioned,
1441+
$.keyword_by,
1442+
),
1443+
// Spark SQL
1444+
$.keyword_partition,
1445+
),
1446+
choice(
1447+
seq('(', $.identifier, ')'), // postgres
1448+
$.column_definitions, // impala/hive
1449+
seq('(', $._key_value_pair, repeat(seq(',', $._key_value_pair)), ')',), // Spark SQL
1450+
)
1451+
),
1452+
1453+
_key_value_pair: $ => seq(
1454+
field('key',$.identifier),
1455+
'=',
1456+
field('value', alias($._literal_string, $.literal)),
14411457
),
14421458

14431459
stored_as: $ => seq(

test/corpus/insert.txt

+121
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,124 @@ VALUES('foo','bar', 3);
404404
(literal)
405405
(literal)
406406
(literal)))))
407+
408+
================================================================================
409+
Insert overwrite
410+
================================================================================
411+
412+
INSERT OVERWRITE tab1
413+
SELECT
414+
col1,
415+
col2
416+
FROM
417+
(
418+
SELECT
419+
*
420+
FROM
421+
tab2
422+
WHERE
423+
key1 >= 'val'
424+
) a1;
425+
426+
--------------------------------------------------------------------------------
427+
428+
(program
429+
(statement
430+
(insert
431+
(keyword_insert)
432+
(keyword_overwrite)
433+
(table_reference
434+
(identifier))
435+
(select
436+
(keyword_select)
437+
(select_expression
438+
(term
439+
(field
440+
(identifier)))
441+
(term
442+
(field
443+
(identifier)))))
444+
(from
445+
(keyword_from)
446+
(relation
447+
(subquery
448+
(select
449+
(keyword_select)
450+
(select_expression
451+
(all_fields)))
452+
(from
453+
(keyword_from)
454+
(relation
455+
(table_reference
456+
(identifier)))
457+
(where
458+
(keyword_where)
459+
(binary_expression
460+
(field
461+
(identifier))
462+
(literal)))))
463+
(identifier))))))
464+
465+
================================================================================
466+
Insert overwrite with partition
467+
================================================================================
468+
469+
INSERT OVERWRITE tab1
470+
PARTITION (key1 = 'val1', key2 = 'val2')
471+
SELECT
472+
col1,
473+
col2
474+
FROM
475+
(
476+
SELECT
477+
*
478+
FROM
479+
tab2
480+
WHERE
481+
key1 >= 'val'
482+
) a1;
483+
484+
--------------------------------------------------------------------------------
485+
486+
(program
487+
(statement
488+
(insert
489+
(keyword_insert)
490+
(keyword_overwrite)
491+
(table_reference
492+
(identifier))
493+
(table_partition
494+
(keyword_partition)
495+
(identifier)
496+
(literal)
497+
(identifier)
498+
(literal))
499+
(select
500+
(keyword_select)
501+
(select_expression
502+
(term
503+
(field
504+
(identifier)))
505+
(term
506+
(field
507+
(identifier)))))
508+
(from
509+
(keyword_from)
510+
(relation
511+
(subquery
512+
(select
513+
(keyword_select)
514+
(select_expression
515+
(all_fields)))
516+
(from
517+
(keyword_from)
518+
(relation
519+
(table_reference
520+
(identifier)))
521+
(where
522+
(keyword_where)
523+
(binary_expression
524+
(field
525+
(identifier))
526+
(literal)))))
527+
(identifier))))))

0 commit comments

Comments
 (0)