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

feat: Support Map type in Substrait conversions #11129

Merged
merged 2 commits into from
Jun 27, 2024
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
30 changes: 30 additions & 0 deletions datafusion/substrait/src/logical_plan/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,36 @@ fn from_substrait_type(
)?,
}
}
r#type::Kind::Map(map) => {
let key_type = map.key.as_ref().ok_or_else(|| {
substrait_datafusion_err!("Map type must have key type")
})?;
let value_type = map.value.as_ref().ok_or_else(|| {
substrait_datafusion_err!("Map type must have value type")
})?;
let key_field = Arc::new(Field::new(
"key",
from_substrait_type(key_type, dfs_names, name_idx)?,
false,
));
let value_field = Arc::new(Field::new(
"value",
from_substrait_type(value_type, dfs_names, name_idx)?,
true,
));
match map.type_variation_reference {
DEFAULT_CONTAINER_TYPE_VARIATION_REF => {
Ok(DataType::Map(Arc::new(Field::new_struct(
"entries",
[key_field, value_field],
false, // The inner map field is always non-nullable (Arrow #1697),
)), false))
},
v => not_impl_err!(
"Unsupported Substrait type variation {v} of type {s_kind:?}"
)?,
}
}
r#type::Kind::Decimal(d) => match d.type_variation_reference {
DECIMAL_128_TYPE_VARIATION_REF => {
Ok(DataType::Decimal128(d.precision as u8, d.scale as i8))
Expand Down
34 changes: 34 additions & 0 deletions datafusion/substrait/src/logical_plan/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,27 @@ fn to_substrait_type(dt: &DataType, nullable: bool) -> Result<substrait::proto::
}))),
})
}
DataType::Map(inner, _) => match inner.data_type() {
DataType::Struct(key_and_value) if key_and_value.len() == 2 => {
let key_type = to_substrait_type(
key_and_value[0].data_type(),
key_and_value[0].is_nullable(),
)?;
let value_type = to_substrait_type(
key_and_value[1].data_type(),
key_and_value[1].is_nullable(),
)?;
Ok(substrait::proto::Type {
kind: Some(r#type::Kind::Map(Box::new(r#type::Map {
key: Some(Box::new(key_type)),
value: Some(Box::new(value_type)),
type_variation_reference: DEFAULT_CONTAINER_TYPE_VARIATION_REF,
nullability,
}))),
})
}
_ => plan_err!("Map fields must contain a Struct with exactly 2 fields"),
},
DataType::Struct(fields) => {
let field_types = fields
.iter()
Expand Down Expand Up @@ -2316,6 +2337,19 @@ mod test {
Field::new_list_field(DataType::Int32, true).into(),
))?;

round_trip_type(DataType::Map(
Field::new_struct(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can also add a test using Field::new_map: https://docs.rs/arrow/latest/arrow/datatypes/struct.Field.html#method.new_map

we could do it in a follow on PR too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Field::new_map is a bit annoying as it creates a map field directly, rather than the inner field of a map.. would be nice to have something like new_list_field but for map, but Arrow doesn't have it.

Anyways I could replace this block or add another one with

        round_trip_type(
            Field::new_map(
                "map",
                "entries",
                Field::new("key", DataType::Utf8, false).into(),
                Field::new("value", DataType::Int32, true).into(),
                false,
                false,
            )
            .data_type()
            .to_owned(),
        )?;

frankly I don't know if it adds much value, but I'm fine either way - do you have a preference?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't think it is valuable let's not use it

"entries",
[
Field::new("key", DataType::Utf8, false).into(),
Field::new("value", DataType::Int32, true).into(),
],
false,
)
.into(),
false,
))?;

round_trip_type(DataType::Struct(
vec![
Field::new("c0", DataType::Int32, true),
Expand Down