voonhous commented on code in PR #871:
URL: https://github.com/apache/hudi-rs/pull/871#discussion_r4414563950
##########
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:
Read path now depends on write module? I would approach this differently in
Java where i would put record key helpers in a separate common module where
both read/write context can use.
I looked around, i think it's cleaner if we move the resolver, this way, we
can merge three separate key-generator parsers:
1. is_timestamp_based_keygen
2. key_generator_kind
3. declares_complex_key_generator.
##########
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:
Can you check if tables keyed by DATE/DECIMAL if there might be duplicate
data?
For example, epoch day might get rendered to a date-like object or string,
then when comparing, it finds no duplicate since epoch != date-like
object/string.
If we do not want to handle these cases, we should disable Date32 and
Decimal keys to prevent such things.
Not sure if the same issue might happen to timestamps, i.e. epoch ts vs
datetime.
--
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]