Skip to content

Commit b58af20

Browse files
authored
Support Oracle pivot with alias SQL parse (#36761)
* Support Oracle pivot with alias SQL parse * Support Oracle pivot with alias SQL parse
1 parent 885db3f commit b58af20

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

parser/sql/engine/dialect/oracle/src/main/antlr4/imports/oracle/DMLStatement.g4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ modifyExternalTableProperties
517517

518518
pivotClause
519519
: PIVOT XML?
520-
LP_ aggregationFunction (AS? alias)? (COMMA_ aggregationFunction (AS? alias)?)* pivotForClause pivotInClause RP_
520+
LP_ aggregationFunction (AS? alias)? (COMMA_ aggregationFunction (AS? alias)?)* pivotForClause pivotInClause RP_ alias?
521521
;
522522

523523
pivotForClause
@@ -535,7 +535,7 @@ pivotInClauseExpr
535535
;
536536

537537
unpivotClause
538-
: UNPIVOT ((INCLUDE | EXCLUDE) NULLS)? LP_ columnNames pivotForClause unpivotInClause RP_
538+
: UNPIVOT ((INCLUDE | EXCLUDE) NULLS)? LP_ (columnName | columnNames) pivotForClause unpivotInClause RP_ alias?
539539
;
540540

541541
unpivotInClause

test/it/parser/src/main/resources/case/dml/select-special-function.xml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -676,19 +676,24 @@
676676
</select>
677677

678678
<select sql-case-id="select_pivot">
679-
<projections start-index="7" stop-index="7">
680-
<shorthand-projection start-index="7" stop-index="7"/>
679+
<projections start-index="7" stop-index="18">
680+
<shorthand-projection start-index="7" stop-index="18">
681+
<owner name="pivotAlias" start-index="7" stop-index="16"/>
682+
<actual-projections start-index="0" stop-index="0"/>
683+
</shorthand-projection>
681684
</projections>
682-
<from>
683-
<subquery-table start-index="14" stop-index="34" >
685+
<from start-index="25" stop-index="104">
686+
<subquery-table start-index="25" stop-index="45">
684687
<subquery>
685688
<select>
686-
<projections start-index="22" stop-index="22">
687-
<shorthand-projection start-index="22" stop-index="22"/>
688-
</projections>
689-
<from start-index="29" stop-index="33">
690-
<simple-table name="sales" start-index="29" stop-index="33"/>
689+
<from start-index="40" stop-index="44">
690+
<simple-table name="sales" start-index="40" stop-index="44"/>
691691
</from>
692+
<projections start-index="33" stop-index="33">
693+
<shorthand-projection start-index="33" stop-index="33">
694+
<actual-projections start-index="0" stop-index="0"/>
695+
</shorthand-projection>
696+
</projections>
692697
</select>
693698
</subquery>
694699
</subquery-table>

test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<sql-case id="select_mod_function" value="SELECT MOD(order_id, 1) from t_order" db-types="PostgreSQL,openGauss" />
4949
<sql-case id="select_sys_xml_agg" value="SELECT SYS_XMLAGG(SYS_XMLGEN(last_name)) XMLAGG FROM employees WHERE last_name LIKE 'R%' ORDER BY xmlagg;" db-types="Oracle" />
5050
<sql-case id="select_set_function" value="SELECT customer_id, SET(cust_address_ntab) address FROM customers_demo ORDER BY customer_id;" db-types="Oracle" />
51-
<sql-case id="select_pivot" value="SELECT * FROM (SELECT * FROM sales) PIVOT (SUM(amount) FOR (year, month) IN ((2018, 'Jan'), (2018, 'Feb')))" db-types="Oracle"/>
51+
<sql-case id="select_pivot" value="SELECT pivotAlias.* FROM (SELECT * FROM sales) PIVOT (SUM(amount) FOR month IN ('Jan', 'Feb')) pivotAlias" db-types="Oracle"/>
5252
<sql-case id="select_unpivot" value="SELECT * FROM (SELECT * FROM sales) UNPIVOT ((cert_type, cert_number) FOR cert_source IN ((primary_cert_type, primary_cert_no), (alt_cert_type1, alt_cert_no1), (alt_cert_type2, alt_cert_no2)))" db-types="Oracle"/>
5353
<sql-case id="select_string_split_function" 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"/>
5454
<sql-case id="select_from_open_json_function" value="SELECT * FROM OPENJSON(@array) WITH ( month VARCHAR(3), temp int, month_id tinyint '$.sql:identity()') as months" db-types="SQLServer"/>

0 commit comments

Comments
 (0)