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

feat(expr): support VARIADIC function arguments #14753

Merged
merged 15 commits into from
Mar 7, 2024
Merged

Conversation

wangrunji0408
Copy link
Contributor

@wangrunji0408 wangrunji0408 commented Jan 23, 2024

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

What's changed and what's your intention?

resolve #14705

This PR adds VARIADIC argument support for the following functions:

select format('%s %s', variadic array['Hello', 'World']); -> "Hello World"
select concat(variadic array['abcde', '2', NULL, '22']); -> "abcde222"
select concat_ws(',', variadic array['abcde', 2, NULL, 22] :: varchar[]); -> "abcde,2,22"
select jsonb_build_array(variadic array[1, 2, 4, 5]); -> [1, 2, 4, 5]
select jsonb_build_object(variadic array['foo', '1', '2', 'bar']); -> {"2": "bar", "foo": 1}
select jsonb_extract_path('{"a": {"b": ["foo","bar"]}}', variadic array['a', 'b', '1']); -> bar
select jsonb_extract_path_text('{"a": {"b": ["foo","bar"]}}', variadic array['a', 'b', '1']); -> bar

It adds new functions format_variadic, concat_ws_variadic... on the backend, which take a fixed anyarray as the last argument. On the frontend, we rewrite the variadic calls into new functions.

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

support VARIADIC arguments for the following functions:

  • format
  • concat_ws
  • jsonb_build_array
  • jsonb_build_object
  • jsonb_extract_path
  • jsonb_extract_path_text

The VARIADIC keyword expands the array parameter into one or more occurrences of its element type. See postgres documentation for more details.

Add a new function concat:

concat ( val1 "any" [, val2 "any" [, ...] ] ) → text
Concatenates the text representations of all the arguments. NULL arguments are ignored.
concat('abcde', 2, NULL, 22) → abcde222

Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
@wangrunji0408 wangrunji0408 changed the title feat(expr): support VARIADIC function arguments for format and concat_ws feat(expr): support VARIADIC function arguments Jan 24, 2024
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
proto/expr.proto Outdated
Comment on lines 227 to 232
JSONB_EXTRACT_PATH = 613;
JSONB_EXTRACT_PATH = 626;
JSONB_EXTRACT_PATH_VARIADIC = 613;
Copy link
Member

Choose a reason for hiding this comment

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

Could you explain the safety of this change?

Copy link
Member

Choose a reason for hiding this comment

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

So old impl is already variadic?
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Previously we implemented variadic form in backend, but only accepted non-variadic form in frontend.

Copy link
Member

@xxchan xxchan left a comment

Choose a reason for hiding this comment

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

I'm still a little confused about why we need 2 exprs. Isn't variadic version converted to the variadic version? 🤔

Could you add some planner tests to help understand?

proto/expr.proto Show resolved Hide resolved
@@ -612,6 +612,24 @@ impl Debug for ListRef<'_> {
}
}

impl Row for ListRef<'_> {
Copy link
Member

Choose a reason for hiding this comment

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

Is this a little confusing 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to allow arguments of list type to be accepted as impl Row in Rust functions.

Copy link
Member

Choose a reason for hiding this comment

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

I'm okay with this as I cannot come up with another way to interpret ARRAY as rows to form an ambiguousness. If there's one, making a newtype on ListRef then impl Row on that will work.

proto/expr.proto Outdated
Comment on lines 227 to 232
JSONB_EXTRACT_PATH = 613;
JSONB_EXTRACT_PATH = 626;
JSONB_EXTRACT_PATH_VARIADIC = 613;
Copy link
Member

Choose a reason for hiding this comment

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

So old impl is already variadic?
image

/// abcde222
/// ```
#[function("concat(variadic anyarray) -> varchar")]
fn concat(vals: impl Row, writer: &mut impl Write) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering what the possible receiver types are here for impl Row. 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is RowRef for non-variadic version, and ListRef for variadic version.

@wangrunji0408
Copy link
Contributor Author

wangrunji0408 commented Mar 6, 2024

I'm still a little confused about why we need 2 exprs.

  • Without VARIADIC keyword, arguments are stored in multiple columns.
  • With VARIADIC keyword, arguments are stored as a list in a column.

They are physically different. Converting either form to the other will introduce overhead. So for performance reason, I decided to generate separate code for each version in backend.

@wangrunji0408 wangrunji0408 added the user-facing-changes Contains changes that are visible to users label Mar 6, 2024
Copy link

gitguardian bot commented Mar 7, 2024

⚠️ GitGuardian has uncovered 1 secret following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secret in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
9425213 Triggered Generic Password 9bf4913 ci/scripts/regress-test.sh View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secret safely. Learn here the best practices.
  3. Revoke and rotate this secret.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Our GitHub checks need improvements? Share your feedbacks!

Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Copy link
Member

@BugenZhao BugenZhao left a comment

Choose a reason for hiding this comment

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

LGTM

@wangrunji0408 wangrunji0408 added this pull request to the merge queue Mar 7, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 7, 2024
@wangrunji0408 wangrunji0408 added this pull request to the merge queue Mar 7, 2024
Merged via the queue into main with commit 176eacf Mar 7, 2024
27 of 29 checks passed
@wangrunji0408 wangrunji0408 deleted the wrj/variadic branch March 7, 2024 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/feature user-facing-changes Contains changes that are visible to users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support VARIADIC function arguments
3 participants