You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a leading COALESCE argument is known non-null from the input's
NotNullCols, later arguments aren't needed. Uses NotNullCols to trim
leading COALESCE args in Project and Select.
Example: `SELECT COALESCE(i, j) FROM ij` with `i NOT NULL` no longer
keeps the coalesce in the plan.
Implementation details:
- Add SimplifyCoalesceProject and SimplifyCoalesceSelect norm rules that
call SimplifyCoalesceInScalar on each projection/filter using the
input's not-null columns.
- Introduce scalarContainsSimplifiableCoalesce (in scalar_funcs.go) as
the match guard; it walks the full scalar tree so that nested Coalesce
expressions like COALESCE(a, COALESCE(b, c)) are not missed when only
the inner Coalesce is simplifiable.
- CanSimplifyCoalesce mirrors simplifyCoalesce's logic with a direct
scan, avoiding expression construction on the hot path. The final
return is true (not false) because when the loop exhausts all-null
constant args, simplifyCoalesce returns args[last] unwrapped, which
is a genuine simplification.
- SimplifyCoalesceInScalar recurses into the simplified result via
c.f.Replace so nested Coalesces are handled in a single pass.
- The private simplifyCoalesce helper iterates over all args (including
the last) so ExprIsNeverNull can fire at any position.
Test plan: `bazel test //pkg/sql/opt/norm:norm_test --test_filter=TestNormRules`
Fixes#103596
Release note: None
0 commit comments