|
| 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 [SnapshotsTable](https://github.com/apache/paimon/blob/release-1.3/paimon-core/src/main/java/org/apache/paimon/table/system/SnapshotsTable.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::{Int64Array, RecordBatch, StringArray, TimestampMillisecondArray}; |
| 25 | +use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef, TimeUnit}; |
| 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::table::{SnapshotManager, Table}; |
| 33 | + |
| 34 | +use crate::error::to_datafusion_error; |
| 35 | + |
| 36 | +pub(super) fn build(table: Table) -> DFResult<Arc<dyn TableProvider>> { |
| 37 | + Ok(Arc::new(SnapshotsTable { table })) |
| 38 | +} |
| 39 | + |
| 40 | +fn snapshots_schema() -> SchemaRef { |
| 41 | + static SCHEMA: OnceLock<SchemaRef> = OnceLock::new(); |
| 42 | + SCHEMA |
| 43 | + .get_or_init(|| { |
| 44 | + Arc::new(Schema::new(vec![ |
| 45 | + Field::new("snapshot_id", DataType::Int64, false), |
| 46 | + Field::new("schema_id", DataType::Int64, false), |
| 47 | + Field::new("commit_user", DataType::Utf8, false), |
| 48 | + Field::new("commit_identifier", DataType::Int64, false), |
| 49 | + Field::new("commit_kind", DataType::Utf8, false), |
| 50 | + Field::new( |
| 51 | + "commit_time", |
| 52 | + DataType::Timestamp(TimeUnit::Millisecond, None), |
| 53 | + false, |
| 54 | + ), |
| 55 | + Field::new("base_manifest_list", DataType::Utf8, false), |
| 56 | + Field::new("delta_manifest_list", DataType::Utf8, false), |
| 57 | + Field::new("changelog_manifest_list", DataType::Utf8, true), |
| 58 | + Field::new("total_record_count", DataType::Int64, true), |
| 59 | + Field::new("delta_record_count", DataType::Int64, true), |
| 60 | + Field::new("changelog_record_count", DataType::Int64, true), |
| 61 | + Field::new("watermark", DataType::Int64, true), |
| 62 | + Field::new("next_row_id", DataType::Int64, true), |
| 63 | + ])) |
| 64 | + }) |
| 65 | + .clone() |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Debug)] |
| 69 | +struct SnapshotsTable { |
| 70 | + table: Table, |
| 71 | +} |
| 72 | + |
| 73 | +#[async_trait] |
| 74 | +impl TableProvider for SnapshotsTable { |
| 75 | + fn as_any(&self) -> &dyn Any { |
| 76 | + self |
| 77 | + } |
| 78 | + |
| 79 | + fn schema(&self) -> SchemaRef { |
| 80 | + snapshots_schema() |
| 81 | + } |
| 82 | + |
| 83 | + fn table_type(&self) -> TableType { |
| 84 | + TableType::View |
| 85 | + } |
| 86 | + |
| 87 | + async fn scan( |
| 88 | + &self, |
| 89 | + _state: &dyn Session, |
| 90 | + projection: Option<&Vec<usize>>, |
| 91 | + _filters: &[Expr], |
| 92 | + _limit: Option<usize>, |
| 93 | + ) -> DFResult<Arc<dyn ExecutionPlan>> { |
| 94 | + let sm = SnapshotManager::new( |
| 95 | + self.table.file_io().clone(), |
| 96 | + self.table.location().to_string(), |
| 97 | + ); |
| 98 | + let snapshots = sm.list_all().await.map_err(to_datafusion_error)?; |
| 99 | + |
| 100 | + let n = snapshots.len(); |
| 101 | + let mut snapshot_ids = Vec::with_capacity(n); |
| 102 | + let mut schema_ids = Vec::with_capacity(n); |
| 103 | + let mut commit_users: Vec<String> = Vec::with_capacity(n); |
| 104 | + let mut commit_identifiers = Vec::with_capacity(n); |
| 105 | + let mut commit_kinds: Vec<String> = Vec::with_capacity(n); |
| 106 | + let mut commit_times = Vec::with_capacity(n); |
| 107 | + let mut base_manifest_lists: Vec<String> = Vec::with_capacity(n); |
| 108 | + let mut delta_manifest_lists: Vec<String> = Vec::with_capacity(n); |
| 109 | + let mut changelog_manifest_lists: Vec<Option<String>> = Vec::with_capacity(n); |
| 110 | + let mut total_record_counts: Vec<Option<i64>> = Vec::with_capacity(n); |
| 111 | + let mut delta_record_counts: Vec<Option<i64>> = Vec::with_capacity(n); |
| 112 | + let mut changelog_record_counts: Vec<Option<i64>> = Vec::with_capacity(n); |
| 113 | + let mut watermarks: Vec<Option<i64>> = Vec::with_capacity(n); |
| 114 | + let mut next_row_ids: Vec<Option<i64>> = Vec::with_capacity(n); |
| 115 | + |
| 116 | + for snap in &snapshots { |
| 117 | + snapshot_ids.push(snap.id()); |
| 118 | + schema_ids.push(snap.schema_id()); |
| 119 | + commit_users.push(snap.commit_user().to_string()); |
| 120 | + commit_identifiers.push(snap.commit_identifier()); |
| 121 | + commit_kinds.push(snap.commit_kind().to_string()); |
| 122 | + commit_times.push(snap.time_millis() as i64); |
| 123 | + base_manifest_lists.push(snap.base_manifest_list().to_string()); |
| 124 | + delta_manifest_lists.push(snap.delta_manifest_list().to_string()); |
| 125 | + changelog_manifest_lists.push(snap.changelog_manifest_list().map(str::to_string)); |
| 126 | + total_record_counts.push(snap.total_record_count()); |
| 127 | + delta_record_counts.push(snap.delta_record_count()); |
| 128 | + changelog_record_counts.push(snap.changelog_record_count()); |
| 129 | + watermarks.push(snap.watermark()); |
| 130 | + next_row_ids.push(snap.next_row_id()); |
| 131 | + } |
| 132 | + |
| 133 | + let schema = snapshots_schema(); |
| 134 | + let batch = RecordBatch::try_new( |
| 135 | + schema.clone(), |
| 136 | + vec![ |
| 137 | + Arc::new(Int64Array::from(snapshot_ids)), |
| 138 | + Arc::new(Int64Array::from(schema_ids)), |
| 139 | + Arc::new(StringArray::from(commit_users)), |
| 140 | + Arc::new(Int64Array::from(commit_identifiers)), |
| 141 | + Arc::new(StringArray::from(commit_kinds)), |
| 142 | + Arc::new(TimestampMillisecondArray::from(commit_times)), |
| 143 | + Arc::new(StringArray::from(base_manifest_lists)), |
| 144 | + Arc::new(StringArray::from(delta_manifest_lists)), |
| 145 | + Arc::new(StringArray::from(changelog_manifest_lists)), |
| 146 | + Arc::new(Int64Array::from(total_record_counts)), |
| 147 | + Arc::new(Int64Array::from(delta_record_counts)), |
| 148 | + Arc::new(Int64Array::from(changelog_record_counts)), |
| 149 | + Arc::new(Int64Array::from(watermarks)), |
| 150 | + Arc::new(Int64Array::from(next_row_ids)), |
| 151 | + ], |
| 152 | + )?; |
| 153 | + |
| 154 | + Ok(MemorySourceConfig::try_new_exec( |
| 155 | + &[vec![batch]], |
| 156 | + schema, |
| 157 | + projection.cloned(), |
| 158 | + )?) |
| 159 | + } |
| 160 | +} |
0 commit comments