Skip to content

Support pushdown filters for non-cast date conversion functions (e.g. to_date) #933

@omerhadari

Description

@omerhadari

Currently, for queries that compare timestamps/dates using TO_DATE in order to for example truncate a timestamp column, no pushdown predicates are applied. This is because the functions TO_DATE, TO_TIMESTAMP are not casts, and so they reach the function to_iceberg_predicate as ScalarFunctions, and not matched on any branch.

It would be nice to identify these cases because it is very common to have a partition key on a timestamp column with a Day Transformation, and currently queries such as SELECT * FROM table WHERE TO_DATE(table.timestamp_col) = '2025-01-01' result in no pushdown predicates at all.

Something along the line of these tests would ideally pass:

    #[test]
    fn test_to_timestamp_comparison_creates_predicate() {
        let sql = "TO_TIMESTAMP(ts) >= timestamp '2023-01-05T00:00:00'";
        let predicate = convert_to_iceberg_predicate(sql).unwrap();
        let expected_predicate =
            Reference::new("ts").greater_than_or_equal_to(Datum::string("2023-01-05T00:00:00"));
        assert_eq!(predicate, expected_predicate);
    }

    #[test]
    fn test_to_timestamp_comparison_to_cast_creates_predicate() {
        let sql = "TO_TIMESTAMP(ts) >= CAST('2023-01-05T00:00:00' AS TIMESTAMP)";
        let predicate = convert_to_iceberg_predicate(sql).unwrap();
        let expected_predicate =
            Reference::new("ts").greater_than_or_equal_to(Datum::string("2023-01-05T00:00:00"));
        assert_eq!(predicate, expected_predicate);
    }

    #[test]
    fn test_to_date_comparison_creates_predicate() {
        let sql = "TO_DATE(ts) >= CAST('2023-01-05T00:00:00' AS DATE)";
        let predicate = convert_to_iceberg_predicate(sql).unwrap();
        let expected_predicate =
            Reference::new("ts").greater_than_or_equal_to(Datum::string("2023-01-05"));
        assert_eq!(predicate, expected_predicate);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions