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..789163d2c9 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 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); + + // 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 (BOOLEAN, BYTE, SHORT, INT, LONG) to schema type. + // CONVERT from (BYTE, SHORT, INT, LONG) to schema type. // switch (readerType.getCategory()) { @@ -2035,7 +2087,7 @@ 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 +2167,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 +2178,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 +2257,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..2bc5eee7e8 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");