-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add decimal column writer for ORC file format #11431
Open
rui-mo
wants to merge
2
commits into
facebookincubator:main
Choose a base branch
from
rui-mo:wip_decimal_writer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
#include "velox/common/base/tests/GTestUtils.h" | ||
#include "velox/common/encode/Coding.h" | ||
#include "velox/type/HugeInt.h" | ||
|
||
namespace facebook::velox::encode::test { | ||
|
||
class ZigZagTest : public ::testing::Test {}; | ||
|
||
TEST_F(ZigZagTest, hugeInt) { | ||
auto assertZigZag = [](int128_t value) { | ||
auto encoded = ZigZag::encodeInt128(value); | ||
auto decoded = ZigZag::decode(encoded); | ||
EXPECT_EQ(value, decoded); | ||
}; | ||
|
||
assertZigZag(0); | ||
assertZigZag(HugeInt::parse("1234567890123456789")); | ||
assertZigZag(HugeInt::parse("-1234567890123456789")); | ||
assertZigZag(HugeInt::parse(std::string(38, '9'))); | ||
assertZigZag(std::numeric_limits<__int128_t>::max()); | ||
assertZigZag(std::numeric_limits<__int128_t>::min()); | ||
} | ||
|
||
} // namespace facebook::velox::encode::test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,10 +203,22 @@ constexpr uint32_t ITERATIONS = 100'000; | |
template <typename T> | ||
VectorPtr populateBatch( | ||
std::vector<std::optional<T>> const& data, | ||
MemoryPool* pool) { | ||
MemoryPool* pool, | ||
const TypePtr& type = CppToType<T>::create()) { | ||
BufferPtr values = AlignedBuffer::allocate<T>(data.size(), pool); | ||
auto valuesPtr = values->asMutableRange<T>(); | ||
|
||
const size_t nulloptCount = | ||
std::count(data.begin(), data.end(), std::nullopt); | ||
if (nulloptCount == 0) { | ||
size_t index = 0; | ||
for (auto val : data) { | ||
valuesPtr[index++] = val.value(); | ||
} | ||
return std::make_shared<FlatVector<T>>( | ||
pool, type, nullptr, data.size(), values, std::vector<BufferPtr>{}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When no null, use nullptr as null buffer. This helps test the |
||
} | ||
|
||
BufferPtr nulls = allocateNulls(data.size(), pool); | ||
auto* nullsPtr = nulls->asMutable<uint64_t>(); | ||
size_t index = 0; | ||
|
@@ -223,12 +235,7 @@ VectorPtr populateBatch( | |
} | ||
|
||
auto batch = std::make_shared<FlatVector<T>>( | ||
pool, | ||
CppToType<T>::create(), | ||
nulls, | ||
data.size(), | ||
values, | ||
std::vector<BufferPtr>{}); | ||
pool, type, nulls, data.size(), values, std::vector<BufferPtr>{}); | ||
batch->setNullCount(nullCount); | ||
return batch; | ||
} | ||
|
@@ -278,8 +285,14 @@ void verifyBatch( | |
const uint32_t seed) { | ||
auto size = data.size(); | ||
ASSERT_EQ(out->size(), size) << "Batch size mismatch with seed " << seed; | ||
ASSERT_EQ(nullCount, out->getNullCount()) | ||
<< "nullCount mismatch with seed " << seed; | ||
if (nullCount == std::nullopt) { | ||
const auto outNullCount = out->getNullCount(); | ||
ASSERT_TRUE(outNullCount == std::nullopt || outNullCount == 0) | ||
<< "nullCount mismatch with seed " << seed; | ||
} else { | ||
ASSERT_EQ(nullCount, out->getNullCount()) | ||
<< "nullCount mismatch with seed " << seed; | ||
} | ||
|
||
auto outFv = std::dynamic_pointer_cast<FlatVector<T>>(out); | ||
size_t index = 0; | ||
|
@@ -314,7 +327,8 @@ template <typename T> | |
void testDataTypeWriter( | ||
const TypePtr& type, | ||
std::vector<std::optional<T>>& data, | ||
const uint32_t sequence = 0) { | ||
const uint32_t sequence = 0, | ||
DwrfFormat format = DwrfFormat::kDwrf) { | ||
// Generate a seed and randomly shuffle the data | ||
uint32_t seed = Random::rand32(); | ||
std::shuffle(data.begin(), data.end(), std::default_random_engine(seed)); | ||
|
@@ -327,9 +341,10 @@ void testDataTypeWriter( | |
auto dataTypeWithId = TypeWithId::create(type, 1); | ||
|
||
// write | ||
auto writer = BaseColumnWriter::create(context, *dataTypeWithId, sequence); | ||
auto writer = BaseColumnWriter::create( | ||
context, *dataTypeWithId, sequence, nullptr, format); | ||
auto size = data.size(); | ||
auto batch = populateBatch(data, pool.get()); | ||
auto batch = populateBatch(data, pool.get(), type); | ||
const size_t stripeCount = 2; | ||
const size_t strideCount = 3; | ||
|
||
|
@@ -445,6 +460,43 @@ TEST_F(ColumnWriterTest, TestNullBooleanWriter) { | |
testDataTypeWriter(BOOLEAN(), data); | ||
} | ||
|
||
TEST_F(ColumnWriterTest, testDecimalWriter) { | ||
const auto format = DwrfFormat::kOrc; | ||
auto genShortDecimals = [&](bool hasNull) { | ||
std::vector<std::optional<int64_t>> shortDecimals; | ||
for (auto i = 0; i < ITERATIONS; ++i) { | ||
if (!hasNull || i % 15) { | ||
shortDecimals.emplace_back(i); | ||
} else { | ||
shortDecimals.emplace_back(std::nullopt); | ||
} | ||
} | ||
return shortDecimals; | ||
}; | ||
|
||
auto shortValues = genShortDecimals(false); | ||
testDataTypeWriter(DECIMAL(10, 2), shortValues, 0 /*sequence*/, format); | ||
shortValues = genShortDecimals(true); | ||
testDataTypeWriter(DECIMAL(10, 2), shortValues, 0 /*sequence*/, format); | ||
|
||
auto genLongDecimals = [&](bool hasNull) { | ||
std::vector<std::optional<int128_t>> longDecimals; | ||
for (auto i = 0; i < ITERATIONS; ++i) { | ||
if (!hasNull || i % 15) { | ||
longDecimals.emplace_back(HugeInt::build(123 * i, 456 * i + 789)); | ||
} else { | ||
longDecimals.emplace_back(std::nullopt); | ||
} | ||
} | ||
return longDecimals; | ||
}; | ||
|
||
auto longValues = genLongDecimals(false); | ||
testDataTypeWriter(DECIMAL(38, 4), longValues, 0 /*sequence*/, format); | ||
longValues = genLongDecimals(true); | ||
testDataTypeWriter(DECIMAL(38, 4), longValues, 0 /*sequence*/, format); | ||
} | ||
|
||
TEST_F(ColumnWriterTest, TestTimestampEpochWriter) { | ||
std::vector<std::optional<Timestamp>> data; | ||
// This value will be corrupted. verified in verifyValue method. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a test for this? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
TEST_F(ZigZagTest, hugeInt)
. Thanks.