DRILL-678: Fix maxLen of varchar and precision of decimal type in InfoSchema.
Derive maxLen of Hive string/varchar and precision of decimal from Hive table metadata. Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/4773b57a Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/4773b57a Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/4773b57a Branch: refs/heads/master Commit: 4773b57abc04321e03f4b365a029ee593143513d Parents: 68b9778 Author: vkorukanti <[email protected]> Authored: Wed Jun 11 14:14:56 2014 -0700 Committer: Jacques Nadeau <[email protected]> Committed: Wed Jun 11 16:07:08 2014 -0700 ---------------------------------------------------------------------- .../drill/exec/store/hive/schema/DrillHiveTable.java | 6 ++++-- .../org/apache/drill/exec/store/ischema/Records.java | 12 ++++++++++-- .../org/apache/drill/jdbc/test/TestMetadataDDL.java | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/4773b57a/exec/java-exec/src/main/java/org/apache/drill/exec/store/hive/schema/DrillHiveTable.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/store/hive/schema/DrillHiveTable.java b/exec/java-exec/src/main/java/org/apache/drill/exec/store/hive/schema/DrillHiveTable.java index 1c1e4da..d1a5659 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/store/hive/schema/DrillHiveTable.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/store/hive/schema/DrillHiveTable.java @@ -107,12 +107,14 @@ public class DrillHiveTable extends DrillTable{ return typeFactory.createSqlType(SqlTypeName.BINARY); case DECIMAL: - return typeFactory.createSqlType(SqlTypeName.DECIMAL); + final int precision = 38; // Hive 0.12 has standard precision + return typeFactory.createSqlType(SqlTypeName.DECIMAL, precision); case STRING: case VARCHAR: { + int maxLen = TypeInfoUtils.getCharacterLengthForType(pTypeInfo); return typeFactory.createTypeWithCharsetAndCollation( - typeFactory.createSqlType(SqlTypeName.VARCHAR), /*input type*/ + typeFactory.createSqlType(SqlTypeName.VARCHAR, maxLen), /*input type*/ Charset.forName("ISO-8859-1"), /*unicode char set*/ SqlCollation.IMPLICIT /* TODO: need to decide if implicit is the correct one */ ); http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/4773b57a/exec/java-exec/src/main/java/org/apache/drill/exec/store/ischema/Records.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/store/ischema/Records.java b/exec/java-exec/src/main/java/org/apache/drill/exec/store/ischema/Records.java index 8d10775..9bb182f 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/store/ischema/Records.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/store/ischema/Records.java @@ -76,8 +76,16 @@ public class Records { } this.NUMERIC_PRECISION_RADIX = (sqlType == SqlTypeName.DECIMAL) ? 10 : -1; // TODO: where do we get radix? - this.CHARACTER_MAXIMUM_LENGTH = -1; // TODO: where do we get char length? - this.NUMERIC_PRECISION = (sqlType.allowsPrec())?type.getPrecision(): -1; + + if (sqlType == SqlTypeName.VARCHAR) { + // Max length is stored as precision in Optiq. + this.CHARACTER_MAXIMUM_LENGTH = (sqlType.allowsPrec()) ? type.getPrecision() : -1; + this.NUMERIC_PRECISION = -1; + } else { + this.CHARACTER_MAXIMUM_LENGTH = -1; + this.NUMERIC_PRECISION = (sqlType.allowsPrec()) ? type.getPrecision() : -1; + } + this.NUMERIC_SCALE = (sqlType.allowsScale())?type.getScale(): -1; } } http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/4773b57a/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestMetadataDDL.java ---------------------------------------------------------------------- diff --git a/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestMetadataDDL.java b/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestMetadataDDL.java index 279d637..501556a 100644 --- a/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestMetadataDDL.java +++ b/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestMetadataDDL.java @@ -232,6 +232,21 @@ public class TestMetadataDDL extends TestJdbcQuery { } @Test + public void testVarCharMaxLengthAndDecimalPrecisionInInfoSchema() throws Exception{ + JdbcAssert.withNoDefaultSchema() + .sql("SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION " + + "FROM INFORMATION_SCHEMA.`COLUMNS` " + + "WHERE TABLE_SCHEMA = 'hive.default' AND TABLE_NAME = 'allhivedatatypes' AND " + + "(COLUMN_NAME = 'stringtype' OR COLUMN_NAME = 'varchartype' OR " + + "COLUMN_NAME = 'inttype' OR COLUMN_NAME = 'decimaltype')") + .returns( + "COLUMN_NAME=inttype; DATA_TYPE=INTEGER; CHARACTER_MAXIMUM_LENGTH=-1; NUMERIC_PRECISION=-1\n" + + "COLUMN_NAME=decimaltype; DATA_TYPE=DECIMAL; CHARACTER_MAXIMUM_LENGTH=-1; NUMERIC_PRECISION=38\n" + + "COLUMN_NAME=stringtype; DATA_TYPE=VARCHAR; CHARACTER_MAXIMUM_LENGTH=65535; NUMERIC_PRECISION=-1\n" + + "COLUMN_NAME=varchartype; DATA_TYPE=VARCHAR; CHARACTER_MAXIMUM_LENGTH=20; NUMERIC_PRECISION=-1"); + } + + @Test public void testDefaultSchemaDfs() throws Exception{ JdbcAssert.withFull("dfs") .sql(String.format("SELECT R_REGIONKEY FROM `%s/../../sample-data/region.parquet` LIMIT 2", WORKING_PATH))
