Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-1703] Functions on TIMESTAMP column throws ClassCastException #971

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -636,13 +636,13 @@ public Expression fieldReference(

public Expression fieldReference(
Expression expression, int field, Type storageType) {
Type fieldType;
Class fieldType;
if (storageType == null) {
storageType = fieldClass(field);
fieldType = null;
} else {
fieldType = fieldClass(field);
if (fieldType != java.sql.Date.class) {
if (!java.util.Date.class.isAssignableFrom(fieldType)) {
fieldType = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,18 @@ private static RelDataType toSql(RelDataTypeFactory typeFactory,
if (type instanceof RelDataTypeFactoryImpl.JavaType) {
final SqlTypeName typeName = type.getSqlTypeName();
if (typeName != null && typeName != SqlTypeName.OTHER) {

final RelDataType sqlType;
if (typeName.allowsScale()) {
sqlType = typeFactory.createSqlType(typeName, type.getPrecision(), type.getScale());
} else if (typeName.allowsPrecNoScale()) {
sqlType = typeFactory.createSqlType(typeName, type.getPrecision());
} else {
sqlType = typeFactory.createSqlType(typeName);
}

return typeFactory.createTypeWithNullability(
typeFactory.createSqlType(typeName),
sqlType,
type.isNullable());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,17 +535,17 @@ Expression translateCast(
}
break;
case TIMESTAMP:
int targetScale = targetType.getScale();
if (targetScale == RelDataType.SCALE_NOT_SPECIFIED) {
targetScale = 0;
int targetTimestampPrecision = targetType.getPrecision();
if (targetTimestampPrecision == RelDataType.PRECISION_NOT_SPECIFIED) {
targetTimestampPrecision = 0;
}
if (targetScale < sourceType.getScale()) {
if (targetTimestampPrecision < sourceType.getPrecision()) {
convert =
Expressions.call(
BuiltInMethod.ROUND_LONG.method,
convert,
Expressions.constant(
(long) Math.pow(10, 3 - targetScale)));
(long) Math.pow(10, 3 - targetTimestampPrecision)));
}
break;
case INTERVAL_YEAR:
Expand Down Expand Up @@ -1020,6 +1020,18 @@ public static Expression convert(Expression operand, Type fromType,
} else {
return Expressions.convert_(operand, toType);
}
} else if (fromType == java.sql.Time.class) {
if (toBox == Primitive.INT) {
return Expressions.call(BuiltInMethod.TIME_TO_INT.method, operand);
} else {
return Expressions.convert_(operand, toType);
}
} else if (fromType == java.sql.Timestamp.class) {
if (toBox == Primitive.LONG) {
return Expressions.call(BuiltInMethod.TIMESTAMP_TO_LONG.method, operand);
} else {
return Expressions.convert_(operand, toType);
}
} else if (toType == java.sql.Date.class) {
// E.g. from "int" or "Integer" to "java.sql.Date",
// generate "SqlFunctions.internalToDate".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,20 @@ public SqlTypeName getSqlTypeName() {
}
return typeName;
}

@Override public int getPrecision() {
if (java.sql.Timestamp.class == clazz) {
// Timestamp class can hold fractions up to nano seconds.
// Without doing this, Timestamp fraction part gets lost
// when the value is cast to TIMESTAMP (without precision).
// That affects where clause conditions with '='.
// E.g. where \"ts\" = TIMESTAMP '2018-12-14 18:29:34.123'
// does not match even if the 'ts' has the same timestamp
// because the fraction part 123 is dropped.
return 9;
}
return super.getPrecision();
}
ijokarumawak marked this conversation as resolved.
Show resolved Hide resolved
}

/** Key to the data type cache. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,7 @@ public static int toInt(Object o) {
return o instanceof Integer ? (Integer) o
: o instanceof Number ? toInt((Number) o)
: o instanceof String ? toInt((String) o)
: o instanceof java.sql.Time ? toInt((java.sql.Time) o)
ijokarumawak marked this conversation as resolved.
Show resolved Hide resolved
: o instanceof java.util.Date ? toInt((java.util.Date) o)
: (Integer) cannotConvert(o, int.class);
}
Expand Down Expand Up @@ -1610,6 +1611,7 @@ public static long toLong(Object o) {
return o instanceof Long ? (Long) o
: o instanceof Number ? toLong((Number) o)
: o instanceof String ? toLong((String) o)
: o instanceof java.util.Date ? toLong((java.util.Date) o)
: (Long) cannotConvert(o, long.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
CalciteRemoteDriverTest.class,
StreamTest.class,
SortRemoveRuleTest.class,
ObjectArrayTableTest.class,

// test cases
TableInRootSchemaTest.class,
Expand Down
Loading