Skip to content

Commit 1c6b0d4

Browse files
authored
Support Dictionary types as they're logically equivalent to their value types. Needed for delta-rs partition columns. (#65)
1 parent 694a187 commit 1c6b0d4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

datafusion-postgres/src/datatypes.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub(crate) fn into_pg_type(df_type: &DataType) -> PgWireResult<Type> {
7272
}
7373
}
7474
DataType::Utf8View => Type::TEXT,
75+
DataType::Dictionary(_, value_type) => into_pg_type(value_type)?,
7576
_ => {
7677
return Err(PgWireError::UserError(Box::new(ErrorInfo::new(
7778
"ERROR".to_owned(),
@@ -609,6 +610,45 @@ fn encode_value(
609610
}
610611
}
611612
}
613+
DataType::Dictionary(_, value_type) => {
614+
// Get the dictionary values, ignoring keys
615+
// We'll use Int32Type as a common key type, but we're only interested in values
616+
macro_rules! get_dict_values {
617+
($key_type:ty) => {
618+
arr.as_any()
619+
.downcast_ref::<DictionaryArray<$key_type>>()
620+
.map(|dict| dict.values())
621+
};
622+
}
623+
624+
// Try to extract values using different key types
625+
let values = get_dict_values!(Int8Type)
626+
.or_else(|| get_dict_values!(Int16Type))
627+
.or_else(|| get_dict_values!(Int32Type))
628+
.or_else(|| get_dict_values!(Int64Type))
629+
.or_else(|| get_dict_values!(UInt8Type))
630+
.or_else(|| get_dict_values!(UInt16Type))
631+
.or_else(|| get_dict_values!(UInt32Type))
632+
.or_else(|| get_dict_values!(UInt64Type))
633+
.ok_or_else(|| {
634+
PgWireError::UserError(Box::new(ErrorInfo::new(
635+
"ERROR".to_owned(),
636+
"XX000".to_owned(),
637+
format!(
638+
"Unsupported dictionary key type for value type {}",
639+
value_type
640+
),
641+
)))
642+
})?;
643+
644+
// If the dictionary has only one value, treat it as a primitive
645+
if values.len() == 1 {
646+
encode_value(encoder, values, 0)?
647+
} else {
648+
// Otherwise, use value directly indexed by values array
649+
encode_value(encoder, values, idx)?
650+
}
651+
}
612652
_ => {
613653
return Err(PgWireError::UserError(Box::new(ErrorInfo::new(
614654
"ERROR".to_owned(),

0 commit comments

Comments
 (0)