Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -470,6 +470,7 @@ queryTableExpr
: queryTableExprSampleClause
| queryName
| lateralClause
| lateralClause (AS? alias)?
| tableCollectionExpr
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,9 @@ public ASTNode visitQueryTableExpr(final QueryTableExprContext ctx) {
SubquerySegment subquerySegment = new SubquerySegment(ctx.lateralClause().selectSubquery().start.getStartIndex(), ctx.lateralClause().selectSubquery().stop.getStopIndex(), subquery,
getOriginalText(ctx.lateralClause().selectSubquery()));
result = new SubqueryTableSegment(ctx.lateralClause().LP_().getSymbol().getStartIndex(), ctx.lateralClause().RP_().getSymbol().getStopIndex(), subquerySegment);
if (null != ctx.alias()) {
result.setAlias((AliasSegment) visit(ctx.alias()));
}
} else {
if (null != ctx.tableCollectionExpr().collectionExpr().selectSubquery()) {
SubquerySegment subquerySegment = (SubquerySegment) visit(ctx.tableCollectionExpr());
Expand Down
48 changes: 48 additions & 0 deletions test/it/parser/src/main/resources/case/dml/select-sub-query.xml
Original file line number Diff line number Diff line change
Expand Up @@ -903,4 +903,52 @@
</expr>
</where>
</select>

<select sql-case-id="select_sub_query_with_alias">
<projections start-index="7" stop-index="9">
<shorthand-projection start-index="7" stop-index="9">
<owner start-index="7" stop-index="7" name="B" />
</shorthand-projection>
</projections>
<from>
<subquery-table alias="B" start-index="16" stop-index="93">
<subquery>
<select>
<projections start-index="24" stop-index="26">
<shorthand-projection start-index="24" stop-index="26">
<owner start-index="24" stop-index="24" name="A" />
</shorthand-projection>
</projections>
<from>
<subquery-table alias="A" start-index="33" stop-index="87">
<subquery>
<select>
<projections start-index="41" stop-index="41">
<shorthand-projection start-index="41" stop-index="41" />
</projections>
<from>
<simple-table start-index="48" stop-index="62" name="ACT_DEPLOY_FORM" />
</from>
<where start-index="64" stop-index="81">
<expr>
<binary-operation-expression start-index="70" stop-index="81">
<left>
<column start-index="70" stop-index="77" name="FORM_KEY" />
</left>
<operator>=</operator>
<right>
<literal-expression value="1" start-index="81" stop-index="81" />
</right>
</binary-operation-expression>
</expr>
</where>
</select>
</subquery>
</subquery-table>
</from>
</select>
</subquery>
</subquery-table>
</from>
</select>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@
<sql-case id="select_sub_query_with_inner_join" value="SELECT [T1_1].[BusinessEntityID] AS [BusinessEntityID], [T1_1].[AddressID] AS [AddressID] FROM (SELECT [T2_2].[BusinessEntityID] AS [BusinessEntityID], [T2_1].[AddressID] AS [AddressID] FROM [AdventureWorks2022].[Person].[BusinessEntityAddress] AS T2_1 INNER JOIN [AdventureWorks2022].[Person].[BusinessEntity] AS T2_2 ON ([T2_1].[BusinessEntityID] = [T2_2].[BusinessEntityID])) AS T1_1" db-types="SQLServer"/>
<sql-case id="select_sub_query_with_sum" value="SELECT [T1_1].[col] AS [col] FROM (SELECT SUM([T2_1].[Quantity]) AS [col] FROM [AdventureWorks2022].[Production].[ProductInventory] AS T2_1) AS T1_1" db-types="SQLServer"/>
<sql-case id="select_sub_query_with_rownumber" value="SELECT * FROM (SELECT col1, ROW_NUMBER () OVER (PARTITION by col1 ORDER BY col1) rn FROM tab1 WHERE col1='XYZ') WHERE rn = 1" db-types="SQLServer"/>
<sql-case id="select_sub_query_with_alias" value="SELECT B.* FROM (SELECT A.* FROM (SELECT * FROM ACT_DEPLOY_FORM WHERE FORM_KEY = 1) AS A) AS B" db-types="Oracle"/>
</sql-cases>