sdf-jkl commented on code in PR #12310:
URL: https://github.com/apache/arrow-rs/pull/12310#discussion_r4746688043
##########
parquet-variant-compute/src/variant_get.rs:
##########
@@ -255,6 +308,9 @@ fn shredded_get_path(
} else {
as_field.map(|f| f.data_type())
};
+ if target.typed_value_column().is_none() {
Review Comment:
With this early return, every non-empty path goes to `get_path_from_bytes`:
the only caller that passes one is the `NotShredded` step, and its target has
no `typed_value`. That allows two follow-on changes:
1. The loop below now always gets an empty path, so `VariantPathRowBuilder`,
the `WithPath` variant and the `path` parameter of
`make_variant_to_arrow_row_builder` are unreachable.
2. The `unshred_variant` call above can move into the empty-path return.
Today an explicit `Variant` output (`as_type` with the `VariantType` extension,
which is what datafusion-variant passes) unshreds the `NotShredded` target
before `get_path_from_bytes` reads the same bytes. When some rows have a null
leftover `value`, `unshred_variant` falls back to its row loop and validates
and re-encodes every row first.
Together that is +7/-61:
<details><summary>diff</summary>
```diff
diff --git a/parquet-variant-compute/src/variant_get.rs
b/parquet-variant-compute/src/variant_get.rs
index 4fb2faf4b9..9fe5b44a4a 100644
--- a/parquet-variant-compute/src/variant_get.rs
+++ b/parquet-variant-compute/src/variant_get.rs
@@ -209,7 +209,6 @@ fn get_path_from_bytes(
) -> Result<ArrayRef> {
let mut builder = make_variant_to_arrow_row_builder(
target.metadata_column(),
- VariantPath::default(),
as_type,
cast_options,
target.len(),
@@ -287,20 +286,8 @@ fn shredded_get_path(
));
}
- // Collapse any shredding back to binary. Only the
`NotShredded` step below passes a
- // non-empty `path`, and there `target` is already a plain
`value` column (no
- // `typed_value`) -- so `unshred_variant` hits its clone
fast-path, with nothing deeper
- // to shred. The builder then walks any remaining path per-row,
emitting variant output
- // because `as_type` is `None`.
- let target = if requested_variant {
- unshred_variant(&target)?
- } else {
- target
- };
-
- // Path exhausted, variant requested: return the target
directly.
if requested_variant && path.is_empty() {
- return Ok(ArrayRef::from(target));
+ return Ok(ArrayRef::from(unshred_variant(&target)?));
}
let as_type = if requested_variant {
@@ -311,9 +298,11 @@ fn shredded_get_path(
if target.typed_value_column().is_none() {
return get_path_from_bytes(&target, &path, as_type,
cast_options);
}
+ // Only the `NotShredded` step passes a non-empty `path`, and
its target has no
+ // `typed_value`.
+ debug_assert!(path.is_empty() && !requested_variant);
let mut builder = make_variant_to_arrow_row_builder(
target.metadata_column(),
- path,
as_type,
cast_options,
target.len(),
diff --git a/parquet-variant-compute/src/variant_to_arrow.rs
b/parquet-variant-compute/src/variant_to_arrow.rs
index 52c3956403..59c35fd876 100644
--- a/parquet-variant-compute/src/variant_to_arrow.rs
+++ b/parquet-variant-compute/src/variant_to_arrow.rs
@@ -25,7 +25,7 @@ use arrow::compute::{CastOptions, DecimalCast,
cast_with_options};
use arrow::datatypes::{self, DataType, DecimalType};
use arrow::error::{ArrowError, Result};
use arrow_schema::{FieldRef, Fields, TimeUnit, UnionFields, UnionMode};
-use parquet_variant::{Variant, VariantPath};
+use parquet_variant::Variant;
use std::sync::Arc;
/// Builder for converting variant values into strongly typed Arrow arrays.
@@ -40,9 +40,6 @@ pub(crate) enum VariantToArrowRowBuilder<'a> {
Map(MapVariantToArrowRowBuilder<'a>),
Encoded(EncodedVariantToArrowRowBuilder<'a>),
BinaryVariant(VariantToBinaryVariantArrowRowBuilder),
-
- // Path extraction wrapper - contains a boxed enum for any of the above
- WithPath(VariantPathRowBuilder<'a>),
}
impl VariantToArrowRowBuilder<'_> {
@@ -56,7 +53,6 @@ impl VariantToArrowRowBuilder<'_> {
Map(b) => b.append_null(),
Encoded(b) => b.append_null(),
BinaryVariant(b) => b.append_null(),
- WithPath(path_builder) => path_builder.append_null(),
}
}
@@ -70,7 +66,6 @@ impl VariantToArrowRowBuilder<'_> {
Map(b) => b.append_value(&value),
Encoded(b) => b.append_value(value),
BinaryVariant(b) => b.append_value(value),
- WithPath(path_builder) => path_builder.append_value(value),
}
}
@@ -84,7 +79,6 @@ impl VariantToArrowRowBuilder<'_> {
Map(b) => b.finish(),
Encoded(b) => b.finish(),
BinaryVariant(b) => b.finish(),
- WithPath(path_builder) => path_builder.finish(),
}
}
}
@@ -162,14 +156,13 @@ fn make_typed_variant_to_arrow_row_builder<'a>(
pub(crate) fn make_variant_to_arrow_row_builder<'a>(
metadata: &ArrayRef,
- path: VariantPath<'a>,
data_type: Option<&'a DataType>,
cast_options: &'a CastOptions,
capacity: usize,
) -> Result<VariantToArrowRowBuilder<'a>> {
use VariantToArrowRowBuilder::*;
- let mut builder = match data_type {
+ Ok(match data_type {
// If no data type was requested, build an unshredded VariantArray.
None => BinaryVariant(VariantToBinaryVariantArrowRowBuilder::new(
metadata.clone(),
@@ -181,17 +174,7 @@ pub(crate) fn make_variant_to_arrow_row_builder<'a>(
capacity,
VariantCastMode::Get,
)?,
- };
-
- // Wrap with path extraction if needed
- if !path.is_empty() {
- builder = WithPath(VariantPathRowBuilder {
- builder: Box::new(builder),
- path,
- })
- }
-
- Ok(builder)
+ })
}
/// Builder for converting primitive variant values to Arrow arrays. It is
used by both
@@ -1168,32 +1151,6 @@ impl<'a> ArrayVariantToArrowRowBuilder<'a> {
}
}
-/// A thin wrapper whose only job is to extract a specific path from a
variant value and pass the
-/// result to a nested builder.
-pub(crate) struct VariantPathRowBuilder<'a> {
- builder: Box<VariantToArrowRowBuilder<'a>>,
- path: VariantPath<'a>,
-}
-
-impl VariantPathRowBuilder<'_> {
- fn append_null(&mut self) -> Result<()> {
- self.builder.append_null()
- }
-
- fn append_value(&mut self, value: Variant<'_, '_>) -> Result<bool> {
- if let Some(v) = value.get_path(&self.path) {
- self.builder.append_value(v)
- } else {
- self.builder.append_null()?;
- Ok(false)
- }
- }
-
- fn finish(self) -> Result<ArrayRef> {
- self.builder.finish()
- }
-}
-
macro_rules! define_variant_to_primitive_builder {
(struct $name:ident<$lifetime:lifetime $(, $generic:ident: $bound:path
)?>
|$array_param:ident $(, $field:ident: $field_type:ty)?| ->
$builder_name:ident $(< $array_type:ty >)? { $init_expr: expr },
```
</details>
The crate tests pass, and the output matches this PR on everything I tried
(unshredded, sliced, dictionary metadata, and partially shredded objects, for
typed, `None` and explicit `Variant` outputs, safe and strict). On 8192
partially shredded objects where half the rows have nothing left over,
`variant_get` of a leftover field with an explicit `Variant` output went from
about 2.6 ms to 0.6 ms locally, the same as with `as_type: None`. No existing
benchmark covers that case, so I can add one in a separate PR.
One side effect: on that route, malformed bytes outside the path no longer
raise an error in strict mode, because `unshred_variant` no longer validates
the whole row. That matches the `as_type: None` route and #12513.
##########
parquet-variant/src/decoder.rs:
##########
@@ -120,23 +120,6 @@ pub(crate) enum OffsetSizeBytes {
}
impl OffsetSizeBytes {
- /// Build from the `offset_size_minus_one` bits (see spec).
Review Comment:
No validation was dropped here. Both old callers of `try_new` already passed
only 2 bits (`header_byte >> 6` for metadata, `value_header & 0x03` for lists),
so its error branch could never run, and `from_bits` masks the same way.
It became dead code once the headers store the raw byte, like
`VariantObjectHeader` in #12309. The description gives the reason for that: one
byte is cheaper to build and copy than the decoded fields.
##########
parquet-variant-compute/src/variant_array.rs:
##########
@@ -60,6 +60,29 @@ pub(crate) fn binary_array_value(array: &dyn Array, index:
usize) -> Option<&[u8
}
}
+/// A binary-like column whose array type is resolved once, not on every row.
Review Comment:
nit: only `BinaryView` is resolved once here. Binary, LargeBinary and
dictionary or run-end metadata still go through `binary_array_value` on every
row.
```suggestion
/// A binary-like column that resolves `BinaryView` once and other array
types on every row.
```
##########
parquet-variant/src/variant/view.rs:
##########
@@ -247,6 +248,46 @@ impl<'m, 'v> VariantView<'m, 'v> {
}
}
+ /// Follows `path` from this value, or returns `None` if a step does not
exist.
+ ///
+ /// A [`VariantPathElement::ListElement`] step does not exist, as in
[`Variant::get_path`].
Review Comment:
`ListElement` is the `[*]` wildcard, not an index. Index steps like `[0]`
are followed (the example below reads `scores[1]`). `[*]` matches nothing here,
same as `Variant::get_path`, and `variant_get` rejects it before it gets this
far. Maybe the doc could say that directly:
```suggestion
/// Index steps such as `[0]` are followed. A `[*]` step
([`VariantPathElement::ListElement`])
/// matches nothing and returns `None`, as in [`Variant::get_path`].
```
So I don't think a follow-up is needed. `[*]` only makes sense when building
shredding schemas (`ShreddedSchemaBuilder`), and Spark's `variant_get` path
grammar has keys and indexes only, no wildcard.
--
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]