Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions c++/src/ColumnReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ namespace orc {
}
}

template <typename BatchType>
class BooleanColumnReader : public ColumnReader {
private:
std::unique_ptr<orc::ByteRleDecoder> rle;
Expand All @@ -148,35 +149,42 @@ namespace orc {
void seekToRowGroup(std::unordered_map<uint64_t, PositionProvider>& positions) override;
};

BooleanColumnReader::BooleanColumnReader(const Type& type, StripeStreams& stripe)
template <typename BatchType>
BooleanColumnReader<BatchType>::BooleanColumnReader(const Type& type, StripeStreams& stripe)
: ColumnReader(type, stripe) {
std::unique_ptr<SeekableInputStream> stream =
stripe.getStream(columnId, proto::Stream_Kind_DATA, true);
if (stream == nullptr) throw ParseError("DATA stream not found in Boolean column");
rle = createBooleanRleDecoder(std::move(stream), metrics);
}

BooleanColumnReader::~BooleanColumnReader() {
template <typename BatchType>
BooleanColumnReader<BatchType>::~BooleanColumnReader() {
// PASS
}

uint64_t BooleanColumnReader::skip(uint64_t numValues) {
template <typename BatchType>
uint64_t BooleanColumnReader<BatchType>::skip(uint64_t numValues) {
numValues = ColumnReader::skip(numValues);
rle->skip(numValues);
return numValues;
}

void BooleanColumnReader::next(ColumnVectorBatch& rowBatch, uint64_t numValues, char* notNull) {
template <typename BatchType>
void BooleanColumnReader<BatchType>::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<LongVectorBatch&>(rowBatch).data.data();
// 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<BatchType&>(rowBatch).data.data();
rle->next(reinterpret_cast<char*>(ptr), numValues,
rowBatch.hasNulls ? rowBatch.notNull.data() : nullptr);
expandBytesToIntegers(ptr, numValues);
}

void BooleanColumnReader::seekToRowGroup(
template <typename BatchType>
void BooleanColumnReader<BatchType>::seekToRowGroup(
std::unordered_map<uint64_t, PositionProvider>& positions) {
ColumnReader::seekToRowGroup(positions);
rle->seek(positions.at(columnId));
Expand Down Expand Up @@ -1721,8 +1729,13 @@ namespace orc {
throw NotImplementedYet("buildReader unhandled string encoding");
}

case BOOLEAN:
return std::make_unique<BooleanColumnReader>(type, stripe);
case BOOLEAN: {
if (useTightNumericVector) {
return std::make_unique<BooleanColumnReader<ByteVectorBatch>>(type, stripe);
} else {
return std::make_unique<BooleanColumnReader<LongVectorBatch>>(type, stripe);
}
}

case BYTE:
if (useTightNumericVector) {
Expand Down
42 changes: 29 additions & 13 deletions c++/src/ColumnWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ namespace orc {
byteRleEncoder->recordPosition(rowIndexPosition.get());
}

template <typename BatchType>
class BooleanColumnWriter : public ColumnWriter {
public:
BooleanColumnWriter(const Type& type, const StreamsFactory& factory,
Expand All @@ -655,8 +656,10 @@ namespace orc {
std::unique_ptr<ByteRleEncoder> rleEncoder;
};

BooleanColumnWriter::BooleanColumnWriter(const Type& type, const StreamsFactory& factory,
const WriterOptions& options)
template <typename BatchType>
BooleanColumnWriter<BatchType>::BooleanColumnWriter(const Type& type,
const StreamsFactory& factory,
const WriterOptions& options)
: ColumnWriter(type, factory, options) {
std::unique_ptr<BufferedOutputStream> dataStream =
factory.createStream(proto::Stream_Kind_DATA);
Expand All @@ -667,11 +670,14 @@ namespace orc {
}
}

void BooleanColumnWriter::add(ColumnVectorBatch& rowBatch, uint64_t offset, uint64_t numValues,
const char* incomingMask) {
LongVectorBatch* byteBatch = dynamic_cast<LongVectorBatch*>(&rowBatch);
template <typename BatchType>
void BooleanColumnWriter<BatchType>::add(ColumnVectorBatch& rowBatch, uint64_t offset,
uint64_t numValues, const char* incomingMask) {
BatchType* byteBatch = dynamic_cast<BatchType*>(&rowBatch);
if (byteBatch == nullptr) {
throw InvalidArgument("Failed to cast to LongVectorBatch");
std::stringstream ss;
ss << "Failed to cast to " << typeid(BatchType).name();
throw InvalidArgument(ss.str());
}
BooleanColumnStatisticsImpl* boolStats =
dynamic_cast<BooleanColumnStatisticsImpl*>(colIndexStatistics.get());
Expand All @@ -681,7 +687,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<char*>(data);
Expand All @@ -706,7 +712,8 @@ namespace orc {
}
}

void BooleanColumnWriter::flush(std::vector<proto::Stream>& streams) {
template <typename BatchType>
void BooleanColumnWriter<BatchType>::flush(std::vector<proto::Stream>& streams) {
ColumnWriter::flush(streams);

proto::Stream stream;
Expand All @@ -716,13 +723,16 @@ namespace orc {
streams.push_back(stream);
}

uint64_t BooleanColumnWriter::getEstimatedSize() const {
template <typename BatchType>
uint64_t BooleanColumnWriter<BatchType>::getEstimatedSize() const {
uint64_t size = ColumnWriter::getEstimatedSize();
size += rleEncoder->getBufferSize();
return size;
}

void BooleanColumnWriter::getColumnEncoding(std::vector<proto::ColumnEncoding>& encodings) const {
template <typename BatchType>
void BooleanColumnWriter<BatchType>::getColumnEncoding(
std::vector<proto::ColumnEncoding>& encodings) const {
proto::ColumnEncoding encoding;
encoding.set_kind(proto::ColumnEncoding_Kind_DIRECT);
encoding.set_dictionarysize(0);
Expand All @@ -732,7 +742,8 @@ namespace orc {
encodings.push_back(encoding);
}

void BooleanColumnWriter::recordPosition() const {
template <typename BatchType>
void BooleanColumnWriter<BatchType>::recordPosition() const {
ColumnWriter::recordPosition();
rleEncoder->recordPosition(rowIndexPosition.get());
}
Expand Down Expand Up @@ -2824,8 +2835,13 @@ namespace orc {
return std::make_unique<ByteColumnWriter<ByteVectorBatch>>(type, factory, options);
}
return std::make_unique<ByteColumnWriter<LongVectorBatch>>(type, factory, options);
case BOOLEAN:
return std::make_unique<BooleanColumnWriter>(type, factory, options);
case BOOLEAN: {
if (options.getUseTightNumericVector()) {
return std::make_unique<BooleanColumnWriter<ByteVectorBatch>>(type, factory, options);
} else {
return std::make_unique<BooleanColumnWriter<LongVectorBatch>>(type, factory, options);
}
}
case DOUBLE:
return std::make_unique<FloatingColumnWriter<double, DoubleVectorBatch>>(type, factory,
options, false);
Expand Down
6 changes: 5 additions & 1 deletion c++/src/TypeImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ namespace orc {
MemoryPool& memoryPool, bool encoded,
bool useTightNumericVector) const {
switch (static_cast<int64_t>(kind)) {
case BOOLEAN:
case BOOLEAN: {
Comment thread
wgtmac marked this conversation as resolved.
Resolved
if (useTightNumericVector) {
return std::make_unique<ByteVectorBatch>(capacity, memoryPool);
}
}
case BYTE: {
if (useTightNumericVector) {
return std::make_unique<ByteVectorBatch>(capacity, memoryPool);
Expand Down
12 changes: 10 additions & 2 deletions c++/test/TestWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1897,8 +1897,9 @@ namespace orc {
TEST_P(WriterTest, testWriteFixedWidthNumericVectorBatch) {
MemoryOutputStream memStream(DEFAULT_MEM_STREAM_SIZE);
MemoryPool* pool = getDefaultPool();
std::unique_ptr<Type> type(Type::buildTypeFromString(
"struct<col1:double,col2:float,col3:int,col4:smallint,col5:tinyint,col6:bigint>"));
std::unique_ptr<Type> type(
Type::buildTypeFromString("struct<col1:double,col2:float,col3:int,col4:smallint,col5:"
"tinyint,col6:bigint,col7:boolean>"));

uint64_t stripeSize = 16 * 1024;
uint64_t compressionBlockSize = 1024;
Expand All @@ -1921,13 +1922,15 @@ namespace orc {
ShortVectorBatch* shortBatch = dynamic_cast<ShortVectorBatch*>(structBatch->fields[3]);
ByteVectorBatch* byteBatch = dynamic_cast<ByteVectorBatch*>(structBatch->fields[4]);
LongVectorBatch* longBatch = dynamic_cast<LongVectorBatch*>(structBatch->fields[5]);
ByteVectorBatch* boolBatch = dynamic_cast<ByteVectorBatch*>(structBatch->fields[6]);
structBatch->resize(rowCount);
doubleBatch->resize(rowCount);
floatBatch->resize(rowCount);
intBatch->resize(rowCount);
shortBatch->resize(rowCount);
byteBatch->resize(rowCount);
longBatch->resize(rowCount);
boolBatch->resize(rowCount);

for (uint64_t i = 0; i < rowCount; ++i) {
structBatch->notNull[i] = 1;
Expand All @@ -1937,13 +1940,15 @@ 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<float>(data[i]);
intBatch->data[i] = static_cast<int32_t>(i);
shortBatch->data[i] = static_cast<int16_t>(i);
byteBatch->data[i] = static_cast<int8_t>(i);
longBatch->data[i] = static_cast<int64_t>(i);
boolBatch->data[i] = static_cast<bool>((i % 17) % 2);
}

structBatch->numElements = rowCount;
Expand All @@ -1953,6 +1958,7 @@ namespace orc {
shortBatch->numElements = rowCount;
byteBatch->numElements = rowCount;
longBatch->numElements = rowCount;
boolBatch->numElements = rowCount;

writer->add(*batch);
writer->close();
Expand All @@ -1974,6 +1980,7 @@ namespace orc {
shortBatch = dynamic_cast<ShortVectorBatch*>(structBatch->fields[3]);
byteBatch = dynamic_cast<ByteVectorBatch*>(structBatch->fields[4]);
longBatch = dynamic_cast<LongVectorBatch*>(structBatch->fields[5]);
boolBatch = dynamic_cast<ByteVectorBatch*>(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<float>(data[i]) - static_cast<float>(floatBatch->data[i])) <
Expand All @@ -1982,6 +1989,7 @@ namespace orc {
EXPECT_EQ(shortBatch->data[i], static_cast<int16_t>(i));
EXPECT_EQ(byteBatch->data[i], static_cast<int8_t>(i));
EXPECT_EQ(longBatch->data[i], static_cast<int64_t>(i));
EXPECT_EQ(boolBatch->data[i], static_cast<bool>((i % 17) % 2));
}
EXPECT_FALSE(rowReader->next(*batch));
}
Expand Down