risingwave_connector/parser/unified/
kv_event.rsuse risingwave_common::types::{DataType, DatumCow};
use risingwave_pb::plan_common::additional_column::ColumnType as AdditionalColumnType;
use super::{Access, AccessResult};
use crate::parser::unified::AccessError;
use crate::source::SourceColumnDesc;
pub struct KvEvent<K, V> {
key_accessor: Option<K>,
value_accessor: Option<V>,
}
impl<K, V> Default for KvEvent<K, V> {
fn default() -> Self {
Self {
key_accessor: None,
value_accessor: None,
}
}
}
impl<K, V> KvEvent<K, V> {
pub fn with_key(&mut self, key: K)
where
K: Access,
{
self.key_accessor = Some(key);
}
pub fn with_value(&mut self, value: V)
where
V: Access,
{
self.value_accessor = Some(value);
}
}
impl<K, V> KvEvent<K, V>
where
K: Access,
V: Access,
{
fn access_key(&self, path: &[&str], type_expected: &DataType) -> AccessResult<DatumCow<'_>> {
if let Some(ka) = &self.key_accessor {
ka.access(path, type_expected)
} else {
Err(AccessError::Undefined {
name: "key".to_string(),
path: String::new(),
})
}
}
fn access_value(&self, path: &[&str], type_expected: &DataType) -> AccessResult<DatumCow<'_>> {
if let Some(va) = &self.value_accessor {
va.access(path, type_expected)
} else {
Err(AccessError::Undefined {
name: "value".to_string(),
path: String::new(),
})
}
}
pub fn access_field<const KEY_ONLY: bool>(
&self,
desc: &SourceColumnDesc,
) -> AccessResult<DatumCow<'_>> {
match (&desc.additional_column.column_type, KEY_ONLY) {
(Some(AdditionalColumnType::Key(_)), _) => {
self.access_key(&[&desc.name], &desc.data_type)
}
(Some(AdditionalColumnType::Payload(_)), _) => self.access_value(&[], &desc.data_type),
(None, false) => self.access_value(&[&desc.name], &desc.data_type),
(_, true) => Ok(DatumCow::Owned(None)),
_ => unreachable!(),
}
}
}