Skip to content

fix: match SPARK-60262 when two requested fields select one Parquet field - #7820

Open
snmvaughan wants to merge 4 commits into
apache:mainfrom
snmvaughan:fix/parquet-field-requested-more-than-once
Open

snmvaughan wants to merge 4 commits into
apache:mainfrom
snmvaughan:fix/parquet-field-requested-more-than-once

Conversation

@snmvaughan

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #7819.

Rationale for this change

SPARK-60262 (apache/spark#59510) is in Spark 4.0.5, 4.1.4, 4.2.1 and 4.3.0. With spark.sql.parquet.fieldId.read.enabled=true, ParquetReadSupport now fails the read with PARQUET_FIELD_REQUESTED_MORE_THAN_ONCE when two requested fields of one group, the root or a struct, resolve to the same Parquet field. That covers a field matched by id plus one matched by name, and two fields with the same id. Earlier releases read without an error.

Comet's native scan resolves these fields itself (#7196). It gave the file column to the id match and NULL to the name match, so on a Spark with SPARK-60262 Spark fails the read where Comet returned rows. The Parquet field claims tests compare Comet with Spark's own read, so on such a Spark most of them fail in that read, before Comet runs.

The conflict depends on the file's own schema, so scan planning cannot see it, and nothing in the plan tells Comet which behavior applies. Comet picks it by Spark version, as #7171 and #7522 do for other Spark fixes.

What changes are included in this PR?

  • SparkFixes.failsOnFieldRequestedMoreThanOnce holds the version check: 4.0.5+, 4.1.4+, 4.2.1+ and 4.3+, the same releases as the other fixes there.
  • NativeScanCommon.fail_on_field_requested_more_than_once carries it to the native scan. CometNativeScan sets it when field-id reads are on and the check holds, and it reaches SparkParquetOptions through init_datasource_exec.
  • FieldResolver finds the first file field that a second requested field selects, and every requested field that selects it, in request order. Spark does the same in checkNoFieldRequestedMoreThanOnce once it has clipped every requested field of the group, so the error is raised at the point where Spark raises it:
    • A nested group records it in FieldMatches, and check_nested_conversion raises it after the sibling verdicts. A rejection of a field's shape still comes first, as in Spark, where clipping a struct over a primitive column fails with "is not a group". A deferred leaf rejection, which Spark raises only while it decodes, comes after it. The runtime struct conversion raises it before it converts a value.
    • The root is checked against the required roots in check_file_conversions, after the per-column checks. The scan's own adapter resolves against every root of the data schema, which Spark's pruned requested schema does not hold, so it never raises it. The footer step runs check_file_conversions for every file when the option is on.
  • SparkError::ParquetFieldRequestedMoreThanOnce and the Spark 4.x ShimSparkErrorConverter build Spark's SparkRuntimeException from its error class with Spark's three parameters, because QueryExecutionErrors.parquetFieldRequestedMoreThanOnceError is not in the Spark versions Comet builds against. It is wrapped in FAILED_READ_FILE.NO_HINT like the duplicate field id error next to it.
  • CometNativeReaderSuite: whenSparkReads runs a comparison with Spark only when Spark can read the file. When Spark fails with the new error, the test checks that Comet fails with the same message instead, from the native reader or, where the test expects a fallback, from Spark. The tests of a losing container and of a single-column projection over repeated ids need no change: Spark fails them earlier with its own error, or does not see the conflict.
  • parquet.md describes the behavior by release.

How are these changes tested?

  • Rust unit tests in field_resolver.rs follow Spark's own tests for the error: an id match and a name match in either order and in a case-sensitive read, two fields with one id around an unrelated field, every requester named in request order, no error for fields that select different or no file fields, backticks doubled in the message, and the precedence of a selection error over this one. Tests in schema_adapter.rs cover the order of a nested group's errors (shape first, deferred leaf after) and a root check that uses only the required roots.
  • error.rs checks the new error's full message against Spark's template, and its JSON parameters, error class and exception class.
  • SparkFixesSuite pins the version boundaries of the new check with the other three that share its releases.
  • CometNativeReaderSuite (184 tests) passes on Spark 3.5.9, 4.0.4, 4.0.5, 4.1.3, 4.1.4, 4.2.0 and 4.2.1, run on this change before it was rebased onto main at 785e96170. On 4.0.5, 4.1.4 and 4.2.1, which have SPARK-60262, the tests whose requested schema holds both fields read the file with Spark first, and the log of the 4.2.1 run holds Spark's SparkRuntimeException and Comet's native error for the same fields, which whenSparkReads compares. 3.5.9 needs whenSparkReads to capture the error in a var, because withSQLConf returns Unit before Spark 4.0, and its run used that form.
  • Before the rebase: cargo test for the native workspace passed apart from the five parquet::objectstore::http_metrics tests, which bind a loopback listener and could not do so where they ran, cargo fmt --all -- --check and cargo clippy --all-targets --workspace -- -D warnings were clean, and the scalafix check (-Pspark-4.1 clean compile test-compile scalafix:scalafix -Psemanticdb -Dscalafix.mode=CHECK) passed.
  • After the rebase onto main at 785e96170: cargo fmt --all -- --check is clean, cargo check --all-targets passes for datafusion-comet and datafusion-comet-common, and the Scala test-compile, with scalastyle and spotless, passes on the default profile and on -Pspark-3.5. The tests have not run since the rebase, so this PR's CI is their first run on top of current main.

…ield

Spark 4.0.5, 4.1.4, 4.2.1 and 4.3.0 fail a Parquet read with
PARQUET_FIELD_REQUESTED_MORE_THAN_ONCE when two requested fields of one
group, the root or a struct, resolve to the same Parquet field
(SPARK-60262, apache/spark#59510). That covers a field matched by id plus
one matched by name, and two fields with the same id. Comet's native
scan gave the file column to the id match and NULL to the name match, so
it returned rows where Spark fails.

The conflict depends on the file's own schema, so scan planning cannot
see it. Comet picks the behavior by Spark version, as it does for the
other Spark fixes in SparkFixes: failsOnFieldRequestedMoreThanOnce holds
the check, and a new NativeScanCommon option carries it to the native
scan, where FieldResolver finds the first file field that a second
requested field selects. The error is raised where Spark raises it. A
nested group records it, and check_nested_conversion raises it after the
sibling verdicts, so a rejection of a field's shape still comes first and
a deferred leaf rejection comes after it. The root is checked against the
required roots in check_file_conversions, because the scan's own adapter
resolves against every root of the data schema, which Spark's pruned
requested schema does not hold. The Spark 4.x error converter builds
Spark's SparkRuntimeException from its error class, since the
QueryExecutionErrors method is not in the Spark versions Comet builds
against.

The "Parquet field claims" tests compare Comet with Spark's own read.
whenSparkReads runs a comparison only when Spark can read the file. When
Spark fails with the new error, the test checks that Comet fails with the
same error instead.

The docs describe the behavior by release and say where the error can differ
from Spark's order: Comet raises some rejections at open that Spark raises
while it decodes, and does not yet check the shape of a root field that
loses its file column (apache#7207).

Tests: unit tests in field_resolver.rs and schema_adapter.rs follow Spark's
own tests for the error, error.rs pins the new error's message against
Spark's template together with its JSON parameters, error class and
exception class, and SparkFixesSuite pins the version boundaries.
whenSparkReads reads with parquet-mr and without filter pushdown, because
Spark's vectorized reader returns garbage for this conflict on a Spark
without the check, and it captures the error in a var because
withSQLConf returns Unit before Spark 4.0, so the suite also compiles on
Spark 3.5.

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the version gate against the Spark clone: SPARK-60262 is in v4.0.5, v4.1.4, v4.2.1 and v4.3.0 and not on 3.5, and the native check follows checkNoFieldRequestedMoreThanOnce (first file field selected twice, every requester in request order, a missing id match never collides). No pom profile is at a fixed release (spark-4.0 is 4.0.4, spark-4.1 4.1.3, spark-4.2 4.2.0), so in CI only the Rust unit tests reach the new path, and the shim case and the failure branch of whenSparkReads rest on the local runs in the description. The comments below are about removing a check I think is unreachable and keeping the flag out of FieldResolver. #7731 and #6246 also take NativeScanCommon field 20, so whichever lands second has to renumber.

Comment on lines +566 to +570
let matches = match_struct_fields(from_fields, to_fields, parquet_options)?;
// The plan-time check raises this first. Raise it here too, before converting any
// value, as Spark does before it reads any.
matches.check_requested_once()?;
let from_indices = matches.into_owned();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell this check is unreachable. A conflict inside a struct means the requested and file struct types differ, so the adapter's rewrite calls check_field_conversion on that column at open, and check_nested_conversion raises the error there. With the flag on, the footer step also runs check_file_conversions for every file before that. Dropping this call would also stop resolve_all from computing requested_more_than_once, and allocating its taken vector, for every struct of every batch. I did not run this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it was unreachable. Apart from the scalar conversion of a column default, which has no file field to request twice, parquet_convert_struct_to_struct runs only through a CometCastColumnExpr. The adapter builds one for a struct, list or map column only after check_field_conversion has accepted it, so check_nested_conversion has already raised the error at open. (Variant columns skip that check, but their two fields cannot select the same file field.) When the flag is on, the footer step also runs check_file_conversions for every file before that, and no test reached the runtime call.

Removed in e8e95b6. resolve_all no longer computes the verdict, or allocates the taken vector, for every struct of every batch.

Comment on lines +357 to +363
/// The requested fields and the file field of `PARQUET_FIELD_REQUESTED_MORE_THAN_ONCE`, both
/// quoted, when two requested fields select the same file field and this resolver fails on
/// it, as Spark's `checkNoFieldRequestedMoreThanOnce` does once it has clipped every requested
/// field of the group. `selected` holds the file field each requested field reads, in request
/// order. It names the first file field that a second requested field selects and every
/// requested field that selects it, in request order.
fn requested_more_than_once(&self, selected: &[Option<usize>]) -> Option<(String, String)> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the runtime caller, only check_nested_conversion and the root check in check_file_conversions need this, and both already hold options.fail_on_field_requested_more_than_once. If this were a pub(crate) method over selected (with FieldMatches exposing its selected) that those two call when the option is on, the new FieldResolver::new parameter would go away, along with the hard-coded false in SparkPhysicalExprAdapterFactory::create and in the test helpers, the requested_more_than_once field on FieldMatches, and the extra selected vector in file_claims. The root check can build selected from select(i)?.file() over the required roots. I did not try this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done as you describe, in e8e95b6. check_requested_once(requested, file, selected) is a free function in field_resolver.rs over the file field each requested field selects. check_nested_conversion calls it with FieldMatches::selected(), and the root check in check_file_conversions builds selected from select(i)?.file() over the required roots; both call it only when the option is on.

FieldResolver::new lost the parameter, so create and the test helpers lost the hard-coded false. FieldMatches lost requested_more_than_once, and file_claims is back to selecting and recording claims. The resolver tests now run both ways of resolving a group (resolve_all, and one select per root) through the same function and compare the errors.

Comment on lines 1633 to 1641
let roots = options.fail_on_field_requested_more_than_once.then(|| {
(
Arc::clone(&physical),
options.case_sensitive,
options.use_field_id,
)
});
let adapter = SparkPhysicalExprAdapterFactory::new(options, None)
.create(Arc::clone(&required), physical)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

roots exists only to keep physical and two flags alive after options moves into the factory. Reading the three flags into locals and passing Arc::clone(&physical) to create would make the tuple unnecessary and read more directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in e8e95b6: check_file_conversions reads the three flags into locals before options moves, and clones physical into create, so the tuple is gone.

Comment on lines +92 to +100
def failsOnFieldRequestedMoreThanOnce(sparkVersion: String): Boolean =
// On branch-4.0, branch-4.1, branch-4.2, branch-4.3 and master, and in the 4.0.5, 4.1.4, 4.2.1
// and 4.3.0 releases, the same releases as the three fixes above.
Utils.majorMinorPatchVersion(sparkVersion).exists {
case (4, 0, patch) => patch >= 5
case (4, 1, patch) => patch >= 4
case (4, 2, patch) => patch >= 1
case (major, minor, _) => major > 4 || (major == 4 && minor >= 3)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike the other fixes here, this one leaves a marker that Comet can see at run time: the PARQUET_FIELD_REQUESTED_MORE_THAN_ONCE condition in the running Spark's error classes. The 4.x shim is already under org.apache.spark, so it could expose SparkThrowableHelper.isValidErrorClass("PARQUET_FIELD_REQUESTED_MORE_THAN_ONCE"), which exists on 4.0.4, 4.1.3 and 4.2.0, and the 3.5 shim could return false. Then the flag would be on exactly when ShimSparkErrorConverter can build the SparkRuntimeException. Building it for a missing class throws Cannot find main error class instead. It would also cover a fork or snapshot that carries the backport under an older version string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 5931859. SparkFixes.failsOnFieldRequestedMoreThanOnce and its two tests are gone. CometNativeScan now asks ShimParquetErrors.failsOnFieldRequestedMoreThanOnce, which is false in spark-3.x and SparkThrowableHelper.isValidErrorClass("PARQUET_FIELD_REQUESTED_MORE_THAN_ONCE") in spark-4.x. The flag is on exactly when ShimSparkErrorConverter can build the exception, and a build that carries the fix under an older version string gets it too.

SparkErrorConverterSuite has two new tests. One pins the shim to the release table for plain releases and is skipped for snapshots and other builds. The other checks that the converter wraps the SparkRuntimeException in FAILED_READ_FILE.NO_HINT when Spark has the class. That is the shim case that CI cannot reach, since it is skipped on the releases CI builds against. I ran both on 3.5.9, 4.0.4, 4.0.5, 4.1.3, 4.1.4, 4.2.0 and 4.2.1: the flag is on for exactly 4.0.5, 4.1.4 and 4.2.1, and the converter test passes on those three.

Review of the SPARK-60262 change.

Drop the check in `parquet_convert_struct_to_struct`. A column reaches that conversion only
through a `CometCastColumnExpr`, which the adapter builds after `check_field_conversion` has
raised the error for the column at open, and the footer step runs `check_file_conversions` for
every file before that. No test reached the runtime call. `resolve_all` no longer computes the
verdict, or allocates its `taken` vector, for every struct of every batch.

Move the check out of `FieldResolver`. `check_requested_once` takes the requested fields, the
file fields and the file field each requested field selects, and `check_nested_conversion` and
the root check in `check_file_conversions` call it when the option is on. `FieldResolver::new`
loses its parameter, `FieldMatches` its verdict and `file_claims` goes back to selecting and
recording claims. The root check builds the selections from `select` over the required roots.

Read the option into locals in `check_file_conversions` instead of keeping a tuple alive for it.
… error classes

Replace the version table for SPARK-60262 with a check of the running Spark's error classes.
`ShimParquetErrors.failsOnFieldRequestedMoreThanOnce` is false for Spark 3.x and, for Spark 4.x,
`SparkThrowableHelper.isValidErrorClass("PARQUET_FIELD_REQUESTED_MORE_THAN_ONCE")`, so the native
reader fails the read exactly when `ShimSparkErrorConverter` can build the exception, and a build
that carries the fix under an older version string is covered. Building the exception for a
Spark without the class fails with `Cannot find main error class`.

`SparkErrorConverterSuite` pins the shim to the release table for plain releases and checks that
the converter builds `FAILED_READ_FILE.NO_HINT` around the `SparkRuntimeException` when Spark
has the class. The second test needs 4.0.5, 4.1.4, 4.2.1 or later, so it is skipped on the
Spark releases that CI builds against.
@snmvaughan

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Two commits answer the four comments, with a reply on each thread:

  • e8e95b6 drops the unreachable runtime check, moves the check out of FieldResolver, and removes the roots tuple.
  • 5931859 derives the flag from the running Spark's error classes instead of a version table.

With both commits, CometNativeReaderSuite passes 184 of 184 on Spark 4.1.3, where the flag is off, and on 4.1.4, where it is on.

On the field number: noted. I will renumber fail_on_field_requested_more_than_once if #7731 or #6246 lands first.

"requestedFields" -> params("requestedFields").toString,
"parquetField" -> params("parquetField").toString,
"fieldIdReadConf" -> ("\"" + SQLConf.PARQUET_FIELD_ID_READ_ENABLED.key + "\"")))
val requestedPath = params.get("filePath").map(_.toString).getOrElse("")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we carry the failing file’s path with this error? The Rust payload currently contains only requestedFields and parquetField, so requestedPath is always empty on the native error path. This leaves FAILED_READ_FILE.NO_HINT without the filename, which makes a multi-file scan failure difficult to diagnose.

The converter test supplies filePath manually, so it does not exercise this gap. Could we add a reader test with multiple files that checks the outer error identifies the failing file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. The native error carried no path, so the shim wrapped it with an empty one. Fixed in 201b71f.

ParquetFieldRequestedMoreThanOnce now has a file_path, which goes out as the filePath parameter that ShimSparkErrorConverter already reads. The schema adapter that finds the conflict knows no file and leaves it empty. The footer check, which runs for every file when the flag is on, fills it in with the path Spark knows the file by (spark_file_path, which FileContext now shares), so the error names the file whichever file of the scan fails.

Tests: the footer check test in eager_page_index_reader_factory.rs names the file, by the Spark path when the scan kept it and by the object-store location otherwise, and leaves a file without a conflict alone. In CometNativeReaderSuite, "Parquet field claims - the error names the file that holds the conflict" reads two files, one with the conflict, and checks that the FAILED_READ_FILE error names it, as Spark's does. It is skipped on a Spark that does not fail the read.

I ran CometNativeReaderSuite on Spark 4.1.4, where the flag is on: 185 of 185 pass, including the new test. On 4.1.3, where it is off, 184 pass and the new test is skipped.

Review of the SPARK-60262 change. The native error carried only the requested fields and the
Parquet field, so on the native path the 4.x shim wrapped it in `FAILED_READ_FILE.NO_HINT` with an
empty path, which leaves a scan of several files without the name of the file that failed.

`ParquetFieldRequestedMoreThanOnce` now has a `file_path`, which travels as the `filePath`
parameter that the shim already reads. The schema adapter that finds the conflict knows no file
and leaves it empty. The footer check, which runs for every file when the option is on, fills it
in with the path Spark knows the file by, taken with `spark_file_path`, which `FileContext` uses
too.

The footer check test names the file, by the Spark path when the scan kept it and by the
object-store location otherwise, and does not name a file without the conflict. A reader test
reads two files, one with the conflict, and checks that the `FAILED_READ_FILE` error names it, as
Spark's does.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Native Parquet scan returns rows where Spark 4.0.5, 4.1.4, 4.2.1 and 4.3.0 fail with PARQUET_FIELD_REQUESTED_MORE_THAN_ONCE (SPARK-60262)

3 participants