hudi-core 0.5.0

The native Rust implementation for Apache Hudi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * 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.
 */
//! Read options shared by all read APIs (eager and streaming).

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

use strum::IntoEnumIterator;

use crate::config::HudiConfigs;
use crate::config::error::ConfigError;
use crate::config::read::HudiReadConfig;
pub use crate::config::read::QueryType;
use crate::expr::filter::{Filter, from_str_tuples};

/// Options for all Hudi read APIs (snapshot, time-travel, incremental, etc).
///
/// `filters` and `projection` are typed because they don't have a Hudi config equivalent.
/// Every other knob — query type, timestamps, batch size, ad-hoc Hudi configs — is stored
/// in `hudi_options` under its `HudiReadConfig` key. Builder methods like
/// [`Self::with_query_type`], [`Self::with_as_of_timestamp`], [`Self::with_start_timestamp`],
/// [`Self::with_end_timestamp`], and [`Self::with_batch_size`] are convenience wrappers that
/// insert into `hudi_options` under the right key. Typed accessors
/// ([`Self::query_type`], [`Self::as_of_timestamp`], etc.) read from the bag.
///
/// One source of truth: the bag. Builders + accessors are the ergonomic API on top.
///
/// # Example
///
/// ```ignore
/// use hudi::table::{ReadOptions, QueryType};
///
/// let options = ReadOptions::new()
///     .with_filters([("city", "=", "san_francisco")])?
///     .with_projection(["id", "name", "city"])
///     .with_batch_size(4096)?;
///
/// // Time-travel snapshot
/// let options = ReadOptions::new().with_as_of_timestamp("20240101000000000");
///
/// // Incremental read between two commits
/// let options = ReadOptions::new()
///     .with_query_type(QueryType::Incremental)
///     .with_start_timestamp("20240101000000000")
///     .with_end_timestamp("20240201000000000");
///
/// // Read-optimized snapshot (skip log files)
/// let options = ReadOptions::new()
///     .with_hudi_option("hoodie.read.use.read_optimized.mode", "true");
/// ```
#[derive(Clone, Debug, Default)]
pub struct ReadOptions {
    /// Column filters, parsed and cardinality-validated at construction.
    /// `field` is any column name (partition or data).
    ///
    /// Filters drive both **pruning** and **row-level filtering**:
    /// - When the field is a **partition column**, the filter prunes whole partitions
    ///   for both snapshot and incremental queries.
    /// - When the field is a **data column** with column statistics (min/max) in the
    ///   metadata table, the filter prunes whole files **for snapshot queries only**.
    ///   Incremental file planning currently does partition pruning only — data-column
    ///   filters apply at the row-level mask but do not prune files.
    /// - All filters are applied as a row-level mask after reading, so callers
    ///   always get only rows that match regardless of the planning path.
    pub filters: Vec<Filter>,

    /// Column names to project (select). If None, all columns are read.
    pub projection: Option<Vec<String>>,

    /// Resolved bag of Hudi configs for this read. Populated by typed builders
    /// (e.g. [`Self::with_query_type`], [`Self::with_start_timestamp`]) and by
    /// [`Self::with_hudi_option`] for any keys that don't have a typed builder.
    /// Keys are `HudiReadConfig` strings (`hoodie.*`).
    pub hudi_options: HashMap<String, String>,
}

impl ReadOptions {
    /// Creates a new ReadOptions with default values.
    pub fn new() -> Self {
        Self::default()
    }

    // ---- typed builders (insert into hudi_options under the right key) ----

    /// Sets the query type. Stored as [`HudiReadConfig::QueryType`].
    pub fn with_query_type(mut self, query_type: QueryType) -> Self {
        self.hudi_options.insert(
            HudiReadConfig::QueryType.as_ref().to_string(),
            query_type.as_ref().to_string(),
        );
        self
    }

    /// Sets the as-of timestamp for snapshot/time-travel queries.
    /// Stored as [`HudiReadConfig::AsOfTimestamp`].
    pub fn with_as_of_timestamp<S: AsRef<str>>(mut self, timestamp: S) -> Self {
        self.hudi_options.insert(
            HudiReadConfig::AsOfTimestamp.as_ref().to_string(),
            timestamp.as_ref().to_string(),
        );
        self
    }

    /// Sets the lower-bound timestamp (exclusive) for incremental queries.
    /// Stored as [`HudiReadConfig::StartTimestamp`].
    pub fn with_start_timestamp<S: AsRef<str>>(mut self, timestamp: S) -> Self {
        self.hudi_options.insert(
            HudiReadConfig::StartTimestamp.as_ref().to_string(),
            timestamp.as_ref().to_string(),
        );
        self
    }

    /// Sets the upper-bound timestamp (inclusive) for incremental queries.
    /// Stored as [`HudiReadConfig::EndTimestamp`].
    pub fn with_end_timestamp<S: AsRef<str>>(mut self, timestamp: S) -> Self {
        self.hudi_options.insert(
            HudiReadConfig::EndTimestamp.as_ref().to_string(),
            timestamp.as_ref().to_string(),
        );
        self
    }

    /// Sets the target batch size (rows per batch).
    /// Stored as [`HudiReadConfig::StreamBatchSize`]. Errors if `size == 0`
    /// — a zero-row batch yields no batches at the parquet stream reader and
    /// is almost certainly a caller mistake.
    pub fn with_batch_size(mut self, size: usize) -> crate::Result<Self> {
        if size == 0 {
            let key = HudiReadConfig::StreamBatchSize.as_ref();
            return Err(ConfigError::InvalidValue(format!("{key} must be > 0, got 0")).into());
        }
        self.hudi_options.insert(
            HudiReadConfig::StreamBatchSize.as_ref().to_string(),
            size.to_string(),
        );
        Ok(self)
    }

    /// Sets column filters from `(field, op, value)` tuples. Parses and
    /// validates operator + cardinality at build time; an unrecognized
    /// operator or empty `IN`/`NOT IN` value list errors here rather than
    /// at read time. Schema-level validation (column existence, value
    /// castability) still happens at read time when the schema is known.
    pub fn with_filters<I, S1, S2, S3>(mut self, filters: I) -> crate::Result<Self>
    where
        I: IntoIterator<Item = (S1, S2, S3)>,
        S1: AsRef<str>,
        S2: AsRef<str>,
        S3: AsRef<str>,
    {
        self.filters = from_str_tuples(filters.into_iter().map(|(f, o, v)| {
            (
                f.as_ref().to_string(),
                o.as_ref().to_string(),
                v.as_ref().to_string(),
            )
        }))?;
        Ok(self)
    }

    /// Sets the column projection (which columns to read).
    pub fn with_projection<I, S>(mut self, columns: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.projection = Some(columns.into_iter().map(|s| s.into()).collect());
        self
    }

    /// Sets a single Hudi config that applies to this read only.
    pub fn with_hudi_option<K, V>(mut self, key: K, value: V) -> Self
    where
        K: Into<String>,
        V: Into<String>,
    {
        self.hudi_options.insert(key.into(), value.into());
        self
    }

    /// Sets a batch of Hudi configs that apply to this read only.
    pub fn with_hudi_options<I, K, V>(mut self, opts: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        for (k, v) in opts {
            self.hudi_options.insert(k.into(), v.into());
        }
        self
    }

    // ---- typed accessors (read from hudi_options) ----

    /// The query type (defaults to [`QueryType::Snapshot`] when unset). Errors if
    /// the stored string is not a recognized variant.
    pub fn query_type(&self) -> crate::Result<QueryType> {
        match self.hudi_options.get(HudiReadConfig::QueryType.as_ref()) {
            Some(s) => Ok(QueryType::from_str(s)?),
            None => Ok(QueryType::default()),
        }
    }

    /// The as-of timestamp for snapshot/time-travel queries, if set.
    pub fn as_of_timestamp(&self) -> Option<&str> {
        self.hudi_options
            .get(HudiReadConfig::AsOfTimestamp.as_ref())
            .map(|s| s.as_str())
    }

    /// The start timestamp (exclusive) for incremental queries, if set.
    pub fn start_timestamp(&self) -> Option<&str> {
        self.hudi_options
            .get(HudiReadConfig::StartTimestamp.as_ref())
            .map(|s| s.as_str())
    }

    /// The end timestamp (inclusive) for incremental queries, if set.
    pub fn end_timestamp(&self) -> Option<&str> {
        self.hudi_options
            .get(HudiReadConfig::EndTimestamp.as_ref())
            .map(|s| s.as_str())
    }

    /// Return a copy with timestamps irrelevant to the resolved query type stripped.
    ///
    /// Snapshot keeps only `as_of_timestamp`; incremental keeps only
    /// `start_timestamp` / `end_timestamp`.
    pub(crate) fn with_sanitized_timestamps(&self) -> Self {
        let mut opts = self.clone();
        match opts.query_type().unwrap_or_default() {
            QueryType::Snapshot => {
                opts.hudi_options
                    .remove(HudiReadConfig::StartTimestamp.as_ref());
                opts.hudi_options
                    .remove(HudiReadConfig::EndTimestamp.as_ref());
            }
            QueryType::Incremental => {
                opts.hudi_options
                    .remove(HudiReadConfig::AsOfTimestamp.as_ref());
            }
        }
        opts
    }

    /// Whether read-optimized mode is enabled (base files only, skip log merging).
    pub fn is_read_optimized(&self) -> crate::Result<bool> {
        let key = HudiReadConfig::UseReadOptimizedMode.as_ref();
        match self.hudi_options.get(key) {
            Some(s) => {
                let parsed = s
                    .parse::<bool>()
                    .map_err(|e| ConfigError::ParseBool(key.to_string(), s.clone(), e))?;
                Ok(parsed)
            }
            None => Ok(false),
        }
    }

    /// The target batch size (rows per batch) for streaming reads, if set.
    /// Errors if the stored string is not a valid `usize` or if the value is `0`
    /// (a zero-row batch yields no batches at the parquet stream reader and is
    /// almost certainly a caller mistake).
    /// Fill in defaults from the given configs for keys not already set.
    ///
    /// Values already present in this `ReadOptions` take precedence.
    pub fn with_defaults_from(&self, configs: &HudiConfigs) -> crate::Result<Self> {
        let mut resolved = self.clone();
        for key in HudiReadConfig::iter() {
            let key_str = key.key_str();
            if !resolved.hudi_options.contains_key(key_str)
                && let Some(val) = configs.try_get(key)?
            {
                let s: String = val.into();
                resolved.hudi_options.insert(key_str.to_string(), s);
            }
        }
        Ok(resolved)
    }

    pub fn batch_size(&self) -> crate::Result<Option<usize>> {
        let key = HudiReadConfig::StreamBatchSize.as_ref();
        match self.hudi_options.get(key) {
            Some(s) => {
                let parsed = s
                    .parse::<usize>()
                    .map_err(|e| ConfigError::ParseInt(key.to_string(), s.clone(), e))?;
                if parsed == 0 {
                    return Err(
                        ConfigError::InvalidValue(format!("{key} must be > 0, got 0")).into(),
                    );
                }
                Ok(Some(parsed))
            }
            None => Ok(None),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_with_projection() {
        let options = ReadOptions::new().with_projection(["col1", "col2", "col3"]);

        assert_eq!(
            options.projection,
            Some(vec![
                "col1".to_string(),
                "col2".to_string(),
                "col3".to_string()
            ])
        );
    }

    #[test]
    fn test_with_query_type_round_trip() -> crate::Result<()> {
        let snapshot = ReadOptions::new();
        assert_eq!(snapshot.query_type()?, QueryType::Snapshot);

        let incr = ReadOptions::new().with_query_type(QueryType::Incremental);
        assert_eq!(incr.query_type()?, QueryType::Incremental);
        assert_eq!(
            incr.hudi_options
                .get(HudiReadConfig::QueryType.as_ref())
                .map(String::as_str),
            Some("incremental")
        );
        Ok(())
    }

    #[test]
    fn test_with_timestamps_round_trip() -> crate::Result<()> {
        let opts = ReadOptions::new()
            .with_as_of_timestamp("20240101120000000")
            .with_start_timestamp("20240101000000000")
            .with_end_timestamp("20240201000000000");
        assert_eq!(opts.as_of_timestamp(), Some("20240101120000000"));
        assert_eq!(opts.start_timestamp(), Some("20240101000000000"));
        assert_eq!(opts.end_timestamp(), Some("20240201000000000"));
        Ok(())
    }

    #[test]
    fn test_with_batch_size_round_trip() -> crate::Result<()> {
        let opts = ReadOptions::new();
        assert_eq!(opts.batch_size()?, None);

        let opts = ReadOptions::new().with_batch_size(2048)?;
        assert_eq!(opts.batch_size()?, Some(2048));
        assert_eq!(
            opts.hudi_options
                .get(HudiReadConfig::StreamBatchSize.as_ref())
                .map(String::as_str),
            Some("2048")
        );

        // Builder rejects 0 immediately rather than deferring to read time.
        let err = ReadOptions::new().with_batch_size(0).unwrap_err();
        assert!(err.to_string().contains("must be > 0"));

        // Out-of-band insertion via the bag still surfaces at the accessor.
        let opts = ReadOptions::new()
            .with_hudi_option(HudiReadConfig::StreamBatchSize.as_ref(), "not_a_number");
        let err = opts.batch_size().unwrap_err();
        assert!(err.to_string().contains("not_a_number"));
        Ok(())
    }

    #[test]
    fn test_with_filters_validates_at_build_time() {
        // Bad operator surfaces at build, not at read.
        let err = ReadOptions::new()
            .with_filters([("col", "BAD_OP", "x")])
            .unwrap_err();
        assert!(err.to_string().contains("BAD_OP"));

        // Empty IN value list (after split + trim) errors via cardinality check.
        let err = ReadOptions::new()
            .with_filters([("col", "IN", "")])
            .unwrap_err();
        assert!(err.to_string().contains("at least one value"));
    }

    #[test]
    fn test_with_hudi_options() {
        let options = ReadOptions::new()
            .with_hudi_option("hoodie.read.use.read_optimized.mode", "true")
            .with_hudi_options([("a", "1"), ("b", "2")]);
        assert_eq!(
            options
                .hudi_options
                .get("hoodie.read.use.read_optimized.mode"),
            Some(&"true".to_string())
        );
        assert_eq!(options.hudi_options.get("a"), Some(&"1".to_string()));
        assert_eq!(options.hudi_options.get("b"), Some(&"2".to_string()));
    }

    #[test]
    fn test_is_read_optimized_round_trip() -> crate::Result<()> {
        assert!(!ReadOptions::new().is_read_optimized()?);

        let opts = ReadOptions::new()
            .with_hudi_option(HudiReadConfig::UseReadOptimizedMode.as_ref(), "true");
        assert!(opts.is_read_optimized()?);

        let opts = ReadOptions::new()
            .with_hudi_option(HudiReadConfig::UseReadOptimizedMode.as_ref(), "false");
        assert!(!opts.is_read_optimized()?);

        for invalid in ["1", "yes", "on", "TRUE_ISH"] {
            let opts = ReadOptions::new()
                .with_hudi_option(HudiReadConfig::UseReadOptimizedMode.as_ref(), invalid);
            let err = opts.is_read_optimized().unwrap_err();
            assert!(
                err.to_string().contains(invalid),
                "expected error to mention '{invalid}'"
            );
        }
        Ok(())
    }

    #[test]
    fn test_query_type_from_str_invalid_errors() {
        let opts =
            ReadOptions::new().with_hudi_option(HudiReadConfig::QueryType.as_ref(), "garbage");
        let err = opts.query_type().unwrap_err();
        assert!(err.to_string().contains("garbage"));
    }

    #[test]
    fn test_debug_format() -> crate::Result<()> {
        let options = ReadOptions::new()
            .with_filters([("city", "=", "sf")])?
            .with_projection(["id"])
            .with_batch_size(1000)?;

        let debug_str = format!("{options:?}");
        assert!(debug_str.contains("ReadOptions"));
        assert!(debug_str.contains("filters"));
        assert!(debug_str.contains("projection"));
        assert!(debug_str.contains("hudi_options"));
        Ok(())
    }
}