From d5681504a3583752350b6abda3093e1324246bcd Mon Sep 17 00:00:00 2001 From: ffacs Date: Thu, 23 Mar 2023 10:54:24 +0800 Subject: [PATCH 1/3] Fix mismatch behavior with option useTightNumericVector --- c++/src/TypeImpl.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/c++/src/TypeImpl.cc b/c++/src/TypeImpl.cc index 4e0f7f2687..030b28ef83 100644 --- a/c++/src/TypeImpl.cc +++ b/c++/src/TypeImpl.cc @@ -286,7 +286,9 @@ namespace orc { MemoryPool& memoryPool, bool encoded, bool useTightNumericVector) const { switch (static_cast(kind)) { - case BOOLEAN: + case BOOLEAN: { + return std::make_unique(capacity, memoryPool); + } case BYTE: { if (useTightNumericVector) { return std::make_unique(capacity, memoryPool); From b4868d2840f59bd04f932c11a3ba42ee23a94dee Mon Sep 17 00:00:00 2001 From: ffacs Date: Fri, 24 Mar 2023 18:31:00 +0800 Subject: [PATCH 2/3] Support using ByteVectorBatch for BOOLEAN Type --- c++/src/ColumnReader.cc | 31 +++++++++++++++++++++---------- c++/src/ColumnWriter.cc | 34 +++++++++++++++++++++++----------- c++/src/TypeImpl.cc | 4 +++- c++/test/TestWriter.cc | 9 ++++++++- 4 files changed, 55 insertions(+), 23 deletions(-) diff --git a/c++/src/ColumnReader.cc b/c++/src/ColumnReader.cc index 179f86ca82..cee21ce14a 100644 --- a/c++/src/ColumnReader.cc +++ b/c++/src/ColumnReader.cc @@ -133,6 +133,7 @@ namespace orc { } } + template class BooleanColumnReader : public ColumnReader { private: std::unique_ptr rle; @@ -148,7 +149,8 @@ namespace orc { void seekToRowGroup(std::unordered_map& positions) override; }; - BooleanColumnReader::BooleanColumnReader(const Type& type, StripeStreams& stripe) + template + BooleanColumnReader::BooleanColumnReader(const Type& type, StripeStreams& stripe) : ColumnReader(type, stripe) { std::unique_ptr stream = stripe.getStream(columnId, proto::Stream_Kind_DATA, true); @@ -156,27 +158,31 @@ namespace orc { rle = createBooleanRleDecoder(std::move(stream), metrics); } - BooleanColumnReader::~BooleanColumnReader() { + template + BooleanColumnReader::~BooleanColumnReader() { // PASS } - uint64_t BooleanColumnReader::skip(uint64_t numValues) { + template + uint64_t BooleanColumnReader::skip(uint64_t numValues) { numValues = ColumnReader::skip(numValues); rle->skip(numValues); return numValues; } - void BooleanColumnReader::next(ColumnVectorBatch& rowBatch, uint64_t numValues, char* notNull) { + template + void BooleanColumnReader::next(ColumnVectorBatch& rowBatch, uint64_t numValues, char* notNull) { ColumnReader::next(rowBatch, numValues, notNull); - // Since the byte rle places the output in a char* instead of long*, - // we cheat here and use the long* and then expand it in a second pass. - int64_t* ptr = dynamic_cast(rowBatch).data.data(); + // Since the byte rle places the output in a char* + // we cheat here and use the other type and then expand it in a second pass. + auto* ptr = dynamic_cast(rowBatch).data.data(); rle->next(reinterpret_cast(ptr), numValues, rowBatch.hasNulls ? rowBatch.notNull.data() : nullptr); expandBytesToIntegers(ptr, numValues); } - void BooleanColumnReader::seekToRowGroup( + template + void BooleanColumnReader::seekToRowGroup( std::unordered_map& positions) { ColumnReader::seekToRowGroup(positions); rle->seek(positions.at(columnId)); @@ -1721,8 +1727,13 @@ namespace orc { throw NotImplementedYet("buildReader unhandled string encoding"); } - case BOOLEAN: - return std::make_unique(type, stripe); + case BOOLEAN: { + if (useTightNumericVector) { + return std::make_unique>(type, stripe); + } else { + return std::make_unique>(type, stripe); + } + } case BYTE: if (useTightNumericVector) { diff --git a/c++/src/ColumnWriter.cc b/c++/src/ColumnWriter.cc index 2565cbe1cb..2c69d06f55 100644 --- a/c++/src/ColumnWriter.cc +++ b/c++/src/ColumnWriter.cc @@ -635,6 +635,7 @@ namespace orc { byteRleEncoder->recordPosition(rowIndexPosition.get()); } + template class BooleanColumnWriter : public ColumnWriter { public: BooleanColumnWriter(const Type& type, const StreamsFactory& factory, @@ -655,7 +656,8 @@ namespace orc { std::unique_ptr rleEncoder; }; - BooleanColumnWriter::BooleanColumnWriter(const Type& type, const StreamsFactory& factory, + template + BooleanColumnWriter::BooleanColumnWriter(const Type& type, const StreamsFactory& factory, const WriterOptions& options) : ColumnWriter(type, factory, options) { std::unique_ptr dataStream = @@ -667,11 +669,12 @@ namespace orc { } } - void BooleanColumnWriter::add(ColumnVectorBatch& rowBatch, uint64_t offset, uint64_t numValues, + template + void BooleanColumnWriter::add(ColumnVectorBatch& rowBatch, uint64_t offset, uint64_t numValues, const char* incomingMask) { - LongVectorBatch* byteBatch = dynamic_cast(&rowBatch); + BatchType* byteBatch = dynamic_cast(&rowBatch); if (byteBatch == nullptr) { - throw InvalidArgument("Failed to cast to LongVectorBatch"); + throw InvalidArgument("Failed to cast to BooleanVectorBatch"); } BooleanColumnStatisticsImpl* boolStats = dynamic_cast(colIndexStatistics.get()); @@ -681,7 +684,7 @@ namespace orc { ColumnWriter::add(rowBatch, offset, numValues, incomingMask); - int64_t* data = byteBatch->data.data() + offset; + auto* data = byteBatch->data.data() + offset; const char* notNull = byteBatch->hasNulls ? byteBatch->notNull.data() + offset : nullptr; char* byteData = reinterpret_cast(data); @@ -706,7 +709,8 @@ namespace orc { } } - void BooleanColumnWriter::flush(std::vector& streams) { + template + void BooleanColumnWriter::flush(std::vector& streams) { ColumnWriter::flush(streams); proto::Stream stream; @@ -716,13 +720,15 @@ namespace orc { streams.push_back(stream); } - uint64_t BooleanColumnWriter::getEstimatedSize() const { + template + uint64_t BooleanColumnWriter::getEstimatedSize() const { uint64_t size = ColumnWriter::getEstimatedSize(); size += rleEncoder->getBufferSize(); return size; } - void BooleanColumnWriter::getColumnEncoding(std::vector& encodings) const { + template + void BooleanColumnWriter::getColumnEncoding(std::vector& encodings) const { proto::ColumnEncoding encoding; encoding.set_kind(proto::ColumnEncoding_Kind_DIRECT); encoding.set_dictionarysize(0); @@ -732,7 +738,8 @@ namespace orc { encodings.push_back(encoding); } - void BooleanColumnWriter::recordPosition() const { + template + void BooleanColumnWriter::recordPosition() const { ColumnWriter::recordPosition(); rleEncoder->recordPosition(rowIndexPosition.get()); } @@ -2824,8 +2831,13 @@ namespace orc { return std::make_unique>(type, factory, options); } return std::make_unique>(type, factory, options); - case BOOLEAN: - return std::make_unique(type, factory, options); + case BOOLEAN: { + if (options.getUseTightNumericVector()) { + return std::make_unique>(type, factory, options); + } else { + return std::make_unique>(type, factory, options); + } + } case DOUBLE: return std::make_unique>(type, factory, options, false); diff --git a/c++/src/TypeImpl.cc b/c++/src/TypeImpl.cc index 030b28ef83..0075d0478a 100644 --- a/c++/src/TypeImpl.cc +++ b/c++/src/TypeImpl.cc @@ -287,7 +287,9 @@ namespace orc { bool useTightNumericVector) const { switch (static_cast(kind)) { case BOOLEAN: { - return std::make_unique(capacity, memoryPool); + if (useTightNumericVector) { + return std::make_unique(capacity, memoryPool); + } } case BYTE: { if (useTightNumericVector) { diff --git a/c++/test/TestWriter.cc b/c++/test/TestWriter.cc index b2bee3ba11..c1534f0ad1 100644 --- a/c++/test/TestWriter.cc +++ b/c++/test/TestWriter.cc @@ -1898,7 +1898,7 @@ namespace orc { MemoryOutputStream memStream(DEFAULT_MEM_STREAM_SIZE); MemoryPool* pool = getDefaultPool(); std::unique_ptr type(Type::buildTypeFromString( - "struct")); + "struct")); uint64_t stripeSize = 16 * 1024; uint64_t compressionBlockSize = 1024; @@ -1921,6 +1921,7 @@ namespace orc { ShortVectorBatch* shortBatch = dynamic_cast(structBatch->fields[3]); ByteVectorBatch* byteBatch = dynamic_cast(structBatch->fields[4]); LongVectorBatch* longBatch = dynamic_cast(structBatch->fields[5]); + ByteVectorBatch* boolBatch = dynamic_cast(structBatch->fields[6]); structBatch->resize(rowCount); doubleBatch->resize(rowCount); floatBatch->resize(rowCount); @@ -1928,6 +1929,7 @@ namespace orc { shortBatch->resize(rowCount); byteBatch->resize(rowCount); longBatch->resize(rowCount); + boolBatch->resize(rowCount); for (uint64_t i = 0; i < rowCount; ++i) { structBatch->notNull[i] = 1; @@ -1937,6 +1939,7 @@ namespace orc { shortBatch->notNull[i] = 1; byteBatch->notNull[i] = 1; longBatch->notNull[i] = 1; + boolBatch->notNull[i] = 1; doubleBatch->data[i] = data[i]; floatBatch->data[i] = static_cast(data[i]); @@ -1944,6 +1947,7 @@ namespace orc { shortBatch->data[i] = static_cast(i); byteBatch->data[i] = static_cast(i); longBatch->data[i] = static_cast(i); + boolBatch->data[i] = static_cast((i % 17) % 2); } structBatch->numElements = rowCount; @@ -1953,6 +1957,7 @@ namespace orc { shortBatch->numElements = rowCount; byteBatch->numElements = rowCount; longBatch->numElements = rowCount; + boolBatch->numElements = rowCount; writer->add(*batch); writer->close(); @@ -1974,6 +1979,7 @@ namespace orc { shortBatch = dynamic_cast(structBatch->fields[3]); byteBatch = dynamic_cast(structBatch->fields[4]); longBatch = dynamic_cast(structBatch->fields[5]); + boolBatch = dynamic_cast(structBatch->fields[6]); for (uint64_t i = 0; i < rowCount; ++i) { EXPECT_TRUE(std::abs(data[i] - doubleBatch->data[i]) < 0.000001); EXPECT_TRUE(std::abs(static_cast(data[i]) - static_cast(floatBatch->data[i])) < @@ -1982,6 +1988,7 @@ namespace orc { EXPECT_EQ(shortBatch->data[i], static_cast(i)); EXPECT_EQ(byteBatch->data[i], static_cast(i)); EXPECT_EQ(longBatch->data[i], static_cast(i)); + EXPECT_EQ(boolBatch->data[i], static_cast((i % 17) % 2)); } EXPECT_FALSE(rowReader->next(*batch)); } From 78e85a06f385030944bbb2e3a963d323f2326518 Mon Sep 17 00:00:00 2001 From: ffacs Date: Mon, 27 Mar 2023 14:31:25 +0800 Subject: [PATCH 3/3] Format source codes and refactor error messages --- c++/src/ColumnReader.cc | 8 +++++--- c++/src/ColumnWriter.cc | 16 ++++++++++------ c++/test/TestWriter.cc | 5 +++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/c++/src/ColumnReader.cc b/c++/src/ColumnReader.cc index cee21ce14a..2a72c80eeb 100644 --- a/c++/src/ColumnReader.cc +++ b/c++/src/ColumnReader.cc @@ -171,10 +171,12 @@ namespace orc { } template - void BooleanColumnReader::next(ColumnVectorBatch& rowBatch, uint64_t numValues, char* notNull) { + void BooleanColumnReader::next(ColumnVectorBatch& rowBatch, uint64_t numValues, + char* notNull) { ColumnReader::next(rowBatch, numValues, notNull); - // Since the byte rle places the output in a char* - // we cheat here and use the other type and then expand it in a second pass. + // Since the byte rle places the output in a char* and BatchType here may be + // LongVectorBatch with long*. We cheat here in that case and use the long* + // and then expand it in a second pass.. auto* ptr = dynamic_cast(rowBatch).data.data(); rle->next(reinterpret_cast(ptr), numValues, rowBatch.hasNulls ? rowBatch.notNull.data() : nullptr); diff --git a/c++/src/ColumnWriter.cc b/c++/src/ColumnWriter.cc index 2c69d06f55..0c72e1ccf4 100644 --- a/c++/src/ColumnWriter.cc +++ b/c++/src/ColumnWriter.cc @@ -657,8 +657,9 @@ namespace orc { }; template - BooleanColumnWriter::BooleanColumnWriter(const Type& type, const StreamsFactory& factory, - const WriterOptions& options) + BooleanColumnWriter::BooleanColumnWriter(const Type& type, + const StreamsFactory& factory, + const WriterOptions& options) : ColumnWriter(type, factory, options) { std::unique_ptr dataStream = factory.createStream(proto::Stream_Kind_DATA); @@ -670,11 +671,13 @@ namespace orc { } template - void BooleanColumnWriter::add(ColumnVectorBatch& rowBatch, uint64_t offset, uint64_t numValues, - const char* incomingMask) { + void BooleanColumnWriter::add(ColumnVectorBatch& rowBatch, uint64_t offset, + uint64_t numValues, const char* incomingMask) { BatchType* byteBatch = dynamic_cast(&rowBatch); if (byteBatch == nullptr) { - throw InvalidArgument("Failed to cast to BooleanVectorBatch"); + std::stringstream ss; + ss << "Failed to cast to " << typeid(BatchType).name(); + throw InvalidArgument(ss.str()); } BooleanColumnStatisticsImpl* boolStats = dynamic_cast(colIndexStatistics.get()); @@ -728,7 +731,8 @@ namespace orc { } template - void BooleanColumnWriter::getColumnEncoding(std::vector& encodings) const { + void BooleanColumnWriter::getColumnEncoding( + std::vector& encodings) const { proto::ColumnEncoding encoding; encoding.set_kind(proto::ColumnEncoding_Kind_DIRECT); encoding.set_dictionarysize(0); diff --git a/c++/test/TestWriter.cc b/c++/test/TestWriter.cc index c1534f0ad1..c8c3ca1396 100644 --- a/c++/test/TestWriter.cc +++ b/c++/test/TestWriter.cc @@ -1897,8 +1897,9 @@ namespace orc { TEST_P(WriterTest, testWriteFixedWidthNumericVectorBatch) { MemoryOutputStream memStream(DEFAULT_MEM_STREAM_SIZE); MemoryPool* pool = getDefaultPool(); - std::unique_ptr type(Type::buildTypeFromString( - "struct")); + std::unique_ptr type( + Type::buildTypeFromString("struct")); uint64_t stripeSize = 16 * 1024; uint64_t compressionBlockSize = 1024;