sdf-jkl commented on code in PR #12303:
URL: https://github.com/apache/arrow-rs/pull/12303#discussion_r4724415267
##########
parquet-variant/src/canonicalize.rs:
##########
@@ -0,0 +1,469 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+//! Canonical encoding of [`Variant`] values.
+//!
+//! The Variant binary encoding allows one logical value to be written in many
ways: the metadata
+//! dictionary may hold unused or unsorted field names, an integer may use a
wider width than it
+//! needs, a decimal may carry trailing zeros, and so on. Byte-oriented
operations such as hashing
+//! for `GROUP BY` or a join therefore treat equal values as different.
+//!
+//! [`Variant::canonicalize`] rebuilds a value so that equal values share one
encoding.
+
+use std::collections::BTreeSet;
+
+use arrow_schema::ArrowError;
+
+use crate::{ObjectFieldBuilder, Variant, VariantBuilder, VariantBuilderExt};
+
+impl Variant<'_, '_> {
+ /// Returns the canonical `(metadata, value)` encoding of this variant.
+ ///
+ /// Two variants that are logically equal canonicalize to identical bytes,
so the result can be
+ /// hashed or compared byte-wise. The rules follow the canonical form used
by Apache Spark
+ /// ([apache/spark#59669]):
Review Comment:
On second thought about my earlier Spark comment: the rules come from the
Parquet spec's equivalence classes, so I'd point there instead of at Spark:
```suggestion
/// Two variants that are logically equal under the Variant spec's
[equivalence classes]
/// canonicalize to identical bytes, so the result can be hashed or
compared byte-wise:
```
##########
parquet-variant/src/canonicalize.rs:
##########
@@ -0,0 +1,469 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+//! Canonical encoding of [`Variant`] values.
+//!
+//! The Variant binary encoding allows one logical value to be written in many
ways: the metadata
+//! dictionary may hold unused or unsorted field names, an integer may use a
wider width than it
+//! needs, a decimal may carry trailing zeros, and so on. Byte-oriented
operations such as hashing
+//! for `GROUP BY` or a join therefore treat equal values as different.
+//!
+//! [`Variant::canonicalize`] rebuilds a value so that equal values share one
encoding.
+
+use std::collections::BTreeSet;
+
+use arrow_schema::ArrowError;
+
+use crate::{ObjectFieldBuilder, Variant, VariantBuilder, VariantBuilderExt};
+
+impl Variant<'_, '_> {
+ /// Returns the canonical `(metadata, value)` encoding of this variant.
+ ///
+ /// Two variants that are logically equal canonicalize to identical bytes,
so the result can be
+ /// hashed or compared byte-wise. The rules follow the canonical form used
by Apache Spark
+ /// ([apache/spark#59669]):
+ ///
+ /// * The metadata dictionary holds only the field names the value
references, sorted by their
+ /// unsigned UTF-8 bytes, with the smallest offset width that fits.
+ /// * Object fields are written in dictionary (key) order; list elements
keep their order.
+ /// * Each primitive uses the representative encoding of its equivalence
class in the Variant
+ /// spec, see [`Variant::canonical_primitive`]: the narrowest integer,
decimals with trailing
+ /// zeros dropped (promoted to an integer when integral and within
`i64`), `-0.0` as `0.0`
+ /// and one NaN pattern per float type, short strings when they fit,
microsecond timestamps
+ /// unless sub-microsecond digits are present. Null, booleans, dates,
times, binary and UUID
+ /// have one encoding already.
+ ///
+ /// Canonicalizing an already canonical value returns the same bytes.
+ ///
+ /// Note that the canonical form may change the Rust-level type of a
value: `Variant::Int64(1)`
+ /// canonicalizes to the bytes of `Variant::Int8(1)`, and
`Variant::Decimal4(1.0)` to those of
+ /// `Variant::Int8(1)`.
+ ///
+ /// # Errors
+ ///
+ /// Returns an error if the variant does not pass [full validation], for
example when an object
+ /// holds unsorted or repeated field names. Validation is cached, so this
is free for values
+ /// created with [`Variant::try_new`].
+ ///
+ /// # Example
+ /// ```
+ /// # use parquet_variant::{Variant, VariantBuilder};
+ /// // Two builders write the same object with a different dictionary order
+ /// let mut a = VariantBuilder::new().with_field_names(["b", "a",
"unused"]);
+ /// a.new_object().with_field("a", 1i64).with_field("b", 2i8).finish();
+ /// let (am, av) = a.finish();
+ ///
+ /// let mut b = VariantBuilder::new();
+ /// b.new_object().with_field("a", 1i8).with_field("b", 2i32).finish();
+ /// let (bm, bv) = b.finish();
+ ///
+ /// assert_ne!((&am, &av), (&bm, &bv));
+ /// let a_canonical = Variant::try_new(&am,
&av).unwrap().canonicalize().unwrap();
+ /// let b_canonical = Variant::try_new(&bm,
&bv).unwrap().canonicalize().unwrap();
+ /// assert_eq!(a_canonical, b_canonical);
+ /// ```
+ ///
+ /// [apache/spark#59669]: https://github.com/apache/spark/pull/59669
Review Comment:
```suggestion
/// [equivalence classes]:
https://github.com/apache/parquet-format/blob/master/VariantEncoding.md#encoding-types
```
--
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]