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
32 changes: 31 additions & 1 deletion 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 Expand Up @@ -808,7 +809,7 @@ mod test {
Decimal, Schema as AvroSchema, from_avro_datum, to_avro_datum, types::Value as AvroValue,
};
use dbsp::algebra::{F32, F64};
use feldera_sqllib::{Date, SqlDecimal, Timestamp};
use feldera_sqllib::{Date, SqlDecimal, Timestamp, TimestampTz};
use feldera_types::{serde_with_context::SerializeWithContext, serialize_table_record};
use num_bigint::BigInt;
use serde::Serialize;
Expand Down Expand Up @@ -880,6 +881,23 @@ mod test {
]
}"#;

#[derive(Serialize)]
struct TestTimestampTz {
ts: TimestampTz,
}

serialize_table_record!(TestTimestampTz[1] {
ts["ts"]: TimestampTz
});

const SCHEMA_TIMESTAMP_TZ: &str = r#"{
"type": "record",
"name": "TimestampTz",
"fields": [
{ "name": "ts", "type": { "type": "long", "logicalType": "timestamp-micros" } }
]
}"#;

macro_rules! serializer_test {
($schema: expr_2021, $record: ident, $avro: ident) => {
let schema = serde_json::Value::from_str($schema).unwrap();
Expand All @@ -900,6 +918,18 @@ mod test {
};
}

#[test]
fn test_avro_serializer_timestamp_tz() {
let micros = 1_713_597_703_000_123;
let record = TestTimestampTz {
ts: TimestampTz::from_microseconds(micros),
};
let expected =
AvroValue::Record(vec![("ts".to_string(), AvroValue::TimestampMicros(micros))]);

serializer_test!(SCHEMA_TIMESTAMP_TZ, record, expected);
}

#[test]
fn test_avro_serializer() {
let record1_1: TestStruct1 = TestStruct1 {
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