dongjoon-hyun commented on code in PR #60746:
URL: https://github.com/apache/spark/pull/60746#discussion_r4646212597
##########
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:
**[9] Minor: following up on item 1
([thread](https://github.com/apache/spark/pull/60746#discussion_r4644907375)),
the clone copies only the array slots.** The connector still receives Spark's
own `V2Predicate` instances, and `GeneralScalarExpression.children()` returns
its internal array (GeneralScalarExpression.java:465). A connector that
normalizes a predicate in place, e.g. `candidates(0).children()(0) =
FieldReference.column("ID")`, and then returns `[0, 0]` passes the `eq` check
at L389, and `DUPLICATE_SELECTED_PREDICATE` reports the changed predicate text
from `offered(index).describe()` at L411. With a null child instead,
`describe()` throws `UNEXPECTED_V2_EXPRESSION` in place of the contract error.
The binding stays right, because `candidates(index)` reads Spark's own
`OfferedLookupPredicate`s, so only the message is affected. Could the duplicate
message describe the predicate from text taken before the call, e.g.
`offeredPredicates.map(_.describe())`?
##########
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:
**[10] Minor: this check runs before the `NULL_RESULT` and
`NO_LOOKUP_PREDICATE` checks and compares by reference, so it also rejects
changes that cannot misbind anything.** A connector that sorts its input in
place to inspect it, finds no access path and returns an empty array now fails
with `DATA_SOURCE_INVALID_EXTERNAL_LOOKUP.MODIFIED_CANDIDATES` instead of
`EXTERNAL_LOOKUP_JOIN_UNSUPPORTED.NO_LOOKUP_PREDICATE`, although the
description lists the empty-array behavior as unchanged. Likewise,
`candidates(i) = new Predicate(p.name(), p.children())`, the rebuild from name
and children that the description names as the motivation, fails even though
position `i` still holds the same predicate. As `LookupParameter` equality is
identity (LookupParameter.java:29), no two offered predicates are equal, so
`==` would still catch every reorder and every null write. Could the check run
only for a non-empty result, after L400, and compare with `==`?
##########
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:
**[12] Minor: "Spark fails the query if the source changes the array"
promises more than the check at PrepareExternalLookupJoin.scala:389 does.**
Spark compares only the array slots, and only once, when this method returns. A
connector that keeps the array and changes it later, in `pruneColumns()` or
`build()`, or that writes into an element's `children()` (item 9), gets no
error. Could it say what Spark checks, e.g. "Spark fails the query if, when
this method returns, the array no longer holds the offered predicates in the
offered order"?
##########
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:
**[14] Nit: following up on my item 3
([thread](https://github.com/apache/spark/pull/60746#discussion_r4644907397)),
the messages now say "index" while the Javadoc says "position" for the same
value.** My suggested wording brought "indexes" into L2423, and this line and
L2413 say "index" too, while SupportsExternalLookup.java:40, :79 and :92 and
the PR title say "positions". A connector author who hits one of these errors
and searches the Javadoc for "index" finds nothing. Could both use one term,
e.g. "position" in the messages, or "index" in the Javadoc to match
`INVALID_SELECTED_PREDICATE_INDEX`?
##########
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:
**[16] Nit: `offered.indices.forall(index => passed(index) eq
offered(index))` is `passed.corresponds(offered)(_ eq _)`**, which reads as the
whole-array comparison that it is (or `_ == _`, with item 10). Could it use
`corresponds`?
##########
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:
**[11] Nit: `MODIFIED_CANDIDATES` is tested only in
PrepareExternalLookupJoinSuite.scala, a `PlanTest`.** Its siblings,
`NULL_RESULT`, `INVALID_SELECTED_PREDICATE_INDEX` and
`DUPLICATE_SELECTED_PREDICATE`, are checked there and also end to end through a
streaming query in this list, and `duplicate` also in
ExternalLookupJoinStartupCheckSuite's "connector contract errors stop the query
when Spark prepares the lookup, not later" (L265 there, outside this diff).
Could `SimpleExternalLookupTable` get a `selectionMode` that writes into its
input array, checked in this list and in that test?
##########
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:
**[13] Nit: `ExternalLookupReader.lookupBatch` still says "Field {@code i}
of each row is the value for the {@code i}-th selected predicate"
(ExternalLookupReader.java:74-75).** With `int[]` positions, "the i-th selected
predicate" can be read as the i-th selected candidate in the offered order,
rather than the predicate at `result[i]`. For the candidates `[tenant = ?,
event_time >= ?]` and the result `[1, 0]`, a connector that reads it that way
treats field 0 as `tenant`, although it holds the `event_time` bound, and
misses rows. ExternalLookupReader.java is outside this diff, so I left this
here. Could it use this line's wording, "the predicate at the {@code i}-th
returned position"?
##########
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:
**[15] Nit: unlike its siblings, `MODIFIED_CANDIDATES` has no parameters, so
it does not say which slot changed.** With ten candidates and one changed slot,
the user sees only "modified its input array of candidates" and has to debug
the connector to find where. Could it report the first changed `<index>` and
the offered `<predicate>` there, as `DUPLICATE_SELECTED_PREDICATE` does?
--
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]