Skip to content

Commit

Permalink
Support parsing SQL Server SELECT s.session_id, sql #29161 (#29800)
Browse files Browse the repository at this point in the history
  • Loading branch information
yydeng626 authored Jan 29, 2024
1 parent 2f1b09b commit 47e9685
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ unreservedWord
| 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 | SUBSTRING | RETURNS | SIZE | CONTAINS | MONTH | INPUT
| TIMESTAMP | TRIM
;

databaseName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,8 @@ public final ASTNode visitCastFunction(final CastFunctionContext ctx) {
result.getParameters().add((ColumnSegment) exprSegment);
} else if (exprSegment instanceof LiteralExpressionSegment) {
result.getParameters().add((LiteralExpressionSegment) exprSegment);
} else if (exprSegment instanceof FunctionSegment) {
result.getParameters().add((FunctionSegment) exprSegment);
}
result.getParameters().add((DataTypeSegment) visit(ctx.dataType()));
return result;
Expand Down
30 changes: 30 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 @@ -3413,4 +3413,34 @@
</from>
</select>
</insert>

<insert sql-case-id="insert_into_production_location">
<table name="Location" start-index="12" stop-index="49">
<owner name="Production" start-index="31" stop-index="40">
<owner name="AdventureWorks2022" start-index="12" stop-index="29" />
</owner>
</table>
<columns start-index="50" stop-index="93">
<column name="Name" start-index="51" stop-index="54" />
<column name="CostRate" start-index="57" stop-index="64" />
<column name="Availability" start-index="67" stop-index="78" />
<column name="ModifiedDate" start-index="81" stop-index="92" />
</columns>
<values>
<value>
<assignment-value>
<function function-name="NEWID" text="NEWID()" start-index="103" stop-index="109" />
</assignment-value>
<assignment-value>
<literal-expression value="0.5" start-index="112" stop-index="113" />
</assignment-value>
<assignment-value>
<literal-expression value="5.2" start-index="116" stop-index="118" />
</assignment-value>
<assignment-value>
<function function-name="GETDATE" text="GETDATE()" start-index="121" stop-index="129" />
</assignment-value>
</value>
</values>
</insert>
</sql-parser-test-cases>
35 changes: 35 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 @@ -924,4 +924,39 @@
<column-item name="tumblingwindow" order-direction="ASC" start-index="60" stop-index="73"/>
</group-by>
</select>

<select sql-case-id="select_group_by_top_column_value">
<projections start-index="7" stop-index="34">
<top-projection start-index="7" stop-index="12">
<top-value value="10" start-index="26" stop-index="26" />
</top-projection>
<column-projection name="hash_unique_bigint_id" start-index="14" stop-index="34" />
</projections>
<from start-index="41" stop-index="55">
<simple-table name="TelemetryDS" start-index="41" stop-index="55">
<owner name="dbo" start-index="41" stop-index="43" />
</simple-table>
</from>
<where start-index="57" stop-index="103">
<expr>
<between-expression start-index="63" stop-index="103">
<left>
<column name="Timestamp" start-index="63" stop-index="71"/>
</left>
<between-expr>
<column name="@StartTime" start-index="81" stop-index="90"/>
</between-expr>
<and-expr>
<column name="@EndTime" start-index="96" stop-index="103" />
</and-expr>
</between-expression>
</expr>
</where>
<group-by start-index="57" stop-index="103">
<column-item name="hash_unique_bigint_id" order-direction="ASC" start-index="114" stop-index="134" />
</group-by>
<order-by start-index="136" stop-index="179">
<expression-item expression="MAX(max_elapsed_time_microsec)" order-direction="DESC" start-index="145" stop-index="174" />
</order-by>
</select>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -917,4 +917,74 @@
</expression-projection>
</projections>
</select>

<select sql-case-id="select_dm_exec_sessions_with_json_object_function">
<projections start-index="7" stop-index="109">
<column-projection name="session_id" start-index="7" stop-index="18">
<owner name="s" start-index="7" stop-index="7" />
</column-projection>
<expression-projection text="JSON_OBJECT('security_id':s.security_id, 'login':s.login_name, 'status':s.status)" alias="info" start-index="21" stop-index="109">
<expr>
<function function-name="JSON_OBJECT" text="JSON_OBJECT('security_id':s.security_id, 'login':s.login_name, 'status':s.status)" start-index="21" stop-index="101">
<parameter>
<key-value text="'security_id':s.security_id" start-index="33" stop-index="59">
<key>
<literal-expression value="security_id" start-index="33" stop-index="45"/>
</key>
<value>
<column name="security_id" start-index="47" stop-index="59">
<owner name="s" start-index="47" stop-index="47" />
</column>
</value>
</key-value>
</parameter>
<parameter>
<key-value text="'login':s.login_name" start-index="62" stop-index="81">
<key>
<literal-expression value="login" start-index="62" stop-index="68" />
</key>
<value>
<column name="login_name" start-index="70" stop-index="81">
<owner name="s" start-index="70" stop-index="70" />
</column>
</value>
</key-value>
</parameter>
<parameter>
<key-value text="JSON_OBJECT('security_id':s.security_id, 'login':s.login_name, 'status':s.status)" start-index="84" stop-index="100">
<key>
<literal-expression value="status" start-index="84" stop-index="91" />
</key>
<value>
<column name="status" start-index="93" stop-index="100">
<owner name="s" start-index="93" stop-index="93" />
</column>
</value>
</key-value>
</parameter>
</function>
</expr>
</expression-projection>
</projections>
<from start-index="116" stop-index="140">
<simple-table name="dm_exec_sessions" alias="s" start-index="116" stop-index="140">
<owner name="sys" start-index="116" stop-index="118" />
</simple-table>
</from>
<where start-index="142" stop-index="168">
<expr>
<binary-operation-expression start-index="148" stop-index="168" text="s.is_user_process = 1">
<left>
<column name="is_user_process" start-index="148" stop-index="164">
<owner name="s" start-index="148" stop-index="148" />
</column>
</left>
<right>
<literal-expression value="1" start-index="168" stop-index="168" />
</right>
<operator>=</operator>
</binary-operation-expression>
</expr>
</where>
</select>
</sql-parser-test-cases>
117 changes: 72 additions & 45 deletions test/it/parser/src/main/resources/case/dml/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3996,6 +3996,9 @@
<expression-projection alias="UsingCast" text="CAST(GETDATE() AS NVARCHAR(30))" start-index="41" stop-index="84">
<expr>
<function function-name="CAST" text="CAST(GETDATE() AS NVARCHAR(30))" start-index="41" stop-index="71">
<parameter>
<function function-name="GETDATE" text="GETDATE()" start-index="46" stop-index="54" />
</parameter>
<parameter>
<data-type value="NVARCHAR" start-index="59" stop-index="70" />
</parameter>
Expand Down Expand Up @@ -8093,51 +8096,7 @@
</expr>
</where>
</select>

<select sql-case-id="select_with_database_files">
<from start-index="108" stop-index="125">
<simple-table name="database_files" start-index="108" stop-index="125">
<owner name="sys" start-index="108" stop-index="110"/>
</simple-table>
</from>
<projections start-index="7" stop-index="101">
<column-projection name="name" start-index="7" stop-index="10" />
<expression-projection text="size / 128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT) / 128.0" alias="AvailableSpaceInMB" start-index="13" stop-index="101">
<expr>
<binary-operation-expression text="size / 128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT) / 128.0" start-index="13" stop-index="79">
<left>
<binary-operation-expression text="size / 128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)" start-index="13" stop-index="71">
<left>
<column name="size" start-index="13" stop-index="16" />
</left>
<operator>/</operator>
<right>
<binary-operation-expression text="128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)" start-index="20" stop-index="71">
<left>
<literal-expression value="128.0" start-index="20" stop-index="24" />
</left>
<operator>-</operator>
<right>
<function function-name="CAST" text="CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)" start-index="28" stop-index="71">
<parameter>
<data-type value="INT" start-index="68" stop-index="70"/>
</parameter>
</function>
</right>
</binary-operation-expression>
</right>
</binary-operation-expression>
</left>
<operator>/</operator>
<right>
<literal-expression value="128.0" start-index="75" stop-index="79" />
</right>
</binary-operation-expression>
</expr>
</expression-projection>
</projections>
</select>


<select sql-case-id="select_with_contains_function">
<projections start-index="7" stop-index="16">
<column-projection name="product_id" start-index="7" stop-index="16"/>
Expand Down Expand Up @@ -8174,6 +8133,74 @@
</expr>
</where>
</select>

<select sql-case-id="select_from_database_files">
<projections start-index="7" stop-index="101">
<column-projection name="name" start-index="7" stop-index="10" />
<expression-projection text="size / 128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT) / 128.0" alias="AvailableSpaceInMB" start-index="13" stop-index="101">
<expr>
<binary-operation-expression text="size / 128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT) / 128.0" start-index="13" stop-index="79">
<left>
<binary-operation-expression text="size / 128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)" start-index="13" stop-index="71">
<left>
<column name="size" start-index="13" stop-index="16" />
</left>
<right>
<binary-operation-expression text="128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)" start-index="20" stop-index="71">
<left>
<literal-expression value="128.0" start-index="20" stop-index="24" />
</left>
<right>
<function function-name="CAST" text="CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)" start-index="28" stop-index="71">
<parameter>
<function function-name="FILEPROPERTY" text="FILEPROPERTY(name, 'SpaceUsed')" start-index="33" stop-index="63">
<parameter>
<column name="name" start-index="46" stop-index="49" />
</parameter>
<parameter>
<literal-expression value="SpaceUsed" start-index="52" stop-index="62" />
</parameter>
</function>
</parameter>
<parameter>
<data-type value="INT" start-index="68" stop-index="70" />
</parameter>
</function>
</right>
<operator>-</operator>
</binary-operation-expression>
</right>
<operator>/</operator>
</binary-operation-expression>
</left>
<right>
<literal-expression value="128.0" start-index="75" stop-index="79" />
</right>
<operator>/</operator>
</binary-operation-expression>
</expr>
</expression-projection>
</projections>
<from>
<simple-table name="database_files" start-index="108" stop-index="125">
<owner name="sys" start-index="108" stop-index="110" />
</simple-table>
</from>
</select>

<select sql-case-id="select_trim_2">
<projections start-index="7" stop-index="38">
<expression-projection text="TRIM( ' test ')" alias="Result" start-index="7" stop-index="38">
<expr>
<function function-name="TRIM" text="TRIM( ' test ')" start-index="7" stop-index="28">
<parameter>
<literal-expression value=" test " start-index="13" stop-index="27" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_with_default_schema">
<projections start-index="7" stop-index="44">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,5 @@
<sql-case id="insert_with_select_from_open_row_2" value="INSERT INTO achievements with (TABLOCK) (id, description) SELECT * FROM OPENROWSET(BULK 'csv/achievements.csv', DATA_SOURCE = 'MyAzureBlobStorage', FORMAT ='CSV', FORMATFILE='csv/achievements-c.xml', FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage') AS DataFile" db-types="SQLServer"/>
<sql-case id="insert_into_table_get_date" value="INSERT INTO #Test VALUES (N'OC', N'Ounces', GETDATE())" db-types="SQLServer"/>
<sql-case id="insert_into_table_from_table" value="INSERT INTO #Test SELECT * FROM Production.UnitMeasure" db-types="SQLServer"/>
<sql-case id="insert_into_production_location" value="INSERT INTO AdventureWorks2022.Production.Location(Name, CostRate, Availability, ModifiedDate) VALUES (NEWID(), .5, 5.2, GETDATE())" db-types="SQLServer"/>
</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@
<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-case id="select_from_input_table" value="SELECT count(*) FROM input GROUP BY PartitionId, clusterid, tumblingwindow;" db-types="SQLServer"/>
<sql-case id="select_group_by_top_column_value" value="SELECT TOP 10 hash_unique_bigint_id FROM dbo.TelemetryDS WHERE Timestamp BETWEEN @StartTime AND @EndTime GROUP BY hash_unique_bigint_id ORDER BY MAX(max_elapsed_time_microsec) DESC" db-types="SQLServer" />
</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@
<sql-case id="select_json_array" value="SELECT JSON_OBJECT('name':'value', 'type':JSON_ARRAY(1, 2))" db-types="SQLServer" />
<sql-case id="select_nest_json_object" value="SELECT JSON_OBJECT('name':'value', 'type':JSON_OBJECT('type_id':1, 'name':'a'))" db-types="SQLServer" />
<sql-case id="select_json_object_with_subquery" value="SELECT JSON_OBJECT('user_name':USER_NAME(), @id_key:@id_value, 'sid':(SELECT @@SPID))" db-types="SQLServer" />
<sql-case id="select_dm_exec_sessions_with_json_object_function" value="SELECT s.session_id, JSON_OBJECT('security_id':s.security_id, 'login':s.login_name, 'status':s.status) as info FROM sys.dm_exec_sessions AS s WHERE s.is_user_process = 1" db-types="SQLServer" />
</sql-cases>
Loading

0 comments on commit 47e9685

Please sign in to comment.