You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/10/01 10:05:33 UTC

[shardingsphere] branch master updated: fix mysql create table and insert statement parse error (#7676)

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

panjuan 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 aeaf4d2  fix mysql create table and insert statement parse error (#7676)
aeaf4d2 is described below

commit aeaf4d2d7a7deda1d72158735d6d4827b39ec50e
Author: DuanZhengqiang <st...@gmail.com>
AuthorDate: Thu Oct 1 18:05:07 2020 +0800

    fix mysql create table and insert statement parse error (#7676)
---
 .../src/test/resources/sharding/insert.xml         | 10 ++++++
 .../sql/token/pojo/generic/InsertValue.java        |  3 ++
 .../src/main/antlr4/imports/mysql/BaseRule.g4      |  5 +--
 .../src/main/antlr4/imports/mysql/DDLStatement.g4  |  3 +-
 .../src/main/resources/case/ddl/create-table.xml   | 36 ++++++++++++++++++++++
 .../main/resources/sql/supported/ddl/create.xml    |  3 ++
 6 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/insert.xml b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/insert.xml
index 2cf66fa..30eaafd 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/insert.xml
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/insert.xml
@@ -67,6 +67,16 @@
         <output sql="INSERT INTO t_account_1(amount, status, account_id) VALUES (1000, 'test๐ŸŽ๐Ÿ˜€', 1)" />
     </rewrite-assertion>
     
+    <rewrite-assertion id="insert_values_with_columns_with_binary_operation_for_parameters" db-type="MySQL">
+        <input sql="INSERT INTO t_account (account_id, amount, status) VALUES (?, ?, ?)" parameters="100, account_id + 100, OK" />
+        <output sql="INSERT INTO t_account_0 (account_id, amount, status) VALUES (?, ?, ?)" parameters="100, account_id + 100, OK" />
+    </rewrite-assertion>
+
+    <rewrite-assertion id="insert_values_with_columns_with_binary_operation_for_literals" db-type="MySQL">
+        <input sql="INSERT INTO t_account (account_id, amount, status) VALUES (100, account_id + 100, OK)" />
+        <output sql="INSERT INTO t_account_0 (account_id, amount, status) VALUES (100, account_id + 100, OK)" />
+    </rewrite-assertion>
+    
     <rewrite-assertion id="insert_multiple_values_with_columns_with_id_for_parameters">
         <input sql="INSERT INTO t_account (account_id, amount, status) VALUES (100, 0, 'OK'), (?, ?, ?), (102, 2000, 'OK'), (?, ?, ?), (?, ?, ?), (105, 5000, 'OK')" parameters="101, 1000, OK, 103, 3000, OK, 104, 4000, OK" />
         <output sql="INSERT INTO t_account_0 (account_id, amount, status) VALUES (100, 0, 'OK'), (102, 2000, 'OK'), (?, ?, ?)" parameters="104, 4000, OK" />
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/InsertValue.java b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/InsertValue.java
index 1a81acf..009dedc 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/InsertValue.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/InsertValue.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic;
 
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.complex.ComplexExpressionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
@@ -53,6 +54,8 @@ public class InsertValue {
         } else if (expressionSegment instanceof LiteralExpressionSegment) {
             Object literals = ((LiteralExpressionSegment) expressionSegment).getLiterals();
             return literals instanceof String ? String.format("'%s'", ((LiteralExpressionSegment) expressionSegment).getLiterals()) : literals.toString();
+        } else if (expressionSegment instanceof BinaryOperationExpression) {
+            return ((BinaryOperationExpression) expressionSegment).getText();
         }
         return ((ComplexExpressionSegment) expressionSegment).getText();
     }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
index 16e0cf8..707c232 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
@@ -510,7 +510,7 @@ regularFunctionName_
     : IF | LOCALTIME | LOCALTIMESTAMP | REPLACE | INTERVAL | MOD
     | DATABASE | LEFT | RIGHT | DATE | DAY | GEOMCOLLECTION | GEOMETRYCOLLECTION
     | LINESTRING | MULTILINESTRING | MULTIPOINT | MULTIPOLYGON | POINT | POLYGON
-    | TIME | TIMESTAMP | TIMESTAMPADD | TIMESTAMPDIFF | DATE | identifier
+    | TIME | TIMESTAMP | TIMESTAMPADD | TIMESTAMPDIFF | DATE | CURRENT_TIMESTAMP | identifier
     ;
 
 matchExpression_
@@ -575,7 +575,8 @@ dataTypeName
     : INTEGER | INT | SMALLINT | TINYINT | MEDIUMINT | BIGINT | DECIMAL| NUMERIC | FLOAT | DOUBLE | BIT | BOOL | BOOLEAN
     | DEC | DATE | DATETIME | TIMESTAMP | TIME | YEAR | CHAR | VARCHAR | BINARY | VARBINARY | TINYBLOB | TINYTEXT | BLOB
     | TEXT | MEDIUMBLOB | MEDIUMTEXT | LONGBLOB | LONGTEXT | ENUM | SET | GEOMETRY | POINT | LINESTRING | POLYGON
-    | MULTIPOINT | MULTILINESTRING | MULTIPOLYGON | GEOMETRYCOLLECTION | JSON | UNSIGNED | SIGNED
+    | MULTIPOINT | MULTILINESTRING | MULTIPOLYGON | GEOMETRYCOLLECTION | JSON | UNSIGNED | SIGNED | CHARACTER VARYING
+    | FIXED | FLOAT4 | FLOAT8 | INT1 | INT2 | INT3 | INT4 | INT8 | LONG VARBINARY | LONG VARCHAR | LONG | MIDDLEINT
     ;
 
 dataTypeLength
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DDLStatement.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DDLStatement.g4
index 34b0a00..0140f82 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DDLStatement.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DDLStatement.g4
@@ -334,7 +334,8 @@ generatedOption
     ;
 
 dataTypeGenericOption
-    : primaryKey | UNIQUE KEY? | NOT? NULL | collateClause_ | checkConstraintDefinition | referenceDefinition | COMMENT STRING_
+    : primaryKey | UNIQUE KEY? | NOT? NULL | collateClause_ | checkConstraintDefinition | referenceDefinition 
+    | COMMENT STRING_ | ON UPDATE CURRENT_TIMESTAMP (LP_ NUMBER_ RP_)*
     ;
 
 checkConstraintDefinition
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/ddl/create-table.xml b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/ddl/create-table.xml
index e66ed8b..4ddf4ca 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/ddl/create-table.xml
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/ddl/create-table.xml
@@ -1401,4 +1401,40 @@
             <column name="order_id" />
         </column-definition>
     </create-table>
+
+    <create-table sql-case-id="create_table_with_on_update_current_timestamp">
+        <table name="t_order" start-index="13" stop-index="19" />
+        <column-definition type="INT" primary-key="true" start-index="22" stop-index="45">
+            <column name="order_id" />
+        </column-definition>
+        <column-definition type="DATETIME" start-index="48" stop-index="93">
+            <column name="create_time" />
+        </column-definition>
+        <column-definition type="DATETIME" start-index="96" stop-index="178">
+            <column name="modify_time" />
+        </column-definition>
+    </create-table>
+
+    <create-table sql-case-id="create_table_with_on_update_current_timestamp_and_fsp">
+        <table name="t_order" start-index="13" stop-index="19" />
+        <column-definition type="INT" primary-key="true" start-index="22" stop-index="45">
+            <column name="order_id" />
+        </column-definition>
+        <column-definition type="DATETIME" start-index="48" stop-index="93">
+            <column name="create_time" />
+        </column-definition>
+        <column-definition type="DATETIME" start-index="96" stop-index="187">
+            <column name="modify_time" />
+        </column-definition>
+    </create-table>
+
+    <create-table sql-case-id="create_table_with_on_other_vendor_data_type">
+        <table name="t_order" start-index="13" stop-index="19" />
+        <column-definition type="INT" primary-key="true" start-index="22" stop-index="45">
+            <column name="order_id" />
+        </column-definition>
+        <column-definition type="MIDDLEINT" start-index="48" stop-index="64">
+            <column name="num" />
+        </column-definition>
+    </create-table>
 </sql-parser-test-cases>
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/ddl/create.xml b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/ddl/create.xml
index 887e23a..e598aca 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/ddl/create.xml
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/ddl/create.xml
@@ -200,4 +200,7 @@
         VALUES (1)
         UNION ALL
         SELECT n+1 FROM nums_1_100 WHERE n = 100" db-types="PostgreSQL" />
+    <sql-case id="create_table_with_on_update_current_timestamp" value="CREATE TABLE t_order (order_id INT PRIMARY KEY, create_time DATETIME DEFAULT CURRENT_TIMESTAMP, modify_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)" db-types="MySQL" />
+    <sql-case id="create_table_with_on_update_current_timestamp_and_fsp" value="CREATE TABLE t_order (order_id INT PRIMARY KEY, create_time DATETIME DEFAULT CURRENT_TIMESTAMP, modify_time DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6))" db-types="MySQL" />
+    <sql-case id="create_table_with_on_other_vendor_data_type" value="CREATE TABLE t_order (order_id INT PRIMARY KEY, num MIDDLEINT(10))" db-types="MySQL" />
 </sql-cases>