comphead commented on code in PR #7820:
URL: https://github.com/apache/datafusion-comet/pull/7820#discussion_r4762656815
##########
native/core/src/parquet/schema_adapter.rs:
##########
@@ -1605,11 +1618,25 @@ pub(crate) const NANOS_TIMESTAMP_KEY: &str =
"comet.parquet.nanos_timestamp";
/// Check only requested columns, using the same resolver and conversion rules
as projection
/// adaptation. Return the first deferred failure, but continue to let
open-time errors win.
+///
+/// When `options` fails on a file field requested more than once, this also
checks the required
+/// roots for one, after the per-column checks, as Spark checks its requested
schema once it has
+/// clipped every field of it. Nested groups are checked by the per-column
checks. Comet does not
+/// yet check the shape of a root field that loses its file column
(apache/datafusion-comet#7207),
+/// so for a root conflict whose losing field has the wrong shape, Spark
reports the shape and
+/// this reports the field requested twice.
pub(crate) fn check_file_conversions(
required: SchemaRef,
physical: SchemaRef,
options: SparkParquetOptions,
) -> DataFusionResult<Option<RejectOnNonEmpty>> {
+ 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)?;
Review Comment:
`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.
##########
native/core/src/parquet/parquet_support.rs:
##########
@@ -555,8 +563,11 @@ fn parquet_convert_struct_to_struct(
) -> DataFusionResult<(ArrayRef, bool)> {
match (from_type, to_type) {
(DataType::Struct(from_fields), DataType::Struct(to_fields)) => {
- let from_indices =
- match_struct_fields(from_fields, to_fields,
parquet_options)?.into_owned();
+ 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();
Review Comment:
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.
##########
native/core/src/parquet/field_resolver.rs:
##########
@@ -299,12 +337,49 @@ impl<'a> FieldResolver<'a> {
/// nested conversion validates every requested field in
[`Self::resolve_all`].
pub(crate) fn file_claims(&self) -> DataFusionResult<Vec<FieldClaim>> {
let mut claims = vec![FieldClaim::Unchanged; self.file.len()];
+ let mut selected = Vec::new();
for requested in 0..self.requested.len() {
- self.record_claims(&mut claims, requested,
self.select(requested)?);
+ let selection = self.select(requested)?;
+ self.record_claims(&mut claims, requested, selection);
+ if self.fail_on_field_requested_more_than_once {
+ selected.push(selection.file());
+ }
+ }
+ if let Some((requested_fields, parquet_field)) =
self.requested_more_than_once(&selected) {
+ return Err(requested_more_than_once_error(
+ &requested_fields,
+ &parquet_field,
+ ));
}
Ok(claims)
}
+ /// 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)> {
Review Comment:
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.
##########
spark/src/main/scala/org/apache/comet/serde/SparkFixes.scala:
##########
@@ -81,6 +81,24 @@ private[comet] object SparkFixes {
case (major, minor, _) => major > 4 || (major == 4 && minor >= 3)
}
+ /**
+ * SPARK-60262: a Parquet read fails with
PARQUET_FIELD_REQUESTED_MORE_THAN_ONCE when two
+ * requested fields of a struct, or of the top-level schema, resolve to the
same Parquet field.
+ * That happens when `spark.sql.parquet.fieldId.read.enabled` is on and one
field is matched by
+ * id and another by name, or two fields have the same id. Earlier releases
read without an
+ * error: the parquet-mr reader gives one of the two fields NULL, and the
vectorized reader
+ * leaves one of the two vectors unwritten.
+ */
+ 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)
+ }
Review Comment:
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.
--
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]