adriangb commented on code in PR #27360:
URL: https://github.com/apache/datafusion/pull/27360#discussion_r4673261423
##########
datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes_view.rs:
##########
@@ -451,20 +451,28 @@ impl<B: ByteViewType> ByteViewGroupValueBuilder<B> {
// Unfortunately, some taken views non-inlined
let view = ByteView::from(*view);
let last_remaining_buffer_index = view.buffer_index as usize;
+ let last_take_len = (view.offset + view.length) as usize;
// Check should we take the whole `last_remaining_buffer_index` buffer
- let take_whole_last_buffer = self.should_take_whole_buffer(
- last_remaining_buffer_index,
- (view.offset + view.length) as usize,
- );
+ let take_whole_last_buffer =
+ self.should_take_whole_buffer(last_remaining_buffer_index,
last_take_len);
+
+ // Remaining views all point into `in_progress` past the bytes drained
from it
+ let offset_shift = if !take_whole_last_buffer
+ && last_remaining_buffer_index == self.completed.len()
+ {
+ last_take_len
+ } else {
+ 0
+ };
Review Comment:
This and `shifts` below are two halves of one fix-up of the remaining views,
but they are computed 15 lines apart. `offset_shift` must be computed before
the take, because the take removes entries from `self.completed`. `shifts` can
move up here too. Then one `if` covers all three cases, and the `let shifts =
...` block below can be deleted:
```
taken remaining
┌──────────────┐ ┌────────────────────────────────┐
completed[0..i] in_progress[0..last_take_len] |
in_progress[last_take_len..]
└─ drained ─┘ └─ views here: offset -=
last_take_len
```
```suggestion
// How far the remaining views move once the taken buffers are gone
let (shifts, offset_shift) = if take_whole_last_buffer {
(last_remaining_buffer_index + 1, 0)
} else if last_remaining_buffer_index == self.completed.len() {
// The take drains the start of `in_progress`, and every
remaining
// view points into `in_progress` past the drained bytes
(last_remaining_buffer_index, last_take_len)
} else {
(last_remaining_buffer_index, 0)
};
```
##########
datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes_view.rs:
##########
@@ -479,6 +487,7 @@ impl<B: ByteViewType> ByteViewGroupValueBuilder<B> {
if (*view as u32) > 12 {
let mut byte_view = ByteView::from(*view);
byte_view.buffer_index -= shifts as u32;
+ byte_view.offset -= offset_shift as u32;
Review Comment:
The code is right only if every remaining non-inline view points past the
drained bytes. A debug assert makes that rule explicit at almost no cost, since
the loop already visits every view. In release builds, a broken rule here would
wrap silently, and the array is built with `new_unchecked`.
```suggestion
debug_assert!(byte_view.offset as usize >= offset_shift);
byte_view.offset -= offset_shift as u32;
```
##########
datafusion/sqllogictest/test_files/aggregate_utf8view_partial_emit.slt:
##########
@@ -0,0 +1,78 @@
+# SPDX-License-Identifier: Apache-2.0
+# SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+# SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under
one or more contributor license agreements
+
+# Reproducer from https://github.com/apache/datafusion/issues/27323
+#
+# GROUP BY k, s on a file sorted by k, where s is Utf8View. The partial
+# aggregate emits a small batch every time k changes. Each batch must hold
+# only its own strings, or the order-preserving repartition runs out of
+# memory under the limits below.
+
+statement ok
+SET datafusion.execution.target_partitions = 8
+
+query I
+COPY (
+ SELECT value % 1000 AS k, repeat('x', 100) || (value % 7)::varchar AS s,
value AS v
+ FROM generate_series(1, 500000)
Review Comment:
With the `repartition_file_min_size` setting above:
```suggestion
FROM generate_series(1, 200000)
```
(The `500000` count on the line under `COPY` changes too.)
##########
datafusion/sqllogictest/test_files/aggregate_utf8view_partial_emit.slt:
##########
@@ -0,0 +1,78 @@
+# SPDX-License-Identifier: Apache-2.0
+# SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+# SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under
one or more contributor license agreements
+
+# Reproducer from https://github.com/apache/datafusion/issues/27323
+#
+# GROUP BY k, s on a file sorted by k, where s is Utf8View. The partial
+# aggregate emits a small batch every time k changes. Each batch must hold
+# only its own strings, or the order-preserving repartition runs out of
+# memory under the limits below.
+
+statement ok
+SET datafusion.execution.target_partitions = 8
Review Comment:
Following up on the earlier question about runtime. I measured locally
(debug build): this file takes about 7 s at 500k rows. The row count mostly
controls whether the file gets split into 8 scan partitions, not the memory
pressure:
| Rows | main | this PR |
|---|---|---|
| 500k | both queries fail (ResourcesExhausted) | pass, 7.1 s |
| 400k | both queries fail | |
| ≤300k | the plan changes to `DataSourceExec` 1 group +
`RoundRobinBatch(8)`, and both queries pass, so no repro | |
Forcing the split brings the repro down to 200k rows on main, and the PR
passes in about 1.7 s:
```suggestion
SET datafusion.execution.target_partitions = 8
# Split even a small file into 8 scan partitions, as in the original report
statement ok
SET datafusion.optimizer.repartition_file_min_size = 1024
```
At 100k rows, main failed once and passed once, so I would not go below
200k. It is worth checking that this still fails on main over a few runs.
##########
datafusion/sqllogictest/test_files/aggregate_utf8view_partial_emit.slt:
##########
@@ -0,0 +1,78 @@
+# SPDX-License-Identifier: Apache-2.0
+# SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+# SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under
one or more contributor license agreements
+
+# Reproducer from https://github.com/apache/datafusion/issues/27323
+#
+# GROUP BY k, s on a file sorted by k, where s is Utf8View. The partial
+# aggregate emits a small batch every time k changes. Each batch must hold
+# only its own strings, or the order-preserving repartition runs out of
+# memory under the limits below.
+
+statement ok
+SET datafusion.execution.target_partitions = 8
+
+query I
+COPY (
+ SELECT value % 1000 AS k, repeat('x', 100) || (value % 7)::varchar AS s,
value AS v
+ FROM generate_series(1, 500000)
+ ORDER BY k
+)
+TO 'test_files/scratch/aggregate_utf8view_partial_emit/data.parquet'
+STORED AS PARQUET;
+----
+500000
+
+statement ok
+CREATE EXTERNAL TABLE t
+STORED AS PARQUET
+LOCATION 'test_files/scratch/aggregate_utf8view_partial_emit/data.parquet'
+WITH ORDER (k ASC);
+
+# Make sure we still get the streaming plan (Partial([0]) + preserve_order)
+query TT
+EXPLAIN SELECT k, s, count(*) FROM t GROUP BY k, s ORDER BY k
+----
+logical_plan
+01)Sort: t.k ASC NULLS LAST
+02)--Projection: t.k, t.s, count(Int64(1)) AS count(*)
+03)----Aggregate: groupBy=[[t.k, t.s]], aggr=[[count(Int64(1))]]
+04)------TableScan: t projection=[k, s]
+physical_plan
+01)SortPreservingMergeExec: [k@0 ASC NULLS LAST]
+02)--ProjectionExec: expr=[k@0 as k, s@1 as s, count(Int64(1))@2 as count(*)]
+03)----AggregateExec: mode=FinalPartitioned, gby=[k@0 as k, s@1 as s],
aggr=[count(Int64(1))], group_clustering_mode=Partial([0])
+04)------RepartitionExec: partitioning=Hash([k@0, s@1], 8),
input_partitions=8, preserve_order=true, sort_exprs=k@0 ASC NULLS LAST
+05)--------AggregateExec: mode=Partial, gby=[k@0 as k, s@1 as s],
aggr=[count(Int64(1))], group_clustering_mode=Partial([0])
+06)----------DataSourceExec: file_groups={8 groups: <slt:ignore>
+
+# Well above what 7000 result rows need
+statement ok
+SET datafusion.runtime.memory_limit = '50M'
Review Comment:
Do we need both the 50M and the 20M query? In my runs on main, they always
failed together at 500k and 400k rows, and passed together at 300k and below.
If one limit is enough, keeping only 20M cuts about a third of the runtime.
--
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]