You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2025/08/26 08:44:03 UTC

(shardingsphere) branch master updated: Support Hive Inserting values into tables from SQL & UPDATE statement parse (#36415)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 83e29fb3b66 Support Hive Inserting values into tables from SQL & UPDATE statement parse (#36415)
83e29fb3b66 is described below

commit 83e29fb3b663882cc4af3e88a7def12c5bb2f248
Author: Claire <cl...@163.com>
AuthorDate: Tue Aug 26 16:43:56 2025 +0800

    Support Hive Inserting values into tables from SQL & UPDATE statement parse (#36415)
    
    * support insert and update
    
    * update release-notes
---
 RELEASE-NOTES.md                                   |   1 +
 .../src/main/antlr4/imports/hive/DMLStatement.g4   |   5 +
 .../statement/type/HiveDMLStatementVisitor.java    |  12 +++
 .../parser/src/main/resources/case/dml/insert.xml  | 106 +++++++++++++++++++++
 .../parser/src/main/resources/case/dml/update.xml  |  88 +++++++++++++++++
 .../main/resources/sql/supported/dml/insert.xml    |   5 +
 .../main/resources/sql/supported/dml/update.xml    |   4 +
 7 files changed, 221 insertions(+)

diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index cecc6206891..e5250c9d4c6 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -84,6 +84,7 @@
 1. SQL Parser: Support Hive ABORT statement parse - [#36378](https://github.com/apache/shardingsphere/pull/36378)
 1. SQL Parser: Support Hive Inserting data into Hive Tables from queries statement parse - [#36320](https://github.com/apache/shardingsphere/pull/36320)
 1. SQL Parser: Support Hive Writing data into the filesystem from queries statement parse - [#36371](https://github.com/apache/shardingsphere/pull/36371)
+1. SQL Parser: Support Hive Inserting values into tables from SQL & UPDATE statement parse - [#36415](https://github.com/apache/shardingsphere/pull/36415)
 1. SQL Parser: Support SQL Server xml methods parse - [#35911](https://github.com/apache/shardingsphere/pull/35911)
 1. SQL Parser: Support SQL Server CHANGETABLE function parse - [#35920](https://github.com/apache/shardingsphere/pull/35920)
 1. SQL Parser: Support SQL Server AI_GENERATE_EMBEDDINGS function parse - [#35922](https://github.com/apache/shardingsphere/pull/35922)
diff --git a/parser/sql/engine/dialect/hive/src/main/antlr4/imports/hive/DMLStatement.g4 b/parser/sql/engine/dialect/hive/src/main/antlr4/imports/hive/DMLStatement.g4
index 6875c2e2ffb..01517bbea87 100644
--- a/parser/sql/engine/dialect/hive/src/main/antlr4/imports/hive/DMLStatement.g4
+++ b/parser/sql/engine/dialect/hive/src/main/antlr4/imports/hive/DMLStatement.g4
@@ -23,6 +23,7 @@ insert
     : INSERT insertSpecification INTO? tableName partitionNames? (insertValuesClause | setAssignmentsClause | insertSelectClause) onDuplicateKeyClause?
     | insertDataIntoTablesFromQueries
     | writingDataIntoFileSystem
+    | insertingValuesIntoTables
     ;
 
 insertSpecification
@@ -397,3 +398,7 @@ writingDataIntoFileSystem
 insertOverwriteStandardSyntax
     : INSERT OVERWRITE LOCAL? DIRECTORY string_ rowFormat? storedClause? select
     ;
+
+insertingValuesIntoTables
+    : INSERT INTO TABLE tableName dynamicPartitionClause? insertValuesClause
+    ;
diff --git a/parser/sql/engine/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/engine/hive/visitor/statement/type/HiveDMLStatementVisitor.java b/parser/sql/engine/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/engine/hive/visitor/statement/type/HiveDMLStatementVisitor.java
index 047d4f7d1da..b9fdd19e66c 100644
--- a/parser/sql/engine/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/engine/hive/visitor/statement/type/HiveDMLStatementVisitor.java
+++ b/parser/sql/engine/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/engine/hive/visitor/statement/type/HiveDMLStatementVisitor.java
@@ -128,6 +128,7 @@ import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.WindowCl
 import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.WindowFunctionContext;
 import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.WindowItemContext;
 import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.WritingDataIntoFileSystemContext;
+import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.InsertingValuesIntoTablesContext;
 import org.apache.shardingsphere.sql.parser.engine.hive.visitor.statement.HiveStatementVisitor;
 import org.apache.shardingsphere.sql.parser.statement.core.enums.AggregationType;
 import org.apache.shardingsphere.sql.parser.statement.core.enums.CombineType;
@@ -878,6 +879,9 @@ public final class HiveDMLStatementVisitor extends HiveStatementVisitor implemen
         if (null != ctx.writingDataIntoFileSystem()) {
             return visit(ctx.writingDataIntoFileSystem());
         }
+        if (null != ctx.insertingValuesIntoTables()) {
+            return visit(ctx.insertingValuesIntoTables());
+        }
         InsertStatement result;
         if (null != ctx.insertValuesClause()) {
             result = (InsertStatement) visit(ctx.insertValuesClause());
@@ -1040,6 +1044,14 @@ public final class HiveDMLStatementVisitor extends HiveStatementVisitor implemen
         return result;
     }
     
+    @Override
+    public ASTNode visitInsertingValuesIntoTables(final InsertingValuesIntoTablesContext ctx) {
+        InsertStatement result = (InsertStatement) visit(ctx.insertValuesClause());
+        result.setTable((SimpleTableSegment) visit(ctx.tableName()));
+        result.addParameterMarkers(getParameterMarkerSegments());
+        return result;
+    }
+    
     private SubquerySegment createInsertSelectSegment(final SelectContext ctx) {
         SelectStatement selectStatement = (SelectStatement) visit(ctx);
         return new SubquerySegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), selectStatement, getOriginalText(ctx));
diff --git a/test/it/parser/src/main/resources/case/dml/insert.xml b/test/it/parser/src/main/resources/case/dml/insert.xml
index b6b9c5880a2..c05597e7b6e 100644
--- a/test/it/parser/src/main/resources/case/dml/insert.xml
+++ b/test/it/parser/src/main/resources/case/dml/insert.xml
@@ -5293,4 +5293,110 @@
             </insert-statement>
         </multi-table-insert-into>
     </insert>
+
+    <insert sql-case-id="insert_into_hive_basic">
+        <table name="students" start-index="18" stop-index="25" />
+        <columns start-index="26" stop-index="26" />
+        <values>
+            <value>
+                <assignment-value>
+                    <literal-expression value="1" start-index="35" stop-index="35" />
+                </assignment-value>
+                <assignment-value>
+                    <literal-expression value="Alice" start-index="38" stop-index="44" />
+                </assignment-value>
+                <assignment-value>
+                    <literal-expression value="20" start-index="47" stop-index="48" />
+                </assignment-value>
+            </value>
+        </values>
+    </insert>
+
+    <insert sql-case-id="insert_into_two_value">
+        <table name="students" start-index="18" stop-index="25" />
+        <columns start-index="26" stop-index="26" />
+        <values>
+            <value>
+                <assignment-value>
+                    <literal-expression value="1" start-index="35" stop-index="35" />
+                </assignment-value>
+                <assignment-value>
+                    <literal-expression value="Alice" start-index="38" stop-index="44" />
+                </assignment-value>
+            </value>
+            <value>
+                <assignment-value>
+                    <literal-expression value="2" start-index="49" stop-index="49" />
+                </assignment-value>
+                <assignment-value>
+                    <literal-expression value="Bob" start-index="52" stop-index="56" />
+                </assignment-value>
+            </value>
+        </values>
+    </insert>
+
+    <insert sql-case-id="insert_into_with_partition">
+        <table name="sales" start-index="18" stop-index="22" />
+        <columns start-index="49" stop-index="49" />
+        <values>
+            <value>
+                <assignment-value>
+                    <literal-expression value="1001" start-index="58" stop-index="61" />
+                </assignment-value>
+                <assignment-value>
+                    <literal-expression value="Laptop" start-index="64" stop-index="71" />
+                </assignment-value>
+                <assignment-value>
+                    <literal-expression value="5999" start-index="74" stop-index="77" />
+                </assignment-value>
+            </value>
+        </values>
+    </insert>
+
+    <insert sql-case-id="insert_into_without_val1">
+        <table name="sales" start-index="18" stop-index="22" />
+        <columns start-index="42" stop-index="42" />
+        <values>
+            <value>
+                <assignment-value>
+                    <literal-expression value="1001" start-index="51" stop-index="54" />
+                </assignment-value>
+                <assignment-value>
+                    <literal-expression value="Laptop" start-index="57" stop-index="64" />
+                </assignment-value>
+                <assignment-value>
+                    <literal-expression value="5999" start-index="67" stop-index="70" />
+                </assignment-value>
+            </value>
+        </values>
+    </insert>
+
+    <insert sql-case-id="insert_into_with_null">
+        <table name="user_info" start-index="18" stop-index="26" />
+        <columns start-index="27" stop-index="27" />
+        <values>
+            <value>
+                <assignment-value>
+                    <literal-expression value="301" start-index="36" stop-index="38" />
+                </assignment-value>
+                <assignment-value>
+                    <literal-expression value="null" start-index="41" stop-index="44" />
+                </assignment-value>
+                <assignment-value>
+                    <literal-expression value="New York" start-index="47" stop-index="56" />
+                </assignment-value>
+            </value>
+            <value>
+                <assignment-value>
+                    <literal-expression value="302" start-index="61" stop-index="63" />
+                </assignment-value>
+                <assignment-value>
+                    <literal-expression value="Charlie" start-index="66" stop-index="74" />
+                </assignment-value>
+                <assignment-value>
+                    <literal-expression value="null" start-index="77" stop-index="80" />
+                </assignment-value>
+            </value>
+        </values>
+    </insert>
 </sql-parser-test-cases>
diff --git a/test/it/parser/src/main/resources/case/dml/update.xml b/test/it/parser/src/main/resources/case/dml/update.xml
index 2c7d84e9ecd..f846230bb1c 100644
--- a/test/it/parser/src/main/resources/case/dml/update.xml
+++ b/test/it/parser/src/main/resources/case/dml/update.xml
@@ -2424,4 +2424,92 @@
         </where>
         <option-hint start-index="71" stop-index="96" text="OPTION (LABEL = N'label1')" />
     </update>
+
+    <update sql-case-id="update_with_where">
+        <table start-index="7" stop-index="15">
+            <simple-table name="employees" start-index="7" stop-index="15" />
+        </table>
+        <set start-index="17" stop-index="33">
+            <assignment start-index="21" stop-index="35">
+                <column name="salary" start-index="21" stop-index="26" />
+                <assignment-value>
+                    <literal-expression value="5000" start-index="30" stop-index="33" />
+                </assignment-value>
+            </assignment>
+        </set>
+        <where start-index="35" stop-index="57">
+            <expr>
+                <binary-operation-expression start-index="41" stop-index="57">
+                    <left>
+                        <column name="department" start-index="41" stop-index="50" />
+                    </left>
+                    <operator>=</operator>
+                    <right>
+                        <literal-expression value="HR" start-index="54" stop-index="57" />
+                    </right>
+                </binary-operation-expression>
+            </expr>
+        </where>
+    </update>
+
+    <update sql-case-id="update_multiple_cols">
+        <table start-index="7" stop-index="15">
+            <simple-table name="customers" start-index="7" stop-index="15" />
+        </table>
+        <set start-index="17" stop-index="64">
+            <assignment start-index="21" stop-index="35">
+                <column name="email" start-index="21" stop-index="25" />
+                <assignment-value>
+                    <literal-expression value="new@example.com" start-index="29" stop-index="45" />
+                </assignment-value>
+            </assignment>
+            <assignment start-index="48" stop-index="64">
+                <column name="status" start-index="48" stop-index="53" />
+                <assignment-value>
+                    <literal-expression value="active" start-index="57" stop-index="64" />
+                </assignment-value>
+            </assignment>
+        </set>
+        <where start-index="66" stop-index="89">
+            <expr>
+                <binary-operation-expression start-index="72" stop-index="89">
+                    <left>
+                        <column name="customer_id" start-index="72" stop-index="82" />
+                    </left>
+                    <operator>=</operator>
+                    <right>
+                        <literal-expression value="1001" start-index="86" stop-index="89" />
+                    </right>
+                </binary-operation-expression>
+            </expr>
+        </where>
+    </update>
+
+    <update sql-case-id="update_basic">
+        <table start-index="7" stop-index="14">
+            <simple-table name="products" start-index="7" stop-index="14" />
+        </table>
+        <set start-index="16" stop-index="28">
+            <assignment start-index="20" stop-index="28">
+                <column name="stock" start-index="20" stop-index="24" />
+                <assignment-value>
+                    <literal-expression value="0" start-index="28" stop-index="28" />
+                </assignment-value>
+            </assignment>
+        </set>
+    </update>
+
+    <update sql-case-id="update_with_null">
+        <table start-index="7" stop-index="18">
+            <simple-table name="user_profiles" start-index="7" stop-index="19" />
+        </table>
+        <set start-index="21" stop-index="41">
+            <assignment start-index="24" stop-index="42">
+                <column name="last_login" start-index="25" stop-index="34" />
+                <assignment-value>
+                    <literal-expression value="null" start-index="38" stop-index="41" />
+                </assignment-value>
+            </assignment>
+        </set>
+    </update>
 </sql-parser-test-cases>
diff --git a/test/it/parser/src/main/resources/sql/supported/dml/insert.xml b/test/it/parser/src/main/resources/sql/supported/dml/insert.xml
index 7f717609979..7a9fa17c777 100644
--- a/test/it/parser/src/main/resources/sql/supported/dml/insert.xml
+++ b/test/it/parser/src/main/resources/sql/supported/dml/insert.xml
@@ -189,4 +189,9 @@
     <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" />
     <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" />
     <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" />
+    <sql-case id="insert_into_hive_basic" value="INSERT INTO TABLE students VALUES (1, 'Alice', 20);" db-types="Hive" />
+    <sql-case id="insert_into_two_value" value="INSERT INTO TABLE students VALUES (1, 'Alice'), (2, 'Bob');" db-types="Hive" />
+    <sql-case id="insert_into_with_partition" value="INSERT INTO TABLE sales PARTITION (region='Asia') VALUES (1001, 'Laptop', 5999);" db-types="Hive" />
+    <sql-case id="insert_into_without_val1" value="INSERT INTO TABLE sales PARTITION (region) VALUES (1001, 'Laptop', 5999);" db-types="Hive" />
+    <sql-case id="insert_into_with_null" value="INSERT INTO TABLE user_info VALUES (301, NULL, 'New York'), (302, 'Charlie', NULL);" db-types="Hive" />
 </sql-cases>
diff --git a/test/it/parser/src/main/resources/sql/supported/dml/update.xml b/test/it/parser/src/main/resources/sql/supported/dml/update.xml
index fcede4be706..1b00e18003f 100644
--- a/test/it/parser/src/main/resources/sql/supported/dml/update.xml
+++ b/test/it/parser/src/main/resources/sql/supported/dml/update.xml
@@ -70,4 +70,8 @@
     <sql-case id="update_with_location_setXY" value="UPDATE Cities SET Location.SetXY(23.5, 23.5) WHERE Name = 'Anchorage'" db-types="SQLServer"/>
     <sql-case id="update_returning_expressions" value="UPDATE t_order SET status = status - ? WHERE order_id = ? AND user_id = ? RETURNING status" db-types="Firebird" />
     <sql-case id="update_with_option_label_hint" value="UPDATE DimProduct SET ProductSubcategoryKey = 2 WHERE ProductKey = 313 OPTION (LABEL = N'label1');" db-types="SQLServer"/>
+    <sql-case id="update_with_where" value="UPDATE employees SET salary = 5000 WHERE department = 'HR';" db-types="Hive" />
+    <sql-case id="update_multiple_cols" value="UPDATE customers SET email = 'new@example.com', status = 'active' WHERE customer_id = 1001;" db-types="Hive" />
+    <sql-case id="update_basic" value="UPDATE products SET stock = 0;" db-types="Hive" />
+    <sql-case id="update_with_null" value="UPDATE user_profiles SET last_login = NULL;" db-types="Hive" />
 </sql-cases>