Skip to content

Commit

Permalink
add LongDecimal->ShortDecimal conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
majetideepak committed Aug 1, 2022
1 parent 02a472e commit 05301ee
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
15 changes: 1 addition & 14 deletions velox/functions/prestosql/aggregates/SimpleNumericAggregate.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,7 @@ class SimpleNumericAggregate : public exec::Aggregate {
if (!decoded.isNullAt(0)) {
updateDuplicateValues(
initialValue, decoded.valueAt<TInput>(0), rows.countSelected());
// Some DECIMAL type aggregations requires conversion from LongDecimal
// to ShortDecimal. However, this conversion is not desired as it
// requires a safety check. Specialize this case instead.
if constexpr (
std::is_same<TInput, ShortDecimal>::value &&
std::is_same<TData, LongDecimal>::value) {
updateNonNullValue<true, TData>(
group,
static_cast<LongDecimal>(initialValue).unscaledValue(),
updateSingleValue);
} else {
updateNonNullValue<true, TData>(
group, initialValue, updateSingleValue);
}
updateNonNullValue<true, TData>(group, initialValue, updateSingleValue);
}
} else if (decoded.mayHaveNulls()) {
rows.applyToSelected([&](vector_size_t i) {
Expand Down
1 change: 0 additions & 1 deletion velox/type/LongDecimal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ string to_string(facebook::velox::int128_t x) {
reverse(ans.begin(), ans.end());
return ans;
}

} // namespace std
9 changes: 7 additions & 2 deletions velox/type/LongDecimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ struct LongDecimal {
// Default required for creating vector with NULL values.
LongDecimal() = default;
constexpr LongDecimal(int128_t value) : unscaledValue_(value) {}
constexpr LongDecimal(ShortDecimal value)
: unscaledValue_(value.unscaledValue()) {}
LongDecimal(ShortDecimal& value) : unscaledValue_(value.unscaledValue()) {}

int128_t unscaledValue() const {
return unscaledValue_;
Expand Down Expand Up @@ -78,6 +77,12 @@ static inline LongDecimal operator*(const int value, const LongDecimal& other) {
return LongDecimal(value * other.unscaledValue());
}

inline ShortDecimal::ShortDecimal(LongDecimal& other) {
// Ensure the value fits.
VELOX_DCHECK((other.unscaledValue() >> 64) == 0);
unscaledValue_ = other.unscaledValue();
}

} // namespace facebook::velox

namespace folly {
Expand Down
3 changes: 3 additions & 0 deletions velox/type/ShortDecimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@

namespace facebook::velox {

struct LongDecimal;

struct ShortDecimal {
public:
// Default required for creating vector with NULL values.
ShortDecimal() = default;
constexpr ShortDecimal(int64_t value) : unscaledValue_(value) {}
ShortDecimal(LongDecimal& value);

int64_t unscaledValue() const {
return unscaledValue_;
Expand Down

0 comments on commit 05301ee

Please sign in to comment.