viirya opened a new pull request, #8631:
URL: https://github.com/apache/arrow-datafusion/pull/8631
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases. You can
link an issue to this PR using the GitHub syntax. For example `Closes #123`
indicates that this PR will close issue #123.
-->
Closes #8492.
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in
the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your
changes and offer better suggestions for fixes.
-->
`regexp_match` scalar expression is quite slow in DataFusion now. Using
`https://github.com/pypi-data/data/releases` data to do test on M1 Mac laptop:
```
DataFusion CLI v34.0.0
❯ SELECT COUNT(*) FROM '../index-0.parquet' WHERE
ARRAY_LENGTH(
REGEXP_MATCH(path,
'\\.(asm|c|cc|cpp|cxx|h|hpp|rs|[Ff][0-9]{0,2}(?:or)?|go)$')
) > 0;
+----------+
| COUNT(*) |
+----------+
| 5834398 |
+----------+
1 row in set. Query took 829.932 seconds.
```
After this change:
```
DataFusion CLI v34.0.0
❯ SELECT COUNT(*) FROM '../index-0.parquet' WHERE
ARRAY_LENGTH(
REGEXP_MATCH(path,
'\\.(asm|c|cc|cpp|cxx|h|hpp|rs|[Ff][0-9]{0,2}(?:or)?|go)$')
) > 0;
+----------+
| COUNT(*) |
+----------+
| 5834398 |
+----------+
1 row in set. Query took 33.935 seconds.
```
It is not too much related to padding constant regex to an array and
recompiling regex per row as discussed in
(https://github.com/apache/arrow-datafusion/issues/8492). Because arrow-rs
`regexp_match` kernel internally maintains a hash map on duplicate regex
patterns, so same regex pattern is only compiled once per batch. To verify it,
I also did a factoring to use single regex if the regex is a constant, and the
result confirms that because the performance didn't get better.
After playing it around, the bad performance is due to cloning `Regex` per
row. `Regex` has a `CachePool`. Cloning `Regex` will create a fresh `CachePool`
and it is somehow expensive to re-creating cache space per row instead of
reusing the cache.
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is
sometimes worth providing a summary of the individual changes in this PR.
-->
Removing unnecessary and expensive cloning on `Regex`.
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are
they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]