fix: preserve projection metadata during optimization - #24670
fix: preserve projection metadata during optimization#24670gene-bordegaray wants to merge 1 commit into
Conversation
gabotechs
left a comment
There was a problem hiding this comment.
Good catch @gene-bordegaray! just to give more context, we were bitten by this in our system while upgrading.
Just left a suggestion for relaxing the requirements, but otherwise LGTM.
| }) && exprs.len() == projection.input().schema().fields().len() | ||
| && projection.schema() == projection.input().schema() |
There was a problem hiding this comment.
This might be putting more restrictions than just metadata equality. It might be fine, but if we want to play it safe it could be better to just do && projection.schema().metadata() == projection.input().schema().metadata()
There was a problem hiding this comment.
We need to check full schema because even if the schema metadata is equal things like the field metadata might not be thus we nee to check this as well.
I don't see anything in the schema which would be overestricting this. I may be missing something though
There was a problem hiding this comment.
Pretty much the order of columns. I bet that's why the current checks are like they are right now.
I think it's fine though, if this becomes too restrictive it will start popping up in tests
|
🤔 there seems to be a CI failure: Do you think it's related to this change? |
looking into |
Found issues, this is a bit more involved than I was hoping. Will the variants with the fix |
|
@gabotechs ok I figured out what was going on and documented it in the PR description. There is also another bug in the codec / serialization where we need to serialize metadata. I am not solving that in this PR to keep scoped / tracked. I will crete issue for this tmrw or you can if you would like 👍 |
42888f7 to
822b3f9
Compare
|
this is also a correctenss issue / regression in 55 so I can note this in the minor version bump |
822b3f9 to
f64100d
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #24670 +/- ##
==========================================
- Coverage 81.61% 81.61% -0.01%
==========================================
Files 1123 1123
Lines 409392 409514 +122
Branches 409392 409514 +122
==========================================
+ Hits 334134 334232 +98
- Misses 55637 55650 +13
- Partials 19621 19632 +11 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
created codec / serialization follow up here: #24695 |
| // `Cast::new` and `TryCast::new` use this field when only a target | ||
| // type is known. In that case, retain the source field's metadata. | ||
| let type_only_target = target_field.name().is_empty() | ||
| && target_field.is_nullable() | ||
| && target_field.metadata().is_empty(); | ||
| let metadata = if type_only_target { |
There was a problem hiding this comment.
Why is it not sufficient to do something simpler like:
Does target_field have metadata? if yes, then use that metadata, if not, then use the source_field metadata.
There was a problem hiding this comment.
Ya I thought this then AI actually ciaght this and I validated.
There can be the case where we are giving an target field with empty metadata and target type of a FiexedSizeBinary, then the source say has metadata that is marking something as a arrow UUID. In this case you would expect the result to be corrctly casted to FixedSizeBinary and empty metadata.
But what would happen is the target metadata is empty so then it would try to use the source. But the source metadata is saying to treated the FixedSizeBinary as a arrow UUID thus wouldnt cast correctly.
Now this check prevents that by checking if its a type only target.
There may be a clarner way to represent this though. I will return with thoughts
There was a problem hiding this comment.
mine can also be wrong. its kinda implicit. I think this difference should be properly marked but might be a larger change like with an enum:
enum CastTarget { DataType, Field }| /// | ||
| /// Such a projection is an execution boundary: a parent expression such as | ||
| /// `arrow_metadata` can observe its output field metadata. | ||
| fn projection_overrides_metadata(projection: &ProjectionExec) -> Result<bool> { |
There was a problem hiding this comment.
Nit: a ProjectionExec::overrides_metadata method rather than a standalone function sounds very slightly more elegant.
| if projection_overrides_metadata(outer)? { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| let mut current_exprs: Vec<ProjectionExpr> = outer.expr().to_vec(); | ||
| let mut current_input: Arc<dyn ExecutionPlan> = Arc::clone(outer.input()); | ||
| let mut column_ref_map: HashMap<Column, usize> = HashMap::new(); | ||
| let mut collapsed_any = false; | ||
|
|
||
| 'outer: while let Some(inner_proj) = current_input.downcast_ref::<ProjectionExec>() { | ||
| if projection_overrides_metadata(inner_proj)? { | ||
| break; | ||
| } | ||
|
|
||
| // Collect the column references usage in the outer projection. |
There was a problem hiding this comment.
Imagine this situation:
ProjectionExec: <- does not override metadata
ProjectionExec: <- overrides metadata
This is collapsible right? but the current code will omit collapsing it.
There was a problem hiding this comment.
This is sometimes collapsable because take the example I put in the PR description. You might have this:
ProjectionExec: arrow_metadata(i, 'event_field') AS metadata
ProjectionExec: i@0 AS i
output metadata = {"event_field": "true"}
DataSourceExec: i
metadata = {}
The outer query does not have override metadata but the inner does. The correct result is true.
The previous projection logic would collapse these, but the outer projection expr reads the metadata. So it would make:
ProjectionExec: arrow_metadata(i, 'event_field') AS metadata
DataSourceExec: i
metadata = {}
Giving use result as NULL now.
This is kinda ocnservative as we could probably just chekc if everything in the outer projection is just referencing columns but I am just trying to get to correctness first. Then could do the optimixation. What you think?
There was a problem hiding this comment.
Damn, this is tricky indeed... I think it's good then 👍
Still working on better fix it, so approach might change
f64100d to
beed4cd
Compare
|
ok I pushed a change that introduces an enum to differentiation between data type and explicit field casts. It is a larger and public api change but it is what I see as properly tracking this information, not an ad hoc check |
beed4cd to
c30f5be
Compare
| /// The `DataType` the expression will yield | ||
| pub field: FieldRef, | ||
| /// The target type and metadata policy. | ||
| pub field: CastTarget, |
There was a problem hiding this comment.
This is a breaking change of a public type, so if we go down this route this PR will not be eligible for a backport to 55. https://datafusion.apache.org/contributor-guide/release_management.html#backport-criteria
| #[prost(message, optional, tag = "5")] | ||
| pub target_field: ::core::option::Option<super::datafusion_common::Field>, |
There was a problem hiding this comment.
This and other places in this file are adding pub fields also making this a breaking change.
|
hey @timsaucer yes, this PR was originally meant to be stacked on #24725 but because of the breaking changes we are gong to take #23169 approach which avoids this for now. Then I will rebase this on that PR and will not hve these breaking change 👍 |
| /// Derives the output field for a cast expression from the source field, using | ||
| /// explicit target metadata when supplied. | ||
| /// For `TryCast`, `force_nullable` is `true` since a failed cast returns NULL. | ||
| fn cast_output_field( | ||
| source_field: &FieldRef, | ||
| target_type: &DataType, | ||
| target: &CastTarget, | ||
| force_nullable: bool, | ||
| ) -> Arc<Field> { | ||
| let metadata = target | ||
| .metadata() | ||
| .cloned() | ||
| .unwrap_or_else(|| source_field.metadata().clone()); | ||
| let mut f = source_field | ||
| .as_ref() | ||
| .clone() | ||
| .with_data_type(target_type.clone()) | ||
| .with_metadata(source_field.metadata().clone()); | ||
| .with_data_type(target.data_type().clone()) | ||
| .with_metadata(metadata); |
There was a problem hiding this comment.
This is the only change I needed on top of projection.rs to make the UUID test pass (counterfactual confirmed: reverting just this reproduces the CI failure).
Note your callsite changes below — cast_output_field(&src, field, false) / (&src, field, true) — work unchanged with this, since Cast.field would go back to being a FieldRef. So this suggestion plus dropping CastTarget is the whole edit; the other ~20 files in the stack come out with it.
| /// Derives the output field for a cast expression from the source field, using | |
| /// explicit target metadata when supplied. | |
| /// For `TryCast`, `force_nullable` is `true` since a failed cast returns NULL. | |
| fn cast_output_field( | |
| source_field: &FieldRef, | |
| target_type: &DataType, | |
| target: &CastTarget, | |
| force_nullable: bool, | |
| ) -> Arc<Field> { | |
| let metadata = target | |
| .metadata() | |
| .cloned() | |
| .unwrap_or_else(|| source_field.metadata().clone()); | |
| let mut f = source_field | |
| .as_ref() | |
| .clone() | |
| .with_data_type(target_type.clone()) | |
| .with_metadata(source_field.metadata().clone()); | |
| .with_data_type(target.data_type().clone()) | |
| .with_metadata(metadata); | |
| /// Derives the output field for a cast expression from the source field. | |
| /// | |
| /// The cast target's metadata is authoritative when it carries any; otherwise the | |
| /// source's metadata is inherited. This mirrors the physical `CastExpr`, whose | |
| /// target field is already authoritative when it is not the synthesized type-only | |
| /// field. | |
| /// | |
| /// For `TryCast`, `force_nullable` is `true` since a failed cast returns NULL. | |
| fn cast_output_field( | |
| source_field: &FieldRef, | |
| target_field: &FieldRef, | |
| force_nullable: bool, | |
| ) -> Arc<Field> { | |
| let metadata = if target_field.metadata().is_empty() { | |
| source_field.metadata().clone() | |
| } else { | |
| target_field.metadata().clone() | |
| }; | |
| let mut f = source_field | |
| .as_ref() | |
| .clone() | |
| .with_data_type(target_field.data_type().clone()) | |
| .with_metadata(metadata); |
There was a problem hiding this comment.
yes sorry about the confusion I should've temporarily made this a draft, the diff for this PR is much smaller. Allthat the CastTarget and protobuf changes are mixed into this PR because I had stacked it on #24725 but they shouldn’t be reviewed as part of the projection optimizer fix. I’m going to rebase this onto #23169 after some discussion to go with that approach for the minor relese.
as far as this particular comment. I originally thought this too and @gabotechs also asked about this, its a subtle one. Let me know if that clarifies 👍
There was a problem hiding this comment.
I will rebase now and the PR should clean up after #23169 merges
c30f5be to
0603df9
Compare
| 00010203040506070809000102030506 NULL | ||
|
|
||
| # arrow_cast to a different type strips extension metadata (type-only cast semantics) | ||
| query ?T |
There was a problem hiding this comment.
Yes, it is for a different bug but when i rooginally made the minimal fix (my top commit now on this brnach) it fails some sqllogictests because of #23169 not being merged. Specifically:
SELECT CAST(raw AS UUID), arrow_metadata(CAST(raw AS UUID), 'ARROW:extension:name');Expected on main:
00010203040506070809000102030506 arrow.uuid
But with the projection fix alone the projection starts to actually preserver logical schema then ignores the UUID target metadata, so the result:
- arrow.uuid
+ NULL#24831 handles this with this check:
let metadata = if target_field.metadata().is_empty() {
source_field.metadata().clone()
} else {
target_field.metadata().clone()
};as prposed in your brnach #24831 but it has the issue that I talk about here which is hwy I opted into stacking on #23169 to handle that case while not allowing another edge case to creep in.
So this is getting a bit tricky to handle. That particular test is from another PR, but solving just the bug at at the surface level unveils more underlying issues with casting and metadata that this relies on. I would think that getting this in the minor patch with #23169 would be the good short term solution, Then I read your comment and I think this could be a viable breaking change after some more discussion regarding how we want these semantics to behave.
Thanks for taking time to investigate all this @adriangb 🙇
There was a problem hiding this comment.
it has the issue that I talk about #24670 (comment) which is hwy I opted into stacking on #23169 to handle that case while not allowing another edge case to creep in
The discussion you linked is suggesting that we go with:
Does target_field have metadata? if yes, then use that metadata, if not, then use the source_field metadata.
You propose that breaks with: arrow_cast(uuid_col, 'FixedSizeBinary') because the result is FixedSizeBinary but with the UUID metadata (invalid).
Under the stricter proposal in #23169 (comment) this would be resolved: we'd ignore the source field metadata.
I think the larger question is if we back port the behavior change to 55.
There was a problem hiding this comment.
yes I think we are in aggreance, long term there is most likely a better solution than what is proposed. This PR is just trying to fix a bug that we saw pop up after the df55 upgrade and then created this cascading effect of more bugs being unveiled.
If we are ok with leaving that known incorrect behavior in the minor patch we can do the check:
Does target_field have metadata? if yes, then use that metadata, if not, then use the source_field metadata.
Seems like it will be sorted in the meeting tmrw 🙇
There was a problem hiding this comment.
Since this PR is fixing 4 issues, can we merge a fix for 1-3 without 4?
There was a problem hiding this comment.
yes we could and have CI pass, but there will be the undelying bug that the other PR is talking about and the check prposed elides. If we are aware of that and ok with it then more than happy to just have the top commit and use the check:
let metadata = if target_field.metadata().is_empty() {
source_field.metadata().clone()
} else {
target_field.metadata().clone()
};This is my first participation in a patch release so some guidance would be great. Thank you again 🙇
EDIT: #23169 properly handles this so if we backpoirt this with it should fix all cases for now until we discuss long term semantics
There was a problem hiding this comment.
This is my first participation in a patch release so some guidance would be great. Thank you again 🙇
You're doing great! The main thing is trying to minimize the amount of code and behavior change that ships with a patch. And in general keeping PRs decoupled (e.g. splitting a bit PR into two smaller ones so that at least one of them is unblocked) is good. Those were my suggestions. Sorry if they didn't make sense here.
There was a problem hiding this comment.
ok cool, thanks! I think the current state after the prerequisite prs got merged is more along the lines of what we are lookiing for in this
0603df9 to
d112760
Compare
There were four ways metadata could disappear.
1. Removing a metadata-only identity projection
Consider:
The check to remove the projection asked:
All answers yes so optimizer removed projection:
Metadata lost.
2 Collapsing across a metadata boundary
Consider:
The correct result is
true.The previous projection colapse logic would substitute the outer expression through the inner projection:
Now the func sees the scan field instead of the inner projection field giving use result as
NULLnow.3 Rebuilding a projection with a new child
Some optimizer paths replace the child of a projection:
The previous
make_with_childimplementation did this:where try_new derives the output schema from the expressions and new child so it woudlnt retain metadata from the original projection.
4 Cast target metadata lost before optimization
This one was a little confusing because main passed the UUID metadata test, but the first version of this PR did not (@gabotechs this is what you called out)
Basically a cast can have an explicit target field with metadata. For example, the UUID type planner produces:
But logical cast schema only used the target data type when deriving and kept th source metadata:
This appeared in CI when common sub-expr elimination extracts a repeated cast into
its own projection:
The inner projection was initially created with incorrect empty metadata, so the physical optimizer rebuilt that projection and rederived its schema so isthe was accidentally repairing the logical schema bug.
Once this PR started preserving projection metadata correctly had this pop up this other bug.
So then I solve the optimizer bugs in this PR