Skip to content
Closed
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
26 changes: 17 additions & 9 deletions bolt/functions/sparksql/UnscaledValueFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,23 @@ class UnscaledValueFunction final : public exec::VectorFunction {
args[0]->type()->isShortDecimal(),
"Expect short decimal type, but got: {}",
args[0]->type());
exec::DecodedArgs decodedArgs(rows, args, context);
auto decimalVector = decodedArgs.at(0);
context.ensureWritable(rows, BIGINT(), result);
result->clearNulls(rows);
auto flatResult =
result->asUnchecked<FlatVector<int64_t>>()->mutableRawValues();
rows.applyToSelected([&](auto row) {
flatResult[row] = decimalVector->valueAt<int64_t>(row);
});
VectorPtr localResult;
const auto& arg = args[0];
if (arg->isConstantEncoding()) {
auto value = arg->as<ConstantVector<int64_t>>()->valueAt(0);
localResult = std::make_shared<ConstantVector<int64_t>>(
context.pool(), rows.end(), false, BIGINT(), std::move(value));
} else {
auto flatInput = arg->asFlatVector<int64_t>();
localResult = std::make_shared<FlatVector<int64_t>>(
context.pool(),
BIGINT(),
nullptr,
rows.end(),
flatInput->values(),
std::vector<BufferPtr>());
}
context.moveOrCopyResult(localResult, rows, result);
}
};
} // namespace
Expand Down
20 changes: 10 additions & 10 deletions bolt/functions/sparksql/tests/UnscaledValueFunctionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ namespace {
class UnscaledValueFunctionTest : public SparkFunctionBaseTest {};

TEST_F(UnscaledValueFunctionTest, unscaledValue) {
auto testUnscaledValue = [&](const std::vector<int64_t>& unscaledValue,
const TypePtr& decimalType) {
auto input = makeFlatVector<int64_t>(unscaledValue, decimalType);
auto expected = makeFlatVector<int64_t>(unscaledValue);
auto result = evaluate("unscaled_value(c0)", makeRowVector({input}));
assertEqualVectors(expected, result);
};
auto input =
makeFlatVector<int64_t>({1000, 2000, -3000, -4000}, DECIMAL(18, 3));
auto expected = makeFlatVector<int64_t>({1000, 2000, -3000, -4000});
auto invalidInput = makeFlatVector<int64_t>({0, 0, 0, 0}, DECIMAL(20, 3));

testUnscaledValue({1000, 2000, -3000, -4000}, DECIMAL(18, 3));
testEncodings(
makeTypedExpr("unscaled_value(c0)", ROW({"c0"}, {input->type()})),
{input},
expected);

BOLT_ASSERT_THROW(
testUnscaledValue({1000, 2000, -3000, -4000}, DECIMAL(20, 3)),
BOLT_ASSERT_USER_THROW(
evaluate("unscaled_value(c0)", makeRowVector({invalidInput})),
"Expect short decimal type, but got: DECIMAL(20, 3)");
}

Expand Down
Loading