Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion crates/iceberg/src/avro/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl SchemaVisitor for SchemaToAvroSchema {
PrimitiveType::TimestampNs => AvroSchema::TimestampNanos,
PrimitiveType::TimestamptzNs => AvroSchema::TimestampNanos,
PrimitiveType::String => AvroSchema::String,
PrimitiveType::Uuid => AvroSchema::Uuid,
PrimitiveType::Uuid => avro_uuid_schema()?,
PrimitiveType::Fixed(len) => avro_fixed_schema((*len) as usize)?,
PrimitiveType::Binary => AvroSchema::Bytes,
PrimitiveType::Decimal { precision, scale } => {
Expand Down Expand Up @@ -289,6 +289,21 @@ pub(crate) fn avro_fixed_schema(len: usize) -> Result<AvroSchema> {
}))
}

/// Build the Avro schema for the Iceberg `uuid` primitive type.
///
/// The Iceberg spec maps `uuid` to `{"type": "fixed", "size": 16, "logicalType": "uuid"}`,
/// i.e. 16 raw bytes, which is what iceberg-java writes.
fn avro_uuid_schema() -> Result<AvroSchema> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused, how is this different from AvroSchema::Uuid?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this pr only changed iceberg schema to avro schema, I think we should also do vice versa?

Ok(AvroSchema::Fixed(FixedSchema {
name: Name::new("uuid_fixed")?,
aliases: None,
doc: None,
size: 16,
attributes: Default::default(),
default: None,
}))
}

pub(crate) fn avro_decimal_schema(precision: usize, scale: usize) -> Result<AvroSchema> {
// Avro decimal logical type annotates Avro bytes _or_ fixed types.
// https://avro.apache.org/docs/1.11.1/specification/_print/#decimal
Expand Down
13 changes: 11 additions & 2 deletions crates/iceberg/src/spec/values/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
//\! Serialization and deserialization support for Iceberg values

pub(crate) mod _serde {
use std::str::FromStr;

use serde::de::Visitor;
use serde::ser::{SerializeMap, SerializeSeq, SerializeStruct};
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
use serde_derive::{Deserialize as DeserializeDerive, Serialize as SerializeDerive};
use uuid::Uuid;
use uuid::fmt::Hyphenated;

use crate::spec::values::{Literal, Map, PrimitiveLiteral, Struct};
use crate::spec::{MAP_KEY_FIELD_NAME, MAP_VALUE_FIELD_NAME, PrimitiveType, Type};
Expand Down Expand Up @@ -444,6 +448,11 @@ pub(crate) mod _serde {
},
RawLiteralEnum::String(v) => match ty {
Type::Primitive(PrimitiveType::String) => Ok(Some(Literal::string(v))),
Type::Primitive(PrimitiveType::Uuid) => Hyphenated::from_str(&v)
.map(|hyphenated_uuid| Some(Literal::uuid(hyphenated_uuid.into_uuid())))
.map_err(|_| {
invalid_err_with_reason("string", "UUID must be a valid UUID string")
}),
_ => Err(invalid_err("string")),
},
RawLiteralEnum::Bytes(v) => match ty {
Expand All @@ -467,7 +476,7 @@ pub(crate) mod _serde {
let bytes: [u8; 16] = v.as_slice().try_into().map_err(|_| {
invalid_err_with_reason("bytes", "UUID must be exactly 16 bytes")
})?;
Ok(Some(Literal::uuid(uuid::Uuid::from_bytes(bytes))))
Ok(Some(Literal::uuid(Uuid::from_bytes(bytes))))
} else {
Err(invalid_err_with_reason(
"bytes",
Expand Down Expand Up @@ -601,7 +610,7 @@ pub(crate) mod _serde {
));
}
}
Ok(Some(Literal::uuid(uuid::Uuid::from_bytes(bytes))))
Ok(Some(Literal::uuid(Uuid::from_bytes(bytes))))
}
Type::Primitive(PrimitiveType::Decimal {
precision: _,
Expand Down
90 changes: 80 additions & 10 deletions crates/iceberg/src/spec/values/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn json_timestamptz_ns_rejects_non_utc_offset() {
// Per the spec, timestamptz_ns single-value serialization must use offset "+00:00"; Java's
// SingleValueParser enforces the same (DateTimeUtil.isUTCTimestamptz). A non-UTC offset is not a
// valid encoding and must be rejected, not silently re-based to UTC.
let record = serde_json::Value::String("2017-11-16T22:31:08.123456789+05:00".to_string());
let record = JsonValue::String("2017-11-16T22:31:08.123456789+05:00".to_string());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this related?

let result = Literal::try_from_json(record, &Primitive(PrimitiveType::TimestamptzNs));
assert!(
result.is_err(),
Expand All @@ -252,7 +252,7 @@ fn json_timestamptz_ns_rejects_non_utc_offset() {
fn json_timestamptz_rejects_non_utc_offset() {
// Micros-precision counterpart, mirroring Java's TestSingleValueParser.testInvalidTimestamptz:
// the offset must be "+00:00", so a non-UTC offset is rejected.
let record = serde_json::Value::String("2017-11-16T22:31:08.123456+05:00".to_string());
let record = JsonValue::String("2017-11-16T22:31:08.123456+05:00".to_string());
let result = Literal::try_from_json(record, &Primitive(PrimitiveType::Timestamptz));
assert!(
result.is_err(),
Expand Down Expand Up @@ -529,20 +529,38 @@ fn check_raw_literal_bytes_serde_via_avro(
expected_literal: Literal,
expected_type: &Type,
) {
use apache_avro::types::Value;

// Create an Avro bytes value and deserialize it through the RawLiteral path
let avro_value = Value::Bytes(input_bytes);
let raw_literal: RawLiteral = apache_avro::from_value(&avro_value).unwrap();
let result = raw_literal.try_into(expected_type).unwrap();
assert_eq!(result, Some(expected_literal));
check_raw_literal_serde_via_avro(avro_value, expected_literal, expected_type);
}

fn check_raw_literal_bytes_error_via_avro(input_bytes: Vec<u8>, expected_type: &Type) {
use apache_avro::types::Value;

let avro_value = Value::Bytes(input_bytes);
let raw_literal: RawLiteral = apache_avro::from_value(&avro_value).unwrap();
check_raw_literal_error_via_avro(avro_value, expected_type);
}

fn check_raw_literal_string_serde_via_avro(
input: &str,
expected_literal: Literal,
expected_type: &Type,
) {
let avro_value = Value::String(input.to_string());
check_raw_literal_serde_via_avro(avro_value, expected_literal, expected_type);
}

fn check_raw_literal_string_error_via_avro(input: &str, expected_type: &Type) {
let avro_value = Value::String(input.to_string());
check_raw_literal_error_via_avro(avro_value, expected_type);
}

fn check_raw_literal_serde_via_avro(input: Value, expected_literal: Literal, expected_type: &Type) {
let raw_literal: RawLiteral = apache_avro::from_value(&input).unwrap();
let result = raw_literal.try_into(expected_type).unwrap();
assert_eq!(result, Some(expected_literal));
}

fn check_raw_literal_error_via_avro(input: Value, expected_type: &Type) {
let raw_literal: RawLiteral = apache_avro::from_value(&input).unwrap();
let result = raw_literal.try_into(expected_type);
assert!(result.is_err(), "Expected error but got: {result:?}");
}
Expand Down Expand Up @@ -616,6 +634,39 @@ fn test_raw_literal_bytes_uuid_wrong_length() {
check_raw_literal_bytes_error_via_avro(bytes, &Primitive(PrimitiveType::Uuid));
}

#[test]
fn test_raw_literal_string_should_accept_hyphenated_uuid_as_value() {
let s = "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8";
check_raw_literal_string_serde_via_avro(
s,
Literal::uuid(Uuid::parse_str(s).unwrap()),
&Primitive(PrimitiveType::Uuid),
);
}

#[test]
fn test_raw_literal_string_should_reject_a_uuid_with_an_invalid_string() {
check_raw_literal_string_error_via_avro("not-a-uuid", &Primitive(PrimitiveType::Uuid));
}

#[test]
fn test_raw_literal_string_should_reject_non_hyphenated_uuids() {
check_raw_literal_string_error_via_avro(
"a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8",
&Primitive(PrimitiveType::Uuid),
);

check_raw_literal_string_error_via_avro(
"urn:uuid:A1A2A3A4-B1B2-C1C2-D1D2-D3D4D5D6D7D8",
&Primitive(PrimitiveType::Uuid),
);

check_raw_literal_string_error_via_avro(
"{a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8}",
&Primitive(PrimitiveType::Uuid),
);
}

#[test]
fn test_raw_literal_bytes_decimal_precision_4_scale_2() {
// Precision 4 requires 2 bytes
Expand Down Expand Up @@ -796,6 +847,25 @@ fn avro_convert_test_string() {
);
}

#[test]
fn test_avro_should_convert_uuid_as_uuid() {
check_convert_with_avro(
Literal::uuid(Uuid::parse_str("f79c3e09-677c-4bbd-a479-3f349cb785e7").unwrap()),
&Primitive(PrimitiveType::Uuid),
);
}

#[test]
fn test_avro_should_serialize_uuid_as_fixed_16_bytes() {
let uuid = Uuid::parse_str("f79c3e09-677c-4bbd-a479-3f349cb785e7").unwrap();

check_serialize_avro(
Literal::uuid(uuid),
&Primitive(PrimitiveType::Uuid),
Value::Fixed(16, uuid.as_bytes().to_vec()),
);
}

#[test]
fn avro_convert_test_date() {
check_convert_with_avro(
Expand Down
16 changes: 14 additions & 2 deletions crates/integration_tests/src/lib.rs
Comment thread
JosephLenton marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
// under the License.

use std::collections::HashMap;
use std::sync::OnceLock;
use std::sync::{Arc, OnceLock};

use iceberg::CatalogBuilder;
use iceberg::io::{
S3_ACCESS_KEY_ID, S3_ENDPOINT, S3_PATH_STYLE_ACCESS, S3_REGION, S3_SECRET_ACCESS_KEY,
};
use iceberg_catalog_rest::REST_CATALOG_PROP_URI;
use iceberg_catalog_rest::{REST_CATALOG_PROP_URI, RestCatalog, RestCatalogBuilder};
use iceberg_storage_opendal::OpenDalStorageFactory;
use iceberg_test_utils::{get_minio_endpoint, get_rest_catalog_endpoint, set_up};

/// Global test fixture that uses environment-based configuration.
Expand Down Expand Up @@ -52,6 +54,16 @@ impl GlobalTestFixture {

GlobalTestFixture { catalog_config }
}

pub async fn rest_catalog(&self) -> RestCatalog {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need this?

RestCatalogBuilder::default()
.with_storage_factory(Arc::new(OpenDalStorageFactory::S3 {
customized_credential_load: None,
}))
.load("rest", self.catalog_config.clone())
.await
.unwrap()
}
}

/// Returns a reference to the global test fixture.
Expand Down
Loading
Loading