viirya commented on code in PR #60746:
URL: https://github.com/apache/spark/pull/60746#discussion_r4646643185
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/PrepareExternalLookupJoin.scala:
##########
@@ -188,8 +186,12 @@ object PrepareExternalLookupJoin extends Rule[LogicalPlan]
with PredicateHelper
FieldReference.column(columnNames(candidate.projectedField)),
new LookupParameter(candidate.parameter.dataType)))
}.toArray
- val selectedPredicates = selectedCandidates(tableName, offeredPredicates,
offeredCandidates,
- lookupBuilder.selectLookupPredicates(offeredPredicates))
+ // The connector selects by position, so it receives a copy, which Spark
checks afterwards: a
+ // copy that the connector reordered would bind the parameters to the
wrong predicates.
+ val passedPredicates = offeredPredicates.clone()
Review Comment:
Done. Spark now takes `offeredPredicates.map(_.describe())` before the call,
and both `DUPLICATE_SELECTED_PREDICATE` and `MODIFIED_CANDIDATES` describe the
predicate from that text. A new case in the selection-failure test writes into
`children()` and returns `[0, 0]`, and expects the offered `id = ?`.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/PrepareExternalLookupJoin.scala:
##########
@@ -372,16 +374,22 @@ object PrepareExternalLookupJoin extends
Rule[LogicalPlan] with PredicateHelper
}
/**
- * Maps the connector's selection back to the offered candidates, in the
connector's order, which
- * defines the fields of each request parameter row. Selections are matched
by object identity
- * rather than by equality, so two candidates of the same shape, for example
the same lookup field
- * compared with two different join-side values, remain distinguishable.
+ * Maps the connector's selection, positions in the offered array, back to
the offered
+ * candidates, in the connector's order, which defines the fields of each
request parameter row.
+ * Positions keep two candidates of the same shape, for example the same
lookup field compared
+ * with two different join-side values, distinguishable. `passed` is the
copy of `offered` that
+ * the connector received; it must still hold the offered predicates in the
offered order.
*/
private def selectedCandidates(
tableName: String,
offered: Array[V2Predicate],
- candidates: Seq[OfferedLookupPredicate],
- selected: Array[V2Predicate]): Seq[OfferedLookupPredicate] = {
+ candidates: IndexedSeq[OfferedLookupPredicate],
+ passed: Array[V2Predicate],
+ selected: Array[Int]): Seq[OfferedLookupPredicate] = {
+ if (!offered.indices.forall(index => passed(index) eq offered(index))) {
Review Comment:
Done, both parts. The check now runs after the null and empty checks, so a
connector that changes its input and returns an empty array gets
`NO_LOOKUP_PREDICATE`, and it compares with `==`. As you say, the
`LookupParameter` in each candidate makes the offered predicates pairwise
unequal, so `==` still catches every reorder and null write. New tests cover
the empty result after a change, and a connector that replaces every candidate
with `new Predicate(name, children)` and still binds in its order.
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ExternalLookupJoinSuite.scala:
##########
@@ -768,9 +769,12 @@ class ExternalLookupJoinSuite extends QueryTest with
SharedSparkSession {
Seq(
("nullKeySelection", "true", "NULL_RESULT",
Map("method" -> "SupportsExternalLookup.selectLookupPredicates()")),
- ("selectionMode", "nullElement", "UNOFFERED_SELECTED_PREDICATE",
Map("predicate" -> "null")),
- ("selectionMode", "duplicate", "DUPLICATE_SELECTED_PREDICATE",
Map("predicate" -> "id = ?")),
- ("selectionMode", "copy", "UNOFFERED_SELECTED_PREDICATE",
Map("predicate" -> "id = ?")),
+ ("selectionMode", "negative", "INVALID_SELECTED_PREDICATE_INDEX",
+ Map("index" -> "-1", "maxIndex" -> "0")),
+ ("selectionMode", "outOfRange", "INVALID_SELECTED_PREDICATE_INDEX",
+ Map("index" -> "1", "maxIndex" -> "0")),
+ ("selectionMode", "duplicate", "DUPLICATE_SELECTED_PREDICATE",
Review Comment:
Done. `SimpleExternalLookupTable` has a `modifyCandidates` selection mode
that writes a null into its input, checked in this list and in "connector
contract errors stop the query when Spark prepares the lookup, not later".
##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/read/SupportsExternalLookup.java:
##########
@@ -76,19 +76,21 @@ public interface SupportsExternalLookup extends ScanBuilder
{
* with a constant that a Scala UDF computes is neither offered nor pushed.
In every case, Spark
* evaluates the comparison again on every returned row.
* <ul>
- * <li>The result must be non-null and hold offered instances, unchanged,
without nulls or
- * duplicates. Spark matches them by identity, not {@code equals}.</li>
+ * <li>The result must be non-null and hold distinct positions in {@code
candidates}, from 0
+ * to {@code candidates.length - 1}. Same-shaped candidates, such as two
{@code id = ?} with
+ * different parameters, are told apart by position.</li>
* <li>Each offered predicate has exactly one {@link LookupParameter}, so
field {@code i} of
- * every parameter row is the value for the {@code i}-th returned
predicate. A field may
- * appear in several predicates, as range bounds do.</li>
+ * every parameter row is the value for the predicate at the {@code i}-th
returned position.
+ * A field may appear in several predicates, as range bounds do.</li>
* <li>Spark evaluates every selected predicate again on each returned row
and drops a row
* that fails it, so extra rows do no harm. Spark cannot recover a missing
row: for each
* parameter row, the source must return every row that passes the filters
this source reports
* as pushed and satisfies all selected predicates under Spark semantics,
as
* {@link ExternalLookupReader#lookupBatch} defines. Do not select a
predicate that the source
* cannot evaluate that way, and do not select unsupported names or
shapes.</li>
* <li>An empty result means no access path fits; the query then
fails.</li>
- * <li>The input array and its elements are read-only.</li>
+ * <li>The input array and its elements are read-only. The returned
positions refer to the
+ * offered order, so Spark fails the query if the source changes the
array.</li>
Review Comment:
Done: "Spark fails the query if, when this method returns a non-empty
result, the array no longer holds the offered predicates in the offered order."
##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/read/SupportsExternalLookup.java:
##########
@@ -76,19 +76,21 @@ public interface SupportsExternalLookup extends ScanBuilder
{
* with a constant that a Scala UDF computes is neither offered nor pushed.
In every case, Spark
* evaluates the comparison again on every returned row.
* <ul>
- * <li>The result must be non-null and hold offered instances, unchanged,
without nulls or
- * duplicates. Spark matches them by identity, not {@code equals}.</li>
+ * <li>The result must be non-null and hold distinct positions in {@code
candidates}, from 0
+ * to {@code candidates.length - 1}. Same-shaped candidates, such as two
{@code id = ?} with
+ * different parameters, are told apart by position.</li>
* <li>Each offered predicate has exactly one {@link LookupParameter}, so
field {@code i} of
- * every parameter row is the value for the {@code i}-th returned
predicate. A field may
- * appear in several predicates, as range bounds do.</li>
+ * every parameter row is the value for the predicate at the {@code i}-th
returned position.
Review Comment:
Done. `lookupBatch` now says field `i` is the value for the predicate at the
`i`-th position that `selectLookupPredicates` returned.
##########
common/utils/src/main/resources/error/error-conditions.json:
##########
@@ -2428,6 +2433,11 @@
"The lookup scan did not return the table column <field>, which the
join requires."
]
},
+ "MODIFIED_CANDIDATES" : {
+ "message" : [
+ "`selectLookupPredicates()` modified its input array of candidates.
It must leave the array as Spark offered it, because the returned indexes refer
to the offered order."
Review Comment:
Done, "position" everywhere: the condition is now
`INVALID_SELECTED_PREDICATE_POSITION` with `<position>` and `<maxPosition>`,
and `DUPLICATE_SELECTED_PREDICATE` and `MODIFIED_CANDIDATES` use `<position>`
too. None of these has been released, so the rename costs nothing.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/PrepareExternalLookupJoin.scala:
##########
@@ -372,16 +374,22 @@ object PrepareExternalLookupJoin extends
Rule[LogicalPlan] with PredicateHelper
}
/**
- * Maps the connector's selection back to the offered candidates, in the
connector's order, which
- * defines the fields of each request parameter row. Selections are matched
by object identity
- * rather than by equality, so two candidates of the same shape, for example
the same lookup field
- * compared with two different join-side values, remain distinguishable.
+ * Maps the connector's selection, positions in the offered array, back to
the offered
+ * candidates, in the connector's order, which defines the fields of each
request parameter row.
+ * Positions keep two candidates of the same shape, for example the same
lookup field compared
+ * with two different join-side values, distinguishable. `passed` is the
copy of `offered` that
+ * the connector received; it must still hold the offered predicates in the
offered order.
*/
private def selectedCandidates(
tableName: String,
offered: Array[V2Predicate],
- candidates: Seq[OfferedLookupPredicate],
- selected: Array[V2Predicate]): Seq[OfferedLookupPredicate] = {
+ candidates: IndexedSeq[OfferedLookupPredicate],
+ passed: Array[V2Predicate],
+ selected: Array[Int]): Seq[OfferedLookupPredicate] = {
+ if (!offered.indices.forall(index => passed(index) eq offered(index))) {
+ throw QueryCompilationErrors.invalidDataSourceExternalLookupError(
+ tableName, "MODIFIED_CANDIDATES", Map.empty)
Review Comment:
Done. `MODIFIED_CANDIDATES` now reports the first changed `<position>` and
the `<predicate>` that Spark offered there.
--
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]