-
Notifications
You must be signed in to change notification settings - Fork 58
feat(datafusion): Add $snapshots system table #264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
plusplusjiajia
wants to merge
1
commit into
apache:main
Choose a base branch
from
plusplusjiajia:fusion-snapshots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
crates/integrations/datafusion/src/system_tables/snapshots.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // 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. | ||
|
|
||
| //! Mirrors Java [SnapshotsTable](https://github.com/apache/paimon/blob/release-1.3/paimon-core/src/main/java/org/apache/paimon/table/system/SnapshotsTable.java). | ||
|
|
||
| use std::any::Any; | ||
| use std::sync::{Arc, OnceLock}; | ||
|
|
||
| use async_trait::async_trait; | ||
| use datafusion::arrow::array::{Int64Array, RecordBatch, StringArray, TimestampMillisecondArray}; | ||
| use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef, TimeUnit}; | ||
| use datafusion::catalog::Session; | ||
| use datafusion::datasource::memory::MemorySourceConfig; | ||
| use datafusion::datasource::{TableProvider, TableType}; | ||
| use datafusion::error::Result as DFResult; | ||
| use datafusion::logical_expr::Expr; | ||
| use datafusion::physical_plan::ExecutionPlan; | ||
| use paimon::table::{SnapshotManager, Table}; | ||
|
|
||
| use crate::error::to_datafusion_error; | ||
|
|
||
| pub(super) fn build(table: Table) -> DFResult<Arc<dyn TableProvider>> { | ||
| Ok(Arc::new(SnapshotsTable { table })) | ||
| } | ||
|
|
||
| fn snapshots_schema() -> SchemaRef { | ||
| static SCHEMA: OnceLock<SchemaRef> = OnceLock::new(); | ||
| SCHEMA | ||
| .get_or_init(|| { | ||
| Arc::new(Schema::new(vec![ | ||
| Field::new("snapshot_id", DataType::Int64, false), | ||
| Field::new("schema_id", DataType::Int64, false), | ||
| Field::new("commit_user", DataType::Utf8, false), | ||
| Field::new("commit_identifier", DataType::Int64, false), | ||
| Field::new("commit_kind", DataType::Utf8, false), | ||
| Field::new( | ||
| "commit_time", | ||
| DataType::Timestamp(TimeUnit::Millisecond, None), | ||
| false, | ||
| ), | ||
| Field::new("base_manifest_list", DataType::Utf8, false), | ||
| Field::new("delta_manifest_list", DataType::Utf8, false), | ||
| Field::new("changelog_manifest_list", DataType::Utf8, true), | ||
| Field::new("total_record_count", DataType::Int64, true), | ||
| Field::new("delta_record_count", DataType::Int64, true), | ||
| Field::new("changelog_record_count", DataType::Int64, true), | ||
| Field::new("watermark", DataType::Int64, true), | ||
| Field::new("next_row_id", DataType::Int64, true), | ||
| ])) | ||
| }) | ||
| .clone() | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct SnapshotsTable { | ||
| table: Table, | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl TableProvider for SnapshotsTable { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn schema(&self) -> SchemaRef { | ||
| snapshots_schema() | ||
| } | ||
|
|
||
| fn table_type(&self) -> TableType { | ||
| TableType::View | ||
| } | ||
|
|
||
| async fn scan( | ||
| &self, | ||
| _state: &dyn Session, | ||
| projection: Option<&Vec<usize>>, | ||
| _filters: &[Expr], | ||
| _limit: Option<usize>, | ||
| ) -> DFResult<Arc<dyn ExecutionPlan>> { | ||
| let sm = SnapshotManager::new( | ||
| self.table.file_io().clone(), | ||
| self.table.location().to_string(), | ||
| ); | ||
| let snapshots = sm.list_all().await.map_err(to_datafusion_error)?; | ||
|
|
||
| let n = snapshots.len(); | ||
| let mut snapshot_ids = Vec::with_capacity(n); | ||
| let mut schema_ids = Vec::with_capacity(n); | ||
| let mut commit_users: Vec<String> = Vec::with_capacity(n); | ||
| let mut commit_identifiers = Vec::with_capacity(n); | ||
| let mut commit_kinds: Vec<String> = Vec::with_capacity(n); | ||
| let mut commit_times = Vec::with_capacity(n); | ||
| let mut base_manifest_lists: Vec<String> = Vec::with_capacity(n); | ||
| let mut delta_manifest_lists: Vec<String> = Vec::with_capacity(n); | ||
| let mut changelog_manifest_lists: Vec<Option<String>> = Vec::with_capacity(n); | ||
| let mut total_record_counts: Vec<Option<i64>> = Vec::with_capacity(n); | ||
| let mut delta_record_counts: Vec<Option<i64>> = Vec::with_capacity(n); | ||
| let mut changelog_record_counts: Vec<Option<i64>> = Vec::with_capacity(n); | ||
| let mut watermarks: Vec<Option<i64>> = Vec::with_capacity(n); | ||
| let mut next_row_ids: Vec<Option<i64>> = Vec::with_capacity(n); | ||
|
|
||
| for snap in &snapshots { | ||
| snapshot_ids.push(snap.id()); | ||
| schema_ids.push(snap.schema_id()); | ||
| commit_users.push(snap.commit_user().to_string()); | ||
| commit_identifiers.push(snap.commit_identifier()); | ||
| commit_kinds.push(snap.commit_kind().to_string()); | ||
| commit_times.push(snap.time_millis() as i64); | ||
| base_manifest_lists.push(snap.base_manifest_list().to_string()); | ||
| delta_manifest_lists.push(snap.delta_manifest_list().to_string()); | ||
| changelog_manifest_lists.push(snap.changelog_manifest_list().map(str::to_string)); | ||
| total_record_counts.push(snap.total_record_count()); | ||
| delta_record_counts.push(snap.delta_record_count()); | ||
| changelog_record_counts.push(snap.changelog_record_count()); | ||
| watermarks.push(snap.watermark()); | ||
| next_row_ids.push(snap.next_row_id()); | ||
| } | ||
|
|
||
| let schema = snapshots_schema(); | ||
| let batch = RecordBatch::try_new( | ||
| schema.clone(), | ||
| vec![ | ||
| Arc::new(Int64Array::from(snapshot_ids)), | ||
| Arc::new(Int64Array::from(schema_ids)), | ||
| Arc::new(StringArray::from(commit_users)), | ||
| Arc::new(Int64Array::from(commit_identifiers)), | ||
| Arc::new(StringArray::from(commit_kinds)), | ||
| Arc::new(TimestampMillisecondArray::from(commit_times)), | ||
| Arc::new(StringArray::from(base_manifest_lists)), | ||
| Arc::new(StringArray::from(delta_manifest_lists)), | ||
| Arc::new(StringArray::from(changelog_manifest_lists)), | ||
| Arc::new(Int64Array::from(total_record_counts)), | ||
| Arc::new(Int64Array::from(delta_record_counts)), | ||
| Arc::new(Int64Array::from(changelog_record_counts)), | ||
| Arc::new(Int64Array::from(watermarks)), | ||
| Arc::new(Int64Array::from(next_row_ids)), | ||
| ], | ||
| )?; | ||
|
|
||
| Ok(MemorySourceConfig::try_new_exec( | ||
| &[vec![batch]], | ||
| schema, | ||
| projection.cloned(), | ||
| )?) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why modify this?