|
| 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 [ManifestsTable](https://github.com/apache/paimon/blob/release-1.4/paimon-core/src/main/java/org/apache/paimon/table/system/ManifestsTable.java). |
| 19 | +
|
| 20 | +use std::any::Any; |
| 21 | +use std::sync::{Arc, OnceLock}; |
| 22 | + |
| 23 | +use async_trait::async_trait; |
| 24 | +use datafusion::arrow::array::{new_null_array, Int64Array, RecordBatch, StringArray}; |
| 25 | +use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef}; |
| 26 | +use datafusion::catalog::Session; |
| 27 | +use datafusion::datasource::memory::MemorySourceConfig; |
| 28 | +use datafusion::datasource::{TableProvider, TableType}; |
| 29 | +use datafusion::error::Result as DFResult; |
| 30 | +use datafusion::logical_expr::Expr; |
| 31 | +use datafusion::physical_plan::ExecutionPlan; |
| 32 | +use paimon::spec::{ManifestFileMeta, ManifestList}; |
| 33 | +use paimon::table::{SnapshotManager, Table}; |
| 34 | + |
| 35 | +use crate::error::to_datafusion_error; |
| 36 | + |
| 37 | +pub(super) fn build(table: Table) -> DFResult<Arc<dyn TableProvider>> { |
| 38 | + Ok(Arc::new(ManifestsTable { table })) |
| 39 | +} |
| 40 | + |
| 41 | +fn manifests_schema() -> SchemaRef { |
| 42 | + static SCHEMA: OnceLock<SchemaRef> = OnceLock::new(); |
| 43 | + SCHEMA |
| 44 | + .get_or_init(|| { |
| 45 | + Arc::new(Schema::new(vec![ |
| 46 | + Field::new("file_name", DataType::Utf8, false), |
| 47 | + Field::new("file_size", DataType::Int64, false), |
| 48 | + Field::new("num_added_files", DataType::Int64, false), |
| 49 | + Field::new("num_deleted_files", DataType::Int64, false), |
| 50 | + Field::new("schema_id", DataType::Int64, false), |
| 51 | + Field::new("min_partition_stats", DataType::Utf8, true), |
| 52 | + Field::new("max_partition_stats", DataType::Utf8, true), |
| 53 | + Field::new("min_row_id", DataType::Int64, true), |
| 54 | + Field::new("max_row_id", DataType::Int64, true), |
| 55 | + ])) |
| 56 | + }) |
| 57 | + .clone() |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(Debug)] |
| 61 | +struct ManifestsTable { |
| 62 | + table: Table, |
| 63 | +} |
| 64 | + |
| 65 | +#[async_trait] |
| 66 | +impl TableProvider for ManifestsTable { |
| 67 | + fn as_any(&self) -> &dyn Any { |
| 68 | + self |
| 69 | + } |
| 70 | + |
| 71 | + fn schema(&self) -> SchemaRef { |
| 72 | + manifests_schema() |
| 73 | + } |
| 74 | + |
| 75 | + fn table_type(&self) -> TableType { |
| 76 | + TableType::View |
| 77 | + } |
| 78 | + |
| 79 | + async fn scan( |
| 80 | + &self, |
| 81 | + _state: &dyn Session, |
| 82 | + projection: Option<&Vec<usize>>, |
| 83 | + _filters: &[Expr], |
| 84 | + _limit: Option<usize>, |
| 85 | + ) -> DFResult<Arc<dyn ExecutionPlan>> { |
| 86 | + let metas = collect_manifests(&self.table) |
| 87 | + .await |
| 88 | + .map_err(to_datafusion_error)?; |
| 89 | + |
| 90 | + let n = metas.len(); |
| 91 | + let mut file_names: Vec<String> = Vec::with_capacity(n); |
| 92 | + let mut file_sizes = Vec::with_capacity(n); |
| 93 | + let mut num_added = Vec::with_capacity(n); |
| 94 | + let mut num_deleted = Vec::with_capacity(n); |
| 95 | + let mut schema_ids = Vec::with_capacity(n); |
| 96 | + let mut min_row_ids: Vec<Option<i64>> = Vec::with_capacity(n); |
| 97 | + let mut max_row_ids: Vec<Option<i64>> = Vec::with_capacity(n); |
| 98 | + |
| 99 | + for meta in metas { |
| 100 | + file_names.push(meta.file_name().to_string()); |
| 101 | + file_sizes.push(meta.file_size()); |
| 102 | + num_added.push(meta.num_added_files()); |
| 103 | + num_deleted.push(meta.num_deleted_files()); |
| 104 | + schema_ids.push(meta.schema_id()); |
| 105 | + min_row_ids.push(meta.min_row_id()); |
| 106 | + max_row_ids.push(meta.max_row_id()); |
| 107 | + } |
| 108 | + |
| 109 | + let schema = manifests_schema(); |
| 110 | + let batch = RecordBatch::try_new( |
| 111 | + schema.clone(), |
| 112 | + vec![ |
| 113 | + Arc::new(StringArray::from(file_names)), |
| 114 | + Arc::new(Int64Array::from(file_sizes)), |
| 115 | + Arc::new(Int64Array::from(num_added)), |
| 116 | + Arc::new(Int64Array::from(num_deleted)), |
| 117 | + Arc::new(Int64Array::from(schema_ids)), |
| 118 | + new_null_array(&DataType::Utf8, n), |
| 119 | + new_null_array(&DataType::Utf8, n), |
| 120 | + Arc::new(Int64Array::from(min_row_ids)), |
| 121 | + Arc::new(Int64Array::from(max_row_ids)), |
| 122 | + ], |
| 123 | + )?; |
| 124 | + |
| 125 | + Ok(MemorySourceConfig::try_new_exec( |
| 126 | + &[vec![batch]], |
| 127 | + schema, |
| 128 | + projection.cloned(), |
| 129 | + )?) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +async fn collect_manifests(table: &Table) -> paimon::Result<Vec<ManifestFileMeta>> { |
| 134 | + let file_io = table.file_io(); |
| 135 | + let sm = SnapshotManager::new(file_io.clone(), table.location().to_string()); |
| 136 | + let snapshot = match sm.get_latest_snapshot().await? { |
| 137 | + Some(s) => s, |
| 138 | + None => return Ok(Vec::new()), |
| 139 | + }; |
| 140 | + |
| 141 | + let base_path = sm.manifest_path(snapshot.base_manifest_list()); |
| 142 | + let delta_path = sm.manifest_path(snapshot.delta_manifest_list()); |
| 143 | + let changelog_path = snapshot |
| 144 | + .changelog_manifest_list() |
| 145 | + .map(|c| sm.manifest_path(c)); |
| 146 | + let base_fut = ManifestList::read(file_io, &base_path); |
| 147 | + let delta_fut = ManifestList::read(file_io, &delta_path); |
| 148 | + let changelog_fut = async { |
| 149 | + match &changelog_path { |
| 150 | + Some(p) => ManifestList::read(file_io, p).await, |
| 151 | + None => Ok(Vec::new()), |
| 152 | + } |
| 153 | + }; |
| 154 | + let (base, delta, changelog) = futures::try_join!(base_fut, delta_fut, changelog_fut)?; |
| 155 | + let mut metas = base; |
| 156 | + metas.extend(delta); |
| 157 | + metas.extend(changelog); |
| 158 | + Ok(metas) |
| 159 | +} |
0 commit comments