risingwave_connector/connector_common/iceberg/mock_catalog.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
// Copyright 2024 RisingWave Labs
//
// Licensed 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.
use std::collections::HashMap;
use std::sync::Arc;
use async_trait::async_trait;
use icelake::catalog::{Catalog, UpdateTable};
use icelake::types::{Field, PartitionField, Schema, Struct, TableMetadata};
use icelake::{Table, TableIdentifier};
use opendal::services::Memory;
use opendal::Operator;
/// A mock catalog for iceberg used for plan test.
pub struct MockCatalog;
impl MockCatalog {
const RANGE_TABLE: &'static str = "range_table";
const SPARSE_TABLE: &'static str = "sparse_table";
fn sparse_table(self: &Arc<Self>) -> Table {
Table::builder_from_catalog(
{
let builder = Memory::default().root("/tmp");
Operator::new(builder).unwrap().finish()
},
self.clone(),
TableMetadata {
format_version: icelake::types::TableFormatVersion::V2,
table_uuid: "1".to_string(),
location: "1".to_string(),
last_sequence_number: 1,
last_updated_ms: 1,
last_column_id: 1,
schemas: vec![Schema::new(
1,
None,
Struct::new(vec![
Field::required(
1,
"v1",
icelake::types::Any::Primitive(icelake::types::Primitive::Int),
)
.into(),
Field::required(
2,
"v2",
icelake::types::Any::Primitive(icelake::types::Primitive::Long),
)
.into(),
Field::required(
3,
"v3",
icelake::types::Any::Primitive(icelake::types::Primitive::String),
)
.into(),
Field::required(
4,
"v4",
icelake::types::Any::Primitive(icelake::types::Primitive::Time),
)
.into(),
]),
)],
current_schema_id: 1,
partition_specs: vec![icelake::types::PartitionSpec {
spec_id: 1,
fields: vec![
PartitionField {
source_column_id: 1,
partition_field_id: 5,
transform: icelake::types::Transform::Identity,
name: "f1".to_string(),
},
PartitionField {
source_column_id: 2,
partition_field_id: 6,
transform: icelake::types::Transform::Bucket(1),
name: "f2".to_string(),
},
PartitionField {
source_column_id: 3,
partition_field_id: 7,
transform: icelake::types::Transform::Truncate(1),
name: "f3".to_string(),
},
PartitionField {
source_column_id: 4,
partition_field_id: 8,
transform: icelake::types::Transform::Void,
name: "f4".to_string(),
},
],
}],
default_spec_id: 1,
last_partition_id: 1,
properties: None,
current_snapshot_id: None,
snapshots: None,
snapshot_log: None,
metadata_log: None,
sort_orders: vec![],
default_sort_order_id: 0,
refs: HashMap::new(),
},
TableIdentifier::new(vec![Self::SPARSE_TABLE]).unwrap(),
)
.build()
.unwrap()
}
fn range_table(self: &Arc<Self>) -> Table {
Table::builder_from_catalog(
{
let builder = Memory::default().root("/tmp");
Operator::new(builder).unwrap().finish()
},
self.clone(),
TableMetadata {
format_version: icelake::types::TableFormatVersion::V2,
table_uuid: "1".to_string(),
location: "1".to_string(),
last_sequence_number: 1,
last_updated_ms: 1,
last_column_id: 1,
schemas: vec![Schema::new(
1,
None,
Struct::new(vec![
Field::required(
1,
"v1",
icelake::types::Any::Primitive(icelake::types::Primitive::Date),
)
.into(),
Field::required(
2,
"v2",
icelake::types::Any::Primitive(icelake::types::Primitive::Timestamp),
)
.into(),
Field::required(
3,
"v3",
icelake::types::Any::Primitive(icelake::types::Primitive::Timestampz),
)
.into(),
Field::required(
4,
"v4",
icelake::types::Any::Primitive(icelake::types::Primitive::Timestampz),
)
.into(),
]),
)],
current_schema_id: 1,
partition_specs: vec![icelake::types::PartitionSpec {
spec_id: 1,
fields: vec![
PartitionField {
source_column_id: 1,
partition_field_id: 7,
transform: icelake::types::Transform::Year,
name: "f4".to_string(),
},
PartitionField {
source_column_id: 2,
partition_field_id: 8,
transform: icelake::types::Transform::Month,
name: "f5".to_string(),
},
PartitionField {
source_column_id: 3,
partition_field_id: 9,
transform: icelake::types::Transform::Day,
name: "f6".to_string(),
},
PartitionField {
source_column_id: 4,
partition_field_id: 10,
transform: icelake::types::Transform::Hour,
name: "f7".to_string(),
},
],
}],
default_spec_id: 1,
last_partition_id: 1,
properties: None,
current_snapshot_id: None,
snapshots: None,
snapshot_log: None,
metadata_log: None,
sort_orders: vec![],
default_sort_order_id: 0,
refs: HashMap::new(),
},
TableIdentifier::new(vec![Self::RANGE_TABLE]).unwrap(),
)
.build()
.unwrap()
}
}
#[async_trait]
impl Catalog for MockCatalog {
fn name(&self) -> &str {
"mock"
}
// Mock catalog load mock table according to table_name, there is 2 kinds of table for test:
// 1. sparse partition table
// 2. range partition table
async fn load_table(self: Arc<Self>, table_name: &TableIdentifier) -> icelake::Result<Table> {
match table_name.name.as_ref() {
Self::SPARSE_TABLE => Ok(self.sparse_table()),
Self::RANGE_TABLE => Ok(self.range_table()),
_ => unimplemented!("table {} not found", table_name),
}
}
async fn update_table(self: Arc<Self>, _update_table: &UpdateTable) -> icelake::Result<Table> {
unimplemented!()
}
}