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
1 change: 1 addition & 0 deletions crates/adapters/src/format/avro/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::schema::{OptionalField, schema_unwrap_optional};
pub fn avro_ser_config() -> SqlSerdeConfig {
SqlSerdeConfig::default()
.with_timestamp_format(TimestampFormat::MicrosSinceEpoch)
.with_timestamp_tz_format(TimestampFormat::MicrosSinceEpoch)
.with_time_format(TimeFormat::Micros)
.with_date_format(DateFormat::DaysSinceEpoch)
.with_decimal_format(DecimalFormat::I128)
Expand Down
173 changes: 172 additions & 1 deletion crates/adapters/src/format/avro/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use dbsp::trace::BatchReaderFactories;
use dbsp::typed_batch::{DynSpineSnapshot, SpineSnapshot as TypedSpineSnapshot, TypedBatch};
use dbsp::{DBData, IndexedZSetReader, OrdIndexedZSet, OrdZSet, ZWeight, utils::Tup2};
use feldera_adapterlib::transport::OutputBatchType;
use feldera_sqllib::{ByteArray, Uuid, Variant};
use feldera_sqllib::{ByteArray, TimestampTz, Uuid, Variant};
use feldera_types::{
deserialize_table_record,
format::avro::{AvroEncoderConfig, AvroEncoderKeyMode},
Expand Down Expand Up @@ -1063,6 +1063,177 @@ fn test_ms_time() {
run_parser_test(vec![test]);
}

#[derive(
Debug,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Hash,
SizeOf,
rkyv::Archive,
rkyv::Serialize,
rkyv::Deserialize,
)]
#[archive_attr(derive(Ord, Eq, PartialEq, PartialOrd))]
struct TestTimestampTz {
id: i64,
ts_tz: TimestampTz,
ts_tz_nullable: Option<TimestampTz>,
}

impl TestTimestampTz {
pub fn avro_schema_micros() -> &'static str {
r#"{
"type": "record",
"name": "TestTimestampTz",
"fields": [
{ "name": "id", "type": "long" },
{ "name": "ts_tz", "type": "long", "logicalType": "timestamp-micros" },
{ "name": "ts_tz_nullable", "type": ["null", { "type": "long", "logicalType": "timestamp-micros" }] }
]
}"#
}

pub fn avro_schema_millis() -> &'static str {
r#"{
"type": "record",
"name": "TestTimestampTz",
"fields": [
{ "name": "id", "type": "long" },
{ "name": "ts_tz", "type": "long", "logicalType": "timestamp-millis" },
{ "name": "ts_tz_nullable", "type": ["null", { "type": "long", "logicalType": "timestamp-millis" }] }
]
}"#
}

pub fn relation_schema() -> Relation {
Relation {
name: SqlIdentifier::new("TestTimestampTz", false),
fields: vec![
Field::new("id".into(), ColumnType::bigint(false)),
Field::new("ts_tz".into(), ColumnType::timestamp_tz(false)),
Field::new("ts_tz_nullable".into(), ColumnType::timestamp_tz(true)),
],
materialized: false,
properties: BTreeMap::new(),
primary_key: None,
}
}

/// Whole-millisecond values, so that both the `timestamp-micros` and the
/// `timestamp-millis` encodings round-trip exactly.
pub fn data() -> Vec<Self> {
vec![
TestTimestampTz {
id: 1,
ts_tz: TimestampTz::from_milliseconds(1_713_597_703_123),
ts_tz_nullable: Some(TimestampTz::from_milliseconds(1_713_597_704_456)),
},
TestTimestampTz {
id: 2,
ts_tz: TimestampTz::from_milliseconds(0),
ts_tz_nullable: None,
},
]
}
}

serialize_table_record!(TestTimestampTz[3]{
r#id["id"]: i64,
r#ts_tz["ts_tz"]: TimestampTz,
r#ts_tz_nullable["ts_tz_nullable"]: Option<TimestampTz>
});

deserialize_table_record!(TestTimestampTz["TestTimestampTz", Variant, 3] {
(r#id, "id", false, i64, |_| None),
(r#ts_tz, "ts_tz", false, TimestampTz, |_| None),
(r#ts_tz_nullable, "ts_tz_nullable", false, Option<TimestampTz>, |_| Some(None))
});

/// TIMESTAMP WITH TIME ZONE columns encoded as `timestamp-micros`.
#[test]
fn test_timestamp_tz() {
let mut data = TestTimestampTz::data();
// Sub-millisecond precision survives the micros encoding.
data.push(TestTimestampTz {
id: 3,
ts_tz: TimestampTz::from_microseconds(1_713_597_703_123_456),
ts_tz_nullable: Some(TimestampTz::from_microseconds(1_713_597_704_654_321)),
});

let test_case = gen_raw_parser_test(
&data,
&TestTimestampTz::relation_schema(),
TestTimestampTz::avro_schema_micros(),
);

run_parser_test(vec![test_case]);
}

/// TIMESTAMP WITH TIME ZONE columns encoded as `timestamp-millis`.
#[test]
fn test_timestamp_tz_millis() {
let test_case = gen_raw_parser_test(
&TestTimestampTz::data(),
&TestTimestampTz::relation_schema(),
TestTimestampTz::avro_schema_millis(),
);

run_parser_test(vec![test_case]);
}

/// Decode hand-built Avro values with hard-coded expected results.
#[test]
fn test_timestamp_tz_wire_format() {
fn test_case(
avro_schema_str: &str,
ts_tz: Value,
expected: TimestampTz,
) -> TestCase<TestTimestampTz> {
let schema = AvroSchema::parse_str(avro_schema_str).unwrap();
let record = Value::Record(vec![
("id".to_string(), Value::Long(1)),
("ts_tz".to_string(), ts_tz),
(
"ts_tz_nullable".to_string(),
Value::Union(0, Box::new(Value::Null)),
),
]);

TestCase {
relation_schema: TestTimestampTz::relation_schema(),
config: AvroParserConfig {
update_format: AvroUpdateFormat::Raw,
schema: Some(avro_schema_str.to_string()),
skip_schema_id: false,
registry_config: Default::default(),
},
input_batches: vec![(serialize_value(record, &schema), vec![])],
expected_output: vec![MockUpdate::Insert(TestTimestampTz {
id: 1,
ts_tz: expected,
ts_tz_nullable: None,
})],
}
}

run_parser_test(vec![
test_case(
TestTimestampTz::avro_schema_millis(),
Value::TimestampMillis(1_713_597_703_123),
TimestampTz::from_milliseconds(1_713_597_703_123),
),
test_case(
TestTimestampTz::avro_schema_micros(),
Value::TimestampMicros(1_713_597_703_123_456),
TimestampTz::from_microseconds(1_713_597_703_123_456),
),
]);
}

/// Type used to serialize different integer types as Avro `int`.
#[derive(
Debug,
Expand Down
5 changes: 5 additions & 0 deletions crates/feldera-types/src/serde_with_context/serde_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ impl SqlSerdeConfig {
self
}

pub fn with_timestamp_tz_format(mut self, timestamp_tz_format: TimestampFormat) -> Self {
self.timestamp_tz_format = timestamp_tz_format;
self
}

pub fn with_decimal_format(mut self, decimal_format: DecimalFormat) -> Self {
self.decimal_format = decimal_format;
self
Expand Down
6 changes: 4 additions & 2 deletions python/tests/workloads/test_kafka_avro.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ def sql_source_table(v: Variant) -> str:
create table {v.source} (
id int,
str varchar,
dec decimal,
dec decimal(10, 2),
reall real,
dbl double,
booll boolean,
tmestmp timestamp,
tmestmptz timestamp with time zone,
datee date,
tme time
) with (
Expand Down Expand Up @@ -260,11 +261,12 @@ def sql_loopback_table(v: Variant) -> str:
create table {v.loopback} (
id int,
str varchar,
dec decimal,
dec decimal(10, 2),
reall real,
dbl double,
booll boolean,
tmestmp timestamp,
tmestmptz timestamp with time zone,
datee date,
tme time
) with (
Expand Down