Skip to content

Commit b3f0806

Browse files
edubraqdclaude
andcommitted
review: reject nested-list ORDER BY keys in free RANGE frames
`check_free_range_order_by_types` only runs when the first ORDER BY key has no offset target (Duration, Interval), and it accepted any later key that `extract_window_frame_target_type` accepts. That let `ORDER BY d, l` with `l: List<Struct>` pass planning and fail at execution with "Uncomparable values", whereas on main the Duration key already stopped it in the planner. Reject lists of nested elements in that check so the query keeps failing at planning; the direct `List<Struct>` cases stay as they were on main, for the comparator follow-up. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent 727c003 commit b3f0806

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

‎datafusion/optimizer/src/analyzer/type_coercion.rs‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,16 +1150,35 @@ fn supports_free_range_frame(col_type: &DataType) -> bool {
11501150
}
11511151
}
11521152

1153-
/// Errors if any ORDER BY expression has a type not supported in a free RANGE frame.
1153+
/// Whether `col_type` is a list whose elements are themselves nested
1154+
/// (`List<Struct>`, `List<List<..>>`, ...). Such a key sorts fine, but the
1155+
/// RANGE peer check compares it with `ScalarValue::partial_cmp`, which cannot
1156+
/// compare nested list elements and fails at execution.
1157+
fn is_list_of_nested(col_type: &DataType) -> bool {
1158+
match col_type {
1159+
DataType::List(field)
1160+
| DataType::LargeList(field)
1161+
| DataType::FixedSizeList(field, _) => field.data_type().is_nested(),
1162+
DataType::Dictionary(_, value_type) => is_list_of_nested(value_type),
1163+
DataType::RunEndEncoded(_, value_type) => {
1164+
is_list_of_nested(value_type.data_type())
1165+
}
1166+
_ => false,
1167+
}
1168+
}
1169+
1170+
/// Errors if any ORDER BY expression has a type not supported in a free RANGE
1171+
/// frame: a type with neither an offset target nor a sound peer comparison, or
1172+
/// a list of nested elements (see `is_list_of_nested`).
11541173
fn check_free_range_order_by_types(
11551174
expressions: &[Sort],
11561175
schema: &DFSchema,
11571176
) -> Result<()> {
11581177
for sort in expressions {
11591178
let t = sort.expr.get_type(schema)?;
1160-
if extract_window_frame_target_type(&t).is_none()
1161-
&& !supports_free_range_frame(&t)
1162-
{
1179+
let supported = supports_free_range_frame(&t)
1180+
|| (extract_window_frame_target_type(&t).is_some() && !is_list_of_nested(&t));
1181+
if !supported {
11631182
return plan_err!(
11641183
"RANGE window frames are not supported for ORDER BY type {t}"
11651184
);

‎datafusion/sqllogictest/test_files/window.slt‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7155,9 +7155,17 @@ select count(*) over (order by l) from (values ([struct(1)]), ([struct(2)])) t(l
71557155
query error DataFusion error: Internal error: Uncomparable values: List\(\[\[1\]\]\), List\(\[\[1\]\]\)
71567156
select count(*) over (order by l) from (values ([[1]]), ([[2]])) t(l)
71577157

7158-
# the same list type after a duration key: the duration passes the free-range
7159-
# check, the list is accepted as before, and the execution error is the same
7160-
query error DataFusion error: Internal error: Uncomparable values: List\(\[\{c0: 1\}\]\), List\(\[\{c0: 1\}\]\)
7158+
# after a duration key the free-range check looks at every ORDER BY type: a
7159+
# list of primitives is still fine,
7160+
query I
7161+
select count(*) over (order by d, l) from (values (arrow_cast(1, 'Duration(Second)'), [1]), (arrow_cast(1, 'Duration(Second)'), [2])) t(d, l)
7162+
----
7163+
1
7164+
2
7165+
7166+
# but a list of nested elements is rejected at planning, instead of reaching
7167+
# the execution error above
7168+
query error Error during planning: RANGE window frames are not supported for ORDER BY type List
71617169
select count(*) over (order by d, l) from (values (arrow_cast(1, 'Duration(Second)'), [struct(1)]), (arrow_cast(1, 'Duration(Second)'), [struct(2)])) t(d, l)
71627170

71637171
# Window functions are not allowed in WHERE or HAVING: both are evaluated before

0 commit comments

Comments
 (0)