Skip to content

Commit 83e29fb

Browse files
authored
Support Hive Inserting values into tables from SQL & UPDATE statement parse (#36415)
* support insert and update * update release-notes
1 parent e641125 commit 83e29fb

File tree

7 files changed

+221
-0
lines changed

7 files changed

+221
-0
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
1. SQL Parser: Support Hive ABORT statement parse - [#36378](https://github.com/apache/shardingsphere/pull/36378)
8585
1. SQL Parser: Support Hive Inserting data into Hive Tables from queries statement parse - [#36320](https://github.com/apache/shardingsphere/pull/36320)
8686
1. SQL Parser: Support Hive Writing data into the filesystem from queries statement parse - [#36371](https://github.com/apache/shardingsphere/pull/36371)
87+
1. SQL Parser: Support Hive Inserting values into tables from SQL & UPDATE statement parse - [#36415](https://github.com/apache/shardingsphere/pull/36415)
8788
1. SQL Parser: Support SQL Server xml methods parse - [#35911](https://github.com/apache/shardingsphere/pull/35911)
8889
1. SQL Parser: Support SQL Server CHANGETABLE function parse - [#35920](https://github.com/apache/shardingsphere/pull/35920)
8990
1. SQL Parser: Support SQL Server AI_GENERATE_EMBEDDINGS function parse - [#35922](https://github.com/apache/shardingsphere/pull/35922)

parser/sql/engine/dialect/hive/src/main/antlr4/imports/hive/DMLStatement.g4

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ insert
2323
: INSERT insertSpecification INTO? tableName partitionNames? (insertValuesClause | setAssignmentsClause | insertSelectClause) onDuplicateKeyClause?
2424
| insertDataIntoTablesFromQueries
2525
| writingDataIntoFileSystem
26+
| insertingValuesIntoTables
2627
;
2728

2829
insertSpecification
@@ -397,3 +398,7 @@ writingDataIntoFileSystem
397398
insertOverwriteStandardSyntax
398399
: INSERT OVERWRITE LOCAL? DIRECTORY string_ rowFormat? storedClause? select
399400
;
401+
402+
insertingValuesIntoTables
403+
: INSERT INTO TABLE tableName dynamicPartitionClause? insertValuesClause
404+
;

parser/sql/engine/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/engine/hive/visitor/statement/type/HiveDMLStatementVisitor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.WindowFunctionContext;
129129
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.WindowItemContext;
130130
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.WritingDataIntoFileSystemContext;
131+
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.InsertingValuesIntoTablesContext;
131132
import org.apache.shardingsphere.sql.parser.engine.hive.visitor.statement.HiveStatementVisitor;
132133
import org.apache.shardingsphere.sql.parser.statement.core.enums.AggregationType;
133134
import org.apache.shardingsphere.sql.parser.statement.core.enums.CombineType;
@@ -878,6 +879,9 @@ public ASTNode visitInsert(final InsertContext ctx) {
878879
if (null != ctx.writingDataIntoFileSystem()) {
879880
return visit(ctx.writingDataIntoFileSystem());
880881
}
882+
if (null != ctx.insertingValuesIntoTables()) {
883+
return visit(ctx.insertingValuesIntoTables());
884+
}
881885
InsertStatement result;
882886
if (null != ctx.insertValuesClause()) {
883887
result = (InsertStatement) visit(ctx.insertValuesClause());
@@ -1040,6 +1044,14 @@ private InsertStatement createHiveInsertStatementForDirectory(final SelectContex
10401044
return result;
10411045
}
10421046

1047+
@Override
1048+
public ASTNode visitInsertingValuesIntoTables(final InsertingValuesIntoTablesContext ctx) {
1049+
InsertStatement result = (InsertStatement) visit(ctx.insertValuesClause());
1050+
result.setTable((SimpleTableSegment) visit(ctx.tableName()));
1051+
result.addParameterMarkers(getParameterMarkerSegments());
1052+
return result;
1053+
}
1054+
10431055
private SubquerySegment createInsertSelectSegment(final SelectContext ctx) {
10441056
SelectStatement selectStatement = (SelectStatement) visit(ctx);
10451057
return new SubquerySegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), selectStatement, getOriginalText(ctx));

test/it/parser/src/main/resources/case/dml/insert.xml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5293,4 +5293,110 @@
52935293
</insert-statement>
52945294
</multi-table-insert-into>
52955295
</insert>
5296+
5297+
<insert sql-case-id="insert_into_hive_basic">
5298+
<table name="students" start-index="18" stop-index="25" />
5299+
<columns start-index="26" stop-index="26" />
5300+
<values>
5301+
<value>
5302+
<assignment-value>
5303+
<literal-expression value="1" start-index="35" stop-index="35" />
5304+
</assignment-value>
5305+
<assignment-value>
5306+
<literal-expression value="Alice" start-index="38" stop-index="44" />
5307+
</assignment-value>
5308+
<assignment-value>
5309+
<literal-expression value="20" start-index="47" stop-index="48" />
5310+
</assignment-value>
5311+
</value>
5312+
</values>
5313+
</insert>
5314+
5315+
<insert sql-case-id="insert_into_two_value">
5316+
<table name="students" start-index="18" stop-index="25" />
5317+
<columns start-index="26" stop-index="26" />
5318+
<values>
5319+
<value>
5320+
<assignment-value>
5321+
<literal-expression value="1" start-index="35" stop-index="35" />
5322+
</assignment-value>
5323+
<assignment-value>
5324+
<literal-expression value="Alice" start-index="38" stop-index="44" />
5325+
</assignment-value>
5326+
</value>
5327+
<value>
5328+
<assignment-value>
5329+
<literal-expression value="2" start-index="49" stop-index="49" />
5330+
</assignment-value>
5331+
<assignment-value>
5332+
<literal-expression value="Bob" start-index="52" stop-index="56" />
5333+
</assignment-value>
5334+
</value>
5335+
</values>
5336+
</insert>
5337+
5338+
<insert sql-case-id="insert_into_with_partition">
5339+
<table name="sales" start-index="18" stop-index="22" />
5340+
<columns start-index="49" stop-index="49" />
5341+
<values>
5342+
<value>
5343+
<assignment-value>
5344+
<literal-expression value="1001" start-index="58" stop-index="61" />
5345+
</assignment-value>
5346+
<assignment-value>
5347+
<literal-expression value="Laptop" start-index="64" stop-index="71" />
5348+
</assignment-value>
5349+
<assignment-value>
5350+
<literal-expression value="5999" start-index="74" stop-index="77" />
5351+
</assignment-value>
5352+
</value>
5353+
</values>
5354+
</insert>
5355+
5356+
<insert sql-case-id="insert_into_without_val1">
5357+
<table name="sales" start-index="18" stop-index="22" />
5358+
<columns start-index="42" stop-index="42" />
5359+
<values>
5360+
<value>
5361+
<assignment-value>
5362+
<literal-expression value="1001" start-index="51" stop-index="54" />
5363+
</assignment-value>
5364+
<assignment-value>
5365+
<literal-expression value="Laptop" start-index="57" stop-index="64" />
5366+
</assignment-value>
5367+
<assignment-value>
5368+
<literal-expression value="5999" start-index="67" stop-index="70" />
5369+
</assignment-value>
5370+
</value>
5371+
</values>
5372+
</insert>
5373+
5374+
<insert sql-case-id="insert_into_with_null">
5375+
<table name="user_info" start-index="18" stop-index="26" />
5376+
<columns start-index="27" stop-index="27" />
5377+
<values>
5378+
<value>
5379+
<assignment-value>
5380+
<literal-expression value="301" start-index="36" stop-index="38" />
5381+
</assignment-value>
5382+
<assignment-value>
5383+
<literal-expression value="null" start-index="41" stop-index="44" />
5384+
</assignment-value>
5385+
<assignment-value>
5386+
<literal-expression value="New York" start-index="47" stop-index="56" />
5387+
</assignment-value>
5388+
</value>
5389+
<value>
5390+
<assignment-value>
5391+
<literal-expression value="302" start-index="61" stop-index="63" />
5392+
</assignment-value>
5393+
<assignment-value>
5394+
<literal-expression value="Charlie" start-index="66" stop-index="74" />
5395+
</assignment-value>
5396+
<assignment-value>
5397+
<literal-expression value="null" start-index="77" stop-index="80" />
5398+
</assignment-value>
5399+
</value>
5400+
</values>
5401+
</insert>
52965402
</sql-parser-test-cases>

test/it/parser/src/main/resources/case/dml/update.xml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,4 +2424,92 @@
24242424
</where>
24252425
<option-hint start-index="71" stop-index="96" text="OPTION (LABEL = N'label1')" />
24262426
</update>
2427+
2428+
<update sql-case-id="update_with_where">
2429+
<table start-index="7" stop-index="15">
2430+
<simple-table name="employees" start-index="7" stop-index="15" />
2431+
</table>
2432+
<set start-index="17" stop-index="33">
2433+
<assignment start-index="21" stop-index="35">
2434+
<column name="salary" start-index="21" stop-index="26" />
2435+
<assignment-value>
2436+
<literal-expression value="5000" start-index="30" stop-index="33" />
2437+
</assignment-value>
2438+
</assignment>
2439+
</set>
2440+
<where start-index="35" stop-index="57">
2441+
<expr>
2442+
<binary-operation-expression start-index="41" stop-index="57">
2443+
<left>
2444+
<column name="department" start-index="41" stop-index="50" />
2445+
</left>
2446+
<operator>=</operator>
2447+
<right>
2448+
<literal-expression value="HR" start-index="54" stop-index="57" />
2449+
</right>
2450+
</binary-operation-expression>
2451+
</expr>
2452+
</where>
2453+
</update>
2454+
2455+
<update sql-case-id="update_multiple_cols">
2456+
<table start-index="7" stop-index="15">
2457+
<simple-table name="customers" start-index="7" stop-index="15" />
2458+
</table>
2459+
<set start-index="17" stop-index="64">
2460+
<assignment start-index="21" stop-index="35">
2461+
<column name="email" start-index="21" stop-index="25" />
2462+
<assignment-value>
2463+
<literal-expression value="[email protected]" start-index="29" stop-index="45" />
2464+
</assignment-value>
2465+
</assignment>
2466+
<assignment start-index="48" stop-index="64">
2467+
<column name="status" start-index="48" stop-index="53" />
2468+
<assignment-value>
2469+
<literal-expression value="active" start-index="57" stop-index="64" />
2470+
</assignment-value>
2471+
</assignment>
2472+
</set>
2473+
<where start-index="66" stop-index="89">
2474+
<expr>
2475+
<binary-operation-expression start-index="72" stop-index="89">
2476+
<left>
2477+
<column name="customer_id" start-index="72" stop-index="82" />
2478+
</left>
2479+
<operator>=</operator>
2480+
<right>
2481+
<literal-expression value="1001" start-index="86" stop-index="89" />
2482+
</right>
2483+
</binary-operation-expression>
2484+
</expr>
2485+
</where>
2486+
</update>
2487+
2488+
<update sql-case-id="update_basic">
2489+
<table start-index="7" stop-index="14">
2490+
<simple-table name="products" start-index="7" stop-index="14" />
2491+
</table>
2492+
<set start-index="16" stop-index="28">
2493+
<assignment start-index="20" stop-index="28">
2494+
<column name="stock" start-index="20" stop-index="24" />
2495+
<assignment-value>
2496+
<literal-expression value="0" start-index="28" stop-index="28" />
2497+
</assignment-value>
2498+
</assignment>
2499+
</set>
2500+
</update>
2501+
2502+
<update sql-case-id="update_with_null">
2503+
<table start-index="7" stop-index="18">
2504+
<simple-table name="user_profiles" start-index="7" stop-index="19" />
2505+
</table>
2506+
<set start-index="21" stop-index="41">
2507+
<assignment start-index="24" stop-index="42">
2508+
<column name="last_login" start-index="25" stop-index="34" />
2509+
<assignment-value>
2510+
<literal-expression value="null" start-index="38" stop-index="41" />
2511+
</assignment-value>
2512+
</assignment>
2513+
</set>
2514+
</update>
24272515
</sql-parser-test-cases>

test/it/parser/src/main/resources/sql/supported/dml/insert.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,9 @@
189189
<sql-case id="two_insert_overwrite_directory" value="FROM employee INSERT OVERWRITE DIRECTORY '/user/output/emp_data' SELECT id INSERT OVERWRITE DIRECTORY '/user/output/emp_data' SELECT name;" db-types="Hive" />
190190
<sql-case id="two_insert_overwrite_directory_with_local" value="FROM employee INSERT OVERWRITE LOCAL DIRECTORY '/user/output/emp_data' SELECT id INSERT OVERWRITE LOCAL DIRECTORY '/user/output/emp_data' SELECT name;" db-types="Hive" />
191191
<sql-case id="three_insert_overwrite_directory" value="FROM employee INSERT OVERWRITE DIRECTORY '/user/output/emp_data' SELECT id INSERT OVERWRITE DIRECTORY '/user/output/emp_data' SELECT name INSERT OVERWRITE DIRECTORY '/user/output/emp_data' SELECT user;" db-types="Hive" />
192+
<sql-case id="insert_into_hive_basic" value="INSERT INTO TABLE students VALUES (1, 'Alice', 20);" db-types="Hive" />
193+
<sql-case id="insert_into_two_value" value="INSERT INTO TABLE students VALUES (1, 'Alice'), (2, 'Bob');" db-types="Hive" />
194+
<sql-case id="insert_into_with_partition" value="INSERT INTO TABLE sales PARTITION (region='Asia') VALUES (1001, 'Laptop', 5999);" db-types="Hive" />
195+
<sql-case id="insert_into_without_val1" value="INSERT INTO TABLE sales PARTITION (region) VALUES (1001, 'Laptop', 5999);" db-types="Hive" />
196+
<sql-case id="insert_into_with_null" value="INSERT INTO TABLE user_info VALUES (301, NULL, 'New York'), (302, 'Charlie', NULL);" db-types="Hive" />
192197
</sql-cases>

test/it/parser/src/main/resources/sql/supported/dml/update.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,8 @@
7070
<sql-case id="update_with_location_setXY" value="UPDATE Cities SET Location.SetXY(23.5, 23.5) WHERE Name = 'Anchorage'" db-types="SQLServer"/>
7171
<sql-case id="update_returning_expressions" value="UPDATE t_order SET status = status - ? WHERE order_id = ? AND user_id = ? RETURNING status" db-types="Firebird" />
7272
<sql-case id="update_with_option_label_hint" value="UPDATE DimProduct SET ProductSubcategoryKey = 2 WHERE ProductKey = 313 OPTION (LABEL = N'label1');" db-types="SQLServer"/>
73+
<sql-case id="update_with_where" value="UPDATE employees SET salary = 5000 WHERE department = 'HR';" db-types="Hive" />
74+
<sql-case id="update_multiple_cols" value="UPDATE customers SET email = '[email protected]', status = 'active' WHERE customer_id = 1001;" db-types="Hive" />
75+
<sql-case id="update_basic" value="UPDATE products SET stock = 0;" db-types="Hive" />
76+
<sql-case id="update_with_null" value="UPDATE user_profiles SET last_login = NULL;" db-types="Hive" />
7377
</sql-cases>

0 commit comments

Comments
 (0)