-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: attach Diagnostic to "invalid function argument types" error #23063
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
base: main
Are you sure you want to change the base?
Changes from all commits
347c00a
8fc9954
9e33404
c8a4431
371b705
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -930,21 +930,32 @@ pub struct ScalarFunction { | |
| pub func: Arc<crate::ScalarUDF>, | ||
| /// List of expressions to feed to the functions as arguments | ||
| pub args: Vec<Expr>, | ||
| /// Original source code location, if known | ||
| pub spans: Spans, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Between the two options you mentioned, I'd lean toward keeping the parser-only span information out of these public struct layouts. Validating in SqlToRel, where the call-site span is already available, seems like a good fit and avoids introducing a semver break while still preserving the improved diagnostics. If there's another semver-compatible way to associate the span with these expressions, that would also work, but I don't think adding the new public fields is something we should ship in a patch release. |
||
| } | ||
|
|
||
| impl ScalarFunction { | ||
| // return the Function's name | ||
| pub fn name(&self) -> &str { | ||
| self.func.name() | ||
| } | ||
|
|
||
| /// Returns a mutable reference to the spans | ||
| pub fn spans_mut(&mut self) -> &mut Spans { | ||
| &mut self.spans | ||
| } | ||
| } | ||
|
|
||
| impl ScalarFunction { | ||
| /// Create a new `ScalarFunction` from a [`ScalarUDF`] | ||
| /// | ||
| /// [`ScalarUDF`]: crate::ScalarUDF | ||
| pub fn new_udf(udf: Arc<crate::ScalarUDF>, args: Vec<Expr>) -> Self { | ||
| Self { func: udf, args } | ||
| Self { | ||
| func: udf, | ||
| args, | ||
| spans: Spans::new(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1094,6 +1105,8 @@ pub struct AggregateFunction { | |
| /// Name of the function | ||
| pub func: Arc<AggregateUDF>, | ||
| pub params: AggregateFunctionParams, | ||
| /// Original source code location, if known | ||
| pub spans: Spans, | ||
| } | ||
|
|
||
| #[derive(Clone, PartialEq, Eq, PartialOrd, Hash, Debug)] | ||
|
|
@@ -1127,8 +1140,14 @@ impl AggregateFunction { | |
| order_by, | ||
| null_treatment, | ||
| }, | ||
| spans: Spans::new(), | ||
| } | ||
| } | ||
|
|
||
| /// Returns a mutable reference to the spans | ||
| pub fn spans_mut(&mut self) -> &mut Spans { | ||
| &mut self.spans | ||
| } | ||
| } | ||
|
|
||
| /// A function used as a SQL window function | ||
|
|
@@ -1226,6 +1245,8 @@ pub struct WindowFunction { | |
| /// Name of the function | ||
| pub fun: WindowFunctionDefinition, | ||
| pub params: WindowFunctionParams, | ||
| /// Original source code location, if known | ||
| pub spans: Spans, | ||
| } | ||
|
|
||
| #[derive(Clone, PartialEq, Eq, PartialOrd, Hash, Debug)] | ||
|
|
@@ -1261,9 +1282,15 @@ impl WindowFunction { | |
| null_treatment: None, | ||
| distinct: false, | ||
| }, | ||
| spans: Spans::new(), | ||
| } | ||
| } | ||
|
|
||
| /// Returns a mutable reference to the spans | ||
| pub fn spans_mut(&mut self) -> &mut Spans { | ||
| &mut self.spans | ||
| } | ||
|
|
||
| /// Returns this window function's simplification hook, if any. | ||
| /// | ||
| /// See [`WindowFunctionSimplification`] for more information | ||
|
|
@@ -2287,6 +2314,9 @@ impl Expr { | |
| match self { | ||
| Expr::Column(col) => Some(&col.spans), | ||
| Expr::Not(inner) | Expr::Negative(inner) => inner.spans(), | ||
| Expr::ScalarFunction(func) => Some(&func.spans), | ||
| Expr::AggregateFunction(func) => Some(&func.spans), | ||
| Expr::WindowFunction(func) => Some(&func.spans), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
@@ -2482,10 +2512,12 @@ impl NormalizeEq for Expr { | |
| Expr::ScalarFunction(ScalarFunction { | ||
| func: self_func, | ||
| args: self_args, | ||
| .. | ||
| }), | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func: other_func, | ||
| args: other_args, | ||
| .. | ||
| }), | ||
| ) => { | ||
| self_func.name() == other_func.name() | ||
|
|
@@ -2506,6 +2538,7 @@ impl NormalizeEq for Expr { | |
| order_by: self_order_by, | ||
| null_treatment: self_null_treatment, | ||
| }, | ||
| .. | ||
| }), | ||
| Expr::AggregateFunction(AggregateFunction { | ||
| func: other_func, | ||
|
|
@@ -2517,6 +2550,7 @@ impl NormalizeEq for Expr { | |
| order_by: other_order_by, | ||
| null_treatment: other_null_treatment, | ||
| }, | ||
| .. | ||
| }), | ||
| ) => { | ||
| self_func.name() == other_func.name() | ||
|
|
@@ -2557,6 +2591,7 @@ impl NormalizeEq for Expr { | |
| null_treatment: self_null_treatment, | ||
| distinct: self_distinct, | ||
| }, | ||
| .. | ||
| } = left.as_ref(); | ||
| let WindowFunction { | ||
| fun: other_fun, | ||
|
|
@@ -2570,6 +2605,7 @@ impl NormalizeEq for Expr { | |
| null_treatment: other_null_treatment, | ||
| distinct: other_distinct, | ||
| }, | ||
| .. | ||
| } = other.as_ref(); | ||
|
|
||
| self_fun.name() == other_fun.name() | ||
|
|
@@ -2797,7 +2833,9 @@ impl HashNode for Expr { | |
| | Expr::TryCast(TryCast { expr: _expr, field }) => { | ||
| field.hash(state); | ||
| } | ||
| Expr::ScalarFunction(ScalarFunction { func, args: _args }) => { | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func, args: _args, .. | ||
| }) => { | ||
| func.hash(state); | ||
| } | ||
| Expr::AggregateFunction(AggregateFunction { | ||
|
|
@@ -2810,6 +2848,7 @@ impl HashNode for Expr { | |
| order_by: _, | ||
| null_treatment, | ||
| }, | ||
| .. | ||
| }) => { | ||
| func.hash(state); | ||
| distinct.hash(state); | ||
|
|
@@ -2828,6 +2867,7 @@ impl HashNode for Expr { | |
| null_treatment, | ||
| distinct, | ||
| }, | ||
| .. | ||
| } = window_fun.as_ref(); | ||
| fun.hash(state); | ||
| window_frame.hash(state); | ||
|
|
@@ -2952,7 +2992,7 @@ impl Display for SchemaDisplay<'_> { | |
| | Expr::OuterReferenceColumn(..) | ||
| | Expr::Placeholder(_) | ||
| | Expr::Wildcard { .. } => write!(f, "{}", self.0), | ||
| Expr::AggregateFunction(AggregateFunction { func, params }) => { | ||
| Expr::AggregateFunction(AggregateFunction { func, params, .. }) => { | ||
| match func.schema_name(params) { | ||
| Ok(name) => { | ||
| write!(f, "{name}") | ||
|
|
@@ -3109,7 +3149,7 @@ impl Display for SchemaDisplay<'_> { | |
| Expr::Unnest(Unnest { expr }) => { | ||
| write!(f, "UNNEST({})", SchemaDisplay(expr)) | ||
| } | ||
| Expr::ScalarFunction(ScalarFunction { func, args }) => { | ||
| Expr::ScalarFunction(ScalarFunction { func, args, .. }) => { | ||
| match func.schema_name(args) { | ||
| Ok(name) => { | ||
| write!(f, "{name}") | ||
|
|
@@ -3147,7 +3187,7 @@ impl Display for SchemaDisplay<'_> { | |
| Ok(()) | ||
| } | ||
| Expr::WindowFunction(window_fun) => { | ||
| let WindowFunction { fun, params } = window_fun.as_ref(); | ||
| let WindowFunction { fun, params, .. } = window_fun.as_ref(); | ||
| match fun { | ||
| WindowFunctionDefinition::AggregateUDF(fun) => { | ||
| match fun.window_function_schema_name(params) { | ||
|
|
@@ -3408,7 +3448,7 @@ impl Display for SqlDisplay<'_> { | |
|
|
||
| Ok(()) | ||
| } | ||
| Expr::AggregateFunction(AggregateFunction { func, params }) => { | ||
| Expr::AggregateFunction(AggregateFunction { func, params, .. }) => { | ||
| match func.human_display(params) { | ||
| Ok(name) => { | ||
| write!(f, "{name}") | ||
|
|
@@ -3587,7 +3627,7 @@ impl Display for Expr { | |
| fmt_function(f, fun.name(), false, &fun.args, true) | ||
| } | ||
| Expr::WindowFunction(window_fun) => { | ||
| let WindowFunction { fun, params } = window_fun.as_ref(); | ||
| let WindowFunction { fun, params, .. } = window_fun.as_ref(); | ||
| match fun { | ||
| WindowFunctionDefinition::AggregateUDF(fun) => { | ||
| match fun.window_function_display_name(params) { | ||
|
|
@@ -3639,7 +3679,7 @@ impl Display for Expr { | |
| } | ||
| } | ||
| } | ||
| Expr::AggregateFunction(AggregateFunction { func, params }) => { | ||
| Expr::AggregateFunction(AggregateFunction { func, params, .. }) => { | ||
| match func.display_name(params) { | ||
| Ok(name) => { | ||
| write!(f, "{name}") | ||
|
|
@@ -4399,6 +4439,7 @@ mod test { | |
| let udf = Expr::ScalarFunction(ScalarFunction { | ||
| func: Arc::new(ScalarUDF::new_from_impl(TestUDF {})), | ||
| args: vec![expr(), expr()], | ||
| spans: Spans::new(), | ||
| }); | ||
| let Expr::ScalarFunction(scalar) = &udf else { | ||
| unreachable!() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we avoid adding
spansas a required public field on these expression structs?ScalarFunction,AggregateFunction, andWindowFunctionare publicly constructible with struct literals. Adding a new required field means existing downstream code using those literals will no longer compile.cargo-semver-checksreportsconstructible_struct_adds_fieldforScalarFunction.spans,AggregateFunction.spans, andWindowFunction.spans.Please keep the diagnostic span data outside these public struct layouts, or use another semver-compatible design. The new required public fields should be removed before this is merged.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the slow reply here!
You're right about the semver break — adding
spansto these public structs tripsconstructible_struct_adds_field.My thinking so far: I noticed
Columnalready carries the samepub spans: Spansfield, added the same way to that (publicly constructible) struct in the earlier
diagnostics work (#13664) — so one direction would be to stay consistent with that
precedent. Another would be to keep the span off these struct layouts entirely and
validate arguments eagerly in
SqlToRel, where the call-site span is already in scope,so the diagnostic is attached at planning time.
I'm not sure which fits best here, though, and there may well be a cleaner
semver-compatible approach I'm not seeing. Do you have a direction you'd prefer — or a
better idea than either of these?