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

# Test scanning all primitive types supported by Iceberg
# This tests INT, BIGINT, FLOAT, DOUBLE, DECIMAL, STRING, BOOLEAN, DATE, TIMESTAMP

# Create a table with all primitive types
statement ok
CREATE TABLE default.default.all_primitive_types (
    col_int INT NOT NULL,
    col_long BIGINT NOT NULL,
    col_float FLOAT NOT NULL,
    col_double DOUBLE NOT NULL,
    col_decimal DECIMAL(10, 2),
    col_string STRING,
    col_boolean BOOLEAN
)

# Verify the table is initially empty
query IIRRTTT rowsort
SELECT * FROM default.default.all_primitive_types
----

# Insert test data with various primitive types
query I
INSERT INTO default.default.all_primitive_types VALUES
    (1, 100, 1.5, 2.5, 123.45, 'hello', true),
    (2, 200, 2.5, 3.5, 234.56, 'world', false),
    (3, 300, 3.5, 4.5, 345.67, NULL, true),
    (-1, -100, -1.5, -2.5, -123.45, 'negative', false),
    (0, 0, 0.0, 0.0, 0.00, 'zero', NULL)
----
5

# Verify all inserted rows
query IIRRTTT rowsort
SELECT * FROM default.default.all_primitive_types
----
-1 -100 -1.5 -2.5 -123.45 negative false
0 0 0 0 0 zero NULL
1 100 1.5 2.5 123.45 hello true
2 200 2.5 3.5 234.56 world false
3 300 3.5 4.5 345.67 NULL true

# Test selecting specific columns with string filter
query IT rowsort
SELECT col_int, col_string FROM default.default.all_primitive_types WHERE col_string = 'hello'
----
1 hello

# Test numeric comparisons
query IR rowsort
SELECT col_int, col_decimal FROM default.default.all_primitive_types WHERE col_decimal > 200.00
----
2 234.56
3 345.67

# Test filtering on int column
query IIRRTTT rowsort
SELECT * FROM default.default.all_primitive_types WHERE col_int >= 0 ORDER BY col_int
----
0 0 0 0 0 zero NULL
1 100 1.5 2.5 123.45 hello true
2 200 2.5 3.5 234.56 world false
3 300 3.5 4.5 345.67 NULL true

# Test filtering with NULL
query IT rowsort
SELECT col_int, col_string FROM default.default.all_primitive_types WHERE col_string IS NOT NULL
----
-1 negative
0 zero
1 hello
2 world

# Test aggregations on numeric types
query IIRR
SELECT COUNT(*), SUM(col_int), AVG(col_float), MAX(col_double) FROM default.default.all_primitive_types
----
5 5 1.2 4.5

# Create a table with date and timestamp types
statement ok
CREATE TABLE default.default.temporal_types (
    id INT NOT NULL,
    col_date DATE,
    col_timestamp TIMESTAMP
)

# Insert temporal data
query I
INSERT INTO default.default.temporal_types VALUES
    (1, '2024-01-15', '2024-01-15 10:30:00'),
    (2, '2024-06-20', '2024-06-20 14:45:30'),
    (3, '2023-12-31', '2023-12-31 23:59:59'),
    (4, NULL, NULL)
----
4

# Verify temporal data
query IDP rowsort
SELECT * FROM default.default.temporal_types
----
1 2024-01-15 2024-01-15T10:30:00
2 2024-06-20 2024-06-20T14:45:30
3 2023-12-31 2023-12-31T23:59:59
4 NULL NULL

# Test date comparisons
query ID rowsort
SELECT id, col_date FROM default.default.temporal_types WHERE col_date >= '2024-01-01'
----
1 2024-01-15
2 2024-06-20
