Skip to content

Commit

Permalink
GDV-95:[C++]Match gandiva mod operator to dremio for mod zero. (apach…
Browse files Browse the repository at this point in the history
…e#74)

* Temporarily matching what the dremio does for mod zero.
* Used the latest Arrow APIs for allocating buffers.
  • Loading branch information
praveenbingo committed Sep 10, 2018
1 parent 21c9d8b commit 495956a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
9 changes: 5 additions & 4 deletions cpp/src/gandiva/codegen/projector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,15 @@ Status Projector::AllocArrayData(const DataTypePtr &type, int num_records,
}

arrow::Status astatus;
auto null_bitmap = std::make_shared<arrow::PoolBuffer>(pool_);
astatus = null_bitmap->Resize(arrow::BitUtil::BytesForBits(num_records));
std::shared_ptr<arrow::ResizableBuffer> null_bitmap;
int64_t size = arrow::BitUtil::BytesForBits(num_records);
astatus = arrow::AllocateResizableBuffer(pool_, size, &null_bitmap);
GANDIVA_RETURN_ARROW_NOT_OK(astatus);

auto data = std::make_shared<arrow::PoolBuffer>(pool_);
std::shared_ptr<arrow::ResizableBuffer> data;
const auto &fw_type = dynamic_cast<const arrow::FixedWidthType &>(*type);
int64_t data_len = arrow::BitUtil::BytesForBits(num_records * fw_type.bit_width());
astatus = data->Resize(data_len);
astatus = arrow::AllocateResizableBuffer(pool_, data_len, &data);
GANDIVA_RETURN_ARROW_NOT_OK(astatus);

*array_data = arrow::ArrayData::Make(type, num_records, {null_bitmap, data});
Expand Down
8 changes: 4 additions & 4 deletions cpp/src/gandiva/precompiled/arithmetic_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ extern "C" {
DATE_TYPES(INNER, NAME, OP) \
INNER(NAME, boolean, OP)

#define BINARY_GENERIC_OP(NAME, IN_TYPE1, IN_TYPE2, OUT_TYPE, OP) \
#define MOD_OP(NAME, IN_TYPE1, IN_TYPE2, OUT_TYPE) \
FORCE_INLINE \
OUT_TYPE NAME##_##IN_TYPE1##_##IN_TYPE2(IN_TYPE1 left, IN_TYPE2 right) { \
return left OP right; \
return (right == 0 ? left : left % right); \
}

// Symmetric binary fns : left, right params and return type are same.
Expand All @@ -60,8 +60,8 @@ NUMERIC_TYPES(BINARY_SYMMETRIC, subtract, -)
NUMERIC_TYPES(BINARY_SYMMETRIC, multiply, *)
NUMERIC_TYPES(BINARY_SYMMETRIC, divide, /)

BINARY_GENERIC_OP(mod, int64, int32, int32, %)
BINARY_GENERIC_OP(mod, int64, int64, int64, %)
MOD_OP(mod, int64, int32, int32)
MOD_OP(mod, int64, int64, int64)

// Relational binary fns : left, right params are same, return is bool.
#define BINARY_RELATIONAL(NAME, TYPE, OP) \
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/gandiva/precompiled/arithmetic_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ TEST(TestArithmeticOps, TestIsDistinctFrom) {
EXPECT_EQ(is_not_distinct_from_int32_int32(1000, true, 1000, true), true);
}

TEST(TestArithmeticOps, TestMod) { EXPECT_EQ(mod_int64_int32(10, 0), 10); }

} // namespace gandiva
2 changes: 2 additions & 0 deletions cpp/src/gandiva/precompiled/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ int64 date_trunc_Week_timestamp(timestamp);

int32 mem_compare(const char *left, int32 left_len, const char *right, int32 right_len);

int32 mod_int64_int32(int64 left, int32 right);

} // extern "C"

#endif // PRECOMPILED_TYPES_H

0 comments on commit 495956a

Please sign in to comment.