diff --git a/be/src/vec/runtime/vdatetime_value.cpp b/be/src/vec/runtime/vdatetime_value.cpp index f0ef48f1f39a2e..a8db4d6f66fd83 100644 --- a/be/src/vec/runtime/vdatetime_value.cpp +++ b/be/src/vec/runtime/vdatetime_value.cpp @@ -1965,6 +1965,9 @@ 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], 0, 0, 0, 0)) { + 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