Skip to content

Commit

Permalink
Support parsing SQL Server SELECT d.name, sql apache#29181
Browse files Browse the repository at this point in the history
  • Loading branch information
yydeng626 committed Feb 6, 2024
1 parent 35cd5a7 commit 844f89c
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +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 | YEAR
| TIMESTAMP | TRIM | USER | RIGHT | JSON
| TIMESTAMP | TRIM | USER | RIGHT | JSON | SID
;

databaseName
Expand Down Expand Up @@ -291,7 +291,8 @@ simpleExpr
| CURRENT OF GLOBAL? expr
| ROW? LP_ expr (COMMA_ expr)* RP_
| EXISTS? subquery
| LBE_ identifier expr RBE_
| LBT_ expr RBT_
| LBE_ expr (DOT_ expr)* (COMMA_ expr)* RBE_ (ON (COLUMNS | ROWS))?
| caseExpression
| privateExprOfDb
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ tableReference
;

tableFactor
: tableName (AS? alias)? | subquery AS? alias columnNames? | expr (AS? alias) ? | LP_ tableReferences RP_
: tableName (AS? alias)? | subquery AS? alias columnNames? | expr (AS? alias)? | LP_ tableReferences RP_
;

joinedTable
Expand Down
108 changes: 108 additions & 0 deletions test/it/parser/src/main/resources/case/dml/select-join.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2961,4 +2961,112 @@
</column-item>
</order-by>
</select>

<select sql-case-id="select_sys_databases_join_sys_logins">
<projections start-index="7" stop-index="34">
<column-projection name="name" start-index="7" stop-index="12">
<owner name="d" start-index="7" stop-index="7" />
</column-projection>
<column-projection name="owner_sid" start-index="15" stop-index="25">
<owner name="d" start-index="15" stop-index="15" />
</column-projection>
<column-projection name="name" start-index="28" stop-index="34">
<owner name="sl" start-index="28" stop-index="29" />
</column-projection>
</projections>
<from start-index="41" stop-index="108">
<join-table natural="false" join-type="INNER">
<left>
<simple-table start-index="41" stop-index="58" alias="d" name="databases">
<owner name="sys" start-index="41" stop-index="43" />
</simple-table>
</left>
<right>
<simple-table start-index="65" stop-index="84" alias="sl" name="sql_logins">
<owner name="sys" start-index="65" stop-index="67" />
</simple-table>
</right>
<on-condition>
<binary-operation-expression start-index="89" stop-index="108">
<left>
<column name="owner_sid" start-index="89" stop-index="99">
<owner name="d" stop-index="89" start-index="89" />
</column>
</left>
<right>
<column name="sid" start-index="103" stop-index="108">
<owner name="sl" start-index="103" stop-index="104" />
</column>
</right>
<operator>=</operator>
</binary-operation-expression>
</on-condition>
</join-table>
</from>
</select>

<select sql-case-id="select_distinct_with_inner_join_subquery">
<projections distinct-row="true" start-index="16" stop-index="44">
<column-projection name="FirstName" start-index="16" stop-index="29">
<owner name="user" start-index="16" stop-index="19" />
</column-projection>
<column-projection name="LastName" start-index="32" stop-index="44">
<owner name="user" start-index="32" stop-index="35" />
</column-projection>
</projections>
<into>
<simple-table name="ms_user" start-index="51" stop-index="57" />
</into>
<from start-index="64" stop-index="176">
<join-table join-type="INNER" natural="false">
<left>
<simple-table name="user" start-index="64" stop-index="67" />
</left>
<right>
<subquery-table alias="ms">
<subquery>
<select>
<projections start-index="88" stop-index="88">
<shorthand-projection start-index="88" stop-index="88" />
</projections>
<from start-index="95" stop-index="105">
<simple-table name="ClickStream" start-index="95" stop-index="105" />
</from>
<where start-index="107" stop-index="140">
<expr>
<binary-operation-expression start-index="113" stop-index="140">
<left>
<column name="url" start-index="113" stop-index="118">
<owner name="cs" start-index="113" stop-index="114" />
</column>
</left>
<right>
<literal-expression value="www.microsoft.com" start-index="122" stop-index="140" />
</right>
<operator>=</operator>
</binary-operation-expression>
</expr>
</where>
</select>
</subquery>
</subquery-table>
</right>
<on-condition>
<binary-operation-expression start-index="152" stop-index="176">
<left>
<column name="user_ip" start-index="152" stop-index="163">
<owner name="user" start-index="152" stop-index="155" />
</column>
</left>
<right>
<column name="user_ip" start-index="167" stop-index="176">
<owner name="ms" start-index="167" stop-index="168" />
</column>
</right>
<operator>=</operator>
</binary-operation-expression>
</on-condition>
</join-table>
</from>
</select>
</sql-parser-test-cases>
27 changes: 27 additions & 0 deletions test/it/parser/src/main/resources/case/dml/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8426,4 +8426,31 @@
</simple-table>
</from>
</select>

<select sql-case-id="select_mdx">
<projections start-index="7" stop-index="153">
<expression-projection start-index="7" stop-index="94" text="{[Measures].[Internet Sales Count], [Measures].[Internet Sales-Sales Amount]} ON COLUMNS">
<expr>
<common-expression text="{[Measures].[Internet Sales Count], [Measures].[Internet Sales-Sales Amount]} ON COLUMNS" start-index="7" stop-index="94" />
</expr>
</expression-projection>
<expression-projection text="{[Product].[Product Line].[Product Line].MEMBERS} ON ROWS" start-index="97" stop-index="153">
<expr>
<common-expression text="{[Product].[Product Line].[Product Line].MEMBERS} ON ROWS" start-index="97" stop-index="153" />
</expr>
</expression-projection>
</projections>
<from start-index="160" stop-index="187">
<simple-table name="Analysis Services Tutorial" start-index="160" stop-index="187" start-delimiter="[" end-delimiter="]" />
</from>
<where start-index="189" stop-index="249">
<expr>
<column name="Australia" start-index="195" stop-index="249" start-delimiter="[" end-delimiter="]">
<owner name="Sales Territory Country" start-index="213" stop-index="237" start-delimiter="[" end-delimiter="]">
<owner name="Sales Territory" start-index="195" stop-index="211" start-delimiter="[" end-delimiter="]" />
</owner>
</column>
</expr>
</where>
</select>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@
<sql-case id="select_json_value_with_cross_apply" value="SELECT name,reason,score,JSON_VALUE(details, '$.implementationDetails.script') AS script,details.* FROM sys.dm_db_tuning_recommendations CROSS APPLY OPENJSON(details, '$.planForceDetails') WITH ([query_id] INT '$.queryId',regressed_plan_id INT '$.regressedPlanId',last_good_plan_id INT '$.recommendedPlanId') AS details WHERE JSON_VALUE(STATE, '$.currentValue') = 'Active'" db-types="SQLServer"/>
<sql-case id="select_open_json_with_cross_apply" value="SELECT reason,score,script = JSON_VALUE(details, '$.implementationDetails.script'),planForceDetails.*,estimated_gain = (regressedPlanExecutionCount + recommendedPlanExecutionCount) * (regressedPlanCpuTimeAverage - recommendedPlanCpuTimeAverage) / 1000000,error_prone = IIF(regressedPlanErrorCount &gt; recommendedPlanErrorCount, 'YES', 'NO') FROM sys.dm_db_tuning_recommendations CROSS APPLY OPENJSON(Details, '$.planForceDetails') WITH ([query_id] INT '$.queryId',regressedPlanId INT '$.regressedPlanId',recommendedPlanId INT '$.recommendedPlanId',regressedPlanErrorCount INT,recommendedPlanErrorCount INT,regressedPlanExecutionCount INT,regressedPlanCpuTimeAverage FLOAT,recommendedPlanExecutionCount INT,recommendedPlanCpuTimeAverage FLOAT) AS planForceDetails" db-types="SQLServer"/>
<sql-case id="select_sales_order_record_with_cross_apply" value="SELECT Tab.Id,SalesOrderJsonData.Customer,SalesOrderJsonData.Date FROM SalesOrderRecord AS Tab CROSS APPLY OPENJSON(Tab.json, N'$.Orders.OrdersArray') WITH (Number VARCHAR(200) N'$.Order.Number',Date DATETIME N'$.Order.Date',Customer VARCHAR(200) N'$.AccountNumber',Quantity INT N'$.Item.Quantity') AS SalesOrderJsonData WHERE JSON_VALUE(Tab.json, '$.Status') = N'Closed' ORDER BY JSON_VALUE(Tab.json, '$.Group'),Tab.DateModified" db-types="SQLServer"/>
<sql-case id="select_sys_databases_join_sys_logins" value="SELECT d.name, d.owner_sid, sl.name FROM sys.databases AS d JOIN sys.sql_logins AS sl ON d.owner_sid = sl.sid" db-types="SQLServer"/>
<sql-case id="select_distinct_with_inner_join_subquery" value="SELECT DISTINCT user.FirstName, user.LastName INTO ms_user FROM user INNER JOIN (SELECT * FROM ClickStream WHERE cs.url = 'www.microsoft.com') AS ms ON user.user_ip = ms.user_ip" db-types="SQLServer"/>
</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,5 @@
<sql-case id="select_from_physical_stats_function" value="SELECT page_count, compressed_page_count, forwarded_record_count, * FROM sys.dm_db_index_physical_stats(db_id(), object_id('t3'), NULL, NULL, 'SAMPLED')" db-types="SQLServer"/>
<sql-case id="select_from_msdb_default_schema" value="SELECT backup_size/compressed_backup_size FROM msdb..backupset" db-types="SQLServer"/>
<sql-case id="select_from_database_files_2" value="SELECT file_id, name, type_desc, physical_name, size, max_size FROM sys.database_files" db-types="SQLServer"/>
<sql-case id="select_mdx" value="SELECT {[Measures].[Internet Sales Count], [Measures].[Internet Sales-Sales Amount]} ON COLUMNS, {[Product].[Product Line].[Product Line].MEMBERS} ON ROWS FROM [Analysis Services Tutorial] WHERE [Sales Territory].[Sales Territory Country].[Australia]" db-types="SQLServer"/>
</sql-cases>

0 comments on commit 844f89c

Please sign in to comment.