|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Primary-key reads of a split whose sections each hold one sorted run. |
| 19 | +
|
| 20 | +mod common; |
| 21 | + |
| 22 | +use std::sync::Arc; |
| 23 | + |
| 24 | +use arrow_array::{ArrayRef, Int32Array, RecordBatch}; |
| 25 | +use common::incremental_helpers::{memory_table, persist_table_schema, setup_dirs, write_batch}; |
| 26 | +use futures::TryStreamExt; |
| 27 | +use paimon::spec::{DataType, Datum, IntType, PredicateBuilder, Schema, TableSchema}; |
| 28 | +use paimon::table::Table; |
| 29 | + |
| 30 | +async fn pk_table() -> Table { |
| 31 | + let schema = Schema::builder() |
| 32 | + .column("id", DataType::Int(IntType::new())) |
| 33 | + .column("v", DataType::Int(IntType::new())) |
| 34 | + .primary_key(["id"]) |
| 35 | + .option("bucket", "1") |
| 36 | + .build() |
| 37 | + .unwrap(); |
| 38 | + let path = "memory:/pk_single_run_read"; |
| 39 | + let (io, table) = memory_table(path, TableSchema::new(0, &schema)); |
| 40 | + setup_dirs(&io, path).await; |
| 41 | + persist_table_schema(&io, path, table.schema()).await; |
| 42 | + table |
| 43 | +} |
| 44 | + |
| 45 | +async fn write(table: &Table, ids: std::ops::Range<i32>, offset: i32) { |
| 46 | + let ids: Vec<i32> = ids.collect(); |
| 47 | + let values: Vec<i32> = ids.iter().map(|id| id + offset).collect(); |
| 48 | + write_batch( |
| 49 | + table, |
| 50 | + &RecordBatch::try_from_iter([ |
| 51 | + ("id", Arc::new(Int32Array::from(ids)) as ArrayRef), |
| 52 | + ("v", Arc::new(Int32Array::from(values)) as ArrayRef), |
| 53 | + ]) |
| 54 | + .unwrap(), |
| 55 | + ) |
| 56 | + .await; |
| 57 | +} |
| 58 | + |
| 59 | +async fn read( |
| 60 | + table: &Table, |
| 61 | + projection: &[&str], |
| 62 | + filter: Option<paimon::spec::Predicate>, |
| 63 | +) -> Vec<Vec<i32>> { |
| 64 | + let mut builder = table.new_read_builder(); |
| 65 | + builder.with_projection(projection).unwrap(); |
| 66 | + if let Some(filter) = filter { |
| 67 | + builder.with_filter(filter); |
| 68 | + } |
| 69 | + let plan = builder.new_scan().plan().await.unwrap(); |
| 70 | + let batches: Vec<RecordBatch> = builder |
| 71 | + .new_read() |
| 72 | + .unwrap() |
| 73 | + .to_arrow(plan.splits()) |
| 74 | + .unwrap() |
| 75 | + .try_collect() |
| 76 | + .await |
| 77 | + .unwrap(); |
| 78 | + let mut rows: Vec<Vec<i32>> = batches |
| 79 | + .iter() |
| 80 | + .flat_map(|batch| { |
| 81 | + (0..batch.num_rows()).map(move |row| { |
| 82 | + (0..batch.num_columns()) |
| 83 | + .map(|col| { |
| 84 | + batch |
| 85 | + .column(col) |
| 86 | + .as_any() |
| 87 | + .downcast_ref::<Int32Array>() |
| 88 | + .unwrap() |
| 89 | + .value(row) |
| 90 | + }) |
| 91 | + .collect() |
| 92 | + }) |
| 93 | + }) |
| 94 | + .collect(); |
| 95 | + rows.sort_unstable(); |
| 96 | + rows |
| 97 | +} |
| 98 | + |
| 99 | +#[tokio::test] |
| 100 | +async fn disjoint_files_read_without_merge_and_overlaps_still_merge() { |
| 101 | + let table = pk_table().await; |
| 102 | + // Three files with disjoint keys land in one split, one run per section. |
| 103 | + write(&table, 0..100, 1000).await; |
| 104 | + write(&table, 100..200, 1000).await; |
| 105 | + write(&table, 200..300, 1000).await; |
| 106 | + let plan = table.new_read_builder().new_scan().plan().await.unwrap(); |
| 107 | + assert_eq!(plan.splits().len(), 1); |
| 108 | + |
| 109 | + let expected: Vec<Vec<i32>> = (0..300).map(|id| vec![id, id + 1000]).collect(); |
| 110 | + assert_eq!(read(&table, &["id", "v"], None).await, expected); |
| 111 | + // Value-only projection and a residual filter on the unmerged path. |
| 112 | + let pb = PredicateBuilder::new(table.schema().fields()); |
| 113 | + assert_eq!( |
| 114 | + read( |
| 115 | + &table, |
| 116 | + &["v"], |
| 117 | + Some(pb.greater_or_equal("v", Datum::Int(1250)).unwrap()) |
| 118 | + ) |
| 119 | + .await, |
| 120 | + (1250..1300).map(|v| vec![v]).collect::<Vec<_>>() |
| 121 | + ); |
| 122 | + |
| 123 | + // Overlapping keys make a two-run section, which still merges. |
| 124 | + write(&table, 150..160, 2000).await; |
| 125 | + let expected: Vec<Vec<i32>> = (0..300) |
| 126 | + .map(|id| vec![id, id + if (150..160).contains(&id) { 2000 } else { 1000 }]) |
| 127 | + .collect(); |
| 128 | + assert_eq!(read(&table, &["id", "v"], None).await, expected); |
| 129 | +} |
0 commit comments