Skip to content

Commit

Permalink
Merge pull request apache#86 from zhztheplayer/oap-dev
Browse files Browse the repository at this point in the history
[oap-native-sql] Gandiva: Add bitwise_or, bitwise_and, bitwise_xor, bitwise_not
  • Loading branch information
zhouyuan authored Aug 11, 2020
2 parents 9f71a5e + 3326f2c commit f14fe17
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cpp/src/gandiva/function_registry_arithmetic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ std::vector<NativeFunction> GetArithmeticFunctionRegistry() {
BINARY_GENERIC_SAFE_NULL_IF_NULL(shift_right, {}, int32, int32, int32),
BINARY_GENERIC_SAFE_NULL_IF_NULL(shift_right, {}, int64, int32, int64),

// bitwise operators
BINARY_SYMMETRIC_SAFE_NULL_IF_NULL(bitwise_and, {}, int32),
BINARY_SYMMETRIC_SAFE_NULL_IF_NULL(bitwise_and, {}, int64),
BINARY_SYMMETRIC_SAFE_NULL_IF_NULL(bitwise_or, {}, int32),
BINARY_SYMMETRIC_SAFE_NULL_IF_NULL(bitwise_or, {}, int64),
BINARY_SYMMETRIC_SAFE_NULL_IF_NULL(bitwise_xor, {}, int32),
BINARY_SYMMETRIC_SAFE_NULL_IF_NULL(bitwise_xor, {}, int64),
UNARY_SAFE_NULL_IF_NULL(bitwise_not, {}, int32, int32),
UNARY_SAFE_NULL_IF_NULL(bitwise_not, {}, int64, int64),

// compare functions
BINARY_RELATIONAL_BOOL_FN(equal, ({"eq", "same"})),
BINARY_RELATIONAL_BOOL_FN(not_equal, {}),
Expand Down
15 changes: 15 additions & 0 deletions cpp/src/gandiva/precompiled/arithmetic_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ extern "C" {
NUMERIC_TYPES(BINARY_SYMMETRIC, add, +)
NUMERIC_TYPES(BINARY_SYMMETRIC, subtract, -)
NUMERIC_TYPES(BINARY_SYMMETRIC, multiply, *)
BINARY_SYMMETRIC(bitwise_and, int32, &)
BINARY_SYMMETRIC(bitwise_and, int64, &)
BINARY_SYMMETRIC(bitwise_or, int32, |)
BINARY_SYMMETRIC(bitwise_or, int64, |)
BINARY_SYMMETRIC(bitwise_xor, int32, ^)
BINARY_SYMMETRIC(bitwise_xor, int64, ^)

#undef BINARY_SYMMETRIC

Expand Down Expand Up @@ -308,6 +314,15 @@ SHIFT_RIGHT_INT(int64, int32)

#undef SHIFT_RIGHT_INT

#define BITWISE_NOT(TYPE) \
FORCE_INLINE \
gdv_##TYPE bitwise_not_##TYPE(gdv_##TYPE in) { return static_cast<gdv_##TYPE>(~in); }

BITWISE_NOT(int32)
BITWISE_NOT(int64)

#undef BITWISE_NOT

#undef DATE_FUNCTION
#undef DATE_TYPES
#undef NUMERIC_BOOL_DATE_TYPES
Expand Down
36 changes: 36 additions & 0 deletions cpp/src/gandiva/precompiled/arithmetic_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,40 @@ TEST(TestArithmeticOps, TestCastVarhcar) {
EXPECT_FALSE(ctx.has_error());
}

TEST(TestArithmeticOps, TestBitwiseOps) {
// bitwise AND
EXPECT_EQ(bitwise_and_int32_int32(0x0147D, 0x17159), 0x01059);
EXPECT_EQ(bitwise_and_int32_int32(0xFFFFFFCC, 0x00000297), 0x00000284);
EXPECT_EQ(bitwise_and_int32_int32(0x000, 0x285), 0x000);
EXPECT_EQ(bitwise_and_int64_int64(0x563672F83, 0x0D9FCF85B), 0x041642803);
EXPECT_EQ(bitwise_and_int64_int64(0xFFFFFFFFFFDA8F6A, 0xFFFFFFFFFFFF791C),
0xFFFFFFFFFFDA0908);
EXPECT_EQ(bitwise_and_int64_int64(0x6A5B1, 0x00000), 0x00000);

// bitwise OR
EXPECT_EQ(bitwise_or_int32_int32(0x0147D, 0x17159), 0x1757D);
EXPECT_EQ(bitwise_or_int32_int32(0xFFFFFFCC, 0x00000297), 0xFFFFFFDF);
EXPECT_EQ(bitwise_or_int32_int32(0x000, 0x285), 0x285);
EXPECT_EQ(bitwise_or_int64_int64(0x563672F83, 0x0D9FCF85B), 0x5FBFFFFDB);
EXPECT_EQ(bitwise_or_int64_int64(0xFFFFFFFFFFDA8F6A, 0xFFFFFFFFFFFF791C),
0xFFFFFFFFFFFFFF7E);
EXPECT_EQ(bitwise_or_int64_int64(0x6A5B1, 0x00000), 0x6A5B1);

// bitwise XOR
EXPECT_EQ(bitwise_xor_int32_int32(0x0147D, 0x17159), 0x16524);
EXPECT_EQ(bitwise_xor_int32_int32(0xFFFFFFCC, 0x00000297), 0XFFFFFD5B);
EXPECT_EQ(bitwise_xor_int32_int32(0x000, 0x285), 0x285);
EXPECT_EQ(bitwise_xor_int64_int64(0x563672F83, 0x0D9FCF85B), 0x5BA9BD7D8);
EXPECT_EQ(bitwise_xor_int64_int64(0xFFFFFFFFFFDA8F6A, 0xFFFFFFFFFFFF791C),
0X25F676);
EXPECT_EQ(bitwise_xor_int64_int64(0x6A5B1, 0x00000), 0x6A5B1);

// bitwise NOT
EXPECT_EQ(bitwise_not_int32(0x00017159), 0xFFFE8EA6);
EXPECT_EQ(bitwise_not_int32(0xFFFFF226), 0x00000DD9);
EXPECT_EQ(bitwise_not_int64(0x000000008BCAE9B4), 0xFFFFFFFF7435164B);
EXPECT_EQ(bitwise_not_int64(0xFFFFFF966C8D7997), 0x0000006993728668);
EXPECT_EQ(bitwise_not_int64(0x0000000000000000), 0xFFFFFFFFFFFFFFFF);
}

} // namespace gandiva
9 changes: 9 additions & 0 deletions cpp/src/gandiva/precompiled/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ gdv_uint64 abs_int64(gdv_int64);
gdv_float32 abs_float32(gdv_float32);
gdv_float64 abs_float64(gdv_float64);

gdv_int32 bitwise_and_int32_int32(gdv_int32 in1, gdv_int32 in2);
gdv_int64 bitwise_and_int64_int64(gdv_int64 in1, gdv_int64 in2);
gdv_int32 bitwise_or_int32_int32(gdv_int32 in1, gdv_int32 in2);
gdv_int64 bitwise_or_int64_int64(gdv_int64 in1, gdv_int64 in2);
gdv_int32 bitwise_xor_int32_int32(gdv_int32 in1, gdv_int32 in2);
gdv_int64 bitwise_xor_int64_int64(gdv_int64 in1, gdv_int64 in2);
gdv_int32 bitwise_not_int32(gdv_int32);
gdv_int64 bitwise_not_int64(gdv_int64);

gdv_float64 power_float64_float64(gdv_float64, gdv_float64);

gdv_float64 log_int32_int32(gdv_int64 context, gdv_int32 base, gdv_int32 value);
Expand Down

0 comments on commit f14fe17

Please sign in to comment.