Skip to content

Commit

Permalink
Enable non-literal k in MAP[k], in get_field UDF.
Browse files Browse the repository at this point in the history
Requiring k to be a literal should only apply to STRUCT.k
With MAP[k] it is an excessive typechecking restriction,
compared to what the operation is supposed to do.
  • Loading branch information
vgapeyev authored and findepi committed Oct 7, 2024
1 parent a0089bf commit 85dbc97
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions datafusion/functions/src/core/getfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,8 @@ impl ScalarUDFImpl for GetFieldFunc {
}

let name = match &args[1] {
Expr::Literal(name) => name,
_ => {
return exec_err!(
"get_field function requires the argument field_name to be a string"
);
}
Expr::Literal(name) => name.to_string(),
other => other.display_name()?,
};

Ok(format!("{}[{}]", args[0].display_name()?, name))
Expand All @@ -98,16 +94,9 @@ impl ScalarUDFImpl for GetFieldFunc {
);
}

let name = match &args[1] {
Expr::Literal(name) => name,
_ => {
return exec_err!(
"get_field function requires the argument field_name to be a string"
);
}
};
let key = &args[1];
let data_type = args[0].get_type(schema)?;
match (data_type, name) {
match (data_type, key) {
(DataType::Map(fields, _), _) => {
match fields.data_type() {
DataType::Struct(fields) if fields.len() == 2 => {
Expand All @@ -121,7 +110,7 @@ impl ScalarUDFImpl for GetFieldFunc {
_ => plan_err!("Map fields must contain a Struct with exactly 2 fields"),
}
}
(DataType::Struct(fields), ScalarValue::Utf8(Some(s))) => {
(DataType::Struct(fields), Expr::Literal(ScalarValue::Utf8(Some(s)))) => {
if s.is_empty() {
plan_err!(
"Struct based indexed access requires a non empty string"
Expand Down

0 comments on commit 85dbc97

Please sign in to comment.