[
https://issues.apache.org/jira/browse/CALCITE-6169?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17798762#comment-17798762
]
Mihai Budiu commented on CALCITE-6169:
--------------------------------------
The bug can be localized to EnumUtils.convert which implements casts as follows:
{code}
if (toPrimitive != null) {
if (fromPrimitive != null) {
// E.g. from "float" to "double"
return Expressions.convert_(
operand, toPrimitive.getPrimitiveClass());
}
{code}
This generates a ExpressionType.Convert expression, which has the following
JavaDoc:
{code}
/**
* A cast or conversion operation, such as {@code (SampleType) obj} in
* Java. For a numeric
* conversion, if the converted value is too large for the
* destination type, no exception is thrown.
*/
Convert(null, false, 2, true),
{code}
The corresponding ConvertChecked expression exists but does not really seem to
be implemented yet.
> EnumUtils.convert does not implement the correct SQL cast semantics
> -------------------------------------------------------------------
>
> Key: CALCITE-6169
> URL: https://issues.apache.org/jira/browse/CALCITE-6169
> Project: Calcite
> Issue Type: Bug
> Components: core
> Affects Versions: 1.36.0
> Reporter: Mihai Budiu
> Assignee: Mihai Budiu
> Priority: Major
>
> The code generated by EnumUtil for casts uses the Java semantics, where casts
> just truncate values. The SQL semantics requires a runtime exception if the
> value does not fit in the target datatype.
> Some of the bugs in this code generator are masked by the BlockBuilder
> optimizer which attempts to optimize the generated code using the SQL
> semantics... But if the code is not optimized and is executed in Java it
> generates wrong results for most conversions that overflow.
> One additional wrinkle is that it is not entirely clear whether the bug is in
> EnumUtils or in the code that invokes EnumUtils. It is not specified which of
> the two semantics for casts EnumUtils are supposed to implement: Java or SQL?
--
This message was sent by Atlassian Jira
(v8.20.10#820010)