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


##########
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:
   The comment says STRICT store assignment also rejects it, but only 
`canANSIStoreAssign` is asserted. STRICT goes through `Cast.canUpCast`, which 
is false today only because `UpCastRule` has no nanos arms at all — a future 
blanket datetime arm there would regress this silently. Mind adding:
   ```scala
   assert(!Cast.canUpCast(DateType, TimestampNTZNanosType(p)))
   assert(!Cast.canUpCast(TimestampNTZNanosType(p), DateType))
   assert(!Cast.canUpCast(DateType, TimestampLTZNanosType(p)))
   assert(!Cast.canUpCast(TimestampLTZNanosType(p), DateType))
   ```



##########
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:
   The single-pass resolver has no counterpart for this rewrite. Nothing under 
`analysis/resolver/` knows about nanos casts and `ResolverGuard` doesn't bail 
out on them, so `ExpressionResolver.validateResolvedExpressionGenerically` hits 
`checkInputDataTypes().isFailure` for `CAST(date AS nanos)`:
     - pure single-pass mode: the query fails with DATATYPE_MISMATCH while 
fixed-point succeeds;
     - dual-run mode: `HybridAnalyzer` throws 
`singlePassFailedFixedPointSucceeded`.



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