Skip to content

Commit

Permalink
support parser SQL Server SUBSTRING function (apache#29685)
Browse files Browse the repository at this point in the history
* Support parsing SQL Server SUBSTRING function

* fix convert function parser error
  • Loading branch information
TherChenYang authored and xiaomizhou2 committed Jan 11, 2024
1 parent 5334b48 commit 510ef24
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ unreservedWord
| DATA_RETENTION | TEMPORAL_HISTORY_RETENTION | EDITION | MIXED_PAGE_ALLOCATION | DISABLED | ALLOWED | HADR | MULTI_USER | RESTRICTED_USER | SINGLE_USER | OFFLINE | EMERGENCY | SUSPEND | DATE_CORRELATION_OPTIMIZATION
| ELASTIC_POOL | SERVICE_OBJECTIVE | DATABASE_NAME | ALLOW_CONNECTIONS | GEO | NAMED | DATEFIRST | BACKUP_STORAGE_REDUNDANCY | FORCE_FAILOVER_ALLOW_DATA_LOSS | SECONDARY | FAILOVER | DEFAULT_FULLTEXT_LANGUAGE
| DEFAULT_LANGUAGE | INLINE | NESTED_TRIGGERS | TRANSFORM_NOISE_WORDS | TWO_DIGIT_YEAR_CUTOFF | PERSISTENT_LOG_BUFFER | DIRECTORY_NAME | DATEFORMAT | DELAYED_DURABILITY | TRANSFER | SCHEMA | PASSWORD | AUTHORIZATION
| MEMBER | SEARCH | TEXT | SECOND | PRECISION | VIEWS | PROVIDER | COLUMNS
| MEMBER | SEARCH | TEXT | SECOND | PRECISION | VIEWS | PROVIDER | COLUMNS | SUBSTRING | RETURNS
;

databaseName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ public ASTNode visitConvertFunction(final ConvertFunctionContext ctx) {
result.getParameters().add((DataTypeSegment) visit(ctx.dataType()));
result.getParameters().add((ExpressionSegment) visit(ctx.expr()));
if (null != ctx.NUMBER_()) {
result.getParameters().add(new LiteralExpressionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.NUMBER_().getText()));
result.getParameters().add(new LiteralExpressionSegment(ctx.NUMBER_().getSymbol().getStartIndex(), ctx.NUMBER_().getSymbol().getStopIndex(), ctx.NUMBER_().getText()));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public class LiteralExpressionSegment implements SimpleExpressionSegment {

@Override
public String getText() {
return literals.toString();
return null != literals ? literals.toString() : null;
}
}
12 changes: 12 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 @@ -3061,4 +3061,16 @@
</parameter>
</exec>
</insert>

<insert sql-case-id="insert_into_temp_table">
<table name="#NonExistentTable" start-index="12" stop-index="28"/>
<columns start-index="29" stop-index="29"/>
<values>
<value>
<assignment-value>
<literal-expression value="10" start-index="38" stop-index="39"/>
</assignment-value>
</value>
</values>
</insert>
</sql-parser-test-cases>
285 changes: 285 additions & 0 deletions test/it/parser/src/main/resources/case/dml/select-join.xml

Large diffs are not rendered by default.

52 changes: 44 additions & 8 deletions test/it/parser/src/main/resources/case/dml/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3885,7 +3885,7 @@
<function function-name="GETDATE" text="GETDATE()" start-index="25" stop-index="33" />
</parameter>
<parameter>
<literal-expression value="0" start-index="7" stop-index="37" />
<literal-expression value="0" start-index="36" stop-index="36" />
</parameter>
</function>
</expr>
Expand Down Expand Up @@ -3929,7 +3929,7 @@
<literal-expression value="Name" start-index="26" stop-index="31" />
</parameter>
<parameter>
<literal-expression value="0" start-index="7" stop-index="35" />
<literal-expression value="0" start-index="34" stop-index="34" />
</parameter>
</function>
</expr>
Expand Down Expand Up @@ -3966,7 +3966,7 @@
<literal-expression value="&lt;root&gt; &lt;child/&gt; &lt;/root&gt;" start-index="20" stop-index="61" />
</parameter>
<parameter>
<literal-expression value="1" start-index="7" stop-index="65" />
<literal-expression value="1" start-index="64" stop-index="64" />
</parameter>
</function>
</expr>
Expand Down Expand Up @@ -4000,7 +4000,7 @@
<function function-name="GETDATE" text="GETDATE()" start-index="109" stop-index="117" />
</parameter>
<parameter>
<literal-expression value="126" start-index="87" stop-index="123" />
<literal-expression value="126" start-index="120" stop-index="122" />
</parameter>
</function>
</expr>
Expand Down Expand Up @@ -4037,7 +4037,7 @@
<literal-expression value="2006-04-25T15:50:59.997" start-index="129" stop-index="153" />
</parameter>
<parameter>
<literal-expression value="126" start-index="111" stop-index="159" />
<literal-expression value="126" start-index="156" stop-index="158" />
</parameter>
</function>
</expr>
Expand All @@ -4057,7 +4057,7 @@
<common-expression literal-text="0x4E616d65" start-index="24" stop-index="33" />
</parameter>
<parameter>
<literal-expression value="0" start-index="7" stop-index="37" />
<literal-expression value="0" start-index="36" stop-index="36" />
</parameter>
</function>
</expr>
Expand All @@ -4077,7 +4077,7 @@
<common-expression literal-text="0x4E616d65" start-index="24" stop-index="33" />
</parameter>
<parameter>
<literal-expression value="1" start-index="7" stop-index="37" />
<literal-expression value="1" start-index="36" stop-index="36" />
</parameter>
</function>
</expr>
Expand All @@ -4097,7 +4097,7 @@
<literal-expression value="0x4E616D65" start-index="26" stop-index="37" />
</parameter>
<parameter>
<literal-expression value="1" start-index="7" stop-index="41" />
<literal-expression value="1" start-index="40" stop-index="40" />
</parameter>
</function>
</expr>
Expand Down Expand Up @@ -7990,4 +7990,40 @@
<column-projection name="modify_date" start-index="185" stop-index="195"/>
</projections>
</select>

<select sql-case-id="select_with_substring_function">
<projections start-index="7" stop-index="36">
<column-projection name="ProductID" start-index="7" stop-index="15"/>
<column-projection name="Name" start-index="18" stop-index="21"/>
<column-projection name="ProductNumber" start-index="24" stop-index="36"/>
</projections>
<from start-index="43" stop-index="64">
<simple-table name="Product" start-index="43" stop-index="64" start-delimiter="[" end-delimiter="]">
<owner name="Production" start-index="43" stop-index="54" start-delimiter="[" end-delimiter="]"/>
</simple-table>
</from>
<where start-index="66" stop-index="110">
<expr>
<binary-operation-expression start-index="72" stop-index="110">
<left>
<function function-name="SUBSTRING" text="SUBSTRING(ProductNumber, 0, 4)" start-index="72" stop-index="101">
<parameter>
<column name="ProductNumber" start-index="82" stop-index="94"/>
</parameter>
<parameter>
<literal-expression value="0" start-index="97" stop-index="97"/>
</parameter>
<parameter>
<literal-expression value="4" start-index="100" stop-index="100"/>
</parameter>
</function>
</left>
<right>
<literal-expression value="HN-" start-index="106" stop-index="110"/>
</right>
<operator>=</operator>
</binary-operation-expression>
</expr>
</where>
</select>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,5 @@
<sql-case id="insert_with_data_base_name" value="INSERT INTO AdventureWorks2022.dbo.VariableTest(Col1) VALUES('$(tablename)')" db-types="SQLServer"/>
<sql-case id="insert_with_exec" value="INSERT INTO iris_rx_data (&quot;Sepal.Length&quot;, &quot;Sepal.Width&quot;, &quot;Petal.Length&quot;, &quot;Petal.Width&quot; , &quot;Species&quot;) EXECUTE sp_execute_external_script @language = N'R' , @script = N'iris_data &lt;- iris'" db-types="SQLServer"/>
<sql-case id="insert_with_db_schema_name" value="INSERT INTO ContosoWarehouse.dbo.Affiliation SELECT * FROM My_Lakehouse.dbo.Affiliation" db-types="SQLServer"/>
<sql-case id="insert_into_temp_table" value="INSERT INTO #NonExistentTable values (10)" db-types="SQLServer"/>
</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@
<sql-case id="select_inner_join_from_sys_dm_xe_objects" value="SELECT p.name AS [Package-Name],o.object_type,o.name AS [Object-Name],o.description AS [Object-Descr],p.guid AS [Package-Guid] FROM sys.dm_xe_packages AS p INNER JOIN sys.dm_xe_objects AS o ON p.guid = o.package_guid WHERE o.object_type = 'event' AND p.name LIKE '%' AND o.name LIKE '%sql%' ORDER BY p.name, o.object_type, o.name" db-types="SQLServer"/>
<sql-case id="select_cross_apply_join_string_split" value="SELECT ProductId, Name, value FROM Product CROSS APPLY STRING_SPLIT(Tags, ',')" db-types="SQLServer"/>
<sql-case id="select_cross_apply_join_string_split_with_group_by" value="SELECT value as tag, COUNT(*) AS [number_of_articles] FROM Product CROSS APPLY STRING_SPLIT(Tags, ',') GROUP BY value HAVING COUNT(*) > 2 ORDER BY COUNT(*) DESC" db-types="SQLServer"/>
<sql-case id="select_cross_join_schema_table" value="SELECT s.ticket, s.customer, r.store FROM dbo.Sales AS s CROSS JOIN dbo.Returns AS r WHERE s.ticket = r.ticket AND s.type = 'toy' AND r.date = '2016-05-11'" db-types="SQLServer"/>
<sql-case id="select_with_multi_join_01" value="SELECT tat.transaction_begin_time, getdate() AS 'current time', es.program_name, es.login_time, es.session_id, tst.open_transaction_count, eib.event_info FROM sys.dm_tran_active_transactions tat JOIN sys.dm_tran_session_transactions tst ON tat.transaction_id=tst.transaction_id JOIN sys.dm_exec_sessions es ON tst.session_id=es.session_id CROSS APPLY sys.dm_exec_input_buffer(es.session_id, NULL) eib WHERE es.is_user_process = 1 ORDER BY tat.transaction_begin_time ASC" db-types="SQLServer"/>
<sql-case id="select_with_multi_join_02" value="SELECT text, 'DBCC FREEPROCCACHE (0x' + CONVERT(VARCHAR (512), plan_handle, 2) + ')' AS dbcc_freeproc_command FROM sys.dm_exec_cached_plans CROSS APPLY sys.dm_exec_query_plan(plan_handle) CROSS APPLY sys.dm_exec_sql_text(plan_handle) WHERE text LIKE '%person.person%'" db-types="SQLServer"/>
</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,5 @@
<sql-case id="select_with_top_with_ties" value="SELECT TOP 10 WITH TIES alias1.columnName from TableName alias1" db-types="SQLServer"/>
<sql-case id="select_with_object_id_function" value="SELECT OBJECT_NAME(object_id) AS referencing_object_name,COALESCE(COL_NAME(object_id, column_id), '(n/a)') AS referencing_column_name,* FROM sys.sql_dependencies WHERE referenced_major_id = OBJECT_ID('&lt;schema_name.table_name&gt;') ORDER BY OBJECT_NAME(object_id), COL_NAME(object_id, column_id)" db-types="SQLServer"/>
<sql-case id="select_from_sys_views" value="SELECT name AS view_name,SCHEMA_NAME(schema_id) AS schema_name,OBJECTPROPERTYEX(object_id,'IsIndexed') AS IsIndexed,OBJECTPROPERTYEX(object_id,'IsIndexable') AS IsIndexable,create_date,modify_date FROM sys.views" db-types="SQLServer"/>
<sql-case id="select_with_substring_function" value="SELECT ProductID, Name, ProductNumber FROM [Production].[Product] WHERE SUBSTRING(ProductNumber, 0, 4) = 'HN-'" db-types="SQLServer"/>
</sql-cases>

0 comments on commit 510ef24

Please sign in to comment.