hudi-core 0.5.0

The native Rust implementation for Apache Hudi
/*
 * 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.
 */
//! Hudi internal configurations.

use std::collections::HashMap;
use std::fmt::Display;
use std::str::FromStr;

use strum_macros::EnumIter;

use crate::config::Result;
use crate::config::error::ConfigError::{NotFound, ParseBool};
use crate::config::{ConfigParser, HudiConfigValue};

/// Configurations for internal use.
///
/// **Example**
///
/// ```rust
/// use hudi_core::config::internal::HudiInternalConfig::SkipConfigValidation;
/// use hudi_core::table::Table as HudiTable;
///
/// # #[tokio::main]
/// # async fn main() {
/// let options = [(SkipConfigValidation, "true")];
/// HudiTable::new_with_options("/tmp/hudi_data", options).await;
/// # }
/// ```
///
#[derive(Clone, Debug, PartialEq, Eq, Hash, EnumIter)]
pub enum HudiInternalConfig {
    SkipConfigValidation,
    /// Enable reading archived timeline (v1) and LSM history (v2).
    ///
    /// When enabled, timeline queries with time range filters will include archived instants
    /// in addition to active instants. When disabled (default), only active timeline is read.
    ///
    /// Note: Archived instants are only loaded when BOTH conditions are met:
    /// 1. This config is set to `true`
    /// 2. The query specifies a time range filter (start or end timestamp)
    ///
    /// Queries without time filters (e.g., `get_completed_commits()`) will never load
    /// archived instants, regardless of this setting.
    TimelineArchivedReadEnabled,
    /// The instant times an incremental read admits, comma-separated.
    ///
    /// Set by [`Table::read`](crate::table::Table::read) on the incremental path
    /// and consumed by the file-group reader's commit-time mask. It exists because
    /// the window and the row filter key off *different* timestamps: a layout-v2
    /// window bounds **completion** times, while `_hoodie_commit_time` on a row
    /// holds the **requested** time. Comparing the row's requested time against
    /// completion-time bounds is simply a different question, so the resolved
    /// instant times are passed down and rows are matched by membership instead.
    ///
    /// Mirrors Hudi 1.x, where `IncrementalQueryAnalyzer` resolves the window to a
    /// list of instant times and the reader filters on that list.
    ///
    /// Absent means "fall back to the start/end range comparison", which is what
    /// layout v1 needs — it records no completion times, so its window already
    /// bounds requested times.
    IncrementalInstantTimes,
}

impl AsRef<str> for HudiInternalConfig {
    fn as_ref(&self) -> &str {
        match self {
            Self::SkipConfigValidation => "hoodie.internal.skip.config.validation",
            Self::TimelineArchivedReadEnabled => "hoodie.internal.timeline.archived.enabled",
            Self::IncrementalInstantTimes => "hoodie.internal.read.incremental.instant.times",
        }
    }
}

impl Display for HudiInternalConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_ref())
    }
}

impl ConfigParser for HudiInternalConfig {
    type Output = HudiConfigValue;

    fn default_value(&self) -> Option<HudiConfigValue> {
        match self {
            Self::SkipConfigValidation => Some(HudiConfigValue::Boolean(false)),
            Self::TimelineArchivedReadEnabled => Some(HudiConfigValue::Boolean(false)),
            // No default: absent means "range on start/end instead", which is a
            // different code path rather than an empty list (an empty list would
            // correctly admit nothing).
            Self::IncrementalInstantTimes => None,
        }
    }

    fn parse_value(&self, configs: &HashMap<String, String>) -> Result<Self::Output> {
        let get_result = configs
            .get(self.as_ref())
            .map(|v| v.as_str())
            .ok_or(NotFound(self.key()));

        match self {
            Self::SkipConfigValidation => get_result
                .and_then(|v| {
                    bool::from_str(v).map_err(|e| ParseBool(self.key(), v.to_string(), e))
                })
                .map(HudiConfigValue::Boolean),
            Self::TimelineArchivedReadEnabled => get_result
                .and_then(|v| {
                    bool::from_str(v).map_err(|e| ParseBool(self.key(), v.to_string(), e))
                })
                .map(HudiConfigValue::Boolean),
            Self::IncrementalInstantTimes => {
                get_result.map(|v| HudiConfigValue::String(v.to_string()))
            }
        }
    }
}