Skip to content

[CALCITE-7471] Alias is not auto generated for MATCH_RECOGNIZE#4882

Merged
snuyanzin merged 1 commit intoapache:mainfrom
snuyanzin:calcite7471
Apr 13, 2026
Merged

[CALCITE-7471] Alias is not auto generated for MATCH_RECOGNIZE#4882
snuyanzin merged 1 commit intoapache:mainfrom
snuyanzin:calcite7471

Conversation

@snuyanzin
Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7471

in case of toggled identifierExpansion and missed alias for table there should be generated one.
However in case of MATCH_RECOGNIZE it is missed (the PR is going to fix it)
example of query

SELECT *
FROM emp
MATCH_RECOGNIZE (
  MEASURES
     FINAL COUNT(A.deptno) AS deptno
  PATTERN (A B)
  DEFINE
    A AS A.empno = 123
)

unparsed query (there is EXPR$0 in SELECT however it is not defined anywhere)

SELECT `EXPR$0`.`DEPTNO`
FROM `CATALOG`.`SALES`.`EMP` AS `EMP` MATCH_RECOGNIZE(
MEASURES FINAL COUNT(`A`.`DEPTNO`) AS `DEPTNO`
PATTERN (`A` `B`)
DEFINE `A` AS PREV(`A`.`EMPNO`, 0) = 123)

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.

Copy link
Copy Markdown
Member

@caicancai caicancai left a comment

Choose a reason for hiding this comment

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

LGTM

@sonarqubecloud
Copy link
Copy Markdown

@snuyanzin snuyanzin merged commit 925f490 into apache:main Apr 13, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants