proto: serialize and dedupe dynamic filters v2#21807
proto: serialize and dedupe dynamic filters v2#21807jayshrivastava wants to merge 6 commits intoapache:mainfrom
Conversation
004aa52 to
23fc7f1
Compare
|
|
||
| /// Internal serializer that adds expr_id to expressions. | ||
| /// Created fresh for each serialization operation. | ||
| struct DeduplicatingSerializer { |
There was a problem hiding this comment.
Since the DynamicFilterPhysicalExpr self-generates an expr id and exposes it via expression_id, we don't need this anymore. We can just dedupe during deserialization.
There was a problem hiding this comment.
As per comment above I think we should keep expr_id on PhysicalExprNode. But I think the drop in complexity and the fact that this is added to the public PhysicalExpr trait justifies dropping this specialized serializer and instead having the default one call PhysicalExpr::expr_id() and put that into the proto.
64b5889 to
c042c08
Compare
|
|
||
| /// An atomic snapshot of a [`DynamicFilterPhysicalExpr`] used to reconstruct the expression during | ||
| /// serialization / deserialization. | ||
| pub struct DynamicFilterSnapshot { |
There was a problem hiding this comment.
I don't think this is necessary or makes things any simpler
There was a problem hiding this comment.
I see now that this is for atomicity. That's a good bug catch! But that sort of thing should be called out in the docstring, otherwise readers need to dig into implementations to understand why this module exists.
It's an unfortunate situation that all expressions / execution nodes have to make their internal details public to all callers just so that proto serialization can serialize them. I think we (DataFusion) should at least consider some hybrid where we split the proto crate into proto-models and proto so that anything can depend on proto-models (behind a feature flag?) and thus expressions can define their own serialization into prost structs. cc @alamb
All of this said: maybe it would be easier to just make Inner public and have an inner() -> Inner method that clones it? I would suggest if we do this we add warnings in docstrings to not assume this will always be public/stable, it's only for proto, etc.
There was a problem hiding this comment.
Added public methods and added warnings in the docstrings.
There was a problem hiding this comment.
Thank you. I opened #21835 to track the idea of splitting up proto ser to avoid leaking internal state all over
| /// Generate a new expression id for this filter. | ||
| pub fn new_expression_id() -> u64 { | ||
| random::<u64>() | ||
| } |
There was a problem hiding this comment.
Why is this a public function?
There was a problem hiding this comment.
Fixed. Made it private.
| // Was expr_id | ||
| reserved 30; |
There was a problem hiding this comment.
Why move this? For reasons previously discussed it might be helpful to have an expression id on arbitrary expressions, e.g. to deduplicate large lists or strings. Leaving it here also minimizes changes / breakage.
There was a problem hiding this comment.
If the PhysicalExpr has expression_id and the actual implementations like DynamicFilterPhysicalExpr store it, it felt natural to have expression_ids live in the implementor's proto (ie. PhysicalDynamicFilterNode) rather than here, one level up. This is more significant now since we don't dedupe every PhysicalExpr anymore. I thought "If only dynamic filters have this, then it can live in the dynamic filter proto".
It also avoids having to put expr_id: None everywhere.
I'll defer to your judgement but those were my thoughts.
|
|
||
| /// Internal serializer that adds expr_id to expressions. | ||
| /// Created fresh for each serialization operation. | ||
| struct DeduplicatingSerializer { |
There was a problem hiding this comment.
As per comment above I think we should keep expr_id on PhysicalExprNode. But I think the drop in complexity and the fact that this is added to the public PhysicalExpr trait justifies dropping this specialized serializer and instead having the default one call PhysicalExpr::expr_id() and put that into the proto.
| )), | ||
| }; | ||
| return Ok(protobuf::PhysicalExprNode { | ||
| expr_id: None, |
There was a problem hiding this comment.
Keeping expr_id will also reduce the diff.
| /// [`PhysicalExpr::with_new_children`]. | ||
| pub fn remapped_children(&self) -> Option<&[Arc<dyn PhysicalExpr>]> { | ||
| self.remapped_children.as_deref() | ||
| } |
There was a problem hiding this comment.
These two getters aren't horrible to keep around, so I didn't mark them as unstable. Lmk if you think we should add
/// **Warning:** intended only for `datafusion-proto` (de)serialization.
/// Not a stable API
There was a problem hiding this comment.
I think defaulting to at least marking things as private via comments is a good defensive approach. The situation with proto serialization is unfortunate, I'll see if I can do something about it...
| data_type: Arc::new(RwLock::new(None)), | ||
| nullable: Arc::new(RwLock::new(None)), | ||
| } | ||
| } |
There was a problem hiding this comment.
This method looks like a bit of a smell but it gets the job done.
There was a problem hiding this comment.
Yeah unfortunately I think this is once again just the shape the current proto stuff forces everything into.
adriangb
left a comment
There was a problem hiding this comment.
This is looking very nice @jayshrivastava 👍🏻
Lets leave this open for a day to see if anyone has thoughts on the public APIs being tweaked.
cc @stuhood iirc you mentioned you also plan to ser/de dynamic filters across process (but not node) boundaries
| /// If the implementation returns a [`PhysicalExpr::expression_id`], then | ||
| /// the identifier should be preserved by the new expression. |
| data_type: Arc::new(RwLock::new(None)), | ||
| nullable: Arc::new(RwLock::new(None)), | ||
| } | ||
| } |
There was a problem hiding this comment.
Yeah unfortunately I think this is once again just the shape the current proto stuff forces everything into.
|
@adriangb I've addressed the comments in the last commit. Let me know what you think! |
|
Oh TYFR! That was fast
No problem. I can merge some time next week 👍🏽 |
Informs: datafusion-contrib/datafusion-distributed#180 Closes: apache#20418 Consider you have a plan with a `HashJoinExec` and `DataSourceExec` ``` HashJoinExec(dynamic_filter_1 on a@0) (...left side of join) ProjectionExec(a := Column("a", source_index)) DataSourceExec ParquetSource(predicate = dynamic_filter_2) ``` You serialize the plan, deserialize it, and execute it. What should happen is that the dynamic filter should "work", meaning: 1. When you deserialize the plan, both the `HashJoinExec` and `DataSourceExec` should have pointers to the same `DynamicFilterPhysicalExpr` 2. The `DynamicFilterPhysicalExpr` should be updated during execution by the `HashJoinExec` and the `DataSourceExec` should filter out rows This does not happen today for a few reasons, a couple of which this PR aims to address 1. `DynamicFilterPhysicalExpr` is not survive round-tripping. The internal exprs get inlined (ex. it may be serialized as `Literal`) due to the `PhysicalExpr::snapshot()` API 2. Even if `DynamicFilterPhysicalExpr` survives round-tripping, the one pushed down to the `DataSourceExec` often has different children. In this case, you have two `DynamicFilterPhysicalExpr` which do not survive deduping, causing referential integrity to be lost. This PR aims to fix those problems by: 1. Removing the `snapshot()` call from the serialization process 2. Adding protos for `DynamicFilterPhysicalExpr` so it can be serialized and deserialized 3. Removing `Arc`-based deduplication. We now only dedupe on `expression_id` if the `PhysicalExpr` reports a `expression_id`. After this change, only `DynamicFilterPhysicalExpr` reports an `expression_id` to be deduped. 4. `expression_id` is now just a random u64. Since a given query likely only has a few `DynamicFilterPhysicalExpr` instances, the odds of a collision are very low 5. There's no need for a `DedupingSerializer` anymore since the `expression_id` is already stored in the dynamic filter proto itself Testing - adds tests which roundtrip dynamic filters and assert that referential integrity is maintained - removes tests that test `Arc`-based deduplication and session id rotation
586648b to
09c2d34
Compare
|
FYI I hit rebase |
| /// Unique identifier for this dynamic filter. | ||
| /// | ||
| /// Derived filters (ex. via `with_new_children`) should inherit the expression id of the source filter. | ||
| expression_id: u64, | ||
| /// The source of dynamic filters. | ||
| inner: Arc<RwLock<Inner>>, |
There was a problem hiding this comment.
It would be nice if it were clearer exactly how the Inner relates to the expression_id.
With (mostly) an outsider's perspective, it seems like the expression_id in this case should actually literally be an id for the inner state? So as with #21650 , it feels like this is more like a mutable_state_id, perhaps?
Because you might have two different DynamicFilterPhysicalExpr (different wrapping expressions) wrapped around the same inner state... and at that point, the expression_id is not an "id for the expression".
There was a problem hiding this comment.
Because you might have two different DynamicFilterPhysicalExpr (different wrapping expressions) wrapped around the same inner state... and at that point, the expression_id is not an "id for the expression".
I think the inner state is actually the real expression. The only thing that may change via wrapping is the children expressions. Ex. if you call reassign_expr_columns. When you update a dynamic filter, the new expression is written directly into Inner::expr
There was a problem hiding this comment.
With (mostly) an outsider's perspective, it seems like the expression_id in this case should actually literally be an id for the inner state? So as with #21650 , it feels like this is more like a mutable_state_id, perhaps?
We could use the Arc address of Inner, but it's a smell to rely on pointers. The approach in this PR is randomly generate an id for Inner and ensure that this ID propagates when new expressions are derived using the same Inner.
You make a good point though. Do you think it would be more clear if we put the expression_id inside the Inner struct? I feel like that's better
There was a problem hiding this comment.
Do you think it would be more clear if we put the expression_id inside the
Innerstruct? I feel like that's better
I agree that's cleaner. The con is that you need to acquire the lock just to read it. But maybe that's fine.
| pub is_complete: bool, | ||
| } | ||
|
|
||
| // TODO: Include expression_id in debug output. |
There was a problem hiding this comment.
Ideally even in the DisplayAs representation for plans, I would think. It's actually possibly the most important thing to have in the plan when rendering a dynamic filter?
There was a problem hiding this comment.
I wanted to keep this PR small, so I hid the expression_id to avoid 1 test that was failing. I have code ready to address this TODO which I plan to publish as a next step after this PR is merged. This PR is just an incremental step.
// See https://github.com/apache/datafusion/issues/20418. Currently, plan nodes
// like `HashJoinExec`, `AggregateExec`, `SortExec` do not serialize their
// dynamic filter. This causes round trips to fail on the `expression_id`
// because it is regenerated on deserialization.
| /// Rebuild a `DynamicFilterPhysicalExpr` from its stored parts. Used by | ||
| /// proto deserialization to preserve `expression_id` across a roundtrip | ||
| /// rather than minting a fresh one. | ||
| /// | ||
| /// **Warning:** intended only for `datafusion-proto` (de)serialization. | ||
| /// Not a stable API. |
There was a problem hiding this comment.
Could this be generic via the shared state proposal? Not sure what is up with that. Ditto fn inner.
There was a problem hiding this comment.
The shared state discussion seems promising! I'm hoping it will be an easy migration. I imagine that we would end up storing all Inners in the TaskContext instead of in the actual DynamicFilterPhysicalExpr and looking up the Inners via the expression_id. It's still an ongoing discussion though so I can't be sure how the migration will look.
There was a problem hiding this comment.
Yep that would make sense to me
| itertools = { workspace = true, features = ["use_std"] } | ||
| parking_lot = { workspace = true } | ||
| petgraph = "0.8.3" | ||
| rand = { workspace = true } |
There was a problem hiding this comment.
Not sure what the conventions are, but: introducing random order to the physical-expr crate seems like a big deal? Is this being used to generate something that could be deterministic from some sort of context instead?
There was a problem hiding this comment.
This is a good question! I was thinking about a few alternatives 😄
- We could hash all the exprs in the
DynamicFilterPhysicalExprto get an id. However, this wouldn't solve the sharedInnerstate linking problem. We don't want this type of identifier - we want an identifier for the inner state specifically. - We could use the Arc address of the Inner struct, but it's a bit of a smell to rely on pointer addresses - for example, IDs derived from Arc pointers are only valid until the Arc is dropped. This is what the old code used and something I used as well in initial versions of this PR
A rand u64 is not bad. Realistically, we just need to not have a collision between distinct dynamic filters in a plan. I figure that the probability of more than 2 or 3 distinct dynamic filters in a query must be very low already. And the probability of 2 or 3 rand u64s colliding is negligible.
Lmk what you think!
There was a problem hiding this comment.
I'm less worried about collisions, and more worried about non-determinism (causing flaky tests, different plans to be generated randomly, etc). The other annoying aspect of random ids is that they are huge. If you go ahead and actually render this everywhere, a random ID is going to take up a lot more space than one generated on a context (...starting from 0, etc).
There was a problem hiding this comment.
Right now the ids are also random (generated from a mashup of arc pointer address, process id, etc.). So in that sense it's no better or worse. But I do agree something deterministically generated from context would be better.
The only alternative that occurs to me is a process level atomic. Not sure if that might cause some locking, etc. Do you have any other suggestions?
Lastly: we can always change this. As long as there is no API contract on what this number is going to be we could replace it at any point if it becomes a problem.
Co-authored-by: Stu Hood <stuhood@gmail.com>
Which issue does this PR close?
Informs: datafusion-contrib/datafusion-distributed#180
Closes: #20418
Rationale for this change
Consider you have a plan with a
HashJoinExecandDataSourceExecYou serialize the plan, deserialize it, and execute it. What should happen is that the dynamic filter should "work", meaning:
HashJoinExecandDataSourceExecshould have pointers to the sameDynamicFilterPhysicalExprDynamicFilterPhysicalExprshould be updated during execution by theHashJoinExecand theDataSourceExecshould filter out rowsThis does not happen today for a few reasons, a couple of which this PR aims to address
DynamicFilterPhysicalExpris not survive round-tripping. The internal exprs get inlined (ex. it may be serialized asLiteral) due to thePhysicalExpr::snapshot()APIDynamicFilterPhysicalExprsurvives round-tripping, the one pushed down to theDataSourceExecoften has different children. In this case, you have twoDynamicFilterPhysicalExprwhichdo not survive deduping, causing referential integrity to be lost.
What changes are included in this PR?
This PR aims to fix those problems by:
snapshot()call from the serialization processDynamicFilterPhysicalExprso it can be serialized and deserializedArc-based deduplication. We now only dedupe onexpression_idif thePhysicalExprreports aexpression_id.After this change, only
DynamicFilterPhysicalExprreports anexpression_idto be deduped.
expression_idis now just a random u64. Since a given query likelyonly has a few
DynamicFilterPhysicalExprinstances, the odds of acollision are very low
DedupingSerializeranymore since theexpression_idis already stored in the dynamic filter proto itselfFuture work:
HashJoinExec,AggregateExecandSortExecPhysicalExtensionCodectrait so implementors can utilize deduping logicAre these changes tested?
integrity is maintained
Arc-based deduplication and session idrotation since we don't support that anymore
Are there any user-facing changes?
snapshot()onPhysicalExprduring serialization anymore. This means thatDynamicFilterPhysicalExprare now serialized and deserialized without snapshotting.PhysicalExprare not deduped anymore. OnlyDynamicFilterPhysicalExpris