Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parsing SQL Server SELECT d.name, sql #29181 #30154

Merged
merged 6 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -40,7 +40,7 @@ insertExecClause
;

withTableHint
: WITH LP_ (tableHintLimited+) RP_
: WITH? LP_ (tableHintLimited+) RP_
;

exec
Expand Down Expand Up @@ -98,7 +98,7 @@ aggregationClause
;

selectClause
: selectWithClause? SELECT duplicateSpecification? projections intoClause? fromClause? whereClause? groupByClause? havingClause? orderByClause? forClause?
: selectWithClause? SELECT duplicateSpecification? projections intoClause? (fromClause withTempTable? withTableHint?)? whereClause? groupByClause? havingClause? orderByClause? forClause?
;

duplicateSpecification
Expand Down 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 Expand Up @@ -178,6 +178,10 @@ subquery
: LP_ aggregationClause RP_
;

withTempTable
: WITH LP_ (columnName dataType) (COMMA_ columnName dataType)* RP_
;

withClause
: WITH cteClauseSet
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,9 @@ public ASTNode visitSelectClause(final SelectClauseContext ctx) {
TableSegment tableSource = (TableSegment) visit(ctx.fromClause().tableReferences());
result.setFrom(tableSource);
}
if (null != ctx.withTableHint()) {
result.setWithTableHintSegment((WithTableHintSegment) visit(ctx.withTableHint()));
}
if (null != ctx.whereClause()) {
result.setWhere((WhereSegment) visit(ctx.whereClause()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WithSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.segment.sqlserver.hint.WithTableHintSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.SQLServerStatement;

import java.util.Optional;
Expand All @@ -38,6 +39,8 @@ public final class SQLServerSelectStatement extends SelectStatement implements S

private TableSegment intoSegment;

private WithTableHintSegment withTableHintSegment;

/**
* Get order by segment.
*
Expand All @@ -64,4 +67,13 @@ public Optional<WithSegment> getWithSegment() {
public Optional<TableSegment> getIntoSegment() {
return Optional.ofNullable(intoSegment);
}

/**
* Get with table hint segment.
*
* @return with table hint segment.
*/
public Optional<WithTableHintSegment> getWithTableHintSegment() {
return Optional.ofNullable(withTableHintSegment);
}
}
59 changes: 59 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 @@ -3711,4 +3711,63 @@
</from>
</select>
</insert>

<insert sql-case-id="insert_with_select_with_table_hint_no_with_keyword">
<table name="#tmpdbs" start-index="12" stop-index="18" />
<columns start-index="20" stop-index="47">
<column name="dbid" start-index="21" stop-index="26" start-delimiter="[" end-delimiter="]" />
<column name="dbname" start-index="29" stop-index="36" start-delimiter="[" end-delimiter="]" />
<column name="isdone" start-index="39" stop-index="46" start-delimiter="[" end-delimiter="]" />
</columns>
<select>
<projections start-index="56" stop-index="75">
<column-projection name="database_id" start-index="56" stop-index="66" />
<column-projection name="name" start-index="69" stop-index="72" />
<expression-projection text="0" start-index="75" stop-index="75">
<expr>
<literal-expression value="0" start-index="75" stop-index="75" />
</expr>
</expression-projection>
</projections>
<from>
<simple-table name="databases" start-index="82" stop-index="101">
<owner name="sys" start-index="89" stop-index="91">
<owner name="master" start-index="82" stop-index="87" />
</owner>
</simple-table>
</from>
<table-hints start-index="103" stop-index="110">
<table-hint value="NOLOCK" start-index="104" stop-index="109" />
</table-hints>
<where start-index="112" stop-index="147">
<expr>
<binary-operation-expression start-index="118" stop-index="147">
<left>
<binary-operation-expression start-index="118" stop-index="133">
<left>
<column name="is_read_only" start-index="118" stop-index="129" />
</left>
<right>
<literal-expression value="0" start-index="133" stop-index="133" />
</right>
<operator>=</operator>
</binary-operation-expression>
</left>
<right>
<binary-operation-expression start-index="139" stop-index="147">
<left>
<column name="state" start-index="139" stop-index="143" />
</left>
<right>
<literal-expression value="0" start-index="147" stop-index="147" />
</right>
<operator>=</operator>
</binary-operation-expression>
</right>
<operator>AND</operator>
</binary-operation-expression>
</expr>
</where>
</select>
</insert>
</sql-parser-test-cases>
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 @@ -8577,4 +8577,31 @@
</column-item>
</order-by>
</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 @@ -141,4 +141,5 @@
<sql-case id="insert_with_currency_value_2" value="INSERT INTO [HR].[Employees]([SSN], [FirstName], [LastName], [Salary]) VALUES ('990-00-6818', N'Kim', N'Abercrombie', $55415)" db-types="SQLServer"/>
<sql-case id="insert_with_cross_database_values" value="INSERT [SourceDatabase].[dbo].[SourceTable] VALUES (1, N'Bob'),(2, N'Susan')" db-types="SQLServer"/>
<sql-case id="insert_with_cross_database_select" value="INSERT [DestinationDatabase].[dbo].[DestTable_InMem] SELECT * FROM [SourceDatabase].[dbo].[SourceTable]" db-types="SQLServer"/>
<sql-case id="insert_with_select_with_table_hint_no_with_keyword" value="INSERT INTO #tmpdbs ([dbid], [dbname], [isdone]) SELECT database_id, name, 0 FROM master.sys.databases (NOLOCK) WHERE is_read_only = 0 AND state = 0" db-types="SQLServer"/>
</sql-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 @@ -261,4 +261,5 @@
<sql-case id="select_dm_exec_requests" value="SELECT [req].[session_id],[req].[start_time],[req].[cpu_time] AS [cpu_time_ms],OBJECT_NAME([ST].[objectid], [ST].[dbid]) AS [ObjectName],SUBSTRING(REPLACE(REPLACE(SUBSTRING([ST].[text],
([req].[statement_start_offset] / 2) + 1, ((CASE [req].[statement_end_offset] WHEN -1 THEN DATALENGTH([ST].[text]) ELSE [req].[statement_end_offset] END - [req].[statement_start_offset]) / 2) + 1 ), CHAR(10), ' ' ), CHAR(13), ' ' ), 1, 512 )
AS [statement_text] FROM [sys].[dm_exec_requests] AS [req] CROSS APPLY [sys].dm_exec_sql_text([req].[sql_handle]) AS [ST] ORDER BY [req].[cpu_time] DESC" 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>
Loading