MCPcopy Create free account
hub / github.com/apache/datafusion / sort_batches

Function sort_batches

datafusion/core/tests/sql/unparser.rs:201–219  ·  view source on GitHub ↗

Sorts record batches by all columns for deterministic comparison. When comparing query results, we need a canonical ordering so that semantically equivalent results compare as equal. This function sorts by all columns in the schema to achieve that.

(
    ctx: &SessionContext,
    batches: Vec<RecordBatch>,
)

Source from the content-addressed store, hash-verified

199/// semantically equivalent results compare as equal. This function sorts
200/// by all columns in the schema to achieve that.
201async fn sort_batches(
202 ctx: &SessionContext,
203 batches: Vec<RecordBatch>,
204) -> Result<Vec<RecordBatch>> {
205 let mut df = ctx.read_batches(batches)?;
206 let schema = df.schema().as_arrow().clone();
207 let sort_exprs = schema
208 .fields()
209 .iter()
210 // Use Column directly, col() causes the column names to be normalized to lowercase
211 .map(|f| {
212 Expr::Column(Column::new_unqualified(f.name().to_string())).sort(true, false)
213 })
214 .collect_vec();
215 if !sort_exprs.is_empty() {
216 df = df.sort(sort_exprs)?;
217 }
218 df.collect().await
219}
220
221/// The outcome of running a single roundtrip test.
222///

Callers 1

collect_resultsFunction · 0.85

Calls 13

read_batchesMethod · 0.80
as_arrowMethod · 0.80
collectMethod · 0.80
ColumnClass · 0.50
cloneMethod · 0.45
schemaMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45
fieldsMethod · 0.45
sortMethod · 0.45
to_stringMethod · 0.45
nameMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…