Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add insert overwrite from spark sql #148

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = grammar({
keyword_replace: _ => make_keyword("replace"),
keyword_update: _ => make_keyword("update"),
keyword_into: _ => make_keyword("into"),
keyword_overwrite: _ => make_keyword("overwrite"),
keyword_values: _ => make_keyword("values"),
keyword_set: _ => make_keyword("set"),
keyword_from: _ => make_keyword("from"),
Expand Down Expand Up @@ -1270,8 +1271,14 @@ module.exports = grammar({
),
),
optional($.keyword_ignore),
optional($.keyword_into),
optional(
choice(
$.keyword_into,
$.keyword_overwrite, // Spark SQL
),
),
$.table_reference,
optional($.table_partition), // Spark SQL
optional(
seq(
$.keyword_as,
Expand Down Expand Up @@ -1418,26 +1425,35 @@ module.exports = grammar({
),

table_partition: $ => seq(
choice(
// Postgres/MySQL style
seq(
$.keyword_partition,
$.keyword_by,
choice(
$.keyword_range,
$.keyword_hash,
)
),
// Hive style
seq(
$.keyword_partitioned,
$.keyword_by,
),
choice(
// Postgres/MySQL style
seq(
$.keyword_partition,
$.keyword_by,
choice(
$.keyword_range,
$.keyword_hash,
)
),
choice(
seq('(', $.identifier, ')'), // postgres
$.column_definitions // impala/hive
)
// Hive style
seq(
$.keyword_partitioned,
$.keyword_by,
),
// Spark SQL
$.keyword_partition,
),
choice(
seq('(', $.identifier, ')'), // postgres
$.column_definitions, // impala/hive
seq('(', $._key_value_pair, repeat(seq(',', $._key_value_pair)), ')',), // Spark SQL
)
),

_key_value_pair: $ => seq(
field('key',$.identifier),
'=',
field('value', alias($._literal_string, $.literal)),
),

stored_as: $ => seq(
Expand Down
121 changes: 121 additions & 0 deletions test/corpus/insert.txt
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,124 @@ VALUES('foo','bar', 3);
(literal)
(literal)
(literal)))))

================================================================================
Insert overwrite
================================================================================

INSERT OVERWRITE tab1
SELECT
col1,
col2
FROM
(
SELECT
*
FROM
tab2
WHERE
key1 >= 'val'
) a1;

--------------------------------------------------------------------------------

(program
(statement
(insert
(keyword_insert)
(keyword_overwrite)
(table_reference
(identifier))
(select
(keyword_select)
(select_expression
(term
(field
(identifier)))
(term
(field
(identifier)))))
(from
(keyword_from)
(relation
(subquery
(select
(keyword_select)
(select_expression
(all_fields)))
(from
(keyword_from)
(relation
(table_reference
(identifier)))
(where
(keyword_where)
(binary_expression
(field
(identifier))
(literal)))))
(identifier))))))

================================================================================
Insert overwrite with partition
================================================================================

INSERT OVERWRITE tab1
PARTITION (key1 = 'val1', key2 = 'val2')
SELECT
col1,
col2
FROM
(
SELECT
*
FROM
tab2
WHERE
key1 >= 'val'
) a1;

--------------------------------------------------------------------------------

(program
(statement
(insert
(keyword_insert)
(keyword_overwrite)
(table_reference
(identifier))
(table_partition
(keyword_partition)
(identifier)
(literal)
(identifier)
(literal))
(select
(keyword_select)
(select_expression
(term
(field
(identifier)))
(term
(field
(identifier)))))
(from
(keyword_from)
(relation
(subquery
(select
(keyword_select)
(select_expression
(all_fields)))
(from
(keyword_from)
(relation
(table_reference
(identifier)))
(where
(keyword_where)
(binary_expression
(field
(identifier))
(literal)))))
(identifier))))))