|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::sync::Arc; |
| 16 | + |
| 17 | +use databend_common_catalog::catalog_kind::CATALOG_DEFAULT; |
| 18 | +use databend_common_catalog::plan::DataSourcePlan; |
| 19 | +use databend_common_catalog::table_args::TableArgs; |
| 20 | +use databend_common_exception::ErrorCode; |
| 21 | +use databend_common_exception::Result; |
| 22 | +use databend_common_expression::types::StringType; |
| 23 | +use databend_common_expression::types::VariantType; |
| 24 | +use databend_common_expression::DataBlock; |
| 25 | +use databend_common_expression::FromData; |
| 26 | +use databend_common_expression::TableDataType; |
| 27 | +use databend_common_expression::TableField; |
| 28 | +use databend_common_expression::TableSchema; |
| 29 | +use databend_common_expression::TableSchemaRefExt; |
| 30 | + |
| 31 | +use crate::io::MetaReaders; |
| 32 | +use crate::io::TableMetaLocationGenerator; |
| 33 | +use crate::sessions::TableContext; |
| 34 | +use crate::table_functions::parse_db_tb_args; |
| 35 | +use crate::table_functions::string_literal; |
| 36 | +use crate::table_functions::SimpleTableFunc; |
| 37 | +use crate::FuseTable; |
| 38 | + |
| 39 | +pub struct FuseDumpSnapshotsArgs { |
| 40 | + database_name: String, |
| 41 | + table_name: String, |
| 42 | +} |
| 43 | + |
| 44 | +const DEFAULT_SNAPSHOT_LIMIT: usize = 1; |
| 45 | + |
| 46 | +pub struct FuseDumpSnapshotsFunc { |
| 47 | + args: FuseDumpSnapshotsArgs, |
| 48 | +} |
| 49 | + |
| 50 | +impl From<&FuseDumpSnapshotsArgs> for TableArgs { |
| 51 | + fn from(args: &FuseDumpSnapshotsArgs) -> Self { |
| 52 | + TableArgs::new_positioned(vec![ |
| 53 | + string_literal(args.database_name.as_str()), |
| 54 | + string_literal(args.table_name.as_str()), |
| 55 | + ]) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[async_trait::async_trait] |
| 60 | +impl SimpleTableFunc for FuseDumpSnapshotsFunc { |
| 61 | + fn table_args(&self) -> Option<TableArgs> { |
| 62 | + Some((&self.args).into()) |
| 63 | + } |
| 64 | + |
| 65 | + fn schema(&self) -> Arc<TableSchema> { |
| 66 | + TableSchemaRefExt::create(vec![ |
| 67 | + TableField::new("snapshot_id", TableDataType::String), |
| 68 | + TableField::new("snapshot", TableDataType::Variant), |
| 69 | + ]) |
| 70 | + } |
| 71 | + |
| 72 | + async fn apply( |
| 73 | + &self, |
| 74 | + ctx: &Arc<dyn TableContext>, |
| 75 | + plan: &DataSourcePlan, |
| 76 | + ) -> Result<Option<DataBlock>> { |
| 77 | + let tenant_id = ctx.get_tenant(); |
| 78 | + let tbl = ctx |
| 79 | + .get_catalog(CATALOG_DEFAULT) |
| 80 | + .await? |
| 81 | + .get_table( |
| 82 | + &tenant_id, |
| 83 | + self.args.database_name.as_str(), |
| 84 | + self.args.table_name.as_str(), |
| 85 | + ) |
| 86 | + .await?; |
| 87 | + |
| 88 | + let table = FuseTable::try_from_table(tbl.as_ref()).map_err(|_| { |
| 89 | + ErrorCode::StorageOther( |
| 90 | + "Invalid table engine, only FUSE table supports fuse_dump_snapshots", |
| 91 | + ) |
| 92 | + })?; |
| 93 | + |
| 94 | + let meta_location_generator = table.meta_location_generator.clone(); |
| 95 | + let snapshot_location = table.snapshot_loc(); |
| 96 | + if let Some(snapshot_location) = snapshot_location { |
| 97 | + let limit = plan |
| 98 | + .push_downs |
| 99 | + .as_ref() |
| 100 | + .and_then(|v| v.limit) |
| 101 | + .unwrap_or(DEFAULT_SNAPSHOT_LIMIT); |
| 102 | + |
| 103 | + let table_snapshot_reader = MetaReaders::table_snapshot_reader(table.operator.clone()); |
| 104 | + let format_version = |
| 105 | + TableMetaLocationGenerator::snapshot_version(snapshot_location.as_str()); |
| 106 | + |
| 107 | + use crate::io::read::SnapshotHistoryReader; |
| 108 | + let lite_snapshot_stream = table_snapshot_reader.snapshot_history( |
| 109 | + snapshot_location, |
| 110 | + format_version, |
| 111 | + meta_location_generator.clone(), |
| 112 | + ); |
| 113 | + |
| 114 | + let mut snapshot_ids: Vec<String> = Vec::with_capacity(limit); |
| 115 | + let mut content: Vec<_> = Vec::with_capacity(limit); |
| 116 | + |
| 117 | + use futures::stream::StreamExt; |
| 118 | + let mut stream = lite_snapshot_stream.take(limit); |
| 119 | + |
| 120 | + use jsonb::Value as JsonbValue; |
| 121 | + while let Some(s) = stream.next().await { |
| 122 | + let (s, _v) = s?; |
| 123 | + snapshot_ids.push(s.snapshot_id.simple().to_string()); |
| 124 | + content.push(JsonbValue::from(serde_json::to_value(s)?).to_vec()); |
| 125 | + } |
| 126 | + |
| 127 | + let block = DataBlock::new_from_columns(vec![ |
| 128 | + StringType::from_data(snapshot_ids), |
| 129 | + VariantType::from_data(content), |
| 130 | + ]); |
| 131 | + |
| 132 | + return Ok(Some(block)); |
| 133 | + } |
| 134 | + Ok(Some(DataBlock::empty_with_schema(Arc::new( |
| 135 | + self.schema().into(), |
| 136 | + )))) |
| 137 | + } |
| 138 | + |
| 139 | + fn create(func_name: &str, table_args: TableArgs) -> Result<Self> |
| 140 | + where Self: Sized { |
| 141 | + let (arg_database_name, arg_table_name) = parse_db_tb_args(&table_args, func_name)?; |
| 142 | + Ok(Self { |
| 143 | + args: FuseDumpSnapshotsArgs { |
| 144 | + database_name: arg_database_name, |
| 145 | + table_name: arg_table_name, |
| 146 | + }, |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
0 commit comments