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(udf): allow aggregate: prefixed sql udf #18203

Merged
merged 3 commits into from
Aug 23, 2024
Merged

Conversation

stdrc
Copy link
Member

@stdrc stdrc commented Aug 22, 2024

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

Fixes #18202.

Checklist

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • I have added test labels as necessary. See details.
  • I have added fuzzing tests or opened an issue to track them. (Optional, recommended for new SQL features Sqlsmith: Sql feature generation #7934).
  • My PR contains breaking changes. (If it deprecates some features, please create a tracking issue to remove them in the future).
  • All checks passed in ./risedev check (or alias, ./risedev c)
  • My PR changes performance-critical code. (Please run macro/micro-benchmarks and show the results.)
  • My PR contains critical fixes that are necessary to be merged into the latest release. (Please check out the details)

Documentation

  • My PR needs documentation updates. (Please use the Release note section below to summarize the impact on users)

Release note

If this PR includes changes that directly affect users or other significant modifications relevant to the community, kindly draft a release note to provide a concise summary of these changes. Please prioritize highlighting the impact these changes will have on users.

@stdrc stdrc changed the title correctly bind sql udf for aggregate: prefixed scalar function fix(udf): allow aggregate: prefixed sql udf Aug 22, 2024
@github-actions github-actions bot added type/fix Bug fix and removed Invalid PR Title labels Aug 22, 2024
@stdrc stdrc marked this pull request as ready for review August 22, 2024 16:31
@stdrc stdrc requested review from chenzl25, BugenZhao and xxchan August 22, 2024 16:31
@stdrc stdrc force-pushed the rc/fix-udaf-as-window-function branch from 0ee1e49 to 41a1a96 Compare August 22, 2024 17:04
@stdrc stdrc force-pushed the rc/fix-udf-aggregate-prefix branch from bbf3bdf to 20a7dc5 Compare August 22, 2024 17:04
Base automatically changed from rc/fix-udaf-as-window-function to main August 22, 2024 17:31
@graphite-app graphite-app bot requested a review from a team August 22, 2024 17:32
Copy link
Contributor

@chenzl25 chenzl25 left a comment

Choose a reason for hiding this comment

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

Generally LGTM

Comment on lines 491 to 499
let mut udf_context = HashMap::new();
for (c, e) in context {
// Note that we need to bind the args before actual delve in the function body
// This will update the context in the subsequent inner calling function
// e.g.,
// - create function print(INT) returns int language sql as 'select $1';
// - create function print_add_one(INT) returns int language sql as 'select print($1 + 1)';
// - select print_add_one(1); # The result should be 2 instead of 1.
// Without the pre-binding here, the ($1 + 1) will not be correctly populated,
// causing the result to always be 1.
let Ok(e) = self.bind_expr(e) else {
return Err(ErrorCode::BindError(
"failed to bind the argument, please recheck the syntax".to_string(),
)
.into());
};
udf_context.insert(c, e);
for (i, arg) in args.into_iter().enumerate() {
if func.arg_names[i].is_empty() {
udf_context.insert(format!("${}", i + 1), arg);
} else {
// The index mapping here is accurate
// So that we could directly use the index
udf_context.insert(func.arg_names[i].clone(), arg);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I don't quite get how this context works. Could you please elaborate it more?

Copy link
Member Author

Choose a reason for hiding this comment

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

The context contains a mapping of argument name -> argument expression. If an argument has no name, $n will be used as its name. I just updated the comment to make it more clear.

Signed-off-by: Richard Chien <[email protected]>
@stdrc stdrc added this pull request to the merge queue Aug 23, 2024
Merged via the queue into main with commit 5cae578 Aug 23, 2024
30 of 31 checks passed
@stdrc stdrc deleted the rc/fix-udf-aggregate-prefix branch August 23, 2024 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/fix Bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cannot prefix a SQL UDF to make it an aggregate function
2 participants