dongjoon-hyun opened a new pull request, #60510: URL: https://github.com/apache/spark/pull/60510
### What changes were proposed in this pull request? This PR fixes a `scala.MatchError` that fails query optimization when CBO estimates a filter or a join on a column whose min/max statistics have a type the estimators do not handle. `EstimationUtils.toDouble`, `FilterEstimation.evaluateBinary` and `FilterEstimation.evaluateInSet` match only numeric, `BOOLEAN`, `DATE` and `TIMESTAMP` values (and `STRING`/`BINARY` in the filter), but min/max of other types reach them: - `ANALYZE TABLE ... COMPUTE STATISTICS FOR COLUMNS` stores min/max of `TIMESTAMP_NTZ` columns since Apache Spark 3.4.0 (SPARK-42743, SPARK-42777). On a cached table it also keeps min/max of `TIME` and nanosecond timestamp columns, both types still behind internal configurations. - `UnionEstimation` keeps min/max of `TIMESTAMP_NTZ` and ANSI interval columns (SPARK-37468), for example of a constant column in each branch. Each type is now either estimated or treated as having an unknown range: | Type | Internal value | Estimation | |---|---|---| | `TIMESTAMP_NTZ` | `Long` microseconds | like `TIMESTAMP`, histograms included | | `TIME(p)` | `Long` nanoseconds of the day | like `TIMESTAMP` | | `INTERVAL YEAR TO MONTH` | `Int` months | like `INT` | | `INTERVAL DAY TO SECOND` | `Long` microseconds | like `BIGINT` | | `TIMESTAMP_NTZ(p)` and `TIMESTAMP_LTZ(p)` with p in [7, 9], and any other type | `TimestampNanosVal`, ... | unknown range, like `STRING` | A `Double` cannot hold epoch nanoseconds exactly (adjacent values are 256 ns apart in 2020), so converted bounds could make a range predicate look disjoint from the column and be estimated at 0 rows. Nanosecond timestamp columns also get no histograms (SPARK-60337). The changes: - `EstimationUtils.toDouble` and `fromDouble` convert the first four types. The new `EstimationUtils.canConvertToDouble` lists the types they convert, and `canEstimateWithHistogram` returns it. - `ValueInterval.apply` returns `DefaultValueInterval`, the unknown range of `STRING` and `BINARY` columns, for every other type. - `FilterEstimation.evaluateBinary`, `evaluateInSet` and `evaluateBinaryForTwoColumns` use `canConvertToDouble` instead of their own type lists. The other types take the `STRING`/`BINARY` path: comparisons get no estimate, and `IN` uses the distinct count. ### Why are the changes needed? With CBO on, queries over analyzed `TIMESTAMP_NTZ` columns fail during optimization. On Apache Spark 4.3.0 ([v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0), 2026-10-27): ```sql SET spark.sql.cbo.enabled=true; CREATE TABLE t1(k TIMESTAMP_NTZ) USING parquet; CREATE TABLE t2(k TIMESTAMP_NTZ) USING parquet; INSERT INTO t1 VALUES (TIMESTAMP_NTZ'2020-01-01 00:00:00'), (TIMESTAMP_NTZ'2020-01-02 00:00:00'); INSERT INTO t2 VALUES (TIMESTAMP_NTZ'2020-01-01 00:00:00'); ANALYZE TABLE t1 COMPUTE STATISTICS FOR COLUMNS k; ANALYZE TABLE t2 COMPUTE STATISTICS FOR COLUMNS k; SELECT * FROM t1 JOIN t2 ON t1.k = t2.k WHERE t1.k > TIMESTAMP_NTZ'2020-01-01 12:00:00'; -- [INTERNAL_ERROR] The Spark SQL phase optimization failed with an internal error. ... -- Caused by: scala.MatchError: TimestampNTZType (of class org.apache.spark.sql.types.TimestampNTZType$) -- at org.apache.spark.sql.catalyst.plans.logical.statsEstimation.FilterEstimation.evaluateBinary SELECT * FROM t1 JOIN t2 ON t1.k = t2.k JOIN t1 t3 ON t2.k = t3.k; -- [INTERNAL_ERROR] The Spark SQL phase optimization failed with an internal error. ... -- Caused by: scala.MatchError: TimestampNTZType (of class org.apache.spark.sql.types.TimestampNTZType$) -- at org.apache.spark.sql.catalyst.plans.logical.statsEstimation.EstimationUtils$.toDouble -- at org.apache.spark.sql.catalyst.plans.logical.statsEstimation.ValueInterval$.apply -- at org.apache.spark.sql.catalyst.plans.logical.statsEstimation.JoinEstimation.computeCardinalityAndStats ``` `EXPLAIN COST`, equality and `IN` predicates, and comparisons of two `TIMESTAMP_NTZ` columns fail too, and so does the estimate of a join on a `UNION ALL` of analyzed `TIMESTAMP_NTZ` columns or of constant ANSI interval columns. The filter and join estimates of `TIMESTAMP_NTZ` columns fail the same way on Apache Spark 3.5.9 ([v3.5.9](https://github.com/apache/spark/releases/tag/v3.5.9), 2026-07-12). ### Does this PR introduce _any_ user-facing change? Yes. Queries that failed during optimization with `scala.MatchError` now run, with CBO estimates for `TIMESTAMP_NTZ`, `TIME` and ANSI interval columns. The estimates of queries that ran before are unchanged, except for an equality predicate on a `TIMESTAMP_NTZ`, `TIME` or ANSI interval column with a histogram but no min/max, which a DSv2 connector can report. As on a `TIMESTAMP` column, it now gets no estimate instead of one from the distinct count. ### How was this patch tested? New tests, each failing without the change: - `FilterEstimationSuite`: equality, comparison, `IN` and two-column comparison on `TIMESTAMP_NTZ`, `TIME` and both ANSI interval types, estimated like `INT`. The same predicates on a `TIMESTAMP_NTZ(9)` column with min/max: equality and `IN` use the distinct count, and comparisons get no estimate. - `JoinEstimationSuite`: `test join keys of different types` now covers the four types, a `TIMESTAMP_NTZ` join with histograms uses them, and a `TIMESTAMP_NTZ(9)` join uses the distinct count. - `UnionEstimationSuite`: a join on a union of `TIMESTAMP_NTZ` or ANSI interval columns. - `StatisticsCollectionSuite`: end to end after `ANALYZE` on a `TIMESTAMP_NTZ` column, a self-join, a range filter and a join on a `UNION ALL`. Turning off each part of the change one at a time also fails at least one of these tests. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Opus 5.5 -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
