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
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.
5. Physical-plan serialization
Before
ProjectionExecNodeserialized:On decode, DataFusion reconstructed the projection which is fine when the complete output schema can be derivde from the expression and input but metadata cannot be:
Given:
So then I solve the optimizer bugs in this PR but not the codex one and will create a follow up for that.
TODO: Create follow up for codec / serialization