You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2024/11/15 20:38:00 UTC
[I] Support for "Schema evolution" / Schema Adapters [arrow-rs]
alamb opened a new issue, #6735:
URL: https://github.com/apache/arrow-rs/issues/6735
**Is your feature request related to a problem or challenge? Please describe what you are trying to do.**
Sometimes it is desired to convert RecordBatches from one schema so they match another. This is often done when data is stored in several different sources (like parquet files) that are "compatible" but not exactly the same (e.g. maybe newer files have new columns)
Common transformations are:
- Reorder columns by name (so a file with `(a int, b char)` and one with `(b char, a int)` could be read as a single stream
- Insert missing columns (so a file with `(a int, b char)` and a file with `(a int)` could be merged
It is also common to want to fill in missing columns with either `Null` or some constant (e.g. `0`) so a controllable policy would be nice
Note that these these usecases are pretty similar to casting Structs (e.g. reordering fields with the same name but different position)
- https://github.com/apache/arrow-rs/issues/4908
Often computing the transformation may be non trivial (e.g. matching columns by name) so it would be nice to do the mapping calculation once per schema rather than once per batch / StructArrayschema. For example DF's SchemaAdapter computes the mapping once and can then apply that to multiple batches.
**Describe the solution you'd like**
Add some API in Arrow-rs to do this mapping
One alternative, suggested by @tustvold would be to add a first-party schema adapter into arrow-rs.
**Describe alternatives you've considered**
For anyone interested, here is the API that is in DataFusion (it now even has ASCII art and Examples, thanks to @itsjunetime and myself):
- https://docs.rs/datafusion/latest/datafusion/datasource/schema_adapter/struct.DefaultSchemaAdapterFactory.html
<img width="834" alt="Screenshot 2024-11-13 at 6 57 28 AM" src="https://github.com/user-attachments/assets/de010fae-519a-4844-bff9-32e3770d7e69">
We can/should probably change the names and reduce the levels of indirection of we upstreamed this into arrow-rs
**Additional context**
<!--
Add any other context or screenshots about the feature request here.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Support for "Schema evolution" / Schema Adapters [arrow-rs]
Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on issue #6735:
URL: https://github.com/apache/arrow-rs/issues/6735#issuecomment-3029106131
> > > Often computing the transformation may be non trivial (e.g. matching columns by name) so it would be nice to do the mapping calculation once per schema rather than once per batch / StructArrayschema. For example DF's SchemaAdapter computes the mapping once and can then apply that to multiple batches.
> >
> >
> > I'm tot sure how this would happen in practice: there's no state in `UDFs` in DataFusion. So if we e.g. wanted to implement `cast(...)` in terms of a SchemaAdapter we have nowhere to store the pre-computed value. I think we'd have to introduce some sort of build step that goes around the expression tree and optimizes expressions for the given input / output schemas.
>
> I wonder if we could use the new snapshot machinery 🤔
>
> https://docs.rs/datafusion/latest/datafusion/physical_expr/trait.PhysicalExpr.html#method.snapshot
`snapshot` doesn't know anything about it's input data types. I did propose something similar in https://github.com/apache/datafusion/pull/15057#issuecomment-2800002196 which is actually part of what lead to the idea of `PhysicalExpr::snapshot`.
I think it could be a win in performance to "specialize" the whole expression tree to the work it's actually going to do but it will likely depend on the balance of upfront cost vs. execution gains, which would require benchmarks, etc.
My intuition is that for now we could start by doing something naive: build a SchemaAdapter every time CastExpr::evaluate` gets called and then throw it away. I think that will be more expensive than the current cast kernel but maybe not by much. If that is too expensive then we can think of some system to specialize PhysicalExprs.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Support for "Schema evolution" / Schema Adapters [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #6735:
URL: https://github.com/apache/arrow-rs/issues/6735#issuecomment-3141219708
Since the parquet type system and arrow type system are different, it makes sense for the parquet reader in arrow-rs to read data out as one of the Arrow types that corresponds to the parquet physical types, depending on what the user specifies (what the crate does today)
This makes sense to do in the parquet reader when there can be specialized code for the different target arryw types (e.g. `Utf8View`)
I think any other type of data conversion should be done outside of the parquet crate (via the arrow cast kernel for example)
Especially for (3) and (4) in my mind those are query engine concerns, and as @adriangb has been discovering it is often more efficient to rewrite the expression in terms of the target schema
For example, if the file has no column named `col`, it is liket faster to rewrite a predicate like `col = 5` into `NULL = 5` rather than add a constant NULL array and then evaluate `<NULL> = 5` on it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Support for "Schema evolution" / Schema Adapters [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #6735:
URL: https://github.com/apache/arrow-rs/issues/6735#issuecomment-3029094259
> > Often computing the transformation may be non trivial (e.g. matching columns by name) so it would be nice to do the mapping calculation once per schema rather than once per batch / StructArrayschema. For example DF's SchemaAdapter computes the mapping once and can then apply that to multiple batches.
>
> I'm tot sure how this would happen in practice: there's no state in `UDFs` in DataFusion. So if we e.g. wanted to implement `cast(...)` in terms of a SchemaAdapter we have nowhere to store the pre-computed value. I think we'd have to introduce some sort of build step that goes around the expression tree and optimizes expressions for the given input / output schemas.
I wonder if we could use the new snapshot machinery 🤔
https://docs.rs/datafusion/latest/datafusion/physical_expr/trait.PhysicalExpr.html#method.snapshot
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Support for "Schema evolution" / Schema Adapters [arrow-rs]
Posted by "adragomir (via GitHub)" <gi...@apache.org>.
adragomir commented on issue #6735:
URL: https://github.com/apache/arrow-rs/issues/6735#issuecomment-3206524039
@alamb @tustvold
I want to take a stab at this, and a couple of notes. Our usecase is one of very deeply nested schemas (~10 top level columns expanding into ~900 actual columns arrays. Think map of struct with list with structs etc), which we want to cast to subsets of the initial schema
1. We have a variant of this in arrow cast, and another one in datafusion
2. It seems to be that the code really "wants" to be in the arrow-cast functionality. We can do it in a module in something related to arrow record batch, like `RecordBatchAdapter` or something, but I haven't figured out how to NOT have to duplicate something like the recursive functions in the cast module, with matches based on different data types: * Arrow patch, for arrays, not record batches: https://github.com/hstack/arrow-rs/commit/208bc22ebdd9221a61970166ef4b018b6efd39ea#diff-cf9e72add2db905b7d49d7775b8a362951b2a774e6ed1d13c04d18e0fe09320eR1027-R1075
* The patch adds a couple of settings in the struct `allow_pruning`, and `allow_empty` - which basically implements #6726, and other possible issues related to schema
* Datafusion patch, which could be extracted to arrow-rs: https://github.com/hstack/datafusion/commit/5704c918114b8466907076d94df0548d62dad3fd
* Probably the most relevant part is the `deep.rs`, where we need to duplicate the cast functionality to be able to handle the recurrence etc.
If I understand correctly, would you suggest having some new functionality near arrow recordbatch, something like `RecordBatchCast` or RecordBatchAdapter, and having that functionality separate, even if it would mean duplicating in spirit some code from the cast module ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Support for "Schema evolution" / Schema Adapters [arrow-rs]
Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on issue #6735:
URL: https://github.com/apache/arrow-rs/issues/6735#issuecomment-3138655493
I noticed that in parquet internal there're multiple kinds of evolution:
1. Without any real cast. This happens on type changes (1) Like `Utf8 -> Utf8View`, `List -> ListView` they're store same in parquet (2) Int8/Int16 -> Int32. Parquet stores at least int32 type.
2. Need some casting? Similiar to (1) but might need some explicit cast. ( Schema Adapters)
3. New default / Null column
4. New projection/Expression added. Which might need some expression added. In fact a reader can expose "NULL"
I think (3)(1) might able to push in a layer in parquet/arrow, but (2)(4) might need others ( Maybe iceberg-rust or datafusion can be a good example?)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Support for "Schema evolution" / Schema Adapters [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #6735:
URL: https://github.com/apache/arrow-rs/issues/6735#issuecomment-3032834711
I think starting simple and adding optimization guided by profiling sounds like a great idea
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Support for "Schema evolution" / Schema Adapters [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #6735:
URL: https://github.com/apache/arrow-rs/issues/6735#issuecomment-2483167056
Possibly a duplicate of
- https://github.com/apache/arrow-rs/issues/5996
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Support for "Schema evolution" / Schema Adapters [arrow-rs]
Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on issue #6735:
URL: https://github.com/apache/arrow-rs/issues/6735#issuecomment-3026363292
> Often computing the transformation may be non trivial (e.g. matching columns by name) so it would be nice to do the mapping calculation once per schema rather than once per batch / StructArrayschema. For example DF's SchemaAdapter computes the mapping once and can then apply that to multiple batches.
I'm tot sure how this would happen in practice: there's no state in `UDFs` in DataFusion. So if we e.g. wanted to implement `cast(...)` in terms of a SchemaAdapter we have nowhere to store the pre-computed value. I think we'd have to introduce some sort of build step that goes around the expression tree and optimizes expressions for the given input / output schemas.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org