Skip to content

Commit

Permalink
[Coral-Common] Modify return type of operator "/" (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljfgem authored May 1, 2023
1 parent dba80ac commit 8a5a288
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2022 LinkedIn Corporation. All rights reserved.
* Copyright 2017-2023 LinkedIn Corporation. All rights reserved.
* Licensed under the BSD-2 Clause license.
* See LICENSE in the project root for license information.
*/
Expand All @@ -9,6 +9,7 @@
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeSystemImpl;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.type.SqlTypeUtil;


// Copied from Hive source code
Expand Down Expand Up @@ -161,6 +162,19 @@ public boolean isSchemaCaseSensitive() {
return false;
}

/**
* This method needs to be overridden to make sure that the "/" operator returns {@link org.apache.calcite.sql.type.ReturnTypes#DOUBLE}
* if neither of the operands is decimal type, which is compatible with the expected data type in Hive/Spark.
*/
@Override
public RelDataType deriveDecimalDivideType(RelDataTypeFactory typeFactory, RelDataType type1, RelDataType type2) {
if (SqlTypeUtil.isDecimal(type1) || SqlTypeUtil.isDecimal(type2)) {
return super.deriveDecimalDivideType(typeFactory, type1, type2);
} else {
return nullableType(typeFactory, SqlTypeName.DOUBLE);
}
}

private RelDataType nullableType(RelDataTypeFactory typeFactory, SqlTypeName typeName) {
return typeFactory.createTypeWithNullability(typeFactory.createSqlType(typeName), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1078,5 +1078,16 @@ public void testUnionFloatAndDoublePromoteToDouble() {
TestUtils.loadSchema("testUnionFloatAndDoublePromoteToDouble-expected.avsc"));
}

@Test
public void testDivideReturnType() {
String viewSql = "CREATE VIEW v AS SELECT 1/2 AS a FROM baseprimitive";
TestUtils.executeCreateViewQuery("default", "v", viewSql);

ViewToAvroSchemaConverter viewToAvroSchemaConverter = ViewToAvroSchemaConverter.create(hiveMetastoreClient);
Schema actualSchema = viewToAvroSchemaConverter.toAvroSchema("default", "v");

Assert.assertEquals(actualSchema.toString(true), TestUtils.loadSchema("testDivideReturnType-expected.avsc"));
}

// TODO: add more unit tests
}
10 changes: 10 additions & 0 deletions coral-schema/src/test/resources/testDivideReturnType-expected.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type" : "record",
"name" : "v",
"namespace" : "default.v",
"fields" : [ {
"name" : "a",
"type" : [ "null", "double" ],
"doc" : "Field created in view by applying operator '/' with argument(s): 1, 2"
} ]
}

0 comments on commit 8a5a288

Please sign in to comment.