raghuvanshraj commented on code in PR #12303:
URL: https://github.com/apache/arrow-rs/pull/12303#discussion_r4715765327
##########
parquet-variant/src/canonicalize.rs:
##########
@@ -0,0 +1,552 @@
+// 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,
VariantDecimal4,
+ VariantDecimal8, VariantDecimal16, VariantObject,
+};
+
+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
proposed for 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.
+ /// * Integers use the smallest of `INT8`, `INT16`, `INT32`, `INT64` that
holds the value.
+ /// * Decimals drop trailing zeros. An integral decimal that fits an `i64`
becomes an integer;
+ /// otherwise the smallest of `DECIMAL4`, `DECIMAL8`, `DECIMAL16` that
holds it is used.
+ /// * `FLOAT` and `DOUBLE` keep their type; `-0.0` becomes `0.0` and every
NaN becomes the
+ /// same bit pattern ([`f32::NAN`] / [`f64::NAN`]).
+ /// * Strings use the short-string encoding when they fit (at most 63
bytes).
+ /// * Booleans, `NULL`, dates, times, timestamps, binary and UUID have one
encoding already and
+ /// are copied as-is.
+ ///
+ /// 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 is malformed or an object holds the
same field name twice.
+ ///
+ /// # 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
+ pub fn canonicalize(&self) -> Result<(Vec<u8>, Vec<u8>), ArrowError> {
+ let mut field_names = BTreeSet::new();
+ collect_field_names(self, &mut field_names)?;
+ // `str` orders by bytes, so the set is already in unsigned UTF-8
order.
+ let mut builder =
VariantBuilder::new().with_field_names(field_names.iter().copied());
+ append_canonical(&mut builder, self)?;
+ builder.try_finish()
+ }
+}
+
+/// Collects every field name referenced anywhere inside `value`.
+fn collect_field_names<'m>(
+ value: &Variant<'m, '_>,
+ names: &mut BTreeSet<&'m str>,
+) -> Result<(), ArrowError> {
+ match value {
+ Variant::Object(object) => {
+ for field in object.iter_try() {
+ let (name, value) = field?;
+ names.insert(name);
+ collect_field_names(&value, names)?;
+ }
+ }
+ Variant::List(list) => {
+ for item in list.iter_try() {
+ collect_field_names(&item?, names)?;
+ }
+ }
+ _ => {}
+ }
+ Ok(())
+}
+
+/// Appends `value` to `builder` in canonical form. The builder's metadata
dictionary must already
+/// hold every field name `value` references, in sorted order.
+fn append_canonical<B: VariantBuilderExt>(
+ builder: &mut B,
+ value: &Variant<'_, '_>,
+) -> Result<(), ArrowError> {
Review Comment:
I've moved these to variant/equivalence.rs as pub(crate) fn
canonical_primitive(&self) -> Variant. append_canonical only walks objects and
lists now and calls that for everything else.
--
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]