Skip to content

Commit 6d92694

Browse files
durnerwesm
authored andcommitted
ARROW-8334: [C++] [Gandiva] Missing DATE32 in LLVM Types
Closes apache#6835 from durner/date32 Authored-by: Dominik Durner <durner@zoho.com> Signed-off-by: Wes McKinney <wesm+git@apache.org>
1 parent 739350e commit 6d92694

10 files changed

Lines changed: 59 additions & 1 deletion

File tree

cpp/src/gandiva/function_registry_arithmetic.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ std::vector<NativeFunction> GetArithmeticFunctionRegistry() {
6161
"castDECIMALNullOnOverflow_decimal128"),
6262

6363
UNARY_SAFE_NULL_IF_NULL(castDATE, {}, int64, date64),
64+
UNARY_SAFE_NULL_IF_NULL(castDATE, {}, int32, date32),
6465

6566
// add/sub/multiply/divide/mod
6667
BINARY_SYMMETRIC_FN(add, {}), BINARY_SYMMETRIC_FN(subtract, {}),

cpp/src/gandiva/function_registry_common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace gandiva {
3434

3535
using arrow::binary;
3636
using arrow::boolean;
37+
using arrow::date32;
3738
using arrow::date64;
3839
using arrow::day_time_interval;
3940
using arrow::float32;
@@ -205,7 +206,7 @@ typedef std::unordered_map<const FunctionSignature*, const NativeFunction*, KeyH
205206
// Iterate the inner macro over numeric and date/time types
206207
#define NUMERIC_DATE_TYPES(INNER, NAME, ALIASES) \
207208
NUMERIC_TYPES(INNER, NAME, ALIASES), DATE_TYPES(INNER, NAME, ALIASES), \
208-
TIME_TYPES(INNER, NAME, ALIASES)
209+
TIME_TYPES(INNER, NAME, ALIASES), INNER(NAME, ALIASES, date32)
209210

210211
// Iterate the inner macro over all date types
211212
#define DATE_TYPES(INNER, NAME, ALIASES) \

cpp/src/gandiva/llvm_generator.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,10 @@ void LLVMGenerator::Visitor::Visit(const LiteralDex& dex) {
692692
break;
693693
}
694694

695+
case arrow::Type::DATE32:
696+
value = types->i32_constant(arrow::util::get<int32_t>(dex.holder()));
697+
break;
698+
695699
case arrow::Type::DATE64:
696700
value = types->i64_constant(arrow::util::get<int64_t>(dex.holder()));
697701
break;

cpp/src/gandiva/llvm_types.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ LLVMTypes::LLVMTypes(llvm::LLVMContext& context) : context_(context) {
3434
{arrow::Type::type::UINT64, i64_type()},
3535
{arrow::Type::type::FLOAT, float_type()},
3636
{arrow::Type::type::DOUBLE, double_type()},
37+
{arrow::Type::type::DATE32, i32_type()},
3738
{arrow::Type::type::DATE64, i64_type()},
3839
{arrow::Type::type::TIME32, i32_type()},
3940
{arrow::Type::type::TIME64, i64_type()},

cpp/src/gandiva/precompiled/arithmetic_ops.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern "C" {
3636
// Expand inner macros for all date/time types.
3737
#define DATE_TYPES(INNER, NAME, OP) \
3838
INNER(NAME, date64, OP) \
39+
INNER(NAME, date32, OP) \
3940
INNER(NAME, timestamp, OP) \
4041
INNER(NAME, time32, OP)
4142

@@ -139,6 +140,7 @@ NUMERIC_TYPES(VALIDITY_OP, isnumeric, +)
139140
INNER(float64)
140141

141142
#define DATE_FUNCTION(INNER) \
143+
INNER(date32) \
142144
INNER(date64) \
143145
INNER(timestamp) \
144146
INNER(time32)

cpp/src/gandiva/precompiled/hash.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ FORCE_INLINE gdv_int32 hash32(double val, gdv_int32 seed) {
169169
INNER(NAME, float64) \
170170
INNER(NAME, boolean) \
171171
INNER(NAME, date64) \
172+
INNER(NAME, date32) \
172173
INNER(NAME, time32) \
173174
INNER(NAME, timestamp)
174175

cpp/src/gandiva/precompiled/time.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extern "C" {
3737

3838
// Expand inner macro for all date types.
3939
#define DATE_TYPES(INNER) \
40+
INNER(date32) \
4041
INNER(date64) \
4142
INNER(timestamp)
4243

@@ -453,6 +454,8 @@ DATE_TRUNC_FUNCTIONS(timestamp)
453454

454455
FORCE_INLINE
455456
gdv_date64 castDATE_int64(gdv_int64 in) { return in; }
457+
FORCE_INLINE
458+
gdv_date32 castDATE_int32(gdv_int32 in) { return in; }
456459

457460
static int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
458461

cpp/src/gandiva/precompiled/types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ using gdv_uint64 = uint64_t;
3333
using gdv_float32 = float;
3434
using gdv_float64 = double;
3535
using gdv_date64 = int64_t;
36+
using gdv_date32 = int32_t;
3637
using gdv_time32 = int32_t;
3738
using gdv_timestamp = int64_t;
3839
using gdv_utf8 = char*;
@@ -166,6 +167,10 @@ gdv_int32 utf8_length(gdv_int64 context, const char* data, gdv_int32 data_len);
166167

167168
gdv_date64 castDATE_utf8(int64_t execution_context, const char* input, gdv_int32 length);
168169

170+
gdv_date64 castDATE_int64(gdv_int64 date);
171+
172+
gdv_date32 castDATE_int32(gdv_int32 date);
173+
169174
gdv_timestamp castTIMESTAMP_utf8(int64_t execution_context, const char* input,
170175
gdv_int32 length);
171176
gdv_timestamp castTIMESTAMP_date64(gdv_date64);

cpp/src/gandiva/tests/date_time_test.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
namespace gandiva {
2727

2828
using arrow::boolean;
29+
using arrow::date32;
2930
using arrow::date64;
3031
using arrow::float32;
3132
using arrow::int32;
3233
using arrow::int64;
34+
using arrow::timestamp;
3335

3436
class TestProjector : public ::testing::Test {
3537
public:
@@ -130,6 +132,42 @@ TEST_F(TestProjector, TestIsNull) {
130132
EXPECT_ARROW_ARRAY_EQUALS(exp_isnotnull, outputs.at(1));
131133
}
132134

135+
TEST_F(TestProjector, TestDate32IsNull) {
136+
auto d0 = field("d0", date32());
137+
auto schema = arrow::schema({d0});
138+
139+
// output fields
140+
auto b0 = field("isnull", boolean());
141+
142+
// isnull and isnotnull
143+
auto isnull_expr = TreeExprBuilder::MakeExpression("isnull", {d0}, b0);
144+
145+
std::shared_ptr<Projector> projector;
146+
auto status = Projector::Make(schema, {isnull_expr}, TestConfiguration(), &projector);
147+
ASSERT_TRUE(status.ok());
148+
149+
int num_records = 4;
150+
std::vector<int32_t> d0_data = {0, 100, 0, 1000};
151+
auto validity = {false, true, false, true};
152+
auto d0_array =
153+
MakeArrowTypeArray<arrow::Date32Type, int32_t>(date32(), d0_data, validity);
154+
155+
// expected output
156+
auto exp_isnull =
157+
MakeArrowArrayBool({true, false, true, false}, {true, true, true, true});
158+
159+
// prepare input record batch
160+
auto in_batch = arrow::RecordBatch::Make(schema, num_records, {d0_array});
161+
162+
// Evaluate expression
163+
arrow::ArrayVector outputs;
164+
status = projector->Evaluate(*in_batch, pool_, &outputs);
165+
EXPECT_TRUE(status.ok());
166+
167+
// Validate results
168+
EXPECT_ARROW_ARRAY_EQUALS(exp_isnull, outputs.at(0));
169+
}
170+
133171
TEST_F(TestProjector, TestDateTime) {
134172
auto field0 = field("f0", date64());
135173
auto field2 = field("f2", timestamp(arrow::TimeUnit::MILLI));

cpp/src/gandiva/tree_expr_builder.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ NodePtr TreeExprBuilder::MakeNull(DataTypePtr data_type) {
9191
case arrow::Type::STRING:
9292
case arrow::Type::BINARY:
9393
return std::make_shared<LiteralNode>(data_type, LiteralHolder(empty), true);
94+
case arrow::Type::DATE32:
95+
return std::make_shared<LiteralNode>(data_type, LiteralHolder((int32_t)0), true);
9496
case arrow::Type::DATE64:
9597
return std::make_shared<LiteralNode>(data_type, LiteralHolder((int64_t)0), true);
9698
case arrow::Type::TIME32:

0 commit comments

Comments
 (0)