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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(meta): fix query rename visitor for complex func calls #18023

Merged
merged 2 commits into from
Aug 14, 2024
Merged
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
43 changes: 39 additions & 4 deletions src/meta/src/controller/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl QueryRewriter<'_> {
TableFactor::Derived { subquery, .. } => self.visit_query(subquery),
TableFactor::TableFunction { args, .. } => {
for arg in args {
self.visit_function_args(arg);
self.visit_function_arg(arg);
}
}
TableFactor::NestedJoin(table_with_joins) => {
Expand Down Expand Up @@ -247,8 +247,8 @@ impl QueryRewriter<'_> {
}

/// Visit function arguments and update all references.
fn visit_function_args(&self, function_args: &mut FunctionArg) {
match function_args {
fn visit_function_arg(&self, function_arg: &mut FunctionArg) {
match function_arg {
FunctionArg::Unnamed(arg) | FunctionArg::Named { arg, .. } => match arg {
FunctionArgExpr::Expr(expr) | FunctionArgExpr::ExprQualifiedWildcard(expr, _) => {
self.visit_expr(expr)
Expand All @@ -267,7 +267,18 @@ impl QueryRewriter<'_> {
/// Visit function and update all references.
fn visit_function(&self, function: &mut Function) {
for arg in &mut function.args {
self.visit_function_args(arg);
self.visit_function_arg(arg);
}
for expr in &mut function.order_by {
self.visit_expr(&mut expr.expr)
}
if let Some(over) = &mut function.over {
for expr in &mut over.partition_by {
self.visit_expr(expr);
}
for expr in &mut over.order_by {
self.visit_expr(&mut expr.expr);
}
}
}

Expand Down Expand Up @@ -482,4 +493,28 @@ mod tests {
let actual = alter_relation_rename_refs(definition, from, to);
assert_eq!(expected, actual);
}

#[test]
fn test_rename_with_complex_funcs() {
let definition = "CREATE MATERIALIZED VIEW mv1 AS SELECT \
agg1(\
foo.v1, func2(foo.v2) \
ORDER BY \
(SELECT foo.v3 FROM foo), \
(SELECT first_value(foo.v4) OVER (PARTITION BY (SELECT foo.v5 FROM foo) ORDER BY (SELECT foo.v6 FROM foo)) FROM foo)\
) \
FROM foo";
let from = "foo";
let to = "bar";
let expected = "CREATE MATERIALIZED VIEW mv1 AS SELECT \
agg1(\
foo.v1, func2(foo.v2) \
ORDER BY \
(SELECT foo.v3 FROM bar AS foo), \
(SELECT first_value(foo.v4) OVER (PARTITION BY (SELECT foo.v5 FROM bar AS foo) ORDER BY (SELECT foo.v6 FROM bar AS foo)) FROM bar AS foo)\
) \
FROM bar AS foo";
let actual = alter_relation_rename_refs(definition, from, to);
assert_eq!(expected, actual);
}
}
Loading