From 48862c678442c38afcc961631834b0d7ad8fd8b9 Mon Sep 17 00:00:00 2001 From: PHILO-HE Date: Fri, 17 Dec 2021 10:52:37 +0800 Subject: [PATCH] Add check for input date string (#59) --- cpp/src/gandiva/function_registry_datetime.cc | 4 ++++ cpp/src/gandiva/precompiled/time.cc | 17 +++++++++++++++++ cpp/src/gandiva/precompiled/types.h | 3 +++ 3 files changed, 24 insertions(+) diff --git a/cpp/src/gandiva/function_registry_datetime.cc b/cpp/src/gandiva/function_registry_datetime.cc index 1e003f818db2a..b222967e53d78 100644 --- a/cpp/src/gandiva/function_registry_datetime.cc +++ b/cpp/src/gandiva/function_registry_datetime.cc @@ -69,6 +69,10 @@ std::vector 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), diff --git a/cpp/src/gandiva/precompiled/time.cc b/cpp/src/gandiva/precompiled/time.cc index 48601048ad82b..525b60178428c 100644 --- a/cpp/src/gandiva/precompiled/time.cc +++ b/cpp/src/gandiva/precompiled/time.cc @@ -871,6 +871,23 @@ gdv_timestamp castTIMESTAMP_utf8(int64_t context, const char* input, gdv_int32 l return std::chrono::time_point_cast(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. diff --git a/cpp/src/gandiva/precompiled/types.h b/cpp/src/gandiva/precompiled/types.h index 2e360d3841642..208f71476196a 100644 --- a/cpp/src/gandiva/precompiled/types.h +++ b/cpp/src/gandiva/precompiled/types.h @@ -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);