Skip to content
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 @@ -2640,7 +2640,7 @@ private SqlNode registerFrom(
case MATCH_RECOGNIZE:
registerMatchRecognize(parentScope, usingScope,
(SqlMatchRecognize) node, enclosingNode, alias, forceNullable);
return node;
return newNode;
Copy link
Copy Markdown
Contributor Author

@snuyanzin snuyanzin Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is initialized at

// Add an alias if necessary.
SqlNode newNode = node;

and
alias = SqlValidatorUtil.alias(node, nextGeneratedId++);
if (config.identifierExpansion()) {
// Since we're expanding identifiers, we should make the
// aliases explicit too, otherwise the expanded query
// will not be consistent if we convert back to SQL, e.g.
// "select EXPR$1.EXPR$2 from values (1)".
newNode = SqlValidatorUtil.addAlias(node, alias);

so need to reuse it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your detailed explanation.


case PIVOT:
registerPivot(parentScope, usingScope, (SqlPivot) node, enclosingNode,
Expand Down
25 changes: 25 additions & 0 deletions core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2575,6 +2575,31 @@ void testLikeAndSimilarFails() {
.rewritesTo(expected5);
sql(expected5)
.withParserConfig(c -> c.withQuoting(Quoting.BACK_TICK)).ok();

// Test cases for [CALCITE-7471] https://issues.apache.org/jira/browse/CALCITE-7471
// Alias is not auto generated for `MATCH_RECOGNIZE`
final String sql6 = "SELECT *\n"
+ "FROM emp\n"
+ "MATCH_RECOGNIZE (\n"
+ " MEASURES\n"
+ " FINAL COUNT(A.deptno) AS deptno\n"
+ " PATTERN (A B)\n"
+ " DEFINE\n"
+ " A AS A.empno = 123\n"
+ ")";

final String expected6 = "SELECT `EXPR$0`.`DEPTNO`\n"
+ "FROM `CATALOG`.`SALES`.`EMP` AS `EMP` MATCH_RECOGNIZE(\n"
+ "MEASURES FINAL COUNT(`A`.`DEPTNO`) AS `DEPTNO`\n"
+ "PATTERN (`A` `B`)\n"
+ "DEFINE `A` AS PREV(`A`.`EMPNO`, 0) = 123) AS `EXPR$0`";

sql(sql6)
.withValidatorConfig(c -> c.withIdentifierExpansion(true))
.rewritesTo(expected6);

sql(expected6)
.withParserConfig(c -> c.withQuoting(Quoting.BACK_TICK)).ok();
}

@Test void testIntervalTimeUnitEnumeration() {
Expand Down
Loading