Skip to content

Commit 9f95f56

Browse files
committed
added two stage lookup
1 parent 3140be2 commit 9f95f56

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

arrow-pg/src/encoder.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,37 @@ pub fn encode_value<T: Encoder>(
522522
if arr.is_null(idx) {
523523
return encoder.encode_field_with_type_and_format(&None::<i8>, type_, format);
524524
}
525+
526+
macro_rules! get_value_index {
527+
($key_ty:ty) => {
528+
arr.as_any()
529+
.downcast_ref::<DictionaryArray<$key_ty>>()
530+
.map(|dict| dict.keys().value(idx) as usize)
531+
};
532+
}
533+
534+
let value_idx = get_value_index!(Int8Type)
535+
.or_else(|| get_value_index!(Int16Type))
536+
.or_else(|| get_value_index!(Int32Type))
537+
.or_else(|| get_value_index!(Int64Type))
538+
.or_else(|| get_value_index!(UInt8Type))
539+
.or_else(|| get_value_index!(UInt16Type))
540+
.or_else(|| get_value_index!(UInt32Type))
541+
.or_else(|| get_value_index!(UInt64Type))
542+
.ok_or_else(|| {
543+
ToSqlError::from(format!(
544+
"Unsupported dictionary key type"
545+
))
546+
})?;
547+
548+
525549
// Get the dictionary values, ignoring keys
526550
// We'll use Int32Type as a common key type, but we're only interested in values
527551
macro_rules! get_dict_values {
528552
($key_type:ty) => {
529553
arr.as_any()
530554
.downcast_ref::<DictionaryArray<$key_type>>()
531-
.map(|dict| dict.values())
555+
.map(|dict| dict.values().clone())
532556
};
533557
}
534558

@@ -543,16 +567,16 @@ pub fn encode_value<T: Encoder>(
543567
.or_else(|| get_dict_values!(UInt64Type))
544568
.ok_or_else(|| {
545569
ToSqlError::from(format!(
546-
"Unsupported dictionary key type for value type {value_type}"
570+
"Unsupported dictionary key type"
547571
))
548572
})?;
549573

550574
// If the dictionary has only one value, treat it as a primitive
551575
if values.len() == 1 {
552-
encode_value(encoder, values, 0, type_, format)?
576+
encode_value(encoder, &values, 0, type_, format)?
553577
} else {
554578
// Otherwise, use value directly indexed by values array
555-
encode_value(encoder, values, idx, type_, format)?
579+
encode_value(encoder, &values, idx, type_, format)?
556580
}
557581
}
558582
_ => {

0 commit comments

Comments
 (0)