MaxGekk commented on code in PR #56409:
URL: https://github.com/apache/spark/pull/56409#discussion_r3387885733


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveTimestampNanosCast.scala:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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
+ * unresolved until this rule rewrites it into the resolvable nested form. The 
new casts inherit the
+ * original `timeZoneId` and `evalMode`; the only zone-sensitive part (the 
`micros <-> DATE` cast)
+ * gets its session time zone from [[ResolveTimeZone]] within the same 
fixed-point batch.
+ */
+object ResolveTimestampNanosCast extends Rule[LogicalPlan] {
+
+  /** The microsecond-precision timestamp counterpart of a 
nanosecond-precision timestamp type. */
+  private def microTimestampType(dt: DataType): Option[DataType] = dt match {
+    case _: TimestampLTZNanosType => Some(TimestampType)
+    case _: TimestampNTZNanosType => Some(TimestampNTZType)
+    case _ => None
+  }
+
+  override def apply(plan: LogicalPlan): LogicalPlan =
+    plan.resolveExpressionsWithPruning(_.containsPattern(CAST), ruleId) {

Review Comment:
   Good catch - fixed in 4ddcbc9. I extracted the per-cast rewrite into 
`ResolveTimestampNanosCast.rewriteDateNanosCast` and have 
`TimezoneAwareExpressionResolver` apply it after resolving a `Cast`, so the 
single-pass resolver produces the same plan as fixed-point instead of failing 
`checkInputDataTypes()`.
   
   Tests:
   - `TimezoneAwareExpressionResolverSuite`: the single-pass resolver rewrites 
both directions through the micros timestamp type (NTZ and LTZ).
   - `ResolveTimestampNanosCastHybridAnalyzerSuite`: end-to-end dual-run checks 
for all `DATE <-> nanos` directions. I confirmed these fail with 
`HYBRID_ANALYZER_EXCEPTION.SINGLE_PASS_FAILED_FIXED_POINT_SUCCEEDED` when the 
resolver rewrite is reverted, so they actually guard the gap.



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuiteBase.scala:
##########
@@ -745,6 +745,14 @@ abstract class CastSuiteBase extends SparkFunSuite with 
ExpressionEvalHelper {
       // ... but blocks the lossy narrowing nanos(p) -> micros to avoid silent 
truncation.
       assert(!Cast.canANSIStoreAssign(TimestampNTZNanosType(p), 
TimestampNTZType))
       assert(!Cast.canANSIStoreAssign(TimestampLTZNanosType(p), TimestampType))
+
+      // SPARK-57323: DATE <-> nanos requires an explicit CAST in both 
directions (the explicit
+      // cast is rewritten through the microsecond type by 
ResolveTimestampNanosCast), so STRICT
+      // store assignment and ANSI store assignment both reject it.
+      assert(!Cast.canANSIStoreAssign(DateType, TimestampNTZNanosType(p)))
+      assert(!Cast.canANSIStoreAssign(TimestampNTZNanosType(p), DateType))
+      assert(!Cast.canANSIStoreAssign(DateType, TimestampLTZNanosType(p)))
+      assert(!Cast.canANSIStoreAssign(TimestampLTZNanosType(p), DateType))

Review Comment:
   Done in 4ddcbc9 - added the four `Cast.canUpCast` assertions and reworded 
the comment to call out that STRICT store assignment goes through `canUpCast`, 
so a future blanket datetime arm in `UpCastRule` would now be caught here.



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

Reply via email to