alamb commented on code in PR #12401:
URL: https://github.com/apache/arrow-rs/pull/12401#discussion_r4686086744


##########
parquet/src/arrow/arrow_writer/byte_array.rs:
##########
@@ -1060,29 +1060,91 @@ where
         if let Some(accumulator) = encoder.geo_stats_accumulator.as_mut() {
             update_geo_stats_accumulator(accumulator.as_mut(), values, 
indices);
         } else if let Some((min, max)) = compute_min_max(values, indices, 
encoder.is_decimal) {
-            // Compare before copying: `write_gather` runs once per
-            // mini-batch, and a byte-budgeted mini-batch of large values can
-            // hold a single value, so an unconditional copy here would
-            // duplicate every value once for `min` and once for `max`.
-            let min = min.as_ref();
-            if encoder
-                .min_value
-                .as_ref()
-                .is_none_or(|m| is_greater(encoder.is_decimal, m.data(), min))
-            {
-                encoder.min_value = Some(min.to_vec().into());
-            }
+            update_min_max(encoder, min.as_ref(), max.as_ref());
+        }
+    }
+}
 
-            let max = max.as_ref();
-            if encoder
-                .max_value
-                .as_ref()
-                .is_none_or(|m| is_greater(encoder.is_decimal, max, m.data()))
-            {
-                encoder.max_value = Some(max.to_vec().into());
-            }
+/// [`update_statistics`] for the rows of an arrow dictionary at `indices`, 
comparing each
+/// dictionary value they reference once rather than once per row
+fn update_dictionary_statistics<'a, K, V>(
+    values: TypedDictionaryArray<'a, K, V>,
+    indices: &[usize],
+    encoder: &mut ByteArrayEncoder,
+) where
+    K: ArrowDictionaryKeyType,
+    V: Sync + Send,
+    &'a V: ArrayAccessor,
+    <&'a V as ArrayAccessor>::Item: Copy + Ord + Default + AsRef<[u8]>,
+{
+    let dictionary_values = values.values();
+    // Rows only repeat values for certain when the dictionary is no larger 
than the batch,
+    // which also bounds the pass over the dictionary by the number of rows. 
Decimals
+    // compare numerically, so values with different bytes can tie, and which 
one becomes
+    // the statistic depends on the order of the rows
+    if encoder.statistics_enabled == EnabledStatistics::None
+        || encoder.geo_stats_accumulator.is_some()
+        || encoder.is_decimal
+        || dictionary_values.len() > indices.len()
+    {
+        return update_statistics(values, indices, encoder);
+    }
+
+    let keys = values.keys().values();
+    let mut referenced = vec![false; dictionary_values.len()];

Review Comment:
   I think the bitmaps (aka `BooleanBuffer`_ in this crate are far more 
efficient than `Vec<bool>` for this sort of thing -- did you consider using 
them?
   
   We could also avoid this new allocation by saving a BooleanBuffer on the 
byte array encoder perhaps



##########
parquet/src/arrow/arrow_writer/byte_array.rs:
##########
@@ -1060,29 +1060,91 @@ where
         if let Some(accumulator) = encoder.geo_stats_accumulator.as_mut() {
             update_geo_stats_accumulator(accumulator.as_mut(), values, 
indices);
         } else if let Some((min, max)) = compute_min_max(values, indices, 
encoder.is_decimal) {
-            // Compare before copying: `write_gather` runs once per
-            // mini-batch, and a byte-budgeted mini-batch of large values can
-            // hold a single value, so an unconditional copy here would
-            // duplicate every value once for `min` and once for `max`.
-            let min = min.as_ref();
-            if encoder
-                .min_value
-                .as_ref()
-                .is_none_or(|m| is_greater(encoder.is_decimal, m.data(), min))
-            {
-                encoder.min_value = Some(min.to_vec().into());
-            }
+            update_min_max(encoder, min.as_ref(), max.as_ref());
+        }
+    }
+}
 
-            let max = max.as_ref();
-            if encoder
-                .max_value
-                .as_ref()
-                .is_none_or(|m| is_greater(encoder.is_decimal, max, m.data()))
-            {
-                encoder.max_value = Some(max.to_vec().into());
-            }
+/// [`update_statistics`] for the rows of an arrow dictionary at `indices`, 
comparing each
+/// dictionary value they reference once rather than once per row
+fn update_dictionary_statistics<'a, K, V>(
+    values: TypedDictionaryArray<'a, K, V>,
+    indices: &[usize],
+    encoder: &mut ByteArrayEncoder,
+) where
+    K: ArrowDictionaryKeyType,
+    V: Sync + Send,
+    &'a V: ArrayAccessor,
+    <&'a V as ArrayAccessor>::Item: Copy + Ord + Default + AsRef<[u8]>,
+{
+    let dictionary_values = values.values();
+    // Rows only repeat values for certain when the dictionary is no larger 
than the batch,
+    // which also bounds the pass over the dictionary by the number of rows. 
Decimals
+    // compare numerically, so values with different bytes can tie, and which 
one becomes
+    // the statistic depends on the order of the rows
+    if encoder.statistics_enabled == EnabledStatistics::None
+        || encoder.geo_stats_accumulator.is_some()
+        || encoder.is_decimal
+        || dictionary_values.len() > indices.len()
+    {
+        return update_statistics(values, indices, encoder);
+    }
+
+    let keys = values.keys().values();
+    let mut referenced = vec![false; dictionary_values.len()];
+    for &idx in indices {
+        match referenced.get_mut(keys[idx].as_usize()) {
+            Some(referenced) => *referenced = true,
+            // A key outside the dictionary reads as an empty value
+            None => return update_statistics(values, indices, encoder),
         }
     }
+
+    let mut referenced_values = referenced
+        .iter()
+        .enumerate()
+        .filter(|(_, referenced)| **referenced)
+        .map(|(key, _)| dictionary_values.value(key));
+    if let Some(first) = referenced_values.next() {
+        let (min, max) = referenced_values.fold((first, first), |(min, max), 
value| {
+            (
+                if min.as_ref() > value.as_ref() {

Review Comment:
   the old code uses is_greater -- is there a reason to do something different 
here?



##########
parquet/src/arrow/arrow_writer/byte_array.rs:
##########
@@ -1060,29 +1060,91 @@ where
         if let Some(accumulator) = encoder.geo_stats_accumulator.as_mut() {
             update_geo_stats_accumulator(accumulator.as_mut(), values, 
indices);
         } else if let Some((min, max)) = compute_min_max(values, indices, 
encoder.is_decimal) {
-            // Compare before copying: `write_gather` runs once per
-            // mini-batch, and a byte-budgeted mini-batch of large values can
-            // hold a single value, so an unconditional copy here would
-            // duplicate every value once for `min` and once for `max`.
-            let min = min.as_ref();
-            if encoder
-                .min_value
-                .as_ref()
-                .is_none_or(|m| is_greater(encoder.is_decimal, m.data(), min))
-            {
-                encoder.min_value = Some(min.to_vec().into());
-            }
+            update_min_max(encoder, min.as_ref(), max.as_ref());
+        }
+    }
+}
 
-            let max = max.as_ref();
-            if encoder
-                .max_value
-                .as_ref()
-                .is_none_or(|m| is_greater(encoder.is_decimal, max, m.data()))
-            {
-                encoder.max_value = Some(max.to_vec().into());
-            }
+/// [`update_statistics`] for the rows of an arrow dictionary at `indices`, 
comparing each
+/// dictionary value they reference once rather than once per row
+fn update_dictionary_statistics<'a, K, V>(
+    values: TypedDictionaryArray<'a, K, V>,
+    indices: &[usize],
+    encoder: &mut ByteArrayEncoder,
+) where
+    K: ArrowDictionaryKeyType,
+    V: Sync + Send,
+    &'a V: ArrayAccessor,
+    <&'a V as ArrayAccessor>::Item: Copy + Ord + Default + AsRef<[u8]>,
+{
+    let dictionary_values = values.values();
+    // Rows only repeat values for certain when the dictionary is no larger 
than the batch,
+    // which also bounds the pass over the dictionary by the number of rows. 
Decimals
+    // compare numerically, so values with different bytes can tie, and which 
one becomes
+    // the statistic depends on the order of the rows
+    if encoder.statistics_enabled == EnabledStatistics::None
+        || encoder.geo_stats_accumulator.is_some()

Review Comment:
   why not also with geo stats?



##########
parquet/src/arrow/arrow_writer/byte_array.rs:
##########
@@ -1060,29 +1060,91 @@ where
         if let Some(accumulator) = encoder.geo_stats_accumulator.as_mut() {
             update_geo_stats_accumulator(accumulator.as_mut(), values, 
indices);
         } else if let Some((min, max)) = compute_min_max(values, indices, 
encoder.is_decimal) {
-            // Compare before copying: `write_gather` runs once per
-            // mini-batch, and a byte-budgeted mini-batch of large values can
-            // hold a single value, so an unconditional copy here would
-            // duplicate every value once for `min` and once for `max`.
-            let min = min.as_ref();
-            if encoder
-                .min_value
-                .as_ref()
-                .is_none_or(|m| is_greater(encoder.is_decimal, m.data(), min))
-            {
-                encoder.min_value = Some(min.to_vec().into());
-            }
+            update_min_max(encoder, min.as_ref(), max.as_ref());
+        }
+    }
+}
 
-            let max = max.as_ref();
-            if encoder
-                .max_value
-                .as_ref()
-                .is_none_or(|m| is_greater(encoder.is_decimal, max, m.data()))
-            {
-                encoder.max_value = Some(max.to_vec().into());
-            }
+/// [`update_statistics`] for the rows of an arrow dictionary at `indices`, 
comparing each
+/// dictionary value they reference once rather than once per row
+fn update_dictionary_statistics<'a, K, V>(
+    values: TypedDictionaryArray<'a, K, V>,
+    indices: &[usize],
+    encoder: &mut ByteArrayEncoder,
+) where
+    K: ArrowDictionaryKeyType,
+    V: Sync + Send,
+    &'a V: ArrayAccessor,
+    <&'a V as ArrayAccessor>::Item: Copy + Ord + Default + AsRef<[u8]>,
+{
+    let dictionary_values = values.values();
+    // Rows only repeat values for certain when the dictionary is no larger 
than the batch,
+    // which also bounds the pass over the dictionary by the number of rows. 
Decimals
+    // compare numerically, so values with different bytes can tie, and which 
one becomes
+    // the statistic depends on the order of the rows
+    if encoder.statistics_enabled == EnabledStatistics::None
+        || encoder.geo_stats_accumulator.is_some()
+        || encoder.is_decimal
+        || dictionary_values.len() > indices.len()
+    {
+        return update_statistics(values, indices, encoder);
+    }
+
+    let keys = values.keys().values();
+    let mut referenced = vec![false; dictionary_values.len()];
+    for &idx in indices {
+        match referenced.get_mut(keys[idx].as_usize()) {
+            Some(referenced) => *referenced = true,
+            // A key outside the dictionary reads as an empty value

Review Comment:
   this is an error -- in a valid arrow array there should never be an key that 
is outside the dictionary



-- 
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]

Reply via email to