Skip to content
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

[Epic] Implement support for StringView in DataFusion #10918

Closed
14 of 16 tasks
alamb opened this issue Jun 14, 2024 · 12 comments
Closed
14 of 16 tasks

[Epic] Implement support for StringView in DataFusion #10918

alamb opened this issue Jun 14, 2024 · 12 comments
Labels
enhancement New feature or request

Comments

@alamb
Copy link
Contributor

alamb commented Jun 14, 2024

Is your feature request related to a problem or challenge?

StringView / BinaryView were added to the Arrow format that make it more suitable for certain types of operations on strings. Specifically when filtering with string data, creating the output StringArray requires copying the strings to a new, packed binary buffer.

GenericByteViewArrary was designed to solve this limitation and the arrow-rs implementation, tracked by apache/arrow-rs#5374, is now complete enough to start adding support into DataFusion

I think we can improve performance in certain cases by using StringView (this is also described in more details in the Pola.rs blog post)

  1. Reading strings / binary from Parquet files as StringViewArray/BinaryViewArray rather than StringArray / BinaryArray saves a copy (and @ariesdevil is quite close to having it integrated into the parquet reader Implement StringViewArray and BinaryViewArray reading/writing in parquet arrow-rs#5530)
  2. Evaluating predicates on string expressions (for example substr(url, 4) = 'http') as the intermediate result of substr can be called without copying string values

Describe the solution you'd like

I would like to support StringView / BinaryView support in DataFusion.

While my primary usecase is for reading data from parquet, I think teaching DataFusion to use StringView (at least as intermediate values when evaluating expressions may help significantly

Development branch: string-view

Since this feature requires upstream arrow-rs support apache/arrow-rs#5374 that is not yet released we plan to do development on a string-view feature branch:

https://github.com/apache/datafusion/tree/string-view

Task List

Here are some high level tasks (I can help flesh these out for anyone who is interested in helping)

Describe alternatives you've considered

No response

Additional context

Polars implemented it recently in rust so that can serve as a motivation
Blog Post https://pola.rs/posts/polars-string-type/
https://twitter.com/RitchieVink/status/1749466861069115790

Facebook/Velox's take: https://engineering.fb.com/2024/02/20/developer-tools/velox-apache-arrow-15-composable-data-management/

Related PRs:
pola-rs/polars#13748
pola-rs/polars#13839
pola-rs/polars#13489

@alamb alamb added the enhancement New feature or request label Jun 14, 2024
@alamb
Copy link
Contributor Author

alamb commented Jun 14, 2024

I think we should aim for a first "milestone" of showing improvements for some clickbench queries

@jayzhan211
Copy link
Contributor

jayzhan211 commented Jun 19, 2024

Will we completely change StringArray to StringViewArray in Datafusion?
While I try to utilize StringViewArray in #10976 , I found there is schema mismatched issue UTF8 vs UTF8View. To avoid converting StringViewArray to StringArray, we might need to change the schema to UTF8View overall from logical plan to physical plan. If we need to keep both String and StringView, then we need to think about how to deal with the conversion between these two types.

A more concrete example is

statement ok
create table t(a int, b varchar, c int) as values (1, 'a', 3), (2, 'c', 1), (1, 'c', 2), (1, 'a', 4);

We have the string column b as StringArray and DataType::Utf8 now. Should we convert it to StringViewArray and DataType::Utf8View?

If not, if we somehow want to utilize StringViewArray, how do we minize the cost of conversion between String and StringView?

It seems Polars completely refactor their String to StringView 🤔

@alamb
Copy link
Contributor Author

alamb commented Jun 19, 2024

Will we completely change StringArray to StringViewArray in Datafusion?

I think since they are two separate types in Arrow we couldn't fully switch to StringView the way polars could as it controls the whole stack. Users could still feed DataFusion StringViewArray from custom TableProviders and would expect StringView at the output.

However what I think we could do is internally to DataFusion (e.g. within the plan, before the final output) is use StringView in the batches that flow through intermediate nodes in the plan.

I found there is schema mismatched issue UTF8 vs UTF8View. To avoid converting StringViewArray to StringArray, we might need to change the schema to UTF8View

Indeed, As you point out, I don't think we can transparently switch to using StringView -- instead we would have to start encoding information in the plans about the new types.

I wonder if we could have a new logical optimzier pass that tried to annotate operations that support it to use StringView in their schema rather than String. Then the ExecutionPlans would know if they were supposed to generate StringView as output or the more traditional StringArray 🤔

Here is an idea of one place to start: #9403 (comment)

@alamb
Copy link
Contributor Author

alamb commented Jun 19, 2024

I think @XiangpengHao is looking into another place to use StringView which is #10921 -- where we have a similar idea to use StringView in some sub portion of the plan. Here is more info about the optimizer pass idea: #10921 (comment)

@alamb
Copy link
Contributor Author

alamb commented Jun 20, 2024

I think this project is going pretty well :bowtie:

We are at the point of starting to implement some basic functions using StringView.

@alamb
Copy link
Contributor Author

alamb commented Jul 9, 2024

Now that we have upgraded to arrow 52.1.0, I think we could merge the string-view branch to main. I'll try and make a PR if no one beats me to it

This was referenced Aug 5, 2024
@alamb
Copy link
Contributor Author

alamb commented Aug 16, 2024

In case anyone wants an overview of adding StringView to DataFusion, here is a presentation and slides from @XiangpengHao
Video: https://www.youtube.com/watch?v=RVLshX6fbds
Slides: https://drive.google.com/file/d/1Qqd8V6cfS9rSQ_-JrinasQJwI79qlUEV/view?usp=drive_link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants