-- 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.

-- Routes GetTimestamp through the codegen dispatcher.
-- GetTimestamp is generated by to_timestamp(string, format), to_date(string, format)
-- and try_to_timestamp(string, format).
-- Config: spark.sql.session.timeZone=UTC
-- Config: spark.comet.exec.scalaUDF.codegen.enabled=true

statement
CREATE TABLE test_get_timestamp(s string) USING parquet

statement
INSERT INTO test_get_timestamp VALUES
  ('2024-06-15 10:30:45'),
  ('1970-01-01 00:00:00'),
  ('1969-12-31 23:59:59'),
  ('2024-13-01 00:00:00'),
  ('garbage'),
  (''),
  (NULL)

-- to_timestamp(string, format) -> GetTimestamp with TimestampType output
query
SELECT to_timestamp(s, 'yyyy-MM-dd HH:mm:ss') FROM test_get_timestamp

-- to_date(string, format) -> Cast(GetTimestamp(...), DateType)
query
SELECT to_date(s, 'yyyy-MM-dd HH:mm:ss') FROM test_get_timestamp

-- try_to_timestamp(string, format) -> GetTimestamp with failOnError=false
query
SELECT try_to_timestamp(s, 'yyyy-MM-dd HH:mm:ss') FROM test_get_timestamp

-- literal arguments
query
SELECT to_timestamp('2024-06-15 10:30:45', 'yyyy-MM-dd HH:mm:ss')

query
SELECT to_date('2024-06-15', 'yyyy-MM-dd')

query
SELECT try_to_timestamp('foo', 'yyyy-MM-dd')

query
SELECT to_timestamp(NULL, 'yyyy-MM-dd HH:mm:ss')

-- date-only format
statement
CREATE TABLE test_get_timestamp_dates(s string) USING parquet

statement
INSERT INTO test_get_timestamp_dates VALUES
  ('2024-06-15'),
  ('1970-01-01'),
  (NULL)

query
SELECT to_timestamp(s, 'yyyy-MM-dd') FROM test_get_timestamp_dates

query
SELECT to_date(s, 'yyyy-MM-dd') FROM test_get_timestamp_dates

-- column-as-format (non-literal format)
statement
CREATE TABLE test_get_timestamp_fmt(s string, fmt string) USING parquet

statement
INSERT INTO test_get_timestamp_fmt VALUES
  ('2024-06-15 10:30:45', 'yyyy-MM-dd HH:mm:ss'),
  ('2024-06-15', 'yyyy-MM-dd'),
  ('06/15/2024', 'MM/dd/yyyy'),
  (NULL, 'yyyy-MM-dd'),
  ('2024-06-15', NULL)

query
SELECT to_timestamp(s, fmt) FROM test_get_timestamp_fmt

-- to_timestamp_ntz(string, format) -> GetTimestamp with TimestampNTZType output
query
SELECT to_timestamp_ntz(s, 'yyyy-MM-dd HH:mm:ss') FROM test_get_timestamp
