alamb commented on code in PR #15354: URL: https://github.com/apache/datafusion/pull/15354#discussion_r2008786953
########## datafusion/functions-nested/src/array_has.rs: ########## @@ -121,6 +123,43 @@ impl ScalarUDFImpl for ArrayHas { Ok(DataType::Boolean) } + fn simplify( + &self, + mut args: Vec<Expr>, + _info: &dyn datafusion_expr::simplify::SimplifyInfo, + ) -> Result<ExprSimplifyResult> { + let [haystack, _needle] = take_function_args(self.name(), &args)?; + + // if the haystack is a constant list, we can use an inlist expression which is more + // efficient because the haystack is not varying per-row + if let Expr::Literal(ScalarValue::List(array)) = haystack { + assert_eq!(array.len(), 1); // guarantee of ScalarValue + if let Ok(scalar_values) = + ScalarValue::convert_array_to_scalar_vec(array.as_ref()) + { + assert_eq!(scalar_values.len(), 1); + let list = scalar_values + .into_iter() + .flatten() + .map(Expr::Literal) + .collect(); + + // ok to pop here, we will not use args again + let needle = args.pop().unwrap(); + return Ok(ExprSimplifyResult::Simplified(Expr::InList(InList { + expr: Box::new(needle), + list, + negated: false, + }))); + } + } + + // TODO: support LargeList / FixedSizeList? + // (not supported by `convert_array_to_scalar_vec`) Review Comment: I suggest doing the work in a follow on ticket / PR. So that would mean: 1. Filing a ticket with a reproducer (perhaps modified from https://github.com/apache/datafusion/issues/14533) 2. leaving a TODO with a link to that ticket here -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org