druid icon indicating copy to clipboard operation
druid copied to clipboard

SQL: Update CaseToCoalesceRule to ignore irrelevant casts.

Open gianm opened this issue 4 months ago • 1 comments

Previously, CaseToCoalesceRule ignored nullability casts when deciding if "x" and "y" in "CASE WHEN x IS NOT NULL THEN y" match and could therefore form the basis of a COALESCE. This was too strict, since it considered a cast from VARCHAR to VARCHAR(2000), which can happen after applying JSON_VALUE, to be relevant. It is not relevant, because it does not affect how we execute the query.

This patch updates the logic to ignore nullability casts and also casts that change irrelevant properties of the type. ("Irrelevant" being defined as "properties that do not affect how the query executes".)

This change speeds up queries, because generally COALESCE runs faster than CASE. Note that one of the test cases is now vectorizable, where it wasn't previously. The new tests are also both vectorizable.

gianm avatar Aug 13 '25 22:08 gianm

This pull request has been marked as stale due to 60 days of inactivity. It will be closed in 4 weeks if no further activity occurs. If you think that's incorrect or this pull request should instead be reviewed, please simply write any comment. Even if closed, you can still revive the PR at any time or discuss it on the [email protected] list. Thank you for your contributions.

github-actions[bot] avatar Oct 14 '25 00:10 github-actions[bot]