yihua commented on code in PR #871: URL: https://github.com/apache/hudi-rs/pull/871#discussion_r4421003482
########## crates/core/src/keygen/record_key.rs: ########## @@ -0,0 +1,317 @@ +/* + * 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. + */ +//! Record key encoding shared by the writer and the virtual-key reader, so a +//! key derived at read time equals the one a Java writer stored. + +use arrow::array::{ + Array, BooleanArray, Date32Array, Decimal128Array, Decimal256Array, Int8Array, Int16Array, + Int32Array, Int64Array, LargeStringArray, StringArray, StringViewArray, UInt8Array, + UInt16Array, UInt32Array, UInt64Array, +}; +use arrow::datatypes::DataType; +use chrono::NaiveDate; + +use crate::Result; +use crate::error::CoreError; + +/// Java `KeyGenerator.NULL_RECORDKEY_PLACEHOLDER`. +pub(crate) const NULL_RECORDKEY_PLACEHOLDER: &str = "__null__"; +/// Java `KeyGenerator.EMPTY_RECORDKEY_PLACEHOLDER`. +pub(crate) const EMPTY_RECORDKEY_PLACEHOLDER: &str = "__empty__"; +/// Java `KeyGenerator.DEFAULT_COLUMN_VALUE_SEPARATOR`. +const COLUMN_VALUE_SEPARATOR: char = ':'; +/// Java `KeyGenerator.DEFAULT_RECORD_KEY_PARTS_SEPARATOR`. +const RECORD_KEY_PARTS_SEPARATOR: char = ','; + +/// Render a record key field column as the Spark and Avro key generators do: +/// `toString` of the value after `HoodieAvroUtils.convertValueForAvroLogicalTypes`. +/// Nulls stay null. A date renders as its ISO `LocalDate` and a decimal as +/// `BigDecimal.toString`. Flink's `RowDataKeyGen` renders dates as the epoch +/// day and decimals with `toPlainString`, so a Flink-written table keyed on +/// those types does not round-trip. +/// +/// Every other type is [`CoreError::Unsupported`]: Java writers store +/// timestamp keys in several forms (raw epoch count, JVM-zone +/// `java.sql.Timestamp`, epoch millis) that the table does not record, and +/// float keys depend on the JDK's `Double.toString` digits. +pub(crate) fn render_key_field(column: &dyn Array, field: &str) -> Result<StringArray> { + macro_rules! render { + ($array:ty, $render:expr) => { + downcast::<$array>(column, field)? + .iter() + .map(|value| value.map($render)) + .collect::<Vec<Option<String>>>() + }; Review Comment: Done, date and decimal key fields are now refused like timestamps: Spark renders them as `LocalDate` and `BigDecimal.toString` while Flink uses the epoch day and `toPlainString`, and the table does not record which engine wrote it. ########## crates/core/src/file_group/reader_v2/record_context.rs: ########## @@ -449,25 +428,10 @@ impl RecordContext { if populate_meta_fields || num_record_key_fields != 1 { return false; } - let is_complex_keygen = table_config - .get(HudiTableConfig::KeyGeneratorClass.as_ref()) - .map(|c| c.ends_with("ComplexKeyGenerator") || c.ends_with("ComplexAvroKeyGenerator")) - .unwrap_or(false); - if !is_complex_keygen { - return false; - } - // `tableVersion >= NINE || !COMPLEX_KEYGEN_NEW_ENCODING`. An unparseable / - // absent version falls back to the new-encoding config alone (which defaults - // to `false` → encode), matching the common default. - let table_version = table_config - .get(HudiTableConfig::TableVersion.as_ref()) - .and_then(|v| v.trim().parse::<i32>().ok()); - let new_encoding = table_config - .get(Self::COMPLEX_KEYGEN_NEW_ENCODING_KEY) - .map(|v| v.eq_ignore_ascii_case("true")) - .unwrap_or(false); - table_version.is_some_and(|v| v >= Self::COMPLEX_KEYGEN_ENCODE_MIN_TABLE_VERSION) - || !new_encoding + let configs = HudiConfigs::new(table_config.iter().map(|(k, v)| (k.as_str(), v.clone()))); + // The generators the writer does not reproduce (timestamp, custom) store a + // single-field key as the bare value. + crate::write::encodes_single_key_field_name(&configs).unwrap_or(false) Review Comment: Done, the resolver now lives in `keygen` with one class/type table that `is_timestamp_based_keygen`, `declares_complex_key_generator` and `key_generator_kind` all share, so the reader and `SimpleIndex` no longer import from `write`. -- 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]
