jayzhan211 commented on code in PR #25874:
URL: https://github.com/apache/datafusion/pull/25874#discussion_r4370331709
##########
datafusion/functions/src/unicode/substr.rs:
##########
@@ -112,10 +123,43 @@ impl ScalarUDFImpl for SubstrFunc {
}
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
- Ok(arg_types[0].clone())
+ Ok(match &arg_types[0] {
+ DataType::Dictionary(key_type, value_type) if
!is_wide_key(key_type) => {
+ value_type.as_ref().clone()
+ }
+ other => other.clone(),
+ })
}
fn invoke_with_args(&self, args: ScalarFunctionArgs) ->
Result<ColumnarValue> {
+ if let DataType::Dictionary(key_type, value_type) =
args.args[0].data_type()
+ && !is_wide_key(&key_type)
+ {
+ // Per-row start/length can produce more distinct values than a
+ // narrow key type can index, so decode instead.
+ let mut args = args.args;
+ args[0] = args[0].cast_to(&value_type, None)?;
+ return make_scalar_function(substr, vec![])(&args);
+ }
+
+ if let ColumnarValue::Array(array) = &args.args[0]
+ && matches!(array.data_type(), DataType::Dictionary(_, _))
+ && array.as_any_dictionary().values().len() <= array.len()
+ && args.args[1..]
+ .iter()
+ .all(|v| matches!(v, ColumnarValue::Scalar(_)))
+ {
+ // Fast path: substr the N_distinct values, reuse the keys.
+ let dict = array.as_any_dictionary();
+ let values = Arc::clone(dict.values());
+ let mut inner = Vec::with_capacity(args.args.len());
+ inner.push(ColumnarValue::Array(values));
+ inner.extend_from_slice(&args.args[1..]);
+ let new_values = make_scalar_function(substr, vec![])(&inner)?
Review Comment:
Thanks for adding the guard, Q22 looks good now. There's one more edge case:
this pass runs `substr` on every dictionary value, including values that no
valid row references. So an invalid scalar like a negative length errors here,
while main returns NULL. This was already in the first version of the fast
path, and I missed it in earlier rounds, sorry about that.
```sql
create table p as select arrow_cast(c, 'Dictionary(Int32, Utf8)') c from
(values ('a'), (NULL), (NULL)) t(c);
select substr(c, 1, -1) from p where c is null;
-- main: NULL, NULL
-- this PR: Execution error: negative count not allowed: -1
```
Falling back to the row-wise path when the values pass fails fixes it, since
that path only errors on rows that are actually referenced:
```diff
- let new_values = make_scalar_function(substr, vec![])(&inner)?
- .into_array(dict.values().len())?;
- return Ok(ColumnarValue::Array(dict.with_values(new_values)));
+ // Values that no valid row references can still fail (e.g. a
+ // negative length), so fall back to the row-wise path on error.
+ if let Ok(new_values) = make_scalar_function(substr,
vec![])(&inner) {
+ let new_values =
new_values.into_array(dict.values().len())?;
+ return
Ok(ColumnarValue::Array(dict.with_values(new_values)));
+ }
```
The repro above would make a good case for `dictionary_utf8.slt`. With this
change I don't have anything else on this PR.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]