Skip to content

Commit

Permalink
[CALCITE-6074] The size of REAL, DOUBLE, and FLOAT is not consistent
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
mihaibudiu committed Dec 14, 2023
1 parent e4bd21e commit 08f6856
Show file tree
Hide file tree
Showing 19 changed files with 41 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ private static Class<?> maxMinClass(AggregateCall call) {
switch (call.getType().getSqlTypeName()) {
case INTEGER:
return max ? MaxInt.class : MinInt.class;
case FLOAT:
case REAL:
return max ? MaxFloat.class : MinFloat.class;
case FLOAT:
case DOUBLE:
case REAL:
return max ? MaxDouble.class : MinDouble.class;
case DECIMAL:
return max ? MaxBigDecimal.class : MinBigDecimal.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public VisitorDataContext(@Nullable Object[] values) {
switch (type.getSqlTypeName()) {
case INTEGER:
return Pair.of(index, rexLiteral.getValueAs(Integer.class));
case FLOAT:
case DOUBLE:
return Pair.of(index, rexLiteral.getValueAs(Double.class));
case REAL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ public double typeValueSize(RelDataType type, @Nullable Comparable value) {
case SMALLINT:
return 2d;
case INTEGER:
case FLOAT:
case REAL:
case DATE:
case TIME:
Expand All @@ -370,6 +369,7 @@ public double typeValueSize(RelDataType type, @Nullable Comparable value) {
case INTERVAL_MONTH:
return 4d;
case BIGINT:
case FLOAT: // sic
case DOUBLE:
case TIMESTAMP:
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,7 @@ private RelDataType decimalOf2(RelDataType type) {
return createSqlType(SqlTypeName.DECIMAL, 38, 0);
case REAL:
return createSqlType(SqlTypeName.DECIMAL, 14, 7);
case FLOAT:
return createSqlType(SqlTypeName.DECIMAL, 14, 7);
case FLOAT: // sic
case DOUBLE:
// the default max precision is 19, so this is actually DECIMAL(19, 15)
// but derived system can override the max precision/scale.
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/org/apache/calcite/rex/RexBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ public RexNode makeCast(
case INTEGER:
case SMALLINT:
case TINYINT:
case DOUBLE:
case FLOAT:
case REAL:
case DECIMAL:
Expand Down Expand Up @@ -1783,13 +1784,13 @@ public RexNode makeLiteral(@Nullable Object value, RelDataType type,
o.getClass().getCanonicalName(),
type.getSqlTypeName());
return new BigDecimal(((Number) o).longValue());
case FLOAT:
case REAL:
if (o instanceof BigDecimal) {
return o;
}
return new BigDecimal(((Number) o).doubleValue(), MathContext.DECIMAL32)
.stripTrailingZeros();
case REAL:
case FLOAT:
case DOUBLE:
if (o instanceof BigDecimal) {
return o;
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/apache/calcite/rex/RexLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ public static SqlTypeName strictTypeName(RelDataType type) {
return SqlTypeName.DECIMAL;
case REAL:
case FLOAT:
case DOUBLE:
return SqlTypeName.DOUBLE;
case VARBINARY:
return SqlTypeName.BINARY;
Expand Down Expand Up @@ -653,6 +654,7 @@ private static void appendAsJava(@Nullable Comparable value, StringBuilder sb,
sb.append(value.toString());
break;
case DOUBLE:
case FLOAT:
assert value instanceof BigDecimal;
sb.append(Util.toScientificNotation((BigDecimal) value));
break;
Expand Down Expand Up @@ -806,6 +808,8 @@ private static RexLiteral toLiteral(RelDataType type, Comparable<?> value) {
return new RexLiteral(b, type, typeName);
case DECIMAL:
case DOUBLE:
case REAL:
case FLOAT:
BigDecimal d = new BigDecimal(literal);
return new RexLiteral(d, type, typeName);
case BINARY:
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ public static boolean valueMatchesType(
return value == null;
case DECIMAL:
case DOUBLE:
case FLOAT:
case REAL:
return value instanceof BigDecimal;
case DATE:
return value instanceof DateString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ private static TimeUnit validate(TimeUnit timeUnit) {
case FLOAT:
case DOUBLE:
return createSqlDataTypeSpecByName("FLOAT64", typeName);
case REAL:
return createSqlDataTypeSpecByName("FLOAT32", typeName);
case DECIMAL:
return createSqlDataTypeSpecByName("NUMERIC", typeName);
case BOOLEAN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public ClickHouseSqlDialect(Context context) {
return createSqlDataTypeSpecByName("Int32", typeName);
case BIGINT:
return createSqlDataTypeSpecByName("Int64", typeName);
case FLOAT:
case REAL:
return createSqlDataTypeSpecByName("Float32", typeName);
case FLOAT:
case DOUBLE:
return createSqlDataTypeSpecByName("Float64", typeName);
case DATE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private static SqlTypeName parse(String strType) {
case "double":
return SqlTypeName.DOUBLE;
case "float":
return SqlTypeName.FLOAT;
return SqlTypeName.REAL;
case "string":
return SqlTypeName.VARCHAR;
case "timestamp":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private SqlTypeAssignmentRule(
rule.add(SqlTypeName.BIGINT);
rule.add(SqlTypeName.DECIMAL);
rule.add(SqlTypeName.FLOAT);
rule.add(SqlTypeName.DOUBLE);
rules.add(SqlTypeName.FLOAT, rule);

// REAL (32 bit floating point) is assignable from...
Expand All @@ -109,6 +110,7 @@ private SqlTypeAssignmentRule(
rule.add(SqlTypeName.BIGINT);
rule.add(SqlTypeName.DECIMAL);
rule.add(SqlTypeName.FLOAT);
rule.add(SqlTypeName.DOUBLE);
rule.add(SqlTypeName.REAL);
rules.add(SqlTypeName.REAL, rule);

Expand Down Expand Up @@ -204,6 +206,7 @@ private SqlTypeAssignmentRule(
rule.add(SqlTypeName.BIGINT);
rule.add(SqlTypeName.DECIMAL);
rule.add(SqlTypeName.FLOAT);
rule.add(SqlTypeName.DOUBLE);
rule.add(SqlTypeName.REAL);
rule.add(SqlTypeName.TIME);
rule.add(SqlTypeName.DATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,14 @@ public class SqlTypeExplicitPrecedenceList
.put(SqlTypeName.BIGINT, numeric(SqlTypeName.BIGINT))
.put(SqlTypeName.DECIMAL, numeric(SqlTypeName.DECIMAL))
.put(SqlTypeName.REAL, numeric(SqlTypeName.REAL))
.put(SqlTypeName.FLOAT, list(SqlTypeName.FLOAT, SqlTypeName.REAL, SqlTypeName.DOUBLE))
.put(SqlTypeName.DOUBLE, list(SqlTypeName.DOUBLE, SqlTypeName.DECIMAL))
.put(
SqlTypeName.FLOAT, list(
SqlTypeName.FLOAT, SqlTypeName.REAL,
SqlTypeName.DOUBLE, SqlTypeName.DECIMAL))
.put(
SqlTypeName.DOUBLE, list(
SqlTypeName.FLOAT, SqlTypeName.REAL,
SqlTypeName.DOUBLE, SqlTypeName.DECIMAL))
.put(SqlTypeName.CHAR, list(SqlTypeName.CHAR, SqlTypeName.VARCHAR))
.put(SqlTypeName.VARCHAR, list(SqlTypeName.VARCHAR))
.put(SqlTypeName.BINARY,
Expand Down
18 changes: 0 additions & 18 deletions core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,24 +491,6 @@ public static boolean isDecimal(RelDataType type) {
return typeName == SqlTypeName.DECIMAL;
}

/** Returns whether a type is DOUBLE. */
public static boolean isDouble(RelDataType type) {
SqlTypeName typeName = type.getSqlTypeName();
if (typeName == null) {
return false;
}
return typeName == SqlTypeName.DOUBLE;
}

/** Returns whether a type is BIGINT. */
public static boolean isBigint(RelDataType type) {
SqlTypeName typeName = type.getSqlTypeName();
if (typeName == null) {
return false;
}
return typeName == SqlTypeName.BIGINT;
}

/** Returns whether a type is numeric with exact precision. */
public static boolean isExactNumeric(RelDataType type) {
SqlTypeName typeName = type.getSqlTypeName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3330,6 +3330,8 @@ private void registerOperandSubQueries(
break;

case DOUBLE:
case FLOAT:
case REAL:
validateLiteralAsDouble(literal);
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ private void checkDate(RexLiteral literal) {
final RelDataTypeFactory typeFactory =
new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final RexBuilder rexBuilder = new RexBuilder(typeFactory);
final RelDataType floatType = typeFactory.createSqlType(SqlTypeName.FLOAT);
final RelDataType floatType = typeFactory.createSqlType(SqlTypeName.REAL);
RexNode left = rexBuilder.makeInputRef(floatType, 0);
final RexNode literal1 = rexBuilder.makeLiteral(1.0f, floatType);
final RexNode literal2 = rexBuilder.makeLiteral(2.0f, floatType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/** Druid type. */
public enum DruidType {
LONG(SqlTypeName.BIGINT),
FLOAT(SqlTypeName.FLOAT),
FLOAT(SqlTypeName.REAL),
DOUBLE(SqlTypeName.DOUBLE),
STRING(SqlTypeName.VARCHAR),
COMPLEX(SqlTypeName.OTHER),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,12 @@ abstract static class RowConverter<E> {
return null;
}
return Long.parseLong(string);
case FLOAT:
case REAL:
if (string.length() == 0) {
return null;
}
return Float.parseFloat(string);
case FLOAT:
case DOUBLE:
if (string.length() == 0) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static RelDataType convertSchemaField(LogicalSchema.LogicalFieldSchema pigField,
case DataType.LONG:
return TYPE_FACTORY.createSqlType(SqlTypeName.BIGINT, nullable);
case DataType.FLOAT:
return TYPE_FACTORY.createSqlType(SqlTypeName.FLOAT, nullable);
return TYPE_FACTORY.createSqlType(SqlTypeName.REAL, nullable);
case DataType.DOUBLE:
return TYPE_FACTORY.createSqlType(SqlTypeName.DOUBLE, nullable);
case DataType.DATETIME:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void testMatch() {
@Test void testCast() {
checkTranslation("(int) b", inTree("CAST($1):INTEGER"));
checkTranslation("(long) a", inTree("CAST($0):BIGINT"));
checkTranslation("(float) b", inTree("CAST($1):FLOAT"));
checkTranslation("(float) b", inTree("CAST($1):REAL"));
checkTranslation("(double) b", inTree("CAST($1):DOUBLE"));
checkTranslation("(chararray) b", inTree("CAST($1):VARCHAR"));
checkTranslation("(bytearray) b", inTree("CAST($1):BINARY"));
Expand All @@ -197,18 +197,18 @@ public void testMatch() {
checkTranslation("(bigdecimal) b", inTree("CAST($1):DECIMAL(19, 0)"));
checkTranslation("(tuple()) b", inTree("CAST($1):(DynamicRecordRow[])"));
checkTranslation("(tuple(int, float)) b",
inTree("CAST($1):RecordType(INTEGER $0, FLOAT $1)"));
inTree("CAST($1):RecordType(INTEGER $0, REAL $1)"));
checkTranslation("(bag{}) b",
inTree("CAST($1):(DynamicRecordRow[]) NOT NULL MULTISET"));
checkTranslation("(bag{tuple(int)}) b",
inTree("CAST($1):RecordType(INTEGER $0) MULTISET"));
checkTranslation("(bag{tuple(int, float)}) b",
inTree("CAST($1):RecordType(INTEGER $0, FLOAT $1) MULTISET"));
inTree("CAST($1):RecordType(INTEGER $0, REAL $1) MULTISET"));
checkTranslation("(map[]) b",
inTree("CAST($1):(VARCHAR NOT NULL, BINARY(1) NOT NULL) MAP"));
checkTranslation("(map[int]) b", inTree("CAST($1):(VARCHAR NOT NULL, INTEGER"));
checkTranslation("(map[tuple(int, float)]) b",
inTree("CAST($1):(VARCHAR NOT NULL, RecordType(INTEGER val_0, FLOAT val_1)) MAP"));
inTree("CAST($1):(VARCHAR NOT NULL, RecordType(INTEGER val_0, REAL val_1)) MAP"));
}

@Test void testPigBuiltinFunctions() {
Expand Down

0 comments on commit 08f6856

Please sign in to comment.