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

refactor(optimizer): record error contexts when casting composite types #19449

Merged
merged 10 commits into from
Nov 25, 2024

Conversation

BugenZhao
Copy link
Member

@BugenZhao BugenZhao commented Nov 19, 2024

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

What's changed and what's your intention?

Record contexts for error when recursively calling cast on composite types, to provide better error messages.

Preview:

- name: composite type cast error message (struct)
sql: |
create table t (v struct<a struct<b int>, c bool>);
select v::struct<d struct<e bytea>, f bool> from t;
binder_error: |
Failed to bind expression: CAST(v AS STRUCT<d STRUCT<e BYTEA>, f BOOLEAN>)
Caused by these errors (recent errors listed first):
1: cannot cast type "struct<a struct<b integer>, c boolean>" to "struct<d struct<e bytea>, f boolean>"
2: cannot cast struct field "a" to struct field "d"
3: cannot cast type "struct<b integer>" to "struct<e bytea>"
4: cannot cast struct field "b" to struct field "e"
5: cannot cast type "integer" to "bytea" in Explicit context
- name: composite type cast error message (map)
sql: |
create table t (v map(int, int));
select v::map(int, bytea) from t;
binder_error: |
Failed to bind expression: CAST(v AS MAP(INT,BYTEA))
Caused by these errors (recent errors listed first):
1: cannot cast type "map(integer,integer)" to "map(integer,bytea)"
2: cannot cast map value
3: cannot cast type "integer" to "bytea" in Explicit context

Checklist

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • All checks passed in ./risedev check (or alias, ./risedev c)

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.

@BugenZhao BugenZhao force-pushed the bz/struct-cast-trace branch from c42466f to 8c0a300 Compare November 21, 2024 07:23
Signed-off-by: Bugen Zhao <[email protected]>
Signed-off-by: Bugen Zhao <[email protected]>
@BugenZhao BugenZhao force-pushed the bz/struct-cast-trace branch from 8c0a300 to b2f65b7 Compare November 21, 2024 08:15
Signed-off-by: Bugen Zhao <[email protected]>
@BugenZhao BugenZhao force-pushed the bz/struct-cast-trace branch from b2f65b7 to 3f12a20 Compare November 21, 2024 08:16
@BugenZhao BugenZhao changed the title refactor(optimizer): record error contexts when casting structs refactor(optimizer): record error contexts when casting composite types Nov 21, 2024
Signed-off-by: Bugen Zhao <[email protected]>
@BugenZhao BugenZhao marked this pull request as ready for review November 21, 2024 08:23
@BugenZhao BugenZhao requested a review from a team as a code owner November 21, 2024 08:23
@BugenZhao BugenZhao requested review from cyliu0, xiangjinwu and xxchan and removed request for cyliu0 November 21, 2024 08:23
- only show "context" in the bottom level
- show struct field name
- do not expose internal impl of map type, instead, show "key" or "value"

Signed-off-by: Bugen Zhao <[email protected]>
@@ -204,10 +275,10 @@ pub enum CastContext {
Explicit,
}

pub type CastMap = BTreeMap<(DataTypeName, DataTypeName), CastContext>;
pub type CastTable = BTreeMap<(DataTypeName, DataTypeName), CastContext>;
Copy link
Member Author

Choose a reason for hiding this comment

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

Not to be confused with cast_map.

@BugenZhao BugenZhao requested a review from xxchan November 21, 2024 08:59
Signed-off-by: Bugen Zhao <[email protected]>
if ok {
Ok(())
} else {
bail_cast_error!()
Copy link
Member

Choose a reason for hiding this comment

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

A little confused how it works. So this is an empty leaf CastError? Why it's not shown in the err msg stack? (Why we need it?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. In error report we'll disregard all empty and repeated entries, so it won't appear.

To preserve the reason for a cast failure, we must return a Result in functions like cast_struct. However, the default error message "cannot cast" is uniformly applied in cast, so we simply return an empty leaf error and let the caller to fill the context.

Copy link
Member

Choose a reason for hiding this comment

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

It sounds like Result<(), Option<CastError>> 🤪

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.

very good 👍

@graphite-app graphite-app bot requested a review from a team November 22, 2024 07:12
@BugenZhao BugenZhao added this pull request to the merge queue Nov 25, 2024
Merged via the queue into main with commit 9abd62a Nov 25, 2024
28 of 29 checks passed
@BugenZhao BugenZhao deleted the bz/struct-cast-trace branch November 25, 2024 03:58
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.

2 participants