From f3758a6fa301cbd910ceabeca9075a3772ee4e8b Mon Sep 17 00:00:00 2001 From: ffmax <–fengfeng_max@163.com> Date: Thu, 5 Sep 2019 19:51:32 +0800 Subject: [PATCH] [CALCITE-3282] In JDBC adapter, when generating SQL for Hive, generate INTEGER type as INT (huangfeng) This is necessary because Hive 1.x only supports INT, not INTEGER. Fix-up (by Danny): * Add issue link to the test case; * Fix some doc typos. close apache/calcite#1443 --- .../org/apache/calcite/sql/SqlDialect.java | 2 ++ .../calcite/sql/dialect/HiveSqlDialect.java | 18 ++++++++++++++++++ .../rel/rel2sql/RelToSqlConverterTest.java | 12 ++++++++++++ 3 files changed, 32 insertions(+) diff --git a/core/src/main/java/org/apache/calcite/sql/SqlDialect.java b/core/src/main/java/org/apache/calcite/sql/SqlDialect.java index 8a740a8df19a..7d802b4d2184 100644 --- a/core/src/main/java/org/apache/calcite/sql/SqlDialect.java +++ b/core/src/main/java/org/apache/calcite/sql/SqlDialect.java @@ -749,6 +749,8 @@ public boolean supportsDataType(RelDataType type) { return true; } + /** Returns SqlNode for type in "cast(column as type)", which might be + * different between databases by type name, precision etc. */ public SqlNode getCastSpec(RelDataType type) { if (type instanceof BasicSqlType) { int maxPrecision = -1; diff --git a/core/src/main/java/org/apache/calcite/sql/dialect/HiveSqlDialect.java b/core/src/main/java/org/apache/calcite/sql/dialect/HiveSqlDialect.java index 25254609bceb..afc2290f5555 100644 --- a/core/src/main/java/org/apache/calcite/sql/dialect/HiveSqlDialect.java +++ b/core/src/main/java/org/apache/calcite/sql/dialect/HiveSqlDialect.java @@ -17,15 +17,21 @@ package org.apache.calcite.sql.dialect; import org.apache.calcite.config.NullCollation; +import org.apache.calcite.rel.type.RelDataType; import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlDataTypeSpec; import org.apache.calcite.sql.SqlDialect; +import org.apache.calcite.sql.SqlIdentifier; import org.apache.calcite.sql.SqlLiteral; import org.apache.calcite.sql.SqlNode; import org.apache.calcite.sql.SqlOperator; import org.apache.calcite.sql.SqlSyntax; +import org.apache.calcite.sql.SqlUserDefinedTypeNameSpec; import org.apache.calcite.sql.SqlWriter; import org.apache.calcite.sql.fun.SqlStdOperatorTable; import org.apache.calcite.sql.fun.SqlTrimFunction; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.calcite.sql.type.BasicSqlType; /** * A SqlDialect implementation for the Apache Hive database. @@ -120,6 +126,18 @@ private void unparseTrim(SqlWriter writer, SqlCall call, int leftPrec, @Override public boolean supportsCharSet() { return false; } + + @Override public SqlNode getCastSpec(final RelDataType type) { + if (type instanceof BasicSqlType) { + switch (type.getSqlTypeName()) { + case INTEGER: + SqlUserDefinedTypeNameSpec typeNameSpec = new SqlUserDefinedTypeNameSpec( + new SqlIdentifier("INT", SqlParserPos.ZERO), SqlParserPos.ZERO); + return new SqlDataTypeSpec(typeNameSpec, SqlParserPos.ZERO); + } + } + return super.getCastSpec(type); + } } // End HiveSqlDialect.java diff --git a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java index 7f9fc34ae9d0..8d67024c0e52 100644 --- a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java +++ b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java @@ -803,6 +803,18 @@ private static String toSql(RelNode root, SqlDialect dialect) { sql(query).withHive().ok(expected); } + /** Test case for + * [CALCITE-3282] + * HiveSqlDialect unparse Interger type as Int in order + * to be compatible with Hive1.x. */ + @Test public void testHiveCastAsInt() { + String query = "select cast( cast(\"employee_id\" as varchar) as int) " + + "from \"foodmart\".\"reserve_employee\" "; + final String expected = "SELECT CAST(CAST(employee_id AS VARCHAR) AS INT)\n" + + "FROM foodmart.reserve_employee"; + sql(query).withHive().ok(expected); + } + /** Test case for * [CALCITE-3220] * HiveSqlDialect should transform the SQL-standard TRIM function to TRIM,