Skip to content

Commit

Permalink
[CALCITE-3282] In JDBC adadpter, when generating SQL for Hive, genera…
Browse files Browse the repository at this point in the history
…te 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 #1443
  • Loading branch information
ffmax authored and julianhyde committed Sep 16, 2019
1 parent f95f74a commit 2c19098
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>SqlDialect</code> implementation for the Apache Hive database.
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,18 @@ private static String toSql(RelNode root, SqlDialect dialect) {
sql(query).withHive().ok(expected);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-3282">[CALCITE-3282]
* HiveSqlDialect unparse Interger type as Int in order
* to be compatible with Hive1.x</a>. */
@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
* <a href="https://issues.apache.org/jira/browse/CALCITE-3220">[CALCITE-3220]
* HiveSqlDialect should transform the SQL-standard TRIM function to TRIM,
Expand Down

0 comments on commit 2c19098

Please sign in to comment.