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 implicit cast of arguments for table and aggregate functions #13545

Merged
merged 20 commits into from
Nov 28, 2023

Conversation

wangrunji0408
Copy link
Contributor

@wangrunji0408 wangrunji0408 commented Nov 21, 2023

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

What's changed and what's your intention?

This PR resolves #13205. Table functions and aggregate functions now support implicit cast.

  dev=> SELECT jsonb_each('{"a": 1}');
- ERROR:  QueryError: Bind error: failed to bind expression: jsonb_each('{"a": 1}')
- Caused by:
-  Expr error: Unsupported function: jsonb_each
+  jsonb_each 
+ ------------
+  (a,1)

  dev=> select bool_or('false');
- ERROR:  QueryError: Bind error: failed to bind expression: bool_or('false')
- Caused by:
-   Expr error: Unsupported function: bool_or
+  bool_or 
+ ---------
+  f

To achieve that, the infer_type method is reused for table functions and aggregate functions.

Next, to handle special cases in AggCall::infer_return_type, this PR defines these functions in expr_impl crate, with a newly introduced unimplemented tag. Functions defined like this are visible in the signature map, but can not be built in the backend.

#[aggregate("avg(int2) -> decimal", unimplemented)]
fn _avg() {}

This PR also merges retractable and append-only aggregate into one signature. Otherwise, infer_type might find 2 functions even if there is only one:

dev=> select bool_and(true);
ERROR:  QueryError

Caused by these errors (recent errors listed first):
  1: Failed to bind expression: bool_and(true)
  2: Bind error: function Aggregate(BoolAnd)[Boolean] is not unique
HINT:  Could not choose a best candidate function. You might need to add explicit type casts.

Similarly, the internal sum(int8) -> int8 is marked as deprecated to prevent not unique error.

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)

Signed-off-by: Runji Wang <[email protected]>
Copy link
Member

@stdrc stdrc left a comment

Choose a reason for hiding this comment

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

It's really great to see AggCall::infer_return_type being removed!

src/expr/core/src/sig/mod.rs Outdated Show resolved Hide resolved
src/expr/impl/src/aggregate/general.rs Outdated Show resolved Hide resolved
src/expr/impl/src/aggregate/general.rs Outdated Show resolved Hide resolved
@wangrunji0408 wangrunji0408 requested a review from stdrc November 24, 2023 11:04
Copy link
Member

@stdrc stdrc left a comment

Choose a reason for hiding this comment

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

Rest LGTM!

/// A function to build the aggregate function (append-only version).
pub build_aggregate_append_only: Option<AggregateFunctionBuilder>,
/// A function to build the expression.
pub build: FuncBuilder,

/// A function to infer the return type from argument types.
pub type_infer: fn(args: &[DataType]) -> Result<DataType>,

/// Whether the function is deprecated and should not be used in the frontend.
/// For backward compatibility, it is still available in the backend.
pub deprecated: bool,
Copy link
Member

@stdrc stdrc Nov 27, 2023

Choose a reason for hiding this comment

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

Is it better to have another field representing internal? Since internal functions may also be deprecated. Not a strong opinion though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

internal is just an alias of deprecated. They are the same from the view of signature. 🤔

Signed-off-by: Runji Wang <[email protected]>
Copy link

codecov bot commented Nov 28, 2023

Codecov Report

Attention: 57 lines in your changes are missing coverage. Please review.

Comparison is base (ce73e80) 68.31% compared to head (b55ff3d) 68.26%.

Files Patch % Lines
src/expr/impl/src/aggregate/general.rs 61.11% 28 Missing ⚠️
src/frontend/src/expr/agg_call.rs 11.76% 15 Missing ⚠️
...rc/frontend/src/optimizer/plan_node/generic/agg.rs 68.42% 6 Missing ⚠️
src/frontend/src/binder/select.rs 0.00% 2 Missing ⚠️
src/expr/core/src/sig/mod.rs 97.67% 1 Missing ⚠️
src/expr/impl/src/scalar/date_trunc.rs 50.00% 1 Missing ⚠️
src/expr/impl/src/scalar/to_char.rs 50.00% 1 Missing ⚠️
src/expr/impl/src/scalar/to_timestamp.rs 50.00% 1 Missing ⚠️
src/expr/macro/src/gen.rs 95.83% 1 Missing ⚠️
src/frontend/src/expr/type_inference/func.rs 95.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #13545      +/-   ##
==========================================
- Coverage   68.31%   68.26%   -0.06%     
==========================================
  Files        1523     1523              
  Lines      261730   261805      +75     
==========================================
- Hits       178794   178714      -80     
- Misses      82936    83091     +155     
Flag Coverage Δ
rust 68.26% <75.74%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wangrunji0408 wangrunji0408 added this pull request to the merge queue Nov 28, 2023
Merged via the queue into main with commit c47b9ed Nov 28, 2023
26 of 27 checks passed
@wangrunji0408 wangrunji0408 deleted the wrj/implicit-cast branch November 28, 2023 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

expr: support implicit cast of arguments for table and aggregate functions
2 participants