lowka commented on code in PR #5750: URL: https://github.com/apache/ignite-3/pull/5750#discussion_r2079756924
########## modules/sql-engine/src/integrationTest/java/org/apache/ignite/internal/sql/engine/datatypes/ItDateTimeCastFormatTest.java: ########## @@ -0,0 +1,851 @@ +/* + * 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. + */ + +package org.apache.ignite.internal.sql.engine.datatypes; + +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.fail; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import javax.annotation.Nullable; +import org.apache.ignite.Ignite; +import org.apache.ignite.internal.sql.BaseSqlIntegrationTest; +import org.apache.ignite.internal.sql.SqlCommon; +import org.apache.ignite.sql.SqlException; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +/** Test cases for cast to datetime type with specified format {@code (CAST x AS DATETIME TYPE FORMAT 'FMT')}. */ +public class ItDateTimeCastFormatTest extends BaseSqlIntegrationTest { + + private static final ZoneId TIME_ZONE_ID = ZoneId.of("Europe/Paris"); + + @BeforeAll + public void createDateTimeColumnsTable() { + sql("CREATE TABLE datetime_cols (id INT PRIMARY KEY, " + + "fmt_col VARCHAR," + + "date0_col DATE, " + + + "time0_col TIME(0), " + + "time1_col TIME(1), " + + "time2_col TIME(2), " + + "time3_col TIME(3), " + + + "timestamp0_col TIMESTAMP(0), " + + "timestamp1_col TIMESTAMP(1), " + + "timestamp2_col TIMESTAMP(2), " + + "timestamp3_col TIMESTAMP(3), " + + + "timestamp_with_local_time_zone0_col TIMESTAMP(0) WITH LOCAL TIME ZONE, " + + "timestamp_with_local_time_zone1_col TIMESTAMP(1) WITH LOCAL TIME ZONE, " + + "timestamp_with_local_time_zone2_col TIMESTAMP(2) WITH LOCAL TIME ZONE, " + + "timestamp_with_local_time_zone3_col TIMESTAMP(3) WITH LOCAL TIME ZONE " + + ")"); + sql("INSERT INTO datetime_cols (id) VALUES (1)"); + } + + @ParameterizedTest + @MethodSource("date") + public void dateLiterals(DateTimeArgs<LocalTime> args) { + String sqlCast = format("SELECT CAST('{}' AS DATE FORMAT '{}')", args.str, args.format); + + checkQuery(sqlCast, args.value, args.error); + } + + @ParameterizedTest + @MethodSource("date") + public void dateDynamicParams(DateTimeArgs<LocalTime> args) { + String sqlCast = format("SELECT CAST(? AS DATE FORMAT '{}')", args.format); + + checkQuery(sqlCast, args.value, args.error, args.str); + } + + @ParameterizedTest + @MethodSource("date") + public void dateUpdateFromLiteral(DateTimeArgs<LocalTime> args) { + String sqlCast = format( + "UPDATE datetime_cols SET date0_col=CAST(? AS DATE FORMAT '{}') WHERE id = 1", + args.format + ); + + checkDml(sqlCast, args.error, args.str); + + if (args.value != null) { + assertQuery("SELECT date0_col FROM datetime_cols WHERE id = 1") + .returns(args.value) + .check(); + } + } + + @ParameterizedTest + @MethodSource("date") + public void dateUpdateFromDynamicParam(DateTimeArgs<LocalTime> args) { + String sqlCast = format( + "UPDATE datetime_cols SET date0_col=CAST('{}' AS DATE FORMAT '{}') WHERE id = 1", + args.str, args.format + ); + + checkDml(sqlCast, args.error); + + if (args.value != null) { + assertQuery("SELECT date0_col FROM datetime_cols WHERE id = 1") + .returns(args.value) + .check(); + } + } + + private static Stream<DateTimeArgs<LocalDate>> date() { + return Stream.of( + dateTime("2000-01-01", "yyyy-MM-dd", LocalDate.of(2000, 1, 1), null), + dateTime("2-01-01", "y-MM-dd", LocalDate.of(2, 1, 1), null), + dateTime("02-01-01", "y-MM-dd", LocalDate.of(2002, 1, 1), null), + dateTime("20-01-01", "yy-MM-dd", LocalDate.of(2020, 1, 1), null), + dateTime("020-01-01", "yyy-MM-dd", LocalDate.of(20, 1, 1), null), + dateTime("002-01-01", "yyy-MM-dd", LocalDate.of(2, 1, 1), null), + dateTime("200-01-01", "yyy-MM-dd", LocalDate.of(200, 1, 1), null), + dateTime("20-01-01", "yyyy-MM-dd", LocalDate.of(20, 1, 1), null), + dateTime("9999-01-01", "yyyy-MM-dd", LocalDate.of(9999, 1, 1), null), + + dateTime("2000/01-01", "yyyy/MM-dd", LocalDate.of(2000, 1, 1), null), + + dateTime("10000-01-01", "yyyy-MM-dd", null, "DATE out of range"), + dateTime("10000000-01-01", "yyyy-MM-dd", null, "DATE out of range"), + + dateTime("20-01-01", "RR-MM-dd", LocalDate.of(2020, 1, 1), null), + dateTime("2000-01-01", "RRRR-MM-dd", LocalDate.of(2000, 1, 1), null), + dateTime("20-01-01", "RRRR-MM-dd", LocalDate.of(20, 1, 1), null) Review Comment: Add some test cases. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org