From 53cfe302e494b1b652df87b676ab3f8199ddca64 Mon Sep 17 00:00:00 2001 From: jacktengg <18241664+jacktengg@users.noreply.github.com> Date: Thu, 29 Jun 2023 20:16:22 +0800 Subject: [PATCH 1/2] [fix](cast) fix coredump of cast string of invalid datetime --- be/src/vec/runtime/vdatetime_value.cpp | 4 +++ .../cast_function/test_cast_to_datetime.out | 19 ++++++++++++++ .../test_cast_to_datetime.groovy | 26 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 regression-test/data/query_p0/sql_functions/cast_function/test_cast_to_datetime.out create mode 100644 regression-test/suites/query_p0/sql_functions/cast_function/test_cast_to_datetime.groovy diff --git a/be/src/vec/runtime/vdatetime_value.cpp b/be/src/vec/runtime/vdatetime_value.cpp index f0ef48f1f39a2e..05bb3dd5d596c9 100644 --- a/be/src/vec/runtime/vdatetime_value.cpp +++ b/be/src/vec/runtime/vdatetime_value.cpp @@ -1965,6 +1965,10 @@ bool DateV2Value::from_date_str(const char* date_str, int len, int scale) { } if (num_field < 3) return false; + if (is_invalid(date_val[0], date_val[1], date_val[2], date_val[3], date_val[4], date_val[5], + date_val[6])) { + return false; + } format_datetime(date_val, carry_bits); return check_range_and_set_time(date_val[0], date_val[1], date_val[2], date_val[3], date_val[4], date_val[5], date_val[6]); diff --git a/regression-test/data/query_p0/sql_functions/cast_function/test_cast_to_datetime.out b/regression-test/data/query_p0/sql_functions/cast_function/test_cast_to_datetime.out new file mode 100644 index 00000000000000..7f2d61ad58f004 --- /dev/null +++ b/regression-test/data/query_p0/sql_functions/cast_function/test_cast_to_datetime.out @@ -0,0 +1,19 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !cast_string_to_datetime_invalid0 -- +\N + +-- !cast_string_to_datetime_invalid1 -- +\N + +-- !cast_string_to_datetime_invalid2 -- +\N + +-- !cast_string_to_datetime_invalid3 -- +\N + +-- !cast_string_to_datetime_invalid4 -- +\N + +-- !cast_string_to_datetime_invalid5 -- +\N + diff --git a/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_to_datetime.groovy b/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_to_datetime.groovy new file mode 100644 index 00000000000000..13ea3637259465 --- /dev/null +++ b/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_to_datetime.groovy @@ -0,0 +1,26 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("test_cast_to_datetime") { + // cast string of invalid datetime to datetime + qt_cast_string_to_datetime_invalid0 """ select cast("627492340" as datetime); """ + qt_cast_string_to_datetime_invalid1 """ select cast("" as datetime); """ + qt_cast_string_to_datetime_invalid2 """ select cast("1" as datetime); """ + qt_cast_string_to_datetime_invalid3 """ select cast("a" as datetime); """ + qt_cast_string_to_datetime_invalid4 """ select cast("null" as datetime); """ + qt_cast_string_to_datetime_invalid5 """ select cast(null as datetime); """ +} \ No newline at end of file From 1ef22bafbc4053f0e45591228352646196cff4ca Mon Sep 17 00:00:00 2001 From: jacktengg <18241664+jacktengg@users.noreply.github.com> Date: Fri, 30 Jun 2023 00:29:07 +0800 Subject: [PATCH 2/2] fix --- be/src/vec/runtime/vdatetime_value.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/be/src/vec/runtime/vdatetime_value.cpp b/be/src/vec/runtime/vdatetime_value.cpp index 05bb3dd5d596c9..a8db4d6f66fd83 100644 --- a/be/src/vec/runtime/vdatetime_value.cpp +++ b/be/src/vec/runtime/vdatetime_value.cpp @@ -1965,8 +1965,7 @@ bool DateV2Value::from_date_str(const char* date_str, int len, int scale) { } if (num_field < 3) return false; - if (is_invalid(date_val[0], date_val[1], date_val[2], date_val[3], date_val[4], date_val[5], - date_val[6])) { + if (is_invalid(date_val[0], date_val[1], date_val[2], 0, 0, 0, 0)) { return false; } format_datetime(date_val, carry_bits);