viirya commented on code in PR #60746:
URL: https://github.com/apache/spark/pull/60746#discussion_r4645146647


##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/read/SupportsExternalLookup.java:
##########
@@ -36,12 +36,13 @@
 @Evolving
 public interface SupportsExternalLookup extends ScanBuilder {
   /**
-   * Selects the offered predicates that this source serves as one lookup 
access path. Spark
-   * offers {@code =}, {@code <=>}, {@code <}, {@code <=}, {@code >}, {@code 
>=} comparisons of a
-   * lookup-side {@link NamedReference} with a {@link LookupParameter}; the 
array is never empty. In
-   * every offered comparison the reference is {@code children()[0]} and the 
parameter is
-   * {@code children()[1]}: when the query writes the lookup column on the 
right, Spark flips the
-   * operator, so {@code s.ts < t.event_time} is offered as {@code event_time 
> ?}. Spark offers
+   * Selects the offered predicates that this source serves as one lookup 
access path, and returns
+   * their positions in {@code candidates}. Spark offers {@code =}, {@code 
<=>}, {@code <},
+   * {@code <=}, {@code >}, {@code >=} comparisons of a lookup-side {@link 
NamedReference} with a
+   * {@link LookupParameter}; the array is never empty. In every offered 
comparison the reference
+   * is {@code children()[0]} and the parameter is {@code children()[1]}: when 
the query writes
+   * the lookup column on the right, Spark flips the operator, so {@code s.ts 
< t.event_time} is
+   * offered as {@code event_time > ?}. Spark offers

Review Comment:
   Done, the whole paragraph is rewrapped.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/PrepareExternalLookupJoin.scala:
##########
@@ -390,24 +388,21 @@ object PrepareExternalLookupJoin extends 
Rule[LogicalPlan] with PredicateHelper
       throw QueryCompilationErrors.unsupportedExternalLookupJoinError(
         "NO_LOOKUP_PREDICATE", Map("table" -> toSQLFreeFormId(tableName)))
     }
-    val candidateByPredicate = new IdentityHashMap[V2Predicate, 
OfferedLookupPredicate]()
-    offered.zip(candidates).foreach { case (predicate, candidate) =>
-      candidateByPredicate.put(predicate, candidate)
-    }
-    val seen = Collections.newSetFromMap(new IdentityHashMap[V2Predicate, 
java.lang.Boolean]())
-    selected.toSeq.map { predicate =>
-      // A null element is not an offered instance either.
-      val candidate = Option(predicate).map(candidateByPredicate.get).orNull
-      if (candidate == null) {
+    val byIndex = candidates.toIndexedSeq
+    val seen = new java.util.BitSet(offered.length)
+    selected.toSeq.map { index =>
+      if (index < 0 || index >= offered.length) {
         throw QueryCompilationErrors.invalidDataSourceExternalLookupError(
-          tableName, "UNOFFERED_SELECTED_PREDICATE",
-          Map("predicate" -> 
Option(predicate).map(_.describe()).getOrElse("null")))
+          tableName, "INVALID_SELECTED_PREDICATE_INDEX",
+          Map("index" -> index.toString, "numCandidates" -> 
offered.length.toString))
       }
-      if (!seen.add(predicate)) {
+      if (seen.get(index)) {
         throw QueryCompilationErrors.invalidDataSourceExternalLookupError(
-          tableName, "DUPLICATE_SELECTED_PREDICATE", Map("predicate" -> 
predicate.describe()))
+          tableName, "DUPLICATE_SELECTED_PREDICATE",
+          Map("index" -> index.toString, "predicate" -> 
offered(index).describe()))
       }
-      candidate
+      seen.set(index)
+      byIndex(index)

Review Comment:
   Thanks, the silent misbinding is real: with identity matching, an in-place 
reorder was harmless, and with positions it is not. But a clone alone doesn't 
prevent it. Spark already reads the positions against its own order (`byIndex` 
follows `offeredCandidates`, which the connector cannot touch), so with a clone 
the connector still sorts its copy, still returns `[1, 0]` for its own view, 
and Spark still binds `(event_time, tenant)`. The clone only fixes the NPE from 
a null written into the array.
   
   So the connector now receives a clone, and Spark checks afterwards that the 
clone still holds the offered predicates, by reference, in the offered order. 
If not, the query fails with the new 
`DATA_SOURCE_INVALID_EXTERNAL_LOOKUP.MODIFIED_CANDIDATES`, so a reorder is an 
error instead of a wrong result. The Javadoc bullet on the read-only input now 
says why. The new test "a connector that reorders the candidates in place fails 
instead of binding wrongly" sorts the candidates in place with `Arrays.sort`, 
and the null write is a new case in the selection-failure test.



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