spark-test commented on code in PR #60089:
URL: https://github.com/apache/spark/pull/60089#discussion_r4582065215
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/variant/VariantExpressionEvalUtils.scala:
##########
@@ -145,8 +145,10 @@ object VariantExpressionEvalUtils {
/**
* Insert `value` into `input` at `javaSegments`. `path` is the source
string used in error
- * messages. The cast and insert share one try, so any size overflow maps to
`VARIANT_SIZE_LIMIT`
- * and a type mismatch maps to `VARIANT_PATH_TYPE_MISMATCH`. When
`failOnError` is false (the
+ * messages. `value` is cast to a variant first, and that cast reports its
own size overflow with
+ * `functionName`. The catch below only covers the insert: a size overflow
there maps to
+ * `VARIANT_SIZE_LIMIT` and a type mismatch maps to
`VARIANT_PATH_TYPE_MISMATCH`. When
+ * `failOnError` is false (the
* `try_variant_insert` mode), a duplicate key or path type mismatch returns
null instead of
* throwing; a size overflow (and a malformed path, rejected earlier during
parsing) is still
* raised.
Review Comment:
**Nit (P3):** The cast still runs inside the same try as the insert (line
167), so the catch also covers it. Only the size-limit and type-mismatch arms
are limited to the insert, because the cast now reports its own overflow and
never raises a path type mismatch. The `VARIANT_DUPLICATE_KEY` arm at line 173
still returns null in `try_variant_insert` mode for a duplicate key that the
cast raises while re-encoding a VARIANT value with the strict builder. A
maintainer who trusts this sentence could move the cast out of the try as a
no-op cleanup, and `try_variant_insert` would then raise instead of returning
null when the inserted variant value itself repeats a key. Spark's own variant
producers never create such a value, but non-conforming variant data read from
files can. A possible wording, which also folds in the short `failOnError` line
the rewrap left behind:
```suggestion
* `functionName`, so the size-limit and type-mismatch arms of the catch
below only see the
* insert's failures, which they map to `VARIANT_SIZE_LIMIT` and
`VARIANT_PATH_TYPE_MISMATCH`.
* When `failOnError` is false (the `try_variant_insert` mode), a
duplicate key raised by the cast
* or the insert, or a path type mismatch, returns null instead of
throwing; a size overflow (and
* a malformed path, rejected earlier during parsing) is still raised.
```
`setAtPath` (line 202) and `arrayAppendAtPath` (line 255) carry the same
sentence. There it has no behavioral effect, because the cast raises nothing
their arms match, but the same rewording without the duplicate-key part keeps
the three docs consistent.
This extends an [existing
discussion](https://github.com/apache/spark/pull/60089#discussion_r4499950567).
**Verification:**
- **Inspection:** Read each of the three rewritten Scaladoc blocks against
its method body and confirm that every statement about the try and its catch
arms matches the code, in particular that the duplicate-key arm of
`insertAtPath` also covers a duplicate key raised while casting a variant value.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/python/UserDefinedPythonDataSource.scala:
##########
@@ -462,11 +463,14 @@ private class
UserDefinedPythonDataSourceFilterPushdownRunner(
val variant = for (v <- value) yield {
val catalystValue = CatalystTypeConverters.convertToCatalyst(v)
try {
- VariantExpressionEvalUtils.castToVariant(catalystValue, dataType)
+ VariantExpressionEvalUtils.castToVariant(catalystValue, dataType,
"cast")
} catch {
case _: MatchError =>
// filter is unsupported if we can't cast it to variant
return None
+ case e: SparkRuntimeException if e.getCondition ==
"VARIANT_SIZE_LIMIT" =>
Review Comment:
**Nit (P3):** This fallback is the Python data source change the PR
description lists as user-facing, but no test reaches it: the new tests are all
in `VariantExpressionSuite`, and neither `test_python_datasource.py` nor
`PythonDataSourceSuite` has an oversized-literal case. If this arm were
deleted, or stopped matching because the condition `castToVariant` raises
changed, a Python data source query with pushdown enabled and a filter such as
`x = repeat('a', 16777216)` would fail at planning again, and every existing
suite would still pass. The exposure is narrow, since the path needs the
internal, off-by-default `spark.sql.python.filterPushdown.enabled`, and a
regression would fail loudly, as on the base branch, rather than return wrong
results. One case next to the interval literal in `test_filter_value_type`
would cover it, because constant folding turns the `repeat` call into a literal
before scan pushdown runs:
```python
self._check_filters("string", "x = repeat('a', 16777216)", [])
```
This extends an [existing
discussion](https://github.com/apache/spark/pull/60089#discussion_r4499950563).
**Verification:**
- **Regression:** With Python data source filter pushdown enabled, a query
that filters on a literal over the variant size limit completes, the reader
receives no filter for it apart from any IsNotNull filter, and the same case
fails at planning once the VARIANT_SIZE_LIMIT fallback in the pushdown runner
is removed.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/variant/VariantExpressionEvalUtils.scala:
##########
@@ -380,25 +409,24 @@ object VariantExpressionEvalUtils {
i += 1
}
- val builder = new VariantBuilder(false)
- val start = builder.getWritePos
- val fields = new
java.util.ArrayList[VariantBuilder.FieldEntry](numElements)
- i = 0
- while (i < numElements) {
- val entry = entries.getStruct(i, 2)
- if (entry.isNullAt(0)) {
- throw QueryExecutionErrors.nullAsMapKeyNotAllowedError()
+ buildVariantVal(functionName) { builder =>
+ val start = builder.getWritePos
+ val fields = new
java.util.ArrayList[VariantBuilder.FieldEntry](numElements)
+ i = 0
Review Comment:
**Nit (P3):** The closure passed to `buildVariantVal` reuses the
method-scope `var i` from the null-entry scan (line 404). Scala 2.13 compiles a
local `var` that a closure captures into a heap `scala.runtime.IntRef`, so
every `variant_from_entries` call now allocates one before the scan, and both
loops read and write the counter through its field, which the base branch did
not do. The cost is small next to the per-entry work, and the JIT may remove it
if it inlines the closure, but declaring the counter inside the closure, as
`variantFromArrays` does, avoids it with no change in results:
```suggestion
var i = 0
```
**Verification:**
- **Inspection:** Confirm that the closure passed to `buildVariantVal` in
`variantFromEntries` declares its own loop counter, which starts at 0 and
advances once per entry as before, and reads or writes no `var` declared
outside it.
- **Regression:** The existing `variant_from_entries` cases in
`VariantExpressionSuite`, which cover object construction, null values, a null
entry, a duplicate key and a size overflow, still pass under interpreted and
generated evaluation.
--
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]