Implement monotonicity for ScalarUDF#8799
Merged
alamb merged 2 commits intoapache:mainfrom Jan 11, 2024
Merged
Conversation
7 tasks
alamb
reviewed
Jan 9, 2024
Contributor
alamb
left a comment
There was a problem hiding this comment.
I agree with @comphead 's request for a test
I was thinking about how we could write a test for this feature.
The easiest way would probably be a unit test using create_physical_expr with a scalarUDF that shows the correct monotonicity.
A more end to end style one would be to show different explains depending on the monotonicity of a udf. For example, abs is not monotonic and sqrt is and thus they generate subtlely different plans:
For the abs (non- montonic) the sort is done after the Projection
For sqrt (monotonic) the sort is pushed below the Projection
❯ create table t as values (1), (3), (2);
0 rows in set. Query took 0.003 seconds.
❯ explain select abs(column1) as x from (SELECT * FROM t ORDER BY column1) ORDER BY x;
+---------------+---------------------------------------------------+
| plan_type | plan |
+---------------+---------------------------------------------------+
| logical_plan | Sort: x ASC NULLS LAST |
| | Projection: abs(t.column1) AS x |
| | Sort: t.column1 ASC NULLS LAST |
| | TableScan: t projection=[column1] |
| physical_plan | SortExec: expr=[x@0 ASC NULLS LAST] |
| | ProjectionExec: expr=[abs(column1@0) as x] |
| | MemoryExec: partitions=1, partition_sizes=[1] |
| | |
+---------------+---------------------------------------------------+
2 rows in set. Query took 0.004 seconds.
❯ explain select sqrt(column1) as x from (SELECT * FROM t ORDER BY column1) ORDER BY x;
+---------------+--------------------------------------------------------------+
| plan_type | plan |
+---------------+--------------------------------------------------------------+
| logical_plan | Sort: x ASC NULLS LAST |
| | Projection: sqrt(CAST(t.column1 AS Float64)) AS x |
| | Sort: t.column1 ASC NULLS LAST |
| | TableScan: t projection=[column1] |
| physical_plan | ProjectionExec: expr=[sqrt(CAST(column1@0 AS Float64)) as x] |
| | SortExec: expr=[column1@0 ASC NULLS LAST] |
| | MemoryExec: partitions=1, partition_sizes=[1] |
| | |
+---------------+--------------------------------------------------------------+
2 rows in set. Query took 0.003 seconds.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Closes #8756 .
Rationale for this change
What changes are included in this PR?
add monotonicity function, User can define fun monotonicity by themselves