cloud-fan commented on code in PR #56546:
URL: https://github.com/apache/spark/pull/56546#discussion_r3424310407


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveDeduplicate.scala:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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 scala.collection.mutable
+
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.catalyst.plans.logical.{Deduplicate, 
DeduplicateAllColumnsAsKey, DeduplicateKeyColumns, DeduplicateSpec, 
DeduplicateWithinWatermark, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.UNRESOLVED_DEDUPLICATE
+import org.apache.spark.sql.errors.QueryCompilationErrors
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * Resolves [[UnresolvedDeduplicate]] (built by `Dataset.dropDuplicates*` in 
Spark Classic and by
+ * the Deduplicate relation in the Spark Connect planner) into a 
[[Deduplicate]] /
+ * [[DeduplicateWithinWatermark]] with resolved key attributes, shared by both 
engines.
+ *
+ * The key attributes are computed with 
[[SQLConf.DROP_DUPLICATES_DETERMINISTIC_KEY_ORDER]] read
+ * from the current session: true (the default) produces a stable, 
first-occurrence ordering; false
+ * reproduces each engine's legacy resolution. The resolved node also carries 
the original recipe
+ * (`subset`, `allColumnsAsKeys`, `viaSparkClassic`) so that streaming queries 
can recompute the
+ * keys at query start with the value pinned in the offset log (see
+ * `StreamingQueryManager.createQuery` and [[computeKeys]]). See SPARK-57489.

Review Comment:
   This recipe description is stale: there is no `subset`/`allColumnsAsKeys` 
field anymore — the recipe is a `DeduplicateSpec` (its `keySpec` is 
`DeduplicateKeyColumns(colNames)` / `DeduplicateAllColumnsAsKey`, plus 
`viaSparkClassic`), i.e. the `DeduplicateKeySpec` refactor from the thread 
above. Also, the offset-log-pinned recompute lives in 
`MicroBatchExecution.logicalPlan`, not `StreamingQueryManager.createQuery` 
(which just runs normal analysis). Suggest:
   ```suggestion
    * (a `DeduplicateSpec` holding the key spec and `viaSparkClassic`) so 
streaming queries can
    * recompute the keys at query start with the value pinned in the offset log 
(see
    * `MicroBatchExecution.logicalPlan` and [[computeKeys]]). See SPARK-57489.
   ```
   (The same `createQuery` reference is in the PR description — worth fixing 
there too.)



##########
sql/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/SparkConnectPlanner.scala:
##########
@@ -1455,27 +1455,16 @@ class SparkConnectPlanner(
     if (!rel.getAllColumnsAsKeys && rel.getColumnNamesCount == 0) {
       throw InvalidInputErrors.deduplicateRequiresColumnsOrAll()
     }
-    val queryExecution = new QueryExecution(session, 
transformRelation(rel.getInput))
-    val resolver = session.sessionState.analyzer.resolver
-    val allColumns = queryExecution.analyzed.output
-    if (rel.getAllColumnsAsKeys) {
-      if (rel.getWithinWatermark) DeduplicateWithinWatermark(allColumns, 
queryExecution.analyzed)
-      else Deduplicate(allColumns, queryExecution.analyzed)
+    val keySpec = if (rel.getAllColumnsAsKeys) {

Review Comment:
   With the eager column check removed here, 
`InvalidInputErrors.invalidDeduplicateColumn` (InvalidInputErrors.scala:58) no 
longer has any caller — it's now dead code. An unresolvable column now surfaces 
as the analyzer's `UNRESOLVED_COLUMN_AMONG_FIELD_NAMES` `AnalysisException` 
instead (a reasonable cross-engine-consistency change, but a user-visible error 
change for Connect worth noting in the description). Suggest removing the 
unused `invalidDeduplicateColumn` helper.



##########
sql/connect/server/src/test/scala/org/apache/spark/sql/connect/StreamingDeduplicationConnectInteropSuite.scala:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.connect
+
+import java.io.File
+import java.nio.charset.StandardCharsets
+import java.nio.file.Files
+import java.util.UUID
+
+import org.apache.spark.sql.streaming.Trigger
+import org.apache.spark.sql.types.{IntegerType, StructField, StructType}
+
+/**
+ * Interop tests for dropDuplicates key resolution across Spark Classic and 
Spark Connect. Both
+ * engines now resolve the dedup keys through the same analyzer rule with a 
deterministic
+ * (positional) order, so a checkpoint written by one engine must be 
restartable under the other:
+ * the state-store keys (bound by position) line up and previously-seen rows 
are still recognized
+ * as duplicates.
+ *
+ * The in-process [[SparkConnectServerTest]] harness exposes a Classic session 
(`spark`) and a
+ * Connect session (`withSession`) sharing one SparkContext / filesystem, so 
both can drive the
+ * same checkpoint directory. Input is plain CSV files (no commit markers) 
read by a file source
+ * with Trigger.AvailableNow for deterministic batching; the sink is a 
recoverable parquet file
+ * sink. See SPARK-57489.
+ */
+class StreamingDeduplicationConnectInteropSuite extends SparkConnectServerTest 
{
+
+  private val rowSchema = StructType(
+    Seq(
+      StructField("a", IntegerType),
+      StructField("b", IntegerType),
+      StructField("c", IntegerType),
+      StructField("d", IntegerType),
+      StructField("e", IntegerType)))
+
+  /**
+   * Writes one CSV file (one batch worth of input) into `inputDir` without 
any commit markers.
+   */
+  private def writeInputCsv(inputDir: File, rows: Seq[(Int, Int, Int, Int, 
Int)]): Unit = {
+    inputDir.mkdirs()
+    val content =
+      rows.map { case (a, b, c, d, e) => s"$a,$b,$c,$d,$e" }.mkString("", 
"\n", "\n")
+    Files.write(
+      new File(inputDir, s"${UUID.randomUUID()}.csv").toPath,
+      content.getBytes(StandardCharsets.UTF_8))
+  }
+
+  /**
+   * Starts a dropDuplicates streaming query on `session` reading `inputDir`, 
processes all
+   * currently-available files against `checkpoint`, appending deduplicated 
rows to the parquet
+   * sink at `outputDir`.
+   */
+  // Fully qualified on purpose: this file lives in package 
org.apache.spark.sql.connect, which also
+  // defines a `SparkSession`; an import of the common type is shadowed by it 
under Scala 2.12. The

Review Comment:
   Spark builds on Scala 2.13 only, and this shadowing is ordinary Scala name 
resolution (the `org.apache.spark.sql.connect` package object defines 
`SparkSession`), not 2.12-specific — the `under Scala 2.12` qualifier is 
misleading.
   ```suggestion
     // defines a `SparkSession`; an import of the common type would be 
shadowed by it. The
   ```



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