Skip to content

Commit

Permalink
Support parsing SQL Server INSERT INTO sql #29192 (#29785)
Browse files Browse the repository at this point in the history
* Support parsing SQL Server SELECT JSON_OBJECT('name':'value', sql #29160

* Support parsing SQL Server INSERT INTO sql #29192

* fix code style
  • Loading branch information
yydeng626 authored Jan 20, 2024
1 parent 4be4789 commit ece7d12
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ collationName
;

alias
: identifier | STRING_
: identifier | STRING_ | NCHAR_TEXT
;

dataTypeLength
Expand Down Expand Up @@ -286,7 +286,7 @@ simpleExpr
| columnName
| variableName
| simpleExpr OR_ simpleExpr
| (PLUS_ | MINUS_ | TILDE_ | NOT_ | BINARY) simpleExpr
| (PLUS_ | MINUS_ | TILDE_ | NOT_ | BINARY | DOLLAR_) simpleExpr
| ROW? LP_ expr (COMMA_ expr)* RP_
| EXISTS? subquery
| LBE_ identifier expr RBE_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,9 @@ public ASTNode visitAlias(final AliasContext ctx) {
if (null != ctx.identifier()) {
return new AliasSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), (IdentifierValue) visit(ctx.identifier()));
}
if (null != ctx.NCHAR_TEXT()) {
return new AliasSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), new IdentifierValue(ctx.NCHAR_TEXT().getText().substring(1)));
}
return new AliasSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), new IdentifierValue(ctx.STRING_().getText()));
}

Expand Down
64 changes: 64 additions & 0 deletions test/it/parser/src/main/resources/case/dml/insert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3219,4 +3219,68 @@
</from>
</select>
</insert>

<insert sql-case-id="insert_sales_with_nchar">
<table name="sales" start-index="12" stop-index="16" />
<columns start-index="17" stop-index="17" />
<values>
<value>
<assignment-value>
<literal-expression value="Canada" start-index="26" stop-index="34" />
</assignment-value>
<assignment-value>
<literal-expression value="British Columbia" start-index="37" stop-index="55" />
</assignment-value>
<assignment-value>
<literal-expression value="300" start-index="58" stop-index="60" />
</assignment-value>
</value>
</values>
</insert>

<insert sql-case-id="insert_sales_with_nchar_2">
<table name="sales" start-index="12" stop-index="16" />
<columns start-index="17" stop-index="17" />
<values>
<value>
<assignment-value>
<literal-expression value="United States" start-index="26" stop-index="41" />
</assignment-value>
<assignment-value>
<literal-expression value="Montana" start-index="44" stop-index="53" />
</assignment-value>
<assignment-value>
<literal-expression value="100" start-index="56" stop-index="58" />
</assignment-value>
</value>
</values>
</insert>

<insert sql-case-id="insert_with_currency_value">
<table name="Employees" start-index="12" stop-index="27" start-delimiter="[" end-delimiter="]">
<owner name="HR" start-index="12" stop-index="15" start-delimiter="[" end-delimiter="]" />
</table>
<columns start-index="28" stop-index="69">
<column name="SSN" start-index="29" stop-index="33" start-delimiter="[" end-delimiter="]" />
<column name="FirstName" start-index="36" stop-index="46" start-delimiter="[" end-delimiter="]" />
<column name="LastName" start-index="49" stop-index="58" start-delimiter="[" end-delimiter="]" />
<column name="Salary" start-index="61" stop-index="68" start-delimiter="[" end-delimiter="]" />
</columns>
<values>
<value>
<assignment-value>
<literal-expression value="795-73-9838" start-index="79" stop-index="91" />
</assignment-value>
<assignment-value>
<literal-expression value="Catherine" start-index="94" stop-index="105" />
</assignment-value>
<assignment-value>
<literal-expression value="Abel" start-index="108" stop-index="114" />
</assignment-value>
<assignment-value>
<common-expression text="$31692" start-index="117" stop-index="122" />
</assignment-value>
</value>
</values>
</insert>
</sql-parser-test-cases>
86 changes: 86 additions & 0 deletions test/it/parser/src/main/resources/case/dml/select-group-by.xml
Original file line number Diff line number Diff line change
Expand Up @@ -824,4 +824,90 @@
</column-item>
</order-by>
</select>

<select sql-case-id="select_with_datepart_group_by_with_order_by">
<projections start-index="7" stop-index="81">
<expression-projection text="DATEPART(yyyy,OrderDate)" alias="Year" start-index="7" stop-index="41">
<expr>
<function function-name="DATEPART" text="DATEPART(yyyy,OrderDate)" start-index="7" stop-index="30">
<parameter>
<column name="yyyy" start-index="16" stop-index="19" />
</parameter>
<parameter>
<column name="OrderDate" start-index="21" stop-index="29" />
</parameter>
</function>
</expr>
</expression-projection>
<aggregation-projection type="SUM" expression="SUM(TotalDue)" alias="Total Order Amount" start-index="44" stop-index="56">
<paramters>
<column name="TotalDue" start-index="48" stop-index="55" />
</paramters>
</aggregation-projection>
</projections>
<from>
<simple-table name="SalesOrderHeader" start-index="88" stop-index="109">
<owner name="Sales" start-index="88" stop-index="92"/>
</simple-table>
</from>
<group-by>
<expression-item expression="DATEPART(yyyy,OrderDate)" start-index="120" stop-index="143" />
</group-by>
<order-by>
<expression-item expression="DATEPART(yyyy,OrderDate)" order-direction="ASC" start-index="154" stop-index="177" />
</order-by>
</select>

<select sql-case-id="select_with_datepart_group_by_with_having_order_by">
<projections start-index="7" stop-index="81">
<expression-projection text="DATEPART(yyyy,OrderDate)" alias="Year" start-index="7" stop-index="41">
<expr>
<function function-name="DATEPART" text="DATEPART(yyyy,OrderDate)" start-index="7" stop-index="30">
<parameter>
<column name="yyyy" start-index="16" stop-index="19" />
</parameter>
<parameter>
<column name="OrderDate" start-index="21" stop-index="29" />
</parameter>
</function>
</expr>
</expression-projection>
<aggregation-projection type="SUM" expression="SUM(TotalDue)" alias="Total Order Amount" start-index="44" stop-index="56">
<paramters>
<column name="TotalDue" start-index="48" stop-index="55" />
</paramters>
</aggregation-projection>
</projections>
<from>
<simple-table name="SalesOrderHeader" start-index="88" stop-index="109">
<owner name="Sales" start-index="88" stop-index="92"/>
</simple-table>
</from>
<group-by>
<expression-item expression="DATEPART(yyyy,OrderDate)" start-index="120" stop-index="143" />
</group-by>
<having start-index="145" stop-index="186">
<expr>
<binary-operation-expression text="DATEPART(yyyy,OrderDate) &gt;= N'2003'" start-index="152" stop-index="186">
<left>
<function function-name="DATEPART" text="DATEPART(yyyy,OrderDate)" start-index="152" stop-index="175">
<parameter>
<column name="yyyy" start-index="161" stop-index="164" />
</parameter>
<parameter>
<column name="OrderDate" start-index="166" stop-index="174" />
</parameter>
</function>
</left>
<right>
<literal-expression value="2003" start-index="180" stop-index="186" />
</right>
<operator>&gt;=</operator>
</binary-operation-expression>
</expr>
</having>
<order-by>
<expression-item expression="DATEPART(yyyy,OrderDate)" order-direction="ASC" start-index="197" stop-index="220" />
</order-by>
</select>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,7 @@
<sql-case id="insert_with_exec_mtcars" value="INSERT INTO dbo.MTCars EXEC sp_execute_external_script @language = N'R', @script = N'MTCars &lt;- mtcars' , @input_data_1 = N'', @output_data_1_name = N'MTCars'" db-types="SQLServer"/>
<sql-case id="insert_with_exec_model" value="INSERT INTO GLM_models(model) EXEC generate_GLM" db-types="SQLServer"/>
<sql-case id="insert_with_table_hint" value="INSERT INTO cci_target WITH (TABLOCK) SELECT TOP 300000 * FROM staging" db-types="SQLServer"/>
<sql-case id="insert_sales_with_nchar" value="INSERT INTO sales VALUES (N'Canada', N'British Columbia', 300)" db-types="SQLServer"/>
<sql-case id="insert_sales_with_nchar_2" value="INSERT INTO sales VALUES (N'United States', N'Montana', 100)" db-types="SQLServer"/>
<sql-case id="insert_with_currency_value" value="INSERT INTO [HR].[Employees]([SSN], [FirstName], [LastName], [Salary]) VALUES ('795-73-9838', N'Catherine', N'Abel', $31692)" db-types="SQLServer"/>
</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@
<sql-case id="select_group_by_with_having_with_subquery" value="SELECT department_id, manager_id FROM employees GROUP BY department_id, manager_id HAVING (department_id, manager_id) IN (SELECT department_id, manager_id FROM employees x WHERE x.department_id = employees.department_id) ORDER BY department_id" db-types="Oracle" />
<sql-case id="select_with_case_when_then_in_group_by_item_and_order_by_item" value="SELECT order_id FROM t_order GROUP BY CASE WHEN order_id > 0 AND order_id &lt;= 10 THEN '(0,10]' WHEN order_id > 10 THEN '(10,+∞)' ELSE '' END ORDER BY CASE WHEN order_id > 0 AND order_id &lt;= 10 THEN '(0,10]' WHEN order_id > 10 THEN '(10,+∞)' ELSE '' END" db-types="MySQL,PostgreSQL,openGauss,SQLServer,Oracle" />
<sql-case id="select_with_event_group_by_with_having_order_by" value="SELECT c.name, Count(*) AS [Count-Per-Column-Repeated-Name] FROM sys.syscolumns AS c JOIN sys.sysobjects AS o ON o.id = c.id WHERE o.type = 'V' AND c.name like '%event%' GROUP BY c.name HAVING Count(*) >= 3 ORDER BY c.name" db-types="SQLServer" />
<sql-case id="select_with_datepart_group_by_with_order_by" value="SELECT DATEPART(yyyy,OrderDate) AS N'Year', SUM(TotalDue) AS N'Total Order Amount' FROM Sales.SalesOrderHeader GROUP BY DATEPART(yyyy,OrderDate) ORDER BY DATEPART(yyyy,OrderDate)" db-types="SQLServer"/>
<sql-case id="select_with_datepart_group_by_with_having_order_by" value="SELECT DATEPART(yyyy,OrderDate) AS N'Year', SUM(TotalDue) AS N'Total Order Amount' FROM Sales.SalesOrderHeader GROUP BY DATEPART(yyyy,OrderDate) HAVING DATEPART(yyyy,OrderDate) &gt;= N'2003' ORDER BY DATEPART(yyyy,OrderDate)" db-types="SQLServer"/>
</sql-cases>

0 comments on commit ece7d12

Please sign in to comment.