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 ourterms of serviceand privacy statement.We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-6563] RelToSqlConverter should not merge two window functions #3949

Merged
merged 1 commit into from
Sep 15, 2024

Conversation

suibianwanwank
Copy link
Contributor

No description provided.

if (rel instanceof Project
&& ((Project) rel).containsOver()
&& maxClause == Clause.SELECT) {
// Cannot merge a Project that contains windowed functions onto an
Copy link
Contributor

Choose a reason for hiding this comment

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

you lost this comment. wasn't it useful?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, only the merging of Window functions in Project was handled here, but now window functions are handled whether they are in Project or not. So this comment doesn't quite fit anymore.

@@ -2006,6 +2010,31 @@ private boolean hasSortByOrdinal(@UnknownInitialization Result this,
return false;
}

private boolean containsOver(@UnknownInitialization Result this,
Copy link
Contributor

Choose a reason for hiding this comment

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

this looks very much like an unrolled visitor.
Couldn't the job be done by a shuttle?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this scenario, only the SqlCall in the SqlSelectList needs to be handled, so i don't think using SqlShuttle makes the code easier, WDYT

Copy link
Contributor

Choose a reason for hiding this comment

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

@suibianwanwank
I added a function namedSqlUtil#hasOverto my employer.

public static boolean hasOver(SqlNode node) {
if (node == null) {
return false;
}
try {
node.accept(new SqlShuttle() {
@Override public SqlNode visit(SqlCall call) {
if (call.getKind() == SqlKind.OVER) {
throw new Util.FoundOne(call);
}
return super.visit(call);
}
});
} catch (Util.FoundOne e) {
return true;
}
return false;
}

Can you use it to solve the problem?

if (node instanceof SqlSelect) {
final SqlSelect sqlSelect = (SqlSelect) node;
for (SqlNode sqlNode: sqlSelect.getSelectList()) {
final boolean hasOver = SqlUtil.hasOver(sqlNode);
if (hasOver) {
return true;
}
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In calciteSqlUtil,we can do it like this

/**
* Returns whether a given node contains an over function.
*
* @param node AST tree
*/
public static boolean containsOver(SqlNode node) {
final Predicate<SqlCall> callPredicate = call ->
call.getKind() == SqlKind.OVER;
return containsCall(node, callPredicate);
}

But I can't be sure if the node is necessarily a SqlSelect, it might be a SqlInsert? If so, I still have to visit all the node's inputs before finding a SqlSelect.

@suibianwanwank
Copy link
Contributor Author

I don't know why this workflow fails, is it temporary?

@NobiGo
Copy link
Contributor

NobiGo commented Sep 5, 2024

I don't know why this workflow fails, is it temporary?

Yes. Don't worry about it.

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Sep 5, 2024
@mihaibudiu mihaibudiu merged commitc1b0727 into apache:main Sep 15, 2024
33 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LGTM-will-merge-soon Overall PR looks OK. Only minor things left.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants