Skip to content

Commit

Permalink
Add check for input date string (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE authored and zhztheplayer committed Feb 28, 2022
1 parent 10ef761 commit b77276e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cpp/src/gandiva/function_registry_datetime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ std::vector<NativeFunction> GetDateTimeFunctionRegistry() {
kResultNullIfNull, "castTIMESTAMP_utf8",
NativeFunction::kNeedsContext | NativeFunction::kCanReturnErrors),

NativeFunction("castTIMESTAMP_with_validation_check", {}, DataTypeVector{utf8()}, timestamp(),
kResultNullInternal, "castTIMESTAMP_with_validation_check_utf8",
NativeFunction::kNeedsContext | NativeFunction::kCanReturnErrors),

NativeFunction("castTIMESTAMP_withCarrying", {}, DataTypeVector{utf8()}, timestamp(),
kResultNullInternal, "castTIMESTAMP_withCarrying_utf8",
NativeFunction::kNeedsContext | NativeFunction::kCanReturnErrors),
Expand Down
17 changes: 17 additions & 0 deletions cpp/src/gandiva/precompiled/time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,23 @@ gdv_timestamp castTIMESTAMP_utf8(int64_t context, const char* input, gdv_int32 l
return std::chrono::time_point_cast<milliseconds>(date_time).time_since_epoch().count();
}

gdv_timestamp castTIMESTAMP_with_validation_check_utf8(int64_t context, const char* input,
gdv_int32 length, bool in_valid,
bool* out_valid) {
if (!in_valid) {
*out_valid = false;
return 0;
}
// Suppose the input is yyyy-MM-dd.
// TODO: check validaition during/after parsing.
if (length > 10) {
*out_valid = false;
return 0;
}
return castTIMESTAMP_withCarrying_utf8(context, input, length, in_valid, out_valid);
}


/*
* Input consists of mandatory and optional fields.
* Mandatory fields are year, month and day.
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/gandiva/precompiled/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ gdv_timestamp castTIMESTAMP_utf8(int64_t execution_context, const char* input,
gdv_timestamp castTIMESTAMP_withCarrying_utf8(int64_t context, const char* input,
gdv_int32 length, bool in_valid,
bool* out_valid);
gdv_timestamp castTIMESTAMP_with_validation_check_utf8(int64_t context, const char* input,
gdv_int32 length, bool in_valid,
bool* out_valid);
gdv_timestamp castTIMESTAMP_date64(gdv_date64);
gdv_timestamp castTIMESTAMP_int64(gdv_int64);
gdv_date64 castDATE_timestamp(gdv_timestamp);
Expand Down

0 comments on commit b77276e

Please sign in to comment.