Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
Unwrap casts in comparison expressions #731
This change allows the engine to infer that, for instance, given x::smallint
can be rewritten as
It generalizes to every comparison operator. It can also infer that:
can only return null or false.
It's implemented as a generic rule that can run and apply at arbitrary points
Unlike DomainTranslator, it doesn't require the SATURATED_FLOOR_CAST operator,
Given the following tables:
And the following query:
The plan before this change is:
And after this change:
@findepi, I've realized that since the semantics of CAST with respect to truncation or rounding are not well-defined, we can't rely on the roundtrip logic I have in this PR. Hence, that's why we have the "floor cast" operation, which is a CAST with guaranteed truncation semantics (in addition to saturation if the value is outside the range). I'll adjust the logic to use "saturated floor cast", instead.
If we had both a floor_cast and a ceiling_cast, or a next_value/previous_value, we wouldn't need types to describe their range. Unfortunately, with just floor_cast, it's impossible to distinguish between a value saturating at the upper end vs the literal not having a representation in the source type.