tustvold commented on code in PR #4389:
URL: https://github.com/apache/arrow-rs/pull/4389#discussion_r1225792534


##########
parquet/src/column/writer/mod.rs:
##########
@@ -668,19 +669,79 @@ impl<'a, E: ColumnValueEncoder> GenericColumnWriter<'a, 
E> {
                     self.column_index_builder.to_invalid();
                 }
                 Some(stat) => {
-                    self.column_index_builder.append(
-                        null_page,
-                        stat.min_bytes(),
-                        stat.max_bytes(),
-                        self.page_metrics.num_page_nulls as i64,
-                    );
+                    // We only truncate if the data is represented as binary
+                    match self.descr.physical_type() {
+                        Type::BYTE_ARRAY | Type::FIXED_LEN_BYTE_ARRAY => {
+                            self.column_index_builder.append(
+                                null_page,
+                                self.truncate_min_value(stat.min_bytes()),
+                                self.truncate_max_value(stat.max_bytes()),
+                                self.page_metrics.num_page_nulls as i64,
+                            );
+                        }
+                        _ => {
+                            self.column_index_builder.append(
+                                null_page,
+                                stat.min_bytes().to_vec(),
+                                stat.max_bytes().to_vec(),
+                                self.page_metrics.num_page_nulls as i64,
+                            );
+                        }
+                    }
                 }
             }
+
+            // update the offset index
+            self.offset_index_builder
+                .append_row_count(self.page_metrics.num_buffered_rows as i64);
         }
+    }
+
+    fn truncate_min_value(&self, data: &[u8]) -> Vec<u8> {
+        let effective_column_index_truncate_length = self
+            .props
+            .column_index_truncate_length()
+            .unwrap_or(data.len());
+
+        // If there's no need to truncate, we just return the value without 
any modification.
+        if data.len() <= effective_column_index_truncate_length {
+            return data.to_vec();
+        }
+
+        let result = match str::from_utf8(data) {
+            Ok(str_data) => {
+                truncate_utf8(str_data, effective_column_index_truncate_length)
+            }
+            Err(_) => truncate_binary(data, 
effective_column_index_truncate_length),
+        };
 
-        // update the offset index
-        self.offset_index_builder
-            .append_row_count(self.page_metrics.num_buffered_rows as i64);
+        // If we couldn't truncate the value, we just return the original value
+        result.unwrap_or(data.to_vec())
+    }
+
+    fn truncate_max_value(&self, data: &[u8]) -> Vec<u8> {
+        let effective_column_index_truncate_length = self
+            .props
+            .column_index_truncate_length()
+            .unwrap_or(data.len());
+
+        // If there's no need to truncate, we just return the value without 
any modification.
+        if data.len() <= effective_column_index_truncate_length {
+            return data.to_vec();
+        }
+
+        let result = match str::from_utf8(data) {
+            Ok(utf8_data) => {
+                truncate_utf8(utf8_data, 
effective_column_index_truncate_length)
+                    .and_then(increment_utf8)
+            }
+
+            Err(_) => truncate_binary(data, 
effective_column_index_truncate_length)
+                .and_then(increment),
+        };
+
+        // If we couldn't truncate or increment the value, we just return the 
original value
+        result.unwrap_or(data.to_vec())

Review Comment:
   ```suggestion
           self.props
               .column_index_truncate_length()
               .filter(|l| data.len() > *l)
               .and_then(|l| match str::from_utf8(data) {
                   Ok(str_data) => truncate_utf8(str_data, 
l).and_then(increment_utf8),
                   Err(_) => truncate_binary(data, l).and_then(increment),
               })
               .unwrap_or_else(|| data.to_vec())
   ```



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