|
| 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 | +mod tests { |
| 19 | + use std::collections::HashMap; |
| 20 | + |
| 21 | + use iceberg::io::FileIOBuilder; |
| 22 | + use iceberg::spec::{ |
| 23 | + NestedField, PartitionSpecBuilder, PrimitiveType, Schema, Transform, Type, |
| 24 | + }; |
| 25 | + use iceberg::{Catalog, NamespaceIdent, TableCreation, TableIdent}; |
| 26 | + use iceberg_catalog_memory::MemoryCatalog; |
| 27 | + use tempfile::TempDir; |
| 28 | + |
| 29 | + fn temp_path() -> String { |
| 30 | + let temp_dir = TempDir::new().unwrap(); |
| 31 | + temp_dir.path().to_str().unwrap().to_string() |
| 32 | + } |
| 33 | + |
| 34 | + fn get_iceberg_catalog() -> MemoryCatalog { |
| 35 | + let file_io = FileIOBuilder::new_fs_io().build().unwrap(); |
| 36 | + MemoryCatalog::new(file_io, Some(temp_path())) |
| 37 | + } |
| 38 | + |
| 39 | + #[tokio::test] |
| 40 | + async fn test_partition_table() { |
| 41 | + let catalog = get_iceberg_catalog(); |
| 42 | + |
| 43 | + let namespace_id = NamespaceIdent::from_vec(vec!["ns1".to_string()]).unwrap(); |
| 44 | + catalog |
| 45 | + .create_namespace(&namespace_id, HashMap::new()) |
| 46 | + .await |
| 47 | + .unwrap(); |
| 48 | + |
| 49 | + let table_id = TableIdent::from_strs(["ns1", "t1"]).unwrap(); |
| 50 | + let table_schema = Schema::builder() |
| 51 | + .with_fields(vec![ |
| 52 | + NestedField::optional(1, "a", Type::Primitive(PrimitiveType::String)).into(), |
| 53 | + NestedField::required(2, "b", Type::Primitive(PrimitiveType::Int)).into(), |
| 54 | + NestedField::optional(3, "c", Type::Primitive(PrimitiveType::Boolean)).into(), |
| 55 | + ]) |
| 56 | + .with_schema_id(1) |
| 57 | + .build() |
| 58 | + .unwrap(); |
| 59 | + let p = PartitionSpecBuilder::new(&table_schema) |
| 60 | + .add_partition_field("a", "bucket_a", Transform::Bucket(16)) |
| 61 | + .unwrap() |
| 62 | + .add_partition_field("b", "b", Transform::Identity) |
| 63 | + .unwrap() |
| 64 | + .build() |
| 65 | + .unwrap(); |
| 66 | + |
| 67 | + // Create table |
| 68 | + let table_creation = TableCreation::builder() |
| 69 | + .name(table_id.name.clone()) |
| 70 | + .schema(table_schema.clone()) |
| 71 | + .partition_spec(p) |
| 72 | + .properties(HashMap::from([("owner".to_string(), "testx".to_string())])) |
| 73 | + .build(); |
| 74 | + |
| 75 | + catalog |
| 76 | + .create_table(&table_id.namespace, table_creation) |
| 77 | + .await |
| 78 | + .unwrap(); |
| 79 | + let table = catalog.load_table(&table_id).await.unwrap(); |
| 80 | + let partition_fields = table.metadata().default_partition_spec().unwrap().fields(); |
| 81 | + assert_eq!( |
| 82 | + 2, |
| 83 | + partition_fields.len(), |
| 84 | + "There should be 2 partition fields" |
| 85 | + ); |
| 86 | + |
| 87 | + let partition_field_0 = &partition_fields[0]; |
| 88 | + assert_eq!("bucket_a", partition_field_0.name); |
| 89 | + assert_eq!(1, partition_field_0.source_id); |
| 90 | + assert_eq!(Transform::Bucket(16), partition_field_0.transform); |
| 91 | + |
| 92 | + let partition_field_1 = &partition_fields[1]; |
| 93 | + assert_eq!("b", partition_field_1.name); |
| 94 | + assert_eq!(2, partition_field_1.source_id); |
| 95 | + assert_eq!(Transform::Identity, partition_field_1.transform); |
| 96 | + } |
| 97 | +} |
0 commit comments