Repository navigation
perf: preserve dictionary encoding through regexp_replace #26186
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -138,6 +138,41 @@ SELECT regexp_replace(arrow_cast('foobar', 'Dictionary(Int32, Utf8)'), 'bar', 'x | |
| ---- | ||
| fooxx | ||
|
|
||
| statement ok | ||
| CREATE TABLE regexp_replace_dict AS | ||
| SELECT arrow_cast(column1, 'Dictionary(Int32, Utf8)') AS url | ||
| FROM (VALUES | ||
| ('https://www.example.com/path'), | ||
| ('http://test.org/foo/bar'), | ||
| ('https://example.com/') | ||
| ) AS t(column1); | ||
|
|
||
| query T | ||
| SELECT DISTINCT arrow_typeof(regexp_replace(url, '^https?://(?:www\.)?([^/]+)/.*$', '\1')) | ||
| FROM regexp_replace_dict; | ||
| ---- | ||
| Dictionary(Int32, Utf8) | ||
|
|
||
| statement ok | ||
| DROP TABLE regexp_replace_dict; | ||
|
|
||
| statement ok | ||
| CREATE TABLE regexp_replace_dict_runtime_scalars AS | ||
| SELECT arrow_cast(column1, 'Dictionary(Int32, Utf8)') AS s, | ||
| column2 AS p | ||
| FROM (VALUES ('abc', 'a'), ('abd', 'b')) AS v; | ||
|
|
||
| query T | ||
| SELECT regexp_replace(s, 'a', arrow_typeof(p)) | ||
| FROM regexp_replace_dict_runtime_scalars | ||
| ORDER BY p; | ||
| ---- | ||
| Utf8bc | ||
| Utf8bd | ||
|
|
||
| statement ok | ||
| DROP TABLE regexp_replace_dict_runtime_scalars; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a few cases for the other dictionary value types and the non-literal fallback? CREATE TABLE t AS SELECT
arrow_cast(column1, 'Dictionary(Int32, LargeUtf8)') AS dl,
arrow_cast(column1, 'Dictionary(Int32, Utf8View)') AS dv,
arrow_cast(column1, 'Dictionary(Int32, Binary)') AS db,
arrow_cast(column1, 'Dictionary(Int32, Utf8)') AS d8,
column1 AS s
FROM (VALUES ('abc'), ('abd'), (NULL)) AS v;
-- expect Dictionary(Int32, LargeUtf8) / Dictionary(Int32, Utf8View) / Dictionary(Int32, Utf8)
SELECT arrow_typeof(regexp_replace(dl, 'a', 'x')), regexp_replace(dl, 'a', 'x') FROM t;
SELECT arrow_typeof(regexp_replace(dv, 'a', 'x')), regexp_replace(dv, 'a', 'x') FROM t;
SELECT arrow_typeof(regexp_replace(db, 'a', 'x')), regexp_replace(db, 'a', 'x') FROM t;
-- non-literal pattern: expect Utf8
SELECT arrow_typeof(regexp_replace(d8, s, 'x')), regexp_replace(d8, s, 'x') FROM t; |
||
|
|
||
| query TTT | ||
| select | ||
| regexp_replace(col, NULL, 'c'), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This runs the regex over every value in the dictionary, including values no row references. After a filter, or with a Parquet dictionary shared across batches, that can be much larger than the batch. In a same-build microbench (URL-shaped values, the Q28 pattern) I measured 5.3x slower than the old cast path at 50k values / 8192 rows, and 34x slower at 50k values / 1000 rows.
Compacting the dictionary first fixes it. With this change every case I tried was ≤1.01x the old path, and the small-dictionary win stays:
if preserve_dictionary { - let dict = array.as_any_dictionary(); + // A filtered batch or a shared Parquet dictionary can hold far more + // values than rows; drop unreferenced values so the regex runs at + // most once per row. + let compacted; + let mut dict = array.as_any_dictionary(); + if dict.values().len() > dict.keys().len() { + compacted = garbage_collect_any_dictionary(dict)?; + dict = compacted.as_any_dictionary(); + }with
use arrow_select::dictionary::garbage_collect_any_dictionary;andarrow-select = { workspace = true }added todatafusion/functions/Cargo.toml.This is the same cost issue as the
substrfast path in #25874, and #26696 discusses handling it once for all functions that map over dictionary values, so this guard may later move into a shared helper.