Skip to content

Commit d9119ee

Browse files
committed
Change definition of to_timestamp function to work for numeric types
1 parent 33230ca commit d9119ee

5 files changed

Lines changed: 38 additions & 26 deletions

File tree

cpp/src/gandiva/function_registry_common.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,6 @@ typedef std::unordered_map<const FunctionSignature*, const NativeFunction*, KeyH
174174
NativeFunction(#NAME, std::vector<std::string> ALIASES, DataTypeVector{TYPE()}, \
175175
date64(), kResultNullIfNull, ARROW_STRINGIFY(NAME##_from_##TYPE))
176176

177-
// To timestamp functions (used with data/time types) that :
178-
// - NULL handling is of type NULL_IF_NULL
179-
//
180-
// The pre-compiled fn name includes the base name & input type name. eg:
181-
// - to_timestamp_date64
182-
#define TO_TIMESTAMP_SAFE_NULL_IF_NULL(NAME, ALIASES, TYPE) \
183-
NativeFunction(#NAME, std::vector<std::string> ALIASES, DataTypeVector{TYPE()}, \
184-
timestamp(), kResultNullIfNull, ARROW_STRINGIFY(NAME##_##TYPE))
185-
186177
// Hash32 functions that :
187178
// - NULL handling is of type NULL_NEVER
188179
//
@@ -238,12 +229,16 @@ typedef std::unordered_map<const FunctionSignature*, const NativeFunction*, KeyH
238229
NativeFunction::kNeedsContext | NativeFunction::kCanReturnErrors)
239230

240231
// Iterate the inner macro over all numeric types
241-
#define NUMERIC_TYPES(INNER, NAME, ALIASES) \
232+
#define BASE_NUMERIC_TYPES(INNER, NAME, ALIASES) \
242233
INNER(NAME, ALIASES, int8), INNER(NAME, ALIASES, int16), INNER(NAME, ALIASES, int32), \
243234
INNER(NAME, ALIASES, int64), INNER(NAME, ALIASES, uint8), \
244235
INNER(NAME, ALIASES, uint16), INNER(NAME, ALIASES, uint32), \
245236
INNER(NAME, ALIASES, uint64), INNER(NAME, ALIASES, float32), \
246-
INNER(NAME, ALIASES, float64), INNER(NAME, ALIASES, decimal128)
237+
INNER(NAME, ALIASES, float64)
238+
239+
// Iterate the inner macro over all base numeric types
240+
#define NUMERIC_TYPES(INNER, NAME, ALIASES) \
241+
BASE_NUMERIC_TYPES(INNER, NAME, ALIASES), INNER(NAME, ALIASES, decimal128)
247242

248243
// Iterate the inner macro over numeric and date/time types
249244
#define NUMERIC_DATE_TYPES(INNER, NAME, ALIASES) \

cpp/src/gandiva/function_registry_datetime.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ namespace gandiva {
2929
DATE_TYPES(INNER, name##Hour, {}), DATE_TYPES(INNER, name##Minute, {}), \
3030
DATE_TYPES(INNER, name##Second, {})
3131

32+
#define TO_TIMESTAMP_SAFE_NULL_IF_NULL(NAME, ALIASES, TYPE) \
33+
NativeFunction(#NAME, std::vector<std::string> ALIASES, DataTypeVector{TYPE()}, \
34+
timestamp(), kResultNullIfNull, ARROW_STRINGIFY(NAME##_##TYPE))
35+
3236
#define TIME_EXTRACTION_FNS(name) \
3337
TIME_TYPES(EXTRACT_SAFE_NULL_IF_NULL, name##Hour, {}), \
3438
TIME_TYPES(EXTRACT_SAFE_NULL_IF_NULL, name##Minute, {}), \
@@ -88,7 +92,7 @@ std::vector<NativeFunction> GetDateTimeFunctionRegistry() {
8892

8993
DATE_TYPES(LAST_DAY_SAFE_NULL_IF_NULL, last_day, {}),
9094

91-
DATE_TYPES(TO_TIMESTAMP_SAFE_NULL_IF_NULL, to_timestamp, {})};
95+
BASE_NUMERIC_TYPES(TO_TIMESTAMP_SAFE_NULL_IF_NULL, to_timestamp, {})};
9296

9397
return date_time_fn_registry_;
9498
}

cpp/src/gandiva/precompiled/time.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ extern "C" {
4040
INNER(date64) \
4141
INNER(timestamp)
4242

43+
// Expand inner macro for all base numeric types.
44+
#define NUMERIC_TYPES(INNER) \
45+
INNER(int8) \
46+
INNER(int16) \
47+
INNER(int32) \
48+
INNER(int64) \
49+
INNER(uint8) \
50+
INNER(uint16) \
51+
INNER(uint32) \
52+
INNER(uint64) \
53+
INNER(float32) \
54+
INNER(float64)
55+
4356
// Extract millennium
4457
#define EXTRACT_MILLENNIUM(TYPE) \
4558
FORCE_INLINE \
@@ -834,6 +847,6 @@ gdv_int64 castBIGINT_daytimeinterval(gdv_day_time_interval in) {
834847
return static_cast<gdv_timestamp>(seconds) * MILLIS_IN_SEC; \
835848
}
836849

837-
DATE_TYPES(TO_TIMESTAMP)
850+
NUMERIC_TYPES(TO_TIMESTAMP);
838851

839852
} // extern "C"

cpp/src/gandiva/precompiled/time_test.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -744,30 +744,30 @@ TEST(TestTime, TestLastDay) {
744744
}
745745

746746
TEST(TestTime, TestToTimestamp) {
747-
gdv_timestamp ts = StringToTimestamp("1970-01-01 00:00:00");
747+
auto ts = StringToTimestamp("1970-01-01 00:00:00");
748748
auto seconds = 0;
749-
EXPECT_EQ(ts, to_timestamp_date64(seconds));
750-
EXPECT_EQ(ts, to_timestamp_timestamp(seconds));
749+
EXPECT_EQ(ts, to_timestamp_int32(seconds));
750+
EXPECT_EQ(ts, to_timestamp_int64(seconds));
751751

752752
ts = StringToTimestamp("1970-01-01 00:00:01");
753753
seconds = 1;
754-
EXPECT_EQ(ts, to_timestamp_date64(seconds));
755-
EXPECT_EQ(ts, to_timestamp_timestamp(seconds));
754+
EXPECT_EQ(ts, to_timestamp_int32(seconds));
755+
EXPECT_EQ(ts, to_timestamp_int64(seconds));
756756

757757
ts = StringToTimestamp("1970-01-01 00:01:00");
758758
seconds = 60;
759-
EXPECT_EQ(ts, to_timestamp_date64(seconds));
760-
EXPECT_EQ(ts, to_timestamp_timestamp(seconds));
759+
EXPECT_EQ(ts, to_timestamp_int32(seconds));
760+
EXPECT_EQ(ts, to_timestamp_int64(seconds));
761761

762762
ts = StringToTimestamp("1970-01-01 01:00:00");
763763
seconds = 60 * 60;
764-
EXPECT_EQ(ts, to_timestamp_date64(seconds));
765-
EXPECT_EQ(ts, to_timestamp_timestamp(seconds));
764+
EXPECT_EQ(ts, to_timestamp_int32(seconds));
765+
EXPECT_EQ(ts, to_timestamp_int64(seconds));
766766

767767
ts = StringToTimestamp("1970-01-02 00:00:00");
768768
seconds = 24 * 60 * 60;
769-
EXPECT_EQ(ts, to_timestamp_date64(seconds));
770-
EXPECT_EQ(ts, to_timestamp_timestamp(seconds));
769+
EXPECT_EQ(ts, to_timestamp_int32(seconds));
770+
EXPECT_EQ(ts, to_timestamp_int64(seconds));
771771
}
772772

773773
} // namespace gandiva

cpp/src/gandiva/precompiled/types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ gdv_int64 add_int32_timestamp(gdv_int32, gdv_timestamp);
9999
gdv_int64 date_add_int64_timestamp(gdv_int64, gdv_timestamp);
100100
gdv_timestamp add_date64_int64(gdv_date64, gdv_int64);
101101

102-
gdv_timestamp to_timestamp_date64(gdv_date64);
103-
gdv_timestamp to_timestamp_timestamp(gdv_timestamp);
102+
gdv_timestamp to_timestamp_int32(gdv_int32);
103+
gdv_timestamp to_timestamp_int64(gdv_int64);
104104

105105
gdv_int64 date_sub_timestamp_int32(gdv_timestamp, gdv_int32);
106106
gdv_int64 subtract_timestamp_int32(gdv_timestamp, gdv_int32);

0 commit comments

Comments
 (0)