leekeiabstraction commented on code in PR #366: URL: https://github.com/apache/fluss-rust/pull/366#discussion_r2942178338
########## crates/fluss/src/row/binary/iceberg_binary_row_writer.rs: ########## @@ -0,0 +1,552 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use bytes::{Bytes, BytesMut}; + +use crate::error::{Error, Result}; +use crate::metadata::DataType; +use crate::row::Decimal; +use crate::row::binary::{BinaryWriter, ValueWriter}; + +const MICROS_PER_MILLI: i64 = 1_000; + +/// Iceberg-specific binary writer for encoding key columns. +/// +/// Unlike [`CompactedRowWriter`] which uses varint encoding and length-prefixed +/// variable-length fields, this writer follows Iceberg's encoding conventions: +/// - Integers (int, date) are written as i64 (8 bytes, little-endian) +/// - Time values are converted from milliseconds to microseconds +/// - Timestamps are converted to microseconds +/// - Floats/doubles use fixed-width little-endian encoding +/// - Variable-length types (string, binary) are written without length prefixes +/// - Decimals are written as unscaled big-endian bytes without length prefixes +/// +/// The encoded bytes feed directly into [`IcebergBucketingFunction`]'s MurmurHash +/// for bucket assignment and must match the Java Fluss server's encoding exactly. +/// +/// [`CompactedRowWriter`]: crate::row::compacted::CompactedRowWriter +/// [`IcebergBucketingFunction`]: crate::bucketing::IcebergBucketingFunction +pub struct IcebergBinaryRowWriter { + position: usize, + buffer: BytesMut, +} + +impl Default for IcebergBinaryRowWriter { + fn default() -> Self { + Self::new() + } +} + +impl IcebergBinaryRowWriter { + pub fn new() -> Self { + let buffer = BytesMut::zeroed(64); + Self { + position: 0, + buffer, + } + } + + // Dependency order note: + // 1) Keep this PR scoped to writer-level Java parity. + // 2) Wire the writer through IcebergKeyEncoder in follow-up #308. + // TODO(#308): add end-to-end key-encoding tests via IcebergKeyEncoder + // (similar to CompactedKeyEncoder tests for CompactedKeyWriter). + pub fn create_value_writer(field_type: &DataType) -> Result<ValueWriter> { + match field_type { + // Match Java IcebergBinaryRowWriter.createFieldWriter() supported types exactly. + DataType::Int(_) + | DataType::Date(_) + | DataType::Time(_) + | DataType::BigInt(_) + | DataType::Float(_) + | DataType::Double(_) + | DataType::Timestamp(_) + | DataType::Decimal(_) + | DataType::String(_) + | DataType::Char(_) + | DataType::Binary(_) + | DataType::Bytes(_) => ValueWriter::create_value_writer(field_type, None), + + // Keep Java's explicit scalar-only rejection messaging for ARRAY/MAP. + DataType::Array(_) => Err(Error::UnsupportedOperation { + message: + "Array types cannot be used as bucket keys. Bucket keys must be scalar types." + .to_string(), + }), + DataType::Map(_) => Err(Error::UnsupportedOperation { + message: + "Map types cannot be used as bucket keys. Bucket keys must be scalar types." + .to_string(), + }), + + // BOOLEAN, TINYINT, SMALLINT, TIMESTAMP_LTZ, ROW and any future types. + _ => Err(Error::UnsupportedOperation { + message: format!( + "Unsupported type for Iceberg binary row writer: {:?}", + field_type + ), + }), + } + } + + #[allow(dead_code)] + pub fn position(&self) -> usize { + self.position + } + + #[allow(dead_code)] + pub fn buffer(&self) -> &[u8] { + &self.buffer[..self.position] + } + + pub fn to_bytes(&self) -> Bytes { + Bytes::copy_from_slice(&self.buffer[..self.position]) + } + + fn ensure_capacity(&mut self, need_len: usize) { + if (self.buffer.len() - self.position) < need_len { + let new_len = std::cmp::max(self.buffer.len() * 2, self.buffer.len() + need_len); + self.buffer.resize(new_len, 0); + } + } + + fn write_raw(&mut self, src: &[u8]) { + let end = self.position + src.len(); + self.ensure_capacity(src.len()); + self.buffer[self.position..end].copy_from_slice(src); + self.position = end; + } +} + +impl BinaryWriter for IcebergBinaryRowWriter { + fn reset(&mut self) { + self.position = 0; Review Comment: Do we need to fill buffer with zeroes? https://github.com/apache/fluss/blob/0f4d86532b48d229706f894dff652c40f825b003/fluss-common/src/main/java/org/apache/fluss/row/encode/iceberg/IcebergBinaryRowWriter.java#L64 -- 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]
