Skip to content

Commit

Permalink
Update in tablecreate & addspec
Browse files Browse the repository at this point in the history
  • Loading branch information
my-vegetable-has-exploded committed Nov 30, 2023
1 parent 824d502 commit b85eb16
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
14 changes: 6 additions & 8 deletions crates/catalog/rest/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ mod _serde {

use serde_derive::{Deserialize, Serialize};

use iceberg::spec::{PartitionSpec, Schema, SortOrder, TableMetadata, UnboundPartitionSpec};
use iceberg::spec::{Schema, SortOrder, TableMetadata, UnboundPartitionSpec};
use iceberg::{Error, ErrorKind, Namespace, TableIdent, TableRequirement, TableUpdate};

pub(super) const OK: u16 = 200u16;
Expand Down Expand Up @@ -686,9 +686,9 @@ mod tests {
use chrono::{TimeZone, Utc};
use iceberg::spec::ManifestListLocation::ManifestListFile;
use iceberg::spec::{
FormatVersion, NestedField, NullOrder, Operation, PartitionField, PartitionSpec,
PrimitiveType, Schema, Snapshot, SnapshotLog, SortDirection, SortField, SortOrder, Summary,
Transform, Type,
FormatVersion, NestedField, NullOrder, Operation, PrimitiveType, Schema, Snapshot,
SnapshotLog, SortDirection, SortField, SortOrder, Summary, Transform, Type,
UnboundPartitionField, UnboundPartitionSpec,
};
use iceberg::transaction::Transaction;
use mockito::{Mock, Server, ServerGuard};
Expand Down Expand Up @@ -1233,14 +1233,12 @@ mod tests {
)
.properties(HashMap::from([("owner".to_string(), "testx".to_string())]))
.partition_spec(
PartitionSpec::builder()
.with_fields(vec![PartitionField::builder()
UnboundPartitionSpec::builder()
.with_fields(vec![UnboundPartitionField::builder()
.source_id(1)
.field_id(1000)
.transform(Transform::Truncate(3))
.name("id".to_string())
.build()])
.with_spec_id(1)
.build()
.unwrap(),
)
Expand Down
32 changes: 13 additions & 19 deletions crates/iceberg/src/catalog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
use serde_derive::{Deserialize, Serialize};
use urlencoding::encode;

use crate::spec::{FormatVersion, PartitionSpec, Schema, Snapshot, SnapshotReference, SortOrder, UnboundPartitionSpec};
use crate::spec::{
FormatVersion, Schema, Snapshot, SnapshotReference, SortOrder, UnboundPartitionSpec,
};
use crate::table::Table;
use crate::{Error, ErrorKind, Result};
use async_trait::async_trait;
Expand Down Expand Up @@ -429,9 +431,9 @@ pub enum TableUpdate {
mod tests {
use crate::spec::ManifestListLocation::ManifestListFile;
use crate::spec::{
FormatVersion, NestedField, NullOrder, Operation, PartitionField, PartitionSpec,
PrimitiveType, Schema, Snapshot, SnapshotReference, SnapshotRetention, SortDirection,
SortField, SortOrder, Summary, Transform, Type,
FormatVersion, NestedField, NullOrder, Operation, PrimitiveType, Schema, Snapshot,
SnapshotReference, SnapshotRetention, SortDirection, SortField, SortOrder, Summary,
Transform, Type, UnboundPartitionField, UnboundPartitionSpec,
};
use crate::{NamespaceIdent, TableIdent, TableRequirement, TableUpdate};
use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -758,23 +760,19 @@ mod tests {
{
"action": "add-spec",
"spec": {
"spec-id": 1,
"fields": [
{
"source-id": 4,
"field-id": 1000,
"name": "ts_day",
"transform": "day"
},
{
"source-id": 1,
"field-id": 1001,
"name": "id_bucket",
"transform": "bucket[16]"
},
{
"source-id": 2,
"field-id": 1002,
"name": "id_truncate",
"transform": "truncate[4]"
}
Expand All @@ -783,28 +781,24 @@ mod tests {
}
"#,
TableUpdate::AddSpec {
spec: PartitionSpec::builder()
.with_spec_id(1)
.with_partition_field(
PartitionField::builder()
spec: UnboundPartitionSpec::builder()
.with_unbound_partition_field(
UnboundPartitionField::builder()
.source_id(4)
.field_id(1000)
.name("ts_day".to_string())
.transform(Transform::Day)
.build(),
)
.with_partition_field(
PartitionField::builder()
.with_unbound_partition_field(
UnboundPartitionField::builder()
.source_id(1)
.field_id(1001)
.name("id_bucket".to_string())
.transform(Transform::Bucket(16))
.build(),
)
.with_partition_field(
PartitionField::builder()
.with_unbound_partition_field(
UnboundPartitionField::builder()
.source_id(2)
.field_id(1002)
.name("id_truncate".to_string())
.transform(Transform::Truncate(4))
.build(),
Expand Down
16 changes: 4 additions & 12 deletions crates/iceberg/src/spec/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
/*!
* Partitioning
*/
use crate::error::{Error, ErrorKind, Result};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use typed_builder::TypedBuilder;

use super::DEFAULT_SPEC_ID;
use super::{schema::SchemaRef, transform::Transform};
use super::transform::Transform;

/// Reference to [`PartitionSpec`].
pub type PartitionSpecRef = Arc<PartitionSpec>;
Expand Down Expand Up @@ -62,18 +60,17 @@ impl PartitionSpec {
}
}

static PARTITION_DATA_ID_START: i32 = 1000;

/// Reference to [`UnboundPartitionSpec`].
pub type UnboundPartitionSpecRef = Arc<UnboundPartitionSpec>;
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, TypedBuilder)]
#[serde(rename_all = "kebab-case")]
/// Unbound partition field can be built without a schema and later bound to a schema.
pub struct UnboundPartitionField {
/// A source column id from the table’s schema
pub source_id: i32,
/// A partition field id that is used to identify a partition field and is unique within a partition spec.
/// In v2 table metadata, it is unique across all partition specs.
#[builder(default, setter(strip_option))]
pub partition_id: Option<i32>,
/// A partition name.
pub name: String,
Expand All @@ -87,23 +84,18 @@ pub struct UnboundPartitionField {
/// Unbound partition spec can be built without a schema and later bound to a schema.
pub struct UnboundPartitionSpec {
/// Identifier for PartitionSpec
#[builder(default, setter(strip_option))]
pub spec_id: Option<i32>,
/// Details of the partition spec
#[builder(setter(each(name = "with_unbound_partition_field")))]
pub fields: Vec<UnboundPartitionField>,
}

impl UnboundPartitionSpec {
/// last assigned id for partitioned field
pub fn unpartitioned_last_assigned_id() -> i32 {
PARTITION_DATA_ID_START - 1
}

/// Create unbound partition spec builer
pub fn builder() -> UnboundPartitionSpecBuilder {
UnboundPartitionSpecBuilder::default()
}

}

#[cfg(test)]
Expand Down

0 comments on commit b85eb16

Please sign in to comment.