Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata field to Kafka source #822

Merged
merged 1 commit into from
Jan 15, 2025
Merged
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
4 changes: 4 additions & 0 deletions crates/arroyo-connectors/src/kafka/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ impl Connector for KafkaConnector {
name: "timestamp",
data_type: DataType::Int64,
},
MetadataDef {
name: "key",
data_type: DataType::Binary,
},
]
}

Expand Down
9 changes: 5 additions & 4 deletions crates/arroyo-connectors/src/kafka/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,11 @@ impl KafkaSourceFunc {
let mut connector_metadata = HashMap::new();
for f in &self.metadata_fields {
connector_metadata.insert(f.field_name.as_str(), match f.key.as_str() {
"offset_id" => FieldValueType::Int64(msg.offset()),
"partition" => FieldValueType::Int32(msg.partition()),
"topic" => FieldValueType::String(topic),
"timestamp" => FieldValueType::Int64(timestamp),
"key" => FieldValueType::Bytes(msg.key()),
"offset_id" => FieldValueType::Int64(Some(msg.offset())),
"partition" => FieldValueType::Int32(Some(msg.partition())),
"topic" => FieldValueType::String(Some(topic)),
"timestamp" => FieldValueType::Int64(Some(timestamp)),
k => unreachable!("Invalid metadata key '{}'", k),
});
}
Expand Down
2 changes: 1 addition & 1 deletion crates/arroyo-connectors/src/mqtt/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl MqttSourceFunc {
let mut connector_metadata = HashMap::new();
for mf in &self.metadata_fields {
connector_metadata.insert(mf.field_name.as_str(), match mf.key.as_str() {
"topic" => FieldValueType::String(&topic),
"topic" => FieldValueType::String(Some(&topic)),
k => unreachable!("invalid metadata key '{}' for mqtt", k)
});
}
Expand Down
7 changes: 5 additions & 2 deletions crates/arroyo-connectors/src/redis/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ impl LookupConnector for RedisLookup {
let mut additional = HashMap::new();

for (idx, (v, k)) in vs.iter().zip(keys).enumerate() {
additional.insert(LOOKUP_KEY_INDEX_FIELD, FieldValueType::UInt64(idx as u64));
additional.insert(
LOOKUP_KEY_INDEX_FIELD,
FieldValueType::UInt64(Some(idx as u64)),
);
for m in &self.metadata_fields {
additional.insert(
m.field_name.as_str(),
match m.key.as_str() {
"key" => FieldValueType::String(k.unwrap()),
"key" => FieldValueType::String(Some(k.unwrap())),
k => unreachable!("Invalid metadata key '{}'", k),
},
);
Expand Down
83 changes: 41 additions & 42 deletions crates/arroyo-formats/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use std::sync::Arc;
use std::time::{Instant, SystemTime};
use tokio::sync::Mutex;

#[derive(Debug, Clone)]
#[derive(Debug, Copy, Clone)]
pub enum FieldValueType<'a> {
Int64(i64),
UInt64(u64),
Int32(i32),
String(&'a str),
// Extend with more types as needed
Int64(Option<i64>),
UInt64(Option<u64>),
Int32(Option<i32>),
String(Option<&'a str>),
Bytes(Option<&'a [u8]>),
}

struct ContextBuffer {
Expand Down Expand Up @@ -480,6 +480,7 @@ impl ArrowDeserializer {
FieldValueType::Int64(_) => Box::new(Int64Builder::new()),
FieldValueType::UInt64(_) => Box::new(UInt64Builder::new()),
FieldValueType::String(_) => Box::new(StringBuilder::new()),
FieldValueType::Bytes(_) => Box::new(BinaryBuilder::new()),
};
builders.insert(key.to_string(), builder);
}
Expand All @@ -489,7 +490,7 @@ impl ArrowDeserializer {
let builders = self.additional_fields_builder.as_mut().unwrap();

for (k, v) in additional_fields {
add_additional_fields(builders, k, v, count);
add_additional_fields(builders, k, *v, count);
}
}
}
Expand Down Expand Up @@ -674,52 +675,50 @@ impl ArrowDeserializer {
}
}

macro_rules! append_repeated_value {
($builder:expr, $builder_ty:ty, $value:expr, $count:expr) => {{
let b = $builder
.downcast_mut::<$builder_ty>()
.expect("additional field has incorrect type");

if let Some(v) = $value {
for _ in 0..$count {
b.append_value(v);
}
} else {
for _ in 0..$count {
b.append_null();
}
}
}};
}

fn add_additional_fields(
builders: &mut HashMap<String, Box<dyn ArrayBuilder>>,
key: &str,
value: &FieldValueType<'_>,
value: FieldValueType<'_>,
count: usize,
) {
let builder = builders
.get_mut(key)
.unwrap_or_else(|| panic!("unexpected additional field '{}'", key))
.as_any_mut();
match value {
FieldValueType::Int32(i) => {
let b = builder
.downcast_mut::<Int32Builder>()
.expect("additional field has incorrect type");

for _ in 0..count {
b.append_value(*i);
}
match value {
FieldValueType::Int32(v) => {
append_repeated_value!(builder, Int32Builder, v, count);
}
FieldValueType::Int64(i) => {
let b = builder
.downcast_mut::<Int64Builder>()
.expect("additional field has incorrect type");

for _ in 0..count {
b.append_value(*i);
}
FieldValueType::Int64(v) => {
append_repeated_value!(builder, Int64Builder, v, count);
}
FieldValueType::UInt64(i) => {
let b = builder
.downcast_mut::<UInt64Builder>()
.expect("additional field has incorrect type");

for _ in 0..count {
b.append_value(*i);
}
FieldValueType::UInt64(v) => {
append_repeated_value!(builder, UInt64Builder, v, count);
}
FieldValueType::String(s) => {
let b = builder
.downcast_mut::<StringBuilder>()
.expect("additional field has incorrect type");

for _ in 0..count {
b.append_value(*s);
}
FieldValueType::String(v) => {
append_repeated_value!(builder, StringBuilder, v, count);
}
FieldValueType::Bytes(v) => {
append_repeated_value!(builder, BinaryBuilder, v, count);
}
}
}
Expand Down Expand Up @@ -987,8 +986,8 @@ mod tests {

let time = SystemTime::now();
let mut additional_fields = std::collections::HashMap::new();
additional_fields.insert("y", FieldValueType::Int32(5));
additional_fields.insert("z", FieldValueType::String("hello"));
additional_fields.insert("y", FieldValueType::Int32(Some(5)));
additional_fields.insert("z", FieldValueType::String(Some("hello")));

let result = deserializer
.deserialize_slice(
Expand Down
Loading