fix: allow implicit cast of numbers literals to decimals on insert/select #6005
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.
Description
Fixes #5285
Allows implicitly casting of numeric literals to decimals when executing an
INSERT/SELECT
statement. A conversion from one literal type to another is done in theSelectionUtil.buildSelectExpressions()
method, which uses theImplicitlyCastResolver
to cast a supported type if necessary.A conversion of column schema is used Instead of just allowing the schema during the validation in the
EngineExecutor.validateExistingSink()
. The reason is that SchemaRegistry/AVRO does not allow some type of conversions, like INT/BIGINT -> DECIMAL as they are incompatible.The validation in the
EngineExecutor.validateExistingSink()`` is required only for DECIMAL -> DECIMAL of different precision and scale.
ImplicitlyCastResolver` can only convert to a different scale, but precision is based on the literal number. Without this, the validation will fail due to a different precision.Implicitly cast is supported on the following literal types:
When casting any number to DECIMAL, the
ImplicitlyCastResolver
makes sure that the precision/scale of the number is compatible with the target decimal precision/scale. TheDecimalUtil.canImplicitlyCast()
is used to check that.The reason to not allow non-literals numbers is because INT,BIGINT,DOUBLE numbers found in the topic records might have different precision than the one defined in the target decimal: i.e.
DOUBLE -> DECIMAL(1,0)
.Other implicitly conversions, such as INT -> BIGINT,DOUBLE, BIGINT -> DOUBLE are not possible yet as there's another refactor to do to support that. Turns out that when the INT -> BIGINT is allowed by this PR, then when the node reads the statement text from the command topic and parses it, it creates an IntegerLiteral instead of the desired LongLiteral. More refactoring is needed to support that, but this PR is fixing only #5285 for now.
Testing done
Reviewer checklist