uros-b commented on code in PR #56409: URL: https://github.com/apache/spark/pull/56409#discussion_r3391354888
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveTimestampNanosCast.scala: ########## @@ -0,0 +1,84 @@ +/* + * 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.spark.sql.catalyst.analysis + +import org.apache.spark.sql.catalyst.expressions.Cast +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.trees.TreePattern.CAST +import org.apache.spark.sql.types.{DataType, DateType, TimestampLTZNanosType, TimestampNTZNanosType, TimestampNTZType, TimestampType} + +/** + * Rewrites casts between [[DateType]] and the nanosecond-precision timestamp types + * ([[TimestampLTZNanosType]] / [[TimestampNTZNanosType]]) into a two-step cast that goes through + * the corresponding microsecond-precision timestamp type: + * + * - `nanos(p) -> DATE` ==> `nanos(p) -> micros -> DATE` + * - `DATE -> nanos(p)` ==> `DATE -> micros -> nanos(p)` + * + * where the microsecond counterpart is `TIMESTAMP` for `*_LTZ` and `TIMESTAMP_NTZ` for `*_NTZ`. + * Both component casts already exist (`nanos(p) <-> micros` and `micros <-> DATE`), so no dedicated + * `DATE <-> nanos` conversion is needed in [[Cast]] and the semantics match the microsecond + * `DATE <-> TIMESTAMP` casts: the LTZ directions are resolved in the session time zone and the NTZ + * directions on the UTC wall-clock grid; sub-microsecond digits and the time-of-day are dropped + * when narrowing to `DATE`. + * + * `Cast.canCast` intentionally does not allow `DATE <-> nanos` directly, so such a cast stays Review Comment: Functional gap: the rewrite doesn't reach casts nested in complex types. Cast.canCast recurses into array/map/struct element types, where DATE <-> nanos is still rejected, and the rule only matches top-level DATE <-> nanos casts. So CAST(arr AS ARRAY<DATE>) from ARRAY<TIMESTAMP_NTZ(9)> (or struct/map equivalents) still fails with DATATYPE_MISMATCH, while the same cast works for micros timestamps and for the nanos <-> micros legs (those are directly in canCast). Please extend the rewrite to complex types appropriately. -- 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]
