|
| 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 | +//! Mirrors Java [PartitionsTable](https://github.com/apache/paimon/blob/release-1.4/paimon-core/src/main/java/org/apache/paimon/table/system/PartitionsTable.java). |
| 19 | +
|
| 20 | +use std::any::Any; |
| 21 | +use std::collections::{BTreeMap, HashMap}; |
| 22 | +use std::sync::{Arc, OnceLock}; |
| 23 | + |
| 24 | +use async_trait::async_trait; |
| 25 | +use datafusion::arrow::array::{ |
| 26 | + BooleanArray, Int32Array, Int64Array, RecordBatch, StringArray, TimestampMillisecondArray, |
| 27 | +}; |
| 28 | +use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef, TimeUnit}; |
| 29 | +use datafusion::catalog::Session; |
| 30 | +use datafusion::datasource::memory::MemorySourceConfig; |
| 31 | +use datafusion::datasource::{TableProvider, TableType}; |
| 32 | +use datafusion::error::{DataFusionError, Result as DFResult}; |
| 33 | +use datafusion::logical_expr::Expr; |
| 34 | +use datafusion::physical_plan::ExecutionPlan; |
| 35 | +use paimon::catalog::{Catalog, Identifier}; |
| 36 | +use paimon::spec::{CoreOptions, Partition}; |
| 37 | +use paimon::table::Table; |
| 38 | + |
| 39 | +use crate::error::to_datafusion_error; |
| 40 | + |
| 41 | +pub(super) fn build( |
| 42 | + catalog: Arc<dyn Catalog>, |
| 43 | + identifier: Identifier, |
| 44 | + table: Table, |
| 45 | +) -> DFResult<Arc<dyn TableProvider>> { |
| 46 | + let table_schema = table.schema(); |
| 47 | + let partition_keys = table_schema.partition_keys().to_vec(); |
| 48 | + let default_partition_name = CoreOptions::new(table_schema.options()) |
| 49 | + .partition_default_name() |
| 50 | + .to_string(); |
| 51 | + Ok(Arc::new(PartitionsTable { |
| 52 | + catalog, |
| 53 | + identifier, |
| 54 | + partition_keys, |
| 55 | + default_partition_name, |
| 56 | + })) |
| 57 | +} |
| 58 | + |
| 59 | +fn partitions_schema() -> SchemaRef { |
| 60 | + static SCHEMA: OnceLock<SchemaRef> = OnceLock::new(); |
| 61 | + SCHEMA |
| 62 | + .get_or_init(|| { |
| 63 | + Arc::new(Schema::new(vec![ |
| 64 | + Field::new("partition", DataType::Utf8, true), |
| 65 | + Field::new("record_count", DataType::Int64, false), |
| 66 | + Field::new("file_size_in_bytes", DataType::Int64, false), |
| 67 | + Field::new("file_count", DataType::Int64, false), |
| 68 | + Field::new( |
| 69 | + "last_update_time", |
| 70 | + DataType::Timestamp(TimeUnit::Millisecond, None), |
| 71 | + true, |
| 72 | + ), |
| 73 | + Field::new( |
| 74 | + "created_at", |
| 75 | + DataType::Timestamp(TimeUnit::Millisecond, None), |
| 76 | + true, |
| 77 | + ), |
| 78 | + Field::new("created_by", DataType::Utf8, true), |
| 79 | + Field::new("updated_by", DataType::Utf8, true), |
| 80 | + Field::new("options", DataType::Utf8, true), |
| 81 | + Field::new("total_buckets", DataType::Int32, false), |
| 82 | + Field::new("done", DataType::Boolean, false), |
| 83 | + ])) |
| 84 | + }) |
| 85 | + .clone() |
| 86 | +} |
| 87 | + |
| 88 | +struct PartitionsTable { |
| 89 | + catalog: Arc<dyn Catalog>, |
| 90 | + identifier: Identifier, |
| 91 | + partition_keys: Vec<String>, |
| 92 | + default_partition_name: String, |
| 93 | +} |
| 94 | + |
| 95 | +impl std::fmt::Debug for PartitionsTable { |
| 96 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 97 | + f.debug_struct("PartitionsTable") |
| 98 | + .field("identifier", &self.identifier) |
| 99 | + .finish() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#[async_trait] |
| 104 | +impl TableProvider for PartitionsTable { |
| 105 | + fn as_any(&self) -> &dyn Any { |
| 106 | + self |
| 107 | + } |
| 108 | + |
| 109 | + fn schema(&self) -> SchemaRef { |
| 110 | + partitions_schema() |
| 111 | + } |
| 112 | + |
| 113 | + fn table_type(&self) -> TableType { |
| 114 | + TableType::View |
| 115 | + } |
| 116 | + |
| 117 | + async fn scan( |
| 118 | + &self, |
| 119 | + _state: &dyn Session, |
| 120 | + projection: Option<&Vec<usize>>, |
| 121 | + _filters: &[Expr], |
| 122 | + _limit: Option<usize>, |
| 123 | + ) -> DFResult<Arc<dyn ExecutionPlan>> { |
| 124 | + let catalog = self.catalog.clone(); |
| 125 | + let identifier = self.identifier.clone(); |
| 126 | + let partitions = crate::runtime::await_with_runtime(async move { |
| 127 | + catalog.list_partitions(&identifier).await |
| 128 | + }) |
| 129 | + .await |
| 130 | + .map_err(to_datafusion_error)?; |
| 131 | + |
| 132 | + let mut rows: Vec<(String, Partition)> = partitions |
| 133 | + .into_iter() |
| 134 | + .map(|p| { |
| 135 | + let s = format_partition_string( |
| 136 | + &p.spec, |
| 137 | + &self.partition_keys, |
| 138 | + &self.default_partition_name, |
| 139 | + ); |
| 140 | + (s, p) |
| 141 | + }) |
| 142 | + .collect(); |
| 143 | + rows.sort_by(|a, b| a.0.cmp(&b.0)); |
| 144 | + |
| 145 | + let n = rows.len(); |
| 146 | + let mut partition_strings: Vec<Option<String>> = Vec::with_capacity(n); |
| 147 | + let mut record_counts = Vec::with_capacity(n); |
| 148 | + let mut file_sizes = Vec::with_capacity(n); |
| 149 | + let mut file_counts = Vec::with_capacity(n); |
| 150 | + let mut last_update_times: Vec<Option<i64>> = Vec::with_capacity(n); |
| 151 | + let mut created_ats: Vec<Option<i64>> = Vec::with_capacity(n); |
| 152 | + let mut created_bys: Vec<Option<String>> = Vec::with_capacity(n); |
| 153 | + let mut updated_bys: Vec<Option<String>> = Vec::with_capacity(n); |
| 154 | + let mut options_jsons: Vec<Option<String>> = Vec::with_capacity(n); |
| 155 | + let mut total_buckets = Vec::with_capacity(n); |
| 156 | + let mut dones = Vec::with_capacity(n); |
| 157 | + |
| 158 | + for (s, p) in rows { |
| 159 | + partition_strings.push(Some(s)); |
| 160 | + record_counts.push(p.record_count); |
| 161 | + file_sizes.push(p.file_size_in_bytes); |
| 162 | + file_counts.push(p.file_count); |
| 163 | + // 0 marks "no creation_time on any file"; real wall-clock is never |
| 164 | + // <= 0 in practice, so this never nullifies a genuine timestamp. |
| 165 | + last_update_times.push(if p.last_file_creation_time > 0 { |
| 166 | + Some(p.last_file_creation_time) |
| 167 | + } else { |
| 168 | + None |
| 169 | + }); |
| 170 | + created_ats.push(p.created_at); |
| 171 | + created_bys.push(p.created_by); |
| 172 | + updated_bys.push(p.updated_by); |
| 173 | + // Sort via BTreeMap so the serialised JSON is deterministic across |
| 174 | + // runs (Partition.options is a HashMap with unspecified order). |
| 175 | + options_jsons.push( |
| 176 | + p.options |
| 177 | + .as_ref() |
| 178 | + .map(|m| { |
| 179 | + let sorted: BTreeMap<&String, &String> = m.iter().collect(); |
| 180 | + serde_json::to_string(&sorted) |
| 181 | + .map_err(|e| DataFusionError::External(Box::new(e))) |
| 182 | + }) |
| 183 | + .transpose()?, |
| 184 | + ); |
| 185 | + total_buckets.push(p.total_buckets); |
| 186 | + dones.push(p.done); |
| 187 | + } |
| 188 | + |
| 189 | + let schema = partitions_schema(); |
| 190 | + let batch = RecordBatch::try_new( |
| 191 | + schema.clone(), |
| 192 | + vec![ |
| 193 | + Arc::new(StringArray::from(partition_strings)), |
| 194 | + Arc::new(Int64Array::from(record_counts)), |
| 195 | + Arc::new(Int64Array::from(file_sizes)), |
| 196 | + Arc::new(Int64Array::from(file_counts)), |
| 197 | + Arc::new(TimestampMillisecondArray::from(last_update_times)), |
| 198 | + Arc::new(TimestampMillisecondArray::from(created_ats)), |
| 199 | + Arc::new(StringArray::from(created_bys)), |
| 200 | + Arc::new(StringArray::from(updated_bys)), |
| 201 | + Arc::new(StringArray::from(options_jsons)), |
| 202 | + Arc::new(Int32Array::from(total_buckets)), |
| 203 | + Arc::new(BooleanArray::from(dones)), |
| 204 | + ], |
| 205 | + )?; |
| 206 | + |
| 207 | + Ok(MemorySourceConfig::try_new_exec( |
| 208 | + &[vec![batch]], |
| 209 | + schema, |
| 210 | + projection.cloned(), |
| 211 | + )?) |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +/// Format `spec` as `key1=val1/key2=val2` in `partition_keys` order. Empty |
| 216 | +/// string for non-partitioned tables. NULL spec values fall back to |
| 217 | +/// `default_partition_name`. |
| 218 | +fn format_partition_string( |
| 219 | + spec: &HashMap<String, String>, |
| 220 | + partition_keys: &[String], |
| 221 | + default_partition_name: &str, |
| 222 | +) -> String { |
| 223 | + partition_keys |
| 224 | + .iter() |
| 225 | + .map(|k| { |
| 226 | + let v = spec |
| 227 | + .get(k) |
| 228 | + .map(String::as_str) |
| 229 | + .unwrap_or(default_partition_name); |
| 230 | + format!("{k}={v}") |
| 231 | + }) |
| 232 | + .collect::<Vec<_>>() |
| 233 | + .join("/") |
| 234 | +} |
0 commit comments