This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-2916-219078c9d8d82cdc431af77a430e07c8fe56bd65 in repository https://gitbox.apache.org/repos/asf/iceberg-rust.git
commit 50d58a7c2b973967a3710d67f3740e6cddb6962e Author: Joseph Lenton <[email protected]> AuthorDate: Fri Nov 20 01:00:33 2026 +0000 fix: table UUID partitions not working (#2916) * chore: add a test that recreates the uuid partition error * fix: update to turn UUIDs into Strings for Partitions * chore: add a unit test for RawLiteralEnum::try_from with uuid * chore: move UUID partition test to use datafusion integration tests * feat: restrict UUID partitions to only hyphenated UUIDs * refactor: move from serde solution to avro solution * feat: add logicalType uuid to the uuid transported by avro * revert: undo adding rest_catalog to integration tests * chore: update to use Avro 0.22 changes * revert: go back to using serde_json::Value * docs: add a comment on hyphenated needing to match java * chore: add a test going through ManifestWriter::write_manifest_file * chore: improve the error message on hyphenated UUID * chore: change unreachable path to an error * chore: add Avro UUID roundtrip test * docs: clarified some of the comment behaviour better * chore: add datum roundtrip test * chore: add Datum avro uuid round trip test * refactor: change from RawLiteralEnum::Uuid to RawLiteralEnum::Fixed * chore: add avro uuid to datum test * refactor: remove Result return type from avro_uuid_schema() * refactor: rename datum tests to clarify 16 bytes * refactor: change round trip tests to use manifest reader / writer * chore: add a test to confirm uuid turns into avro fixed 16 * chore: add json tests * chore: add a comment to clarify the serialisation behaviour * refactor: simplify the serde value Primitive UUID to Literal UUID branches * refactor: update manifest tests to match branch building * chore: update explanation of the use of avro's fixed serialize * refactor: merge the manifest writer tests into a single test with multiple cases --- crates/iceberg/src/avro/schema.rs | 42 +++- crates/iceberg/src/spec/manifest/writer.rs | 93 +++++++- crates/iceberg/src/spec/values/serde.rs | 50 +++-- crates/iceberg/src/spec/values/tests.rs | 327 ++++++++++++++++++++++++++++- 4 files changed, 486 insertions(+), 26 deletions(-) diff --git a/crates/iceberg/src/avro/schema.rs b/crates/iceberg/src/avro/schema.rs index 3caf99bfe..230efcc53 100644 --- a/crates/iceberg/src/avro/schema.rs +++ b/crates/iceberg/src/avro/schema.rs @@ -54,6 +54,7 @@ impl SchemaToAvroSchema { fn define_once(&mut self, schema: AvroSchema) -> AvroSchema { let name = match &schema { AvroSchema::Fixed(FixedSchema { name, .. }) + | AvroSchema::Uuid(UuidSchema::Fixed(FixedSchema { name, .. })) | AvroSchema::Decimal(DecimalSchema { inner: InnerDecimalSchema::Fixed(FixedSchema { name, .. }), .. @@ -271,9 +272,7 @@ impl SchemaVisitor for SchemaToAvroSchema { PrimitiveType::TimestampNs => AvroSchema::TimestampNanos, PrimitiveType::TimestamptzNs => AvroSchema::TimestampNanos, PrimitiveType::String => AvroSchema::String, - // TODO(#2913): write the spec's `fixed[16]` uuid. The manifest reader - // already accepts both forms. - PrimitiveType::Uuid => AvroSchema::Uuid(UuidSchema::String), + PrimitiveType::Uuid => self.define_once(avro_uuid_schema()), PrimitiveType::Fixed(len) => self.define_once(avro_fixed_schema((*len) as usize)?), PrimitiveType::Binary | PrimitiveType::Geometry(_) | PrimitiveType::Geography(_) => { AvroSchema::Bytes @@ -330,6 +329,20 @@ 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() -> AvroSchema { + AvroSchema::Uuid(UuidSchema::Fixed(FixedSchema { + name: Name::new("uuid_fixed").expect("The name 'uuid_fixed' should always be valid"), + aliases: None, + doc: None, + size: 16, + attributes: Default::default(), + })) +} + 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 @@ -1368,4 +1381,27 @@ mod tests { }; assert_eq!(record.fields[0].schema, AvroSchema::Bytes); } + + #[test] + fn test_uuid_to_avro_fixed_uuid_schema() { + let schema = Schema::builder() + .with_fields(vec![ + NestedField::required(1, "id", Type::Primitive(PrimitiveType::Uuid)).into(), + ]) + .build() + .unwrap(); + + let AvroSchema::Record(record) = schema_to_avro_schema("t", &schema).unwrap() else { + panic!("expected Avro record schema"); + }; + let output = serde_json::to_value(&record.fields[0].schema).unwrap(); + + let expected = serde_json::json!({ + "type": "fixed", + "name": "uuid_fixed", + "size": 16, + "logicalType": "uuid", + }); + assert_eq!(output, expected); + } } diff --git a/crates/iceberg/src/spec/manifest/writer.rs b/crates/iceberg/src/spec/manifest/writer.rs index c2c842324..0cf1b10fc 100644 --- a/crates/iceberg/src/spec/manifest/writer.rs +++ b/crates/iceberg/src/spec/manifest/writer.rs @@ -589,8 +589,8 @@ mod tests { use crate::avro::{assert_deflate_codec, define_named_types_repeatedly}; use crate::io::FileIO; use crate::spec::{ - DataContentType, DataFileBuilder, DataFileFormat, Literal, Manifest, NestedField, - PrimitiveType, Schema, Struct, Transform, Type, + DataContentType, DataFileBuilder, DataFileFormat, Literal, Manifest, ManifestReader, + NestedField, PrimitiveType, Schema, Struct, Transform, Type, }; #[tokio::test] @@ -929,6 +929,19 @@ mod tests { .await; } + #[tokio::test] + async fn test_write_manifest_with_repeated_uuid_partition_type() { + roundtrip_two_partition_fields_of_type(Type::Primitive(PrimitiveType::Uuid), [ + Literal::uuid(uuid::Uuid::from_u128( + 0xf79c3e09_677c_4bbd_a479_3f349cb785e7, + )), + Literal::uuid(uuid::Uuid::from_u128( + 0x0b1c4f2a_9d3e_4a5b_8c6d_7e8f9a0b1c2d, + )), + ]) + .await; + } + /// Writes a manifest with one added data file and returns the field names /// of its Avro `data_file` record. async fn written_data_file_field_names( @@ -1058,4 +1071,80 @@ mod tests { "content_size_in_bytes", ]); } + + #[tokio::test] + async fn test_manifest_roundtrips_uuid_and_fixed_partitions_in_each_format_version() { + let uuid = uuid::Uuid::from_u128(0xf79c3e09_677c_4bbd_a479_3f349cb785e7); + let cases = [ + ( + Type::Primitive(PrimitiveType::Uuid), + Some(Literal::uuid(uuid)), + ), + (Type::Primitive(PrimitiveType::Uuid), None), + ( + Type::Primitive(PrimitiveType::Fixed(16)), + Some(Literal::fixed(uuid.into_bytes())), + ), + ]; + let builds: [fn(ManifestWriterBuilder) -> ManifestWriter; 3] = [ + ManifestWriterBuilder::build_v1, + ManifestWriterBuilder::build_v2_data, + ManifestWriterBuilder::build_v3_data, + ]; + + for build in builds { + for (partition_field_type, value) in cases.clone() { + let data_file = DataFileBuilder::default() + .content(DataContentType::Data) + .file_path("memory:///table/data/00000.parquet".to_string()) + .file_format(DataFileFormat::Parquet) + .partition(Struct::from_iter([value])) + .record_count(123) + .file_size_in_bytes(456) + .build() + .unwrap(); + + let output = + write_and_read_data_file(build, partition_field_type, data_file.clone()).await; + + assert_eq!(output, data_file); + } + } + } + + /// Writes `data_file` to a manifest partitioned by identity on one column + /// of `partition_field_type`, then reads it back with [`ManifestReader`]. + async fn write_and_read_data_file( + build: fn(ManifestWriterBuilder) -> ManifestWriter, + partition_field_type: Type, + data_file: DataFile, + ) -> DataFile { + let schema = Arc::new( + Schema::builder() + .with_fields(vec![ + NestedField::optional(1, "id", partition_field_type).into(), + ]) + .build() + .unwrap(), + ); + let partition_spec = PartitionSpec::builder(schema.clone()) + .with_spec_id(0) + .add_partition_field("id", "id", Transform::Identity) + .unwrap() + .build() + .unwrap(); + let io = FileIO::new_with_memory(); + let mut writer = build(ManifestWriterBuilder::new( + io.new_output("memory:///table/metadata/manifest.avro") + .unwrap(), + Some(1), + schema, + partition_spec, + )); + writer.add_file(data_file, 1).unwrap(); + let manifest_file = writer.write_manifest_file().await.unwrap(); + + let manifest = ManifestReader::new(io).read(&manifest_file).await.unwrap(); + manifest.entries()[0].data_file().clone() + } } diff --git a/crates/iceberg/src/spec/values/serde.rs b/crates/iceberg/src/spec/values/serde.rs index 028c2d796..d7880c1d7 100644 --- a/crates/iceberg/src/spec/values/serde.rs +++ b/crates/iceberg/src/spec/values/serde.rs @@ -19,12 +19,15 @@ pub(crate) mod _serde { use std::collections::HashMap; + 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::Error; use crate::error::invalid_data; @@ -106,6 +109,11 @@ pub(crate) mod _serde { Double(f64), String(String), Bytes(ByteBuf), + // apache-avro accepts only `Value::Uuid` or `Value::Fixed` for the spec's `fixed` + // uuid schema, and a `ByteBuf` serializes to `Value::Bytes` by default. + // `apache_avro::serde::fixed::serialize` makes `apache_avro::to_value` produce + // `Value::Fixed`. Other serializers, such as serde_json, still see plain bytes. + Fixed(#[serde(serialize_with = "apache_avro::serde::fixed::serialize")] ByteBuf), List(List), StringMap(StringMap), Record(Record), @@ -287,9 +295,17 @@ pub(crate) mod _serde { PrimitiveLiteral::Float(v) => RawLiteralEnum::Float(v.0), PrimitiveLiteral::Double(v) => RawLiteralEnum::Double(v.0), PrimitiveLiteral::String(v) => RawLiteralEnum::String(v), + PrimitiveLiteral::UInt128(v) if *ty == Type::Primitive(PrimitiveType::Uuid) => { + RawLiteralEnum::Fixed(ByteBuf::from(v.to_be_bytes())) + } PrimitiveLiteral::UInt128(v) => { RawLiteralEnum::Bytes(ByteBuf::from(v.to_be_bytes())) } + PrimitiveLiteral::Binary(v) + if matches!(ty, Type::Primitive(PrimitiveType::Fixed(_))) => + { + RawLiteralEnum::Fixed(ByteBuf::from(v)) + } PrimitiveLiteral::Binary(v) => RawLiteralEnum::Bytes(ByteBuf::from(v)), PrimitiveLiteral::Int128(v) => { RawLiteralEnum::Bytes(ByteBuf::from(v.to_be_bytes())) @@ -473,8 +489,25 @@ pub(crate) mod _serde { }, RawLiteralEnum::String(v) => match ty { Type::Primitive(PrimitiveType::String) => Ok(Some(Literal::string(v))), + // apache-avro deserializes a UUID as a hyphenated string. + // UUID values read via `Value` (i.e. `read_data_files_from_avro`), land here. + // The hyphenated restriction matches behaviour with Java. + 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 hyphenated 8-4-4-4-12 string", + ) + }), _ => Err(invalid_err("string")), }, + // `Fixed` is only built when writing, so that apache-avro gets a fixed value. + // Avro fixed values are read back as `Bytes`, and uuid values as `String` or `Bytes`. + RawLiteralEnum::Fixed(_) => Err(invalid_err_with_reason( + "fixed", + "RawLiteralEnum::Fixed is only used for serialization", + )), RawLiteralEnum::Bytes(v) => match ty { Type::Primitive(PrimitiveType::Binary) => Ok(Some(Literal::binary(v.to_vec()))), Type::Primitive(PrimitiveType::Fixed(expected_len)) => { @@ -492,17 +525,10 @@ pub(crate) mod _serde { } } Type::Primitive(PrimitiveType::Uuid) => { - if v.len() == 16 { - 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)))) - } else { - Err(invalid_err_with_reason( - "bytes", - "UUID must be exactly 16 bytes", - )) - } + 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::from_bytes(bytes)))) } Type::Primitive(PrimitiveType::Decimal { precision, .. }) => { let required_bytes = Type::decimal_required_bytes(*precision)? as usize; @@ -630,7 +656,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: _, diff --git a/crates/iceberg/src/spec/values/tests.rs b/crates/iceberg/src/spec/values/tests.rs index 67fc82a56..bc2ee9850 100644 --- a/crates/iceberg/src/spec/values/tests.rs +++ b/crates/iceberg/src/spec/values/tests.rs @@ -99,8 +99,9 @@ fn check_convert_with_avro(expected_literal: Literal, expected_type: &Type) { let encoded = writer.into_inner().unwrap(); let reader = apache_avro::Reader::new(&*encoded).unwrap(); - for record in reader { - let result = apache_avro::from_value::<RawLiteral>(&record.unwrap()).unwrap(); + for maybe_record in reader { + let record = maybe_record.unwrap(); + let result = apache_avro::from_value::<RawLiteral>(&record).unwrap(); let desered_literal = result.try_into(&struct_type).unwrap().unwrap(); assert_eq!(desered_literal, struct_literal); } @@ -723,20 +724,54 @@ 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(); + 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) { + let avro_value = Value::Bytes(input_bytes); + 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_bytes_error_via_avro(input_bytes: Vec<u8>, expected_type: &Type) { - use apache_avro::types::Value; +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:?}"); +} - let avro_value = Value::Bytes(input_bytes); - let raw_literal: RawLiteral = apache_avro::from_value(&avro_value).unwrap(); +fn check_raw_literal_serde_via_json( + input: JsonValue, + expected_literal: Literal, + expected_type: &Type, +) { + let raw_literal: RawLiteral = serde_json::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_json(input: JsonValue, expected_type: &Type) { + let raw_literal: RawLiteral = serde_json::from_value(input).unwrap(); let result = raw_literal.try_into(expected_type); assert!(result.is_err(), "Expected error but got: {result:?}"); } @@ -810,6 +845,75 @@ 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_json_string_should_accept_hyphenated_uuid_as_value() { + let s = "a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8"; + check_raw_literal_serde_via_json( + serde_json::json!(s), + Literal::uuid(Uuid::parse_str(s).unwrap()), + &Primitive(PrimitiveType::Uuid), + ); +} + +#[test] +fn test_raw_literal_json_string_should_reject_a_uuid_with_an_invalid_string() { + check_raw_literal_error_via_json( + serde_json::json!("not-a-uuid"), + &Primitive(PrimitiveType::Uuid), + ); +} + +#[test] +fn test_raw_literal_json_string_should_reject_non_hyphenated_uuids() { + check_raw_literal_error_via_json( + serde_json::json!("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8"), + &Primitive(PrimitiveType::Uuid), + ); + + check_raw_literal_error_via_json( + serde_json::json!("urn:uuid:A1A2A3A4-B1B2-C1C2-D1D2-D3D4D5D6D7D8"), + &Primitive(PrimitiveType::Uuid), + ); + + check_raw_literal_error_via_json( + serde_json::json!("{a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8}"), + &Primitive(PrimitiveType::Uuid), + ); +} + #[test] fn test_raw_literal_bytes_decimal_precision_4_scale_2() { // Precision 4 requires 2 bytes @@ -1136,6 +1240,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_original_uuid() { + let uuid = Uuid::parse_str("f79c3e09-677c-4bbd-a479-3f349cb785e7").unwrap(); + + check_serialize_avro( + Literal::uuid(uuid), + &Primitive(PrimitiveType::Uuid), + Value::Uuid(uuid), + ); +} + #[test] fn avro_convert_test_date() { check_convert_with_avro( @@ -1511,6 +1634,192 @@ fn test_datum_ser_deser() { test_fn(datum); } +#[test] +fn test_datum_uuid_converts_to_avro_fixed_value() { + let uuid_value = Uuid::from_u128(0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee); + let output = to_value(Datum::uuid(uuid_value)).unwrap(); + + let expected = Value::Record(vec![ + ("type".to_string(), Value::String("uuid".to_string())), + ( + "literal".to_string(), + Value::Fixed(16, uuid_value.as_bytes().to_vec()), + ), + ]); + assert_eq!(output, expected); +} + +#[test] +fn test_avro_uuid_value_converts_to_datum_uuid() { + let uuid_value = Uuid::from_u128(0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee); + let avro_value = Value::Record(vec![ + ("type".to_string(), Value::String("uuid".to_string())), + ("literal".to_string(), Value::Uuid(uuid_value)), + ]); + + let output = apache_avro::from_value::<Datum>(&avro_value).unwrap(); + + assert_eq!(output, Datum::uuid(uuid_value)); +} + +#[test] +fn test_avro_fixed_16_bytes_value_converts_to_datum_uuid() { + let uuid_value = Uuid::from_u128(0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee); + let avro_value = Value::Record(vec![ + ("type".to_string(), Value::String("uuid".to_string())), + ( + "literal".to_string(), + Value::Fixed(16, uuid_value.as_bytes().to_vec()), + ), + ]); + + let output = apache_avro::from_value::<Datum>(&avro_value).unwrap(); + + assert_eq!(output, Datum::uuid(uuid_value)); +} + +#[test] +fn test_datum_16_fixed_converts_to_avro_fixed_16_bytes_value() { + let bytes = 0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee_u128.to_be_bytes(); + let output = to_value(Datum::fixed(bytes)).unwrap(); + + let expected = Value::Record(vec![ + ("type".to_string(), Value::String("fixed[16]".to_string())), + ("literal".to_string(), Value::Fixed(16, bytes.to_vec())), + ]); + assert_eq!(output, expected); +} + +#[test] +fn test_avro_fixed_value_converts_to_datum_16_fixed() { + let bytes = 0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee_u128.to_be_bytes(); + let avro_value = Value::Record(vec![ + ("type".to_string(), Value::String("fixed[16]".to_string())), + ("literal".to_string(), Value::Fixed(16, bytes.to_vec())), + ]); + + let output = apache_avro::from_value::<Datum>(&avro_value).unwrap(); + + assert_eq!(output, Datum::fixed(bytes)); +} + +#[test] +fn test_datum_16_binary_converts_to_avro_bytes_value() { + let bytes = 0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee_u128.to_be_bytes(); + + let output = to_value(Datum::binary(bytes)).unwrap(); + + let expected = Value::Record(vec![ + ("type".to_string(), Value::String("binary".to_string())), + ("literal".to_string(), Value::Bytes(bytes.to_vec())), + ]); + assert_eq!(output, expected); +} + +#[test] +fn test_avro_bytes_value_converts_to_datum_16_binary() { + let bytes = 0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee_u128.to_be_bytes(); + let avro_value = Value::Record(vec![ + ("type".to_string(), Value::String("binary".to_string())), + ("literal".to_string(), Value::Bytes(bytes.to_vec())), + ]); + + let output = apache_avro::from_value::<Datum>(&avro_value).unwrap(); + + assert_eq!(output, Datum::binary(bytes)); +} + +#[test] +fn test_datum_uuid_converts_to_json_byte_array() { + let uuid_value = Uuid::from_u128(0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee); + + let output = serde_json::to_value(Datum::uuid(uuid_value)).unwrap(); + + let expected = serde_json::json!({ + "type": "uuid", + "literal": uuid_value.as_bytes().to_vec(), + }); + assert_eq!(output, expected); +} + +#[test] +fn test_json_hyphenated_uuid_string_converts_to_datum_uuid() { + let uuid_value = Uuid::from_u128(0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee); + let json_value = serde_json::json!({ + "type": "uuid", + "literal": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", + }); + + let output = serde_json::from_value::<Datum>(json_value).unwrap(); + + assert_eq!(output, Datum::uuid(uuid_value)); +} + +#[test] +fn test_json_16_byte_array_converts_to_datum_uuid() { + let uuid_value = Uuid::from_u128(0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee); + let json_value = serde_json::json!({ + "type": "uuid", + "literal": uuid_value.as_bytes().to_vec(), + }); + + let output = serde_json::from_value::<Datum>(json_value).unwrap(); + + assert_eq!(output, Datum::uuid(uuid_value)); +} + +#[test] +fn test_datum_16_fixed_converts_to_json_byte_array() { + let bytes = 0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee_u128.to_be_bytes(); + + let output = serde_json::to_value(Datum::fixed(bytes)).unwrap(); + + let expected = serde_json::json!({ + "type": "fixed[16]", + "literal": bytes.to_vec(), + }); + assert_eq!(output, expected); +} + +#[test] +fn test_json_16_byte_array_converts_to_datum_16_fixed() { + let bytes = 0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee_u128.to_be_bytes(); + let json_value = serde_json::json!({ + "type": "fixed[16]", + "literal": bytes.to_vec(), + }); + + let output = serde_json::from_value::<Datum>(json_value).unwrap(); + + assert_eq!(output, Datum::fixed(bytes)); +} + +#[test] +fn test_datum_16_binary_converts_to_json_byte_array() { + let bytes = 0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee_u128.to_be_bytes(); + + let output = serde_json::to_value(Datum::binary(bytes)).unwrap(); + + let expected = serde_json::json!({ + "type": "binary", + "literal": bytes.to_vec(), + }); + assert_eq!(output, expected); +} + +#[test] +fn test_json_16_byte_array_converts_to_datum_16_binary() { + let bytes = 0xaaaaaaaa_bbbb_cccc_dddd_eeeeeeeeeeee_u128.to_be_bytes(); + let json_value = serde_json::json!({ + "type": "binary", + "literal": bytes.to_vec(), + }); + + let output = serde_json::from_value::<Datum>(json_value).unwrap(); + + assert_eq!(output, Datum::binary(bytes)); +} + #[test] fn test_datum_date_convert_to_int() { let datum_date = Datum::date(12345);
