From 59bce52430e7b189ec6671653661b1252b4f5cb2 Mon Sep 17 00:00:00 2001 From: Panagiotis Garefalakis Date: Mon, 28 Dec 2020 23:36:49 +0200 Subject: [PATCH 1/5] ORC-709: FIX Boolean to StringGroup schema evolution --- .../orc/impl/ConvertTreeReaderFactory.java | 71 ++++++++++++++++--- .../apache/orc/impl/TestSchemaEvolution.java | 31 ++++++++ 2 files changed, 92 insertions(+), 10 deletions(-) diff --git a/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java b/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java index bd6bac5bb4..fe75e7b49b 100644 --- a/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java +++ b/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java @@ -1744,12 +1744,64 @@ public void nextVector(ColumnVector previousVector, } } - private static TreeReader createAnyIntegerConvertTreeReader(int columnId, + private static TreeReader createBooleanConvertTreeReader(int columnId, TypeDescription fileType, TypeDescription readerType, Context context) throws IOException { + // CONVERT from BOOLEAN to schema type. + // + switch (readerType.getCategory()) { + + case BOOLEAN: + case BYTE: + case SHORT: + case INT: + case LONG: + if (fileType.getCategory() == readerType.getCategory()) { + throw new IllegalArgumentException("No conversion of type " + + readerType.getCategory() + " to self needed"); + } + return new AnyIntegerFromAnyIntegerTreeReader(columnId, fileType, readerType, + context); + + case FLOAT: + case DOUBLE: + return new DoubleFromAnyIntegerTreeReader(columnId, fileType, + context); + + case DECIMAL: + return new DecimalFromAnyIntegerTreeReader(columnId, fileType, context); + + case STRING: + case CHAR: + case VARCHAR: + return new StringGroupFromBooleanTreeReader(columnId, fileType, readerType, + context); + + case TIMESTAMP: + case TIMESTAMP_INSTANT: + return new TimestampFromAnyIntegerTreeReader(columnId, fileType, context, + readerType.getCategory() == Category.TIMESTAMP_INSTANT); - // CONVERT from (BOOLEAN, BYTE, SHORT, INT, LONG) to schema type. + // Not currently supported conversion(s): + case BINARY: + case DATE: + + case STRUCT: + case LIST: + case MAP: + case UNION: + default: + throw new IllegalArgumentException("Unsupported type " + + readerType.getCategory()); + } + } + + private static TreeReader createAnyIntegerConvertTreeReader(int columnId, + TypeDescription fileType, + TypeDescription readerType, + Context context) throws IOException { + // CONVERT from (BYTE, SHORT, INT, LONG) to schema type. // switch (readerType.getCategory()) { @@ -1802,7 +1854,6 @@ private static TreeReader createDoubleConvertTreeReader(int columnId, TypeDescription fileType, TypeDescription readerType, Context context) throws IOException { - // CONVERT from DOUBLE to schema type. switch (readerType.getCategory()) { @@ -1849,7 +1900,6 @@ private static TreeReader createDecimalConvertTreeReader(int columnId, TypeDescription fileType, TypeDescription readerType, Context context) throws IOException { - // CONVERT from DECIMAL to schema type. switch (readerType.getCategory()) { @@ -1895,7 +1945,6 @@ private static TreeReader createStringConvertTreeReader(int columnId, TypeDescription fileType, TypeDescription readerType, Context context) throws IOException { - // CONVERT from STRING to schema type. switch (readerType.getCategory()) { @@ -1993,7 +2042,6 @@ private static TreeReader createTimestampConvertTreeReader(int columnId, private static TreeReader createDateConvertTreeReader(int columnId, TypeDescription readerType, Context context) throws IOException { - // CONVERT from DATE to schema type. switch (readerType.getCategory()) { @@ -2034,8 +2082,7 @@ private static TreeReader createDateConvertTreeReader(int columnId, private static TreeReader createBinaryConvertTreeReader(int columnId, TypeDescription readerType, Context context) throws IOException { - - // CONVERT from DATE to schema type. + // CONVERT from BINARY to schema type. switch (readerType.getCategory()) { case STRING: @@ -2115,7 +2162,8 @@ private static TreeReader createBinaryConvertTreeReader(int columnId, * DecimalFromStringGroupTreeReader (written) * * To STRING, CHAR, VARCHAR: - * Convert from (BOOLEAN, BYTE, SHORT, INT, LONG) using to string conversion + * Convert from (BYTE, SHORT, INT, LONG) using to string conversion + * Convert from BOOLEAN using boolean (True/False) conversion * Convert from (FLOAT, DOUBLE) using to string conversion * Convert from DECIMAL using HiveDecimal.toString * Convert from CHAR by stripping pads @@ -2125,6 +2173,7 @@ private static TreeReader createBinaryConvertTreeReader(int columnId, * Convert from BINARY using Text.decode * * StringGroupFromAnyIntegerTreeReader (written) + * StringGroupFromBooleanTreeReader (written) * StringGroupFromFloatTreeReader (written) * StringGroupFromDoubleTreeReader (written) * StringGroupFromDecimalTreeReader (written) @@ -2203,13 +2252,15 @@ public static TreeReader createConvertTreeReader(TypeDescription readerType, switch (fileType.getCategory()) { - case BOOLEAN: case BYTE: case SHORT: case INT: case LONG: return createAnyIntegerConvertTreeReader(columnId, fileType, readerType, context); + case BOOLEAN: + return createBooleanConvertTreeReader(columnId, fileType, readerType, context); + case FLOAT: case DOUBLE: return createDoubleConvertTreeReader(columnId, fileType, readerType, context); diff --git a/java/core/src/test/org/apache/orc/impl/TestSchemaEvolution.java b/java/core/src/test/org/apache/orc/impl/TestSchemaEvolution.java index 94c3b2528f..acbead8382 100644 --- a/java/core/src/test/org/apache/orc/impl/TestSchemaEvolution.java +++ b/java/core/src/test/org/apache/orc/impl/TestSchemaEvolution.java @@ -576,6 +576,37 @@ public void testDecimalToDecimal64Evolution() throws Exception { rows.close(); } + @Test + public void testBooleanToStringEvolution() throws Exception { + testFilePath = new Path(workDir, "TestSchemaEvolution." + + testCaseName.getMethodName() + ".orc"); + TypeDescription schema = TypeDescription.createBoolean(); + Writer writer = OrcFile.createWriter(testFilePath, + OrcFile.writerOptions(conf).setSchema(schema).stripeSize(100000) + .bufferSize(10000)); + VectorizedRowBatch batch = new VectorizedRowBatch(1, 1024); + LongColumnVector lcv = new LongColumnVector(1024); + batch.cols[0] = lcv; + batch.reset(); + batch.size = 3; + lcv.vector[0] = 1L; // True + lcv.vector[1] = 0L; // False + lcv.vector[2] = 1L; // True + writer.addRowBatch(batch); + writer.close(); + + Reader reader = OrcFile.createReader(testFilePath, + OrcFile.readerOptions(conf).filesystem(fs)); + TypeDescription schemaOnRead = TypeDescription.createString(); + RecordReader rows = reader.rows(reader.options().schema(schemaOnRead)); + batch = schemaOnRead.createRowBatch(); + rows.nextBatch(batch); + assertEquals("TRUE", ((BytesColumnVector) batch.cols[0]).toString(0)); + assertEquals("FALSE", ((BytesColumnVector) batch.cols[0]).toString(1)); + assertEquals("TRUE", ((BytesColumnVector) batch.cols[0]).toString(2)); + rows.close(); + } + @Test public void testCharToStringEvolution() throws IOException { TypeDescription fileType = TypeDescription.fromString("struct"); From 903ebf406dcd1d9154813e68f4c35a3997d59c58 Mon Sep 17 00:00:00 2001 From: Panagiotis Garefalakis Date: Tue, 29 Dec 2020 09:24:27 +0200 Subject: [PATCH 2/5] Identation Fixes --- .../apache/orc/impl/ConvertTreeReaderFactory.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java b/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java index fe75e7b49b..6f351cf385 100644 --- a/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java +++ b/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java @@ -1745,9 +1745,10 @@ public void nextVector(ColumnVector previousVector, } private static TreeReader createBooleanConvertTreeReader(int columnId, - TypeDescription fileType, - TypeDescription readerType, - Context context) throws IOException { + TypeDescription fileType, + TypeDescription readerType, + Context context) throws IOException { + // CONVERT from BOOLEAN to schema type. // switch (readerType.getCategory()) { @@ -1801,6 +1802,7 @@ private static TreeReader createAnyIntegerConvertTreeReader(int columnId, TypeDescription fileType, TypeDescription readerType, Context context) throws IOException { + // CONVERT from (BYTE, SHORT, INT, LONG) to schema type. // switch (readerType.getCategory()) { @@ -1854,6 +1856,7 @@ private static TreeReader createDoubleConvertTreeReader(int columnId, TypeDescription fileType, TypeDescription readerType, Context context) throws IOException { + // CONVERT from DOUBLE to schema type. switch (readerType.getCategory()) { @@ -1900,6 +1903,7 @@ private static TreeReader createDecimalConvertTreeReader(int columnId, TypeDescription fileType, TypeDescription readerType, Context context) throws IOException { + // CONVERT from DECIMAL to schema type. switch (readerType.getCategory()) { @@ -1945,6 +1949,7 @@ private static TreeReader createStringConvertTreeReader(int columnId, TypeDescription fileType, TypeDescription readerType, Context context) throws IOException { + // CONVERT from STRING to schema type. switch (readerType.getCategory()) { @@ -2042,6 +2047,7 @@ private static TreeReader createTimestampConvertTreeReader(int columnId, private static TreeReader createDateConvertTreeReader(int columnId, TypeDescription readerType, Context context) throws IOException { + // CONVERT from DATE to schema type. switch (readerType.getCategory()) { @@ -2082,6 +2088,7 @@ private static TreeReader createDateConvertTreeReader(int columnId, private static TreeReader createBinaryConvertTreeReader(int columnId, TypeDescription readerType, Context context) throws IOException { + // CONVERT from BINARY to schema type. switch (readerType.getCategory()) { From 6ada75d59e50b83494b0811b2e8b309bb7ceeab0 Mon Sep 17 00:00:00 2001 From: Panagiotis Garefalakis Date: Wed, 30 Dec 2020 12:13:56 +0200 Subject: [PATCH 3/5] FIX Identation --- .../orc/impl/ConvertTreeReaderFactory.java | 72 +++++++++---------- .../apache/orc/impl/TestSchemaEvolution.java | 8 +-- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java b/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java index 6f351cf385..7ffc4165c5 100644 --- a/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java +++ b/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java @@ -1753,48 +1753,48 @@ private static TreeReader createBooleanConvertTreeReader(int columnId, // switch (readerType.getCategory()) { - case BOOLEAN: - case BYTE: - case SHORT: - case INT: - case LONG: - if (fileType.getCategory() == readerType.getCategory()) { - throw new IllegalArgumentException("No conversion of type " + - readerType.getCategory() + " to self needed"); - } - return new AnyIntegerFromAnyIntegerTreeReader(columnId, fileType, readerType, - context); + case BOOLEAN: + case BYTE: + case SHORT: + case INT: + case LONG: + if (fileType.getCategory() == readerType.getCategory()) { + throw new IllegalArgumentException("No conversion of type " + + readerType.getCategory() + " to self needed"); + } + return new AnyIntegerFromAnyIntegerTreeReader(columnId, fileType, readerType, + context); - case FLOAT: - case DOUBLE: - return new DoubleFromAnyIntegerTreeReader(columnId, fileType, - context); + case FLOAT: + case DOUBLE: + return new DoubleFromAnyIntegerTreeReader(columnId, fileType, + context); - case DECIMAL: - return new DecimalFromAnyIntegerTreeReader(columnId, fileType, context); + case DECIMAL: + return new DecimalFromAnyIntegerTreeReader(columnId, fileType, context); - case STRING: - case CHAR: - case VARCHAR: - return new StringGroupFromBooleanTreeReader(columnId, fileType, readerType, - context); + case STRING: + case CHAR: + case VARCHAR: + return new StringGroupFromBooleanTreeReader(columnId, fileType, readerType, + context); - case TIMESTAMP: - case TIMESTAMP_INSTANT: - return new TimestampFromAnyIntegerTreeReader(columnId, fileType, context, - readerType.getCategory() == Category.TIMESTAMP_INSTANT); + case TIMESTAMP: + case TIMESTAMP_INSTANT: + return new TimestampFromAnyIntegerTreeReader(columnId, fileType, context, + readerType.getCategory() == Category.TIMESTAMP_INSTANT); - // Not currently supported conversion(s): - case BINARY: - case DATE: + // Not currently supported conversion(s): + case BINARY: + case DATE: - case STRUCT: - case LIST: - case MAP: - case UNION: - default: - throw new IllegalArgumentException("Unsupported type " + - readerType.getCategory()); + case STRUCT: + case LIST: + case MAP: + case UNION: + default: + throw new IllegalArgumentException("Unsupported type " + + readerType.getCategory()); } } diff --git a/java/core/src/test/org/apache/orc/impl/TestSchemaEvolution.java b/java/core/src/test/org/apache/orc/impl/TestSchemaEvolution.java index acbead8382..2bc5eee7e8 100644 --- a/java/core/src/test/org/apache/orc/impl/TestSchemaEvolution.java +++ b/java/core/src/test/org/apache/orc/impl/TestSchemaEvolution.java @@ -579,11 +579,11 @@ public void testDecimalToDecimal64Evolution() throws Exception { @Test public void testBooleanToStringEvolution() throws Exception { testFilePath = new Path(workDir, "TestSchemaEvolution." + - testCaseName.getMethodName() + ".orc"); + testCaseName.getMethodName() + ".orc"); TypeDescription schema = TypeDescription.createBoolean(); Writer writer = OrcFile.createWriter(testFilePath, - OrcFile.writerOptions(conf).setSchema(schema).stripeSize(100000) - .bufferSize(10000)); + OrcFile.writerOptions(conf).setSchema(schema).stripeSize(100000) + .bufferSize(10000)); VectorizedRowBatch batch = new VectorizedRowBatch(1, 1024); LongColumnVector lcv = new LongColumnVector(1024); batch.cols[0] = lcv; @@ -596,7 +596,7 @@ public void testBooleanToStringEvolution() throws Exception { writer.close(); Reader reader = OrcFile.createReader(testFilePath, - OrcFile.readerOptions(conf).filesystem(fs)); + OrcFile.readerOptions(conf).filesystem(fs)); TypeDescription schemaOnRead = TypeDescription.createString(); RecordReader rows = reader.rows(reader.options().schema(schemaOnRead)); batch = schemaOnRead.createRowBatch(); From 55dc29c6d546cdcbc4d97ad5cb6de27f5e1dcd4f Mon Sep 17 00:00:00 2001 From: Panagiotis Garefalakis Date: Wed, 30 Dec 2020 12:15:39 +0200 Subject: [PATCH 4/5] Consistency --- .../src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java | 1 - 1 file changed, 1 deletion(-) diff --git a/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java b/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java index 7ffc4165c5..91a910379f 100644 --- a/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java +++ b/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java @@ -1787,7 +1787,6 @@ private static TreeReader createBooleanConvertTreeReader(int columnId, // Not currently supported conversion(s): case BINARY: case DATE: - case STRUCT: case LIST: case MAP: From 82e625ab856b5ec873405df0cc0062035dd5d667 Mon Sep 17 00:00:00 2001 From: Panagiotis Garefalakis Date: Wed, 30 Dec 2020 12:35:24 +0200 Subject: [PATCH 5/5] More Consistency --- .../src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java b/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java index 91a910379f..789163d2c9 100644 --- a/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java +++ b/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java @@ -1767,8 +1767,7 @@ private static TreeReader createBooleanConvertTreeReader(int columnId, case FLOAT: case DOUBLE: - return new DoubleFromAnyIntegerTreeReader(columnId, fileType, - context); + return new DoubleFromAnyIntegerTreeReader(columnId, fileType, context); case DECIMAL: return new DecimalFromAnyIntegerTreeReader(columnId, fileType, context);