Skip to content

Commit 640cf3f

Browse files
authored
Add function castBIGINT_timestamp (apache#22)
* Add function castBIGINT_timestamp * fix * wip
1 parent 684ac60 commit 640cf3f

12 files changed

Lines changed: 72 additions & 10 deletions

File tree

cpp/src/arrow/compute/kernels/scalar_cast_test.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,17 @@ TEST(Cast, TimestampToTimestamp) {
10411041
options.allow_time_truncate = true;
10421042
CheckCast(will_be_truncated, coarse, options);
10431043
}
1044+
1045+
for (auto types : {
1046+
TimestampTypePair{timestamp(TimeUnit::MILLI, "UTC+8"), timestamp(TimeUnit::MILLI)}
1047+
}) {
1048+
auto coarse = ArrayFromJSON(types.coarse, "[0, null, 200000000000, 1000000000, 2000000000]");
1049+
auto promoted =
1050+
ArrayFromJSON(types.fine, "[0, null, 200000000000, 1000000000, 2000000000]");
1051+
1052+
// multiply/promote
1053+
CheckCast(coarse, promoted);
1054+
}
10441055
}
10451056

10461057
TEST(Cast, TimestampZeroCopy) {

cpp/src/gandiva/function_registry_common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ inline DataTypePtr time32() { return arrow::time32(arrow::TimeUnit::MILLI); }
5454
inline DataTypePtr time64() { return arrow::time64(arrow::TimeUnit::MICRO); }
5555

5656
inline DataTypePtr timestamp() { return arrow::timestamp(arrow::TimeUnit::MILLI); }
57+
58+
inline DataTypePtr timestampusutc() { return arrow::timestamp(arrow::TimeUnit::MICRO, "UTC"); }
59+
5760
inline DataTypePtr decimal128() { return arrow::decimal(38, 0); }
5861

5962
struct KeyHash {
@@ -243,7 +246,7 @@ typedef std::unordered_map<const FunctionSignature*, const NativeFunction*, KeyH
243246

244247
// Iterate the inner macro over all date types
245248
#define DATE_TYPES(INNER, NAME, ALIASES) \
246-
INNER(NAME, ALIASES, date64), INNER(NAME, ALIASES, timestamp)
249+
INNER(NAME, ALIASES, date64), INNER(NAME, ALIASES, timestamp), INNER(NAME, ALIASES, timestampusutc)
247250

248251
// Iterate the inner macro over all time types
249252
#define TIME_TYPES(INNER, NAME, ALIASES) INNER(NAME, ALIASES, time32)

cpp/src/gandiva/function_registry_datetime.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ std::vector<NativeFunction> GetDateTimeFunctionRegistry() {
8484
NativeFunction("castTIME", {}, DataTypeVector{timestamp()}, time32(),
8585
kResultNullIfNull, "castTIME_timestamp"),
8686

87+
NativeFunction("castBIGINT", {}, DataTypeVector{timestamp()}, int64(),
88+
kResultNullIfNull, "castBIGINT_timestamp"),
89+
8790
NativeFunction("castBIGINT", {}, DataTypeVector{day_time_interval()}, int64(),
8891
kResultNullIfNull, "castBIGINT_daytimeinterval"),
8992

@@ -97,7 +100,14 @@ std::vector<NativeFunction> GetDateTimeFunctionRegistry() {
97100
kResultNullIfNull, "convertTimestampUnit_us"),
98101

99102
NativeFunction("castDATE", {}, DataTypeVector{date64()}, date32(),
100-
kResultNullIfNull, "castDATE_date64"),
103+
kResultNullIfNull, "castDATE32_date64"),
104+
105+
NativeFunction("castTIMESTAMP", {}, DataTypeVector{date32()}, timestamp(),
106+
kResultNullIfNull, "castTIMESTAMP_date32"),
107+
108+
NativeFunction("castDATE", {}, DataTypeVector{timestamp()}, date32(),
109+
kResultNullIfNull, "castDATE32_timestamp"),
110+
101111
DATE_TYPES(LAST_DAY_SAFE_NULL_IF_NULL, last_day, {})};
102112

103113
return date_time_fn_registry_;

cpp/src/gandiva/function_signature.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ bool DataTypeEquals(const DataTypePtr& left, const DataTypePtr& right) {
4545
return (dleft != NULL) && (dright != NULL) &&
4646
(dleft->byte_width() == dright->byte_width());
4747
}
48+
case arrow::Type::TIMESTAMP: {
49+
// Signature for timestamp treated the same if both are with zone or without zone.
50+
auto tleft = checked_cast<arrow::TimestampType *>(left.get());
51+
auto tright = checked_cast<arrow::TimestampType *>(right.get());
52+
return (tleft != NULL) && (tright != NULL) &&
53+
(tleft->unit() == tright->unit()) &&
54+
(tleft->timezone().empty() == tleft->timezone().empty());
55+
}
4856
default:
4957
return left->Equals(right);
5058
}

cpp/src/gandiva/gdv_function_stubs.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ int32_t gdv_fn_populate_varlen_vector(int64_t context_ptr, int8_t* data_ptr,
203203
INNER(date64) \
204204
INNER(date32) \
205205
INNER(time32) \
206-
INNER(timestamp)
206+
INNER(timestamp) \
207+
INNER(timestampusutc)
207208

208209
// Expand inner macro for all numeric types.
209210
#define SHA_VAR_LEN_PARAMS(INNER) \

cpp/src/gandiva/gdv_function_stubs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ using gdv_date64 = int64_t;
3939
using gdv_date32 = int32_t;
4040
using gdv_time32 = int32_t;
4141
using gdv_timestamp = int64_t;
42+
using gdv_timestampusutc = int64_t;
4243
using gdv_utf8 = char*;
4344
using gdv_binary = char*;
4445
using gdv_day_time_interval = int64_t;

cpp/src/gandiva/jni/jni_common.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,26 @@ DataTypePtr ProtoTypeToTime64(const types::ExtGandivaType& ext_type) {
155155
}
156156

157157
DataTypePtr ProtoTypeToTimestamp(const types::ExtGandivaType& ext_type) {
158+
arrow::TimeUnit::type unit;
158159
switch (ext_type.timeunit()) {
159160
case types::SEC:
160-
return arrow::timestamp(arrow::TimeUnit::SECOND);
161+
unit = arrow::TimeUnit::SECOND;
162+
break;
161163
case types::MILLISEC:
162-
return arrow::timestamp(arrow::TimeUnit::MILLI);
164+
unit = arrow::TimeUnit::MILLI;
165+
break;
163166
case types::MICROSEC:
164-
return arrow::timestamp(arrow::TimeUnit::MICRO);
167+
unit = arrow::TimeUnit::MICRO;
168+
break;
165169
case types::NANOSEC:
166-
return arrow::timestamp(arrow::TimeUnit::NANO);
170+
unit = arrow::TimeUnit::NANO;
171+
break;
167172
default:
168173
std::cerr << "Unknown time unit: " << ext_type.timeunit() << " for timestamp\n";
169174
return nullptr;
170175
}
176+
const std::string& zone_id = ext_type.timezone();
177+
return arrow::timestamp(unit, zone_id);
171178
}
172179

173180
DataTypePtr ProtoTypeToInterval(const types::ExtGandivaType& ext_type) {

cpp/src/gandiva/precompiled/arithmetic_ops.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern "C" {
4141
INNER(NAME, date64, OP) \
4242
INNER(NAME, date32, OP) \
4343
INNER(NAME, timestamp, OP) \
44+
INNER(NAME, timestampusutc, OP) \
4445
INNER(NAME, time32, OP)
4546

4647
#define NUMERIC_DATE_TYPES(INNER, NAME, OP) \
@@ -253,6 +254,7 @@ NUMERIC_TYPES(VALIDITY_OP, isnumeric, +)
253254
INNER(date32) \
254255
INNER(date64) \
255256
INNER(timestamp) \
257+
INNER(timestampusutc) \
256258
INNER(time32)
257259

258260
#define NUMERIC_BOOL_DATE_FUNCTION(INNER) \

cpp/src/gandiva/precompiled/hash.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ FORCE_INLINE gdv_int32 hash64_spark_int64_int32(gdv_int64 val, gdv_boolean is_va
307307
INNER(NAME, date64) \
308308
INNER(NAME, date32) \
309309
INNER(NAME, time32) \
310-
INNER(NAME, timestamp)
310+
INNER(NAME, timestamp) \
311+
INNER(NAME, timestampusutc)
311312

312313
NUMERIC_BOOL_DATE_TYPES(HASH32_OP, hash)
313314
NUMERIC_BOOL_DATE_TYPES(HASH32_OP, hash32)

cpp/src/gandiva/precompiled/time.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,18 @@ gdv_timestamp convertTimestampUnit_us(gdv_timestamp timestamp_in_micro) {
813813
return timestamp_in_micro / 1000;
814814
}
815815

816-
gdv_date32 castDATE_date64(gdv_date64 date_in_millis) {
816+
gdv_date32 castDATE32_date64(gdv_date64 date_in_millis) {
817817
return static_cast<gdv_date32>(date_in_millis / (MILLIS_IN_DAY));
818818
}
819819

820+
gdv_timestamp castTIMESTAMP_date32(gdv_date32 in_day) {
821+
return static_cast<gdv_date32>(in_day * (MILLIS_IN_DAY));
822+
}
823+
824+
gdv_date32 castDATE32_timestamp(gdv_timestamp timestamp_in_millis) {
825+
return static_cast<gdv_date32>(timestamp_in_millis / (MILLIS_IN_DAY));
826+
}
827+
820828
const char* castVARCHAR_timestamp_int64(gdv_int64 context, gdv_timestamp in,
821829
gdv_int64 length, gdv_int32* out_len) {
822830
gdv_int64 year = extractYear_timestamp(in);
@@ -877,6 +885,11 @@ gdv_int64 extractMillis_daytimeinterval(gdv_day_time_interval in) {
877885
return static_cast<gdv_int64>(millis);
878886
}
879887

888+
FORCE_INLINE
889+
gdv_int64 castBIGINT_timestamp(gdv_timestamp in) {
890+
return in;
891+
}
892+
880893
FORCE_INLINE
881894
gdv_int64 castBIGINT_daytimeinterval(gdv_day_time_interval in) {
882895
return extractMillis_daytimeinterval(in) +

0 commit comments

Comments
 (0)