This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 a580699 fix sql parse cast function exception (#14088)
a580699 is described below
commit a5806998e7b9689a450c7f7405f7d293c13a1291
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Wed Dec 15 15:44:13 2021 +0800
fix sql parse cast function exception (#14088)
---
.../src/main/antlr4/imports/mysql/BaseRule.g4 | 4 +-
.../src/main/resources/case/dml/select.xml | 84 ++++++++++++++++++++++
.../main/resources/sql/supported/dml/select.xml | 4 ++
3 files changed, 90 insertions(+), 2 deletions(-)
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 085ef2d..96b89fe 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
@@ -1110,7 +1110,7 @@ dataType
| dataTypeName = (BOOL | BOOLEAN)
| dataTypeName = CHAR fieldLength? charsetWithOptBinary?
| (dataTypeName = NCHAR | dataTypeName = NATIONAL CHAR) fieldLength?
BINARY?
- | dataTypeName = SIGNED
+ | dataTypeName = SIGNED (INTEGER | INT)?
| dataTypeName = BINARY fieldLength?
| (dataTypeName = CHAR VARYING | dataTypeName = VARCHAR) fieldLength
charsetWithOptBinary?
| (dataTypeName = NATIONAL VARCHAR | dataTypeName = NVARCHAR |
dataTypeName = NCHAR VARCHAR | dataTypeName = NATIONAL CHAR VARYING |
dataTypeName = NCHAR VARYING) fieldLength BINARY?
@@ -1118,7 +1118,7 @@ dataType
| dataTypeName = YEAR fieldLength? fieldOptions?
| dataTypeName = DATE
| dataTypeName = TIME typeDatetimePrecision?
- | dataTypeName = UNSIGNED
+ | dataTypeName = UNSIGNED (INTEGER | INT)?
| dataTypeName = TIMESTAMP typeDatetimePrecision?
| dataTypeName = DATETIME typeDatetimePrecision?
| dataTypeName = TINYBLOB
diff --git
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select.xml
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select.xml
index 4d7296f..5be9120 100644
---
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select.xml
+++
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select.xml
@@ -3226,6 +3226,90 @@
</projections>
</select>
+ <select sql-case-id="select_with_cast_as_signed_int">
+ <from>
+ <simple-table name="t_order" start-index="49" stop-index="55" />
+ </from>
+ <projections start-index="7" stop-index="42">
+ <column-projection name="user_id" start-index="7" stop-index="13"/>
+ <expression-projection text="CAST(order_id AS SIGNED INT)"
start-index="15" stop-index="42">
+ <expr>
+ <function function-name="CAST" start-index="15"
stop-index="42" text="CAST(order_id AS SIGNED INT)" >
+ <parameter>
+ <column name="order_id" start-index="20"
stop-index="27"/>
+ </parameter>
+ <parameter>
+ <data-type value="SIGNED" start-index="32"
stop-index="41"/>
+ </parameter>
+ </function>
+ </expr>
+ </expression-projection>
+ </projections>
+ </select>
+
+ <select sql-case-id="select_with_cast_as_unsigned_int">
+ <from>
+ <simple-table name="t_order" start-index="51" stop-index="57"/>
+ </from>
+ <projections start-index="7" stop-index="44">
+ <expression-projection text="CAST(order_id AS UNSIGNED INT)"
start-index="7" stop-index="36">
+ <expr>
+ <function function-name="CAST" start-index="7"
stop-index="36" text="CAST(order_id AS UNSIGNED INT)" >
+ <parameter>
+ <column name="order_id" start-index="12"
stop-index="19"/>
+ </parameter>
+ <parameter>
+ <data-type value="UNSIGNED" start-index="24"
stop-index="35"/>
+ </parameter>
+ </function>
+ </expr>
+ </expression-projection>
+ <column-projection name="user_id" start-index="38"
stop-index="44"/>
+ </projections>
+ </select>
+
+ <select sql-case-id="select_with_cast_as_signed_integer">
+ <from>
+ <simple-table name="t_order" start-index="53" stop-index="59" />
+ </from>
+ <projections start-index="7" stop-index="46">
+ <column-projection name="user_id" start-index="7" stop-index="13"/>
+ <expression-projection text="CAST(order_id AS SIGNED INTEGER)"
start-index="15" stop-index="46">
+ <expr>
+ <function function-name="CAST" start-index="15"
stop-index="46" text="CAST(order_id AS SIGNED INTEGER)" >
+ <parameter>
+ <column name="order_id" start-index="20"
stop-index="27"/>
+ </parameter>
+ <parameter>
+ <data-type value="SIGNED" start-index="32"
stop-index="45"/>
+ </parameter>
+ </function>
+ </expr>
+ </expression-projection>
+ </projections>
+ </select>
+
+ <select sql-case-id="select_with_cast_as_unsigned_integer">
+ <from>
+ <simple-table name="t_order" start-index="55" stop-index="61"/>
+ </from>
+ <projections start-index="7" stop-index="48">
+ <expression-projection text="CAST(order_id AS UNSIGNED INTEGER)"
start-index="7" stop-index="40">
+ <expr>
+ <function function-name="CAST" start-index="7"
stop-index="40" text="CAST(order_id AS UNSIGNED INTEGER)" >
+ <parameter>
+ <column name="order_id" start-index="12"
stop-index="19"/>
+ </parameter>
+ <parameter>
+ <data-type value="UNSIGNED" start-index="24"
stop-index="39"/>
+ </parameter>
+ </function>
+ </expr>
+ </expression-projection>
+ <column-projection name="user_id" start-index="42"
stop-index="48"/>
+ </projections>
+ </select>
+
<select sql-case-id="select_with_simple_table" >
<projections start-index="7" stop-index="7">
<shorthand-projection start-index="7" stop-index="7" />
diff --git
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select.xml
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select.xml
index 5b7c9f2..e9877ec 100644
---
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select.xml
+++
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select.xml
@@ -85,6 +85,10 @@
<sql-case id="select_from_dual" value="SELECT 1 FROM DUAL"
db-types="MySQL" />
<sql-case id="select_with_cast_as_signed" value="SELECT
user_id,CAST(order_id AS SIGNED) FROM t_order" db-types="MySQL"/>
<sql-case id="select_with_cast_as_unsigned" value="SELECT CAST(order_id AS
UNSIGNED),user_id FROM t_order" db-types="MySQL"/>
+ <sql-case id="select_with_cast_as_signed_int" value="SELECT
user_id,CAST(order_id AS SIGNED INT) FROM t_order" db-types="MySQL"/>
+ <sql-case id="select_with_cast_as_unsigned_int" value="SELECT
CAST(order_id AS UNSIGNED INT),user_id FROM t_order" db-types="MySQL"/>
+ <sql-case id="select_with_cast_as_signed_integer" value="SELECT
user_id,CAST(order_id AS SIGNED INTEGER) FROM t_order" db-types="MySQL"/>
+ <sql-case id="select_with_cast_as_unsigned_integer" value="SELECT
CAST(order_id AS UNSIGNED INTEGER),user_id FROM t_order" db-types="MySQL"/>
<sql-case id="select_with_simple_table" value="SELECT * FROM employees
WHERE department_id = 30 ORDER BY last_name" db-types="MySQL, Oracle" />
<sql-case id="select_with_binding_tables_with_subquery_without_join"
value="SELECT a.department_id "Department", a.num_emp/b.total_count
"%_Employees", a.sal_sum/b.total_sal "%_Salary"
FROM (SELECT department_id, COUNT(*) num_emp, SUM(salary) sal_sum FROM
employees GROUP BY department_id) a, (SELECT COUNT(*) total_count, SUM(salary)
total_sal FROM employees) b ORDER BY a.department_id" db-types="Oracle" />