Skip to content

Apache DataFusion SQL Query Engine

Implement `CardinalityAwareRowConverter` while doing streaming merge (#7401)

Jayjeet Chakrabortyf4c4ee1e7ffa
diff --git a/datafusion-cli/Cargo.lock b/datafusion-cli/Cargo.lock
index d7d1357a65..5b613cc2b7 100644
--- a/datafusion-cli/Cargo.lock
+++ b/datafusion-cli/Cargo.lock
@@ -347,7 +347,7 @@ checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.33",
+ "syn 2.0.37",
]
[[package]]
@@ -1031,7 +1031,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f34ba9a9bcb8645379e9de8cb3ecfcf4d1c85ba66d90deb3259206fa5aa193b"
dependencies = [
"quote",
- "syn 2.0.33",
+ "syn 2.0.37",
]
[[package]]
@@ -1248,6 +1248,7 @@ dependencies = [
"tempfile",
"termtree",
"tokio",
+ "uuid",
]
[[package]]
@@ -1535,7 +1536,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.33",
+ "syn 2.0.37",
]
[[package]]
@@ -2421,7 +2422,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.33",
+ "syn 2.0.37",
]
[[package]]
@@ -2748,7 +2749,7 @@ dependencies = [
"regex",
"relative-path",
"rustc_version",
- "syn 2.0.33",
+ "syn 2.0.37",
"unicode-ident",
]
@@ -2956,7 +2957,7 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.33",
+ "syn 2.0.37",
]
[[package]]
@@ -3145,7 +3146,7 @@ dependencies = [
"proc-macro2",
"quote",
"rustversion",
- "syn 2.0.33",
+ "syn 2.0.37",
]
[[package]]
@@ -3167,9 +3168,9 @@ dependencies = [
[[package]]
name = "syn"
-version = "2.0.33"
+version = "2.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9caece70c63bfba29ec2fed841a09851b14a235c60010fa4de58089b6c025668"
+checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8"
dependencies = [
"proc-macro2",
"quote",
@@ -3227,7 +3228,7 @@ checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.33",
+ "syn 2.0.37",
]
[[package]]
@@ -3318,7 +3319,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.33",
+ "syn 2.0.37",
]
[[package]]
@@ -3416,7 +3417,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.33",
+ "syn 2.0.37",
]
[[package]]
@@ -3588,7 +3589,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
- "syn 2.0.33",
+ "syn 2.0.37",
"wasm-bindgen-shared",
]
@@ -3622,7 +3623,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.33",
+ "syn 2.0.37",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
diff --git a/datafusion/core/tests/memory_limit.rs b/datafusion/core/tests/memory_limit.rs
index 12cde86600..3367a92302 100644
--- a/datafusion/core/tests/memory_limit.rs
+++ b/datafusion/core/tests/memory_limit.rs
@@ -200,7 +200,8 @@ async fn symmetric_hash_join() {
#[tokio::test]
async fn sort_preserving_merge() {
- let partition_size = batches_byte_size(&dict_batches());
+ let scenario = Scenario::new_dictionary_strings(2);
+ let partition_size = scenario.partition_size();
TestCase::new()
// This query uses the exact same ordering as the input table
@@ -213,7 +214,7 @@ async fn sort_preserving_merge() {
// provide insufficient memory to merge
.with_memory_limit(partition_size / 2)
// two partitions of data, so a merge is required
- .with_scenario(Scenario::DictionaryStrings(2))
+ .with_scenario(scenario)
.with_expected_plan(
// It is important that this plan only has
// SortPreservingMergeExec (not a Sort which would compete
@@ -238,7 +239,10 @@ async fn sort_preserving_merge() {
#[tokio::test]
async fn sort_spill_reservation() {
- let partition_size = batches_byte_size(&dict_batches());
+ let scenario = Scenario::new_dictionary_strings(1)
+ // make the batches small enough to avoid triggering CardinalityAwareRowConverter
+ .with_single_row_batches(true);
+ let partition_size = scenario.partition_size();
let base_config = SessionConfig::new()
// do not allow the sort to use the 'concat in place' path
@@ -248,30 +252,30 @@ async fn sort_spill_reservation() {
// purposely sorting data that requires non trivial memory to
// sort/merge.
let test = TestCase::new()
- // This query uses a different order than the input table to
- // force a sort. It also needs to have multiple columns to
- // force RowFormat / interner that makes merge require
- // substantial memory
+ // This query uses a different order than the input table to
+ // force a sort. It also needs to have multiple columns to
+ // force RowFormat / interner that makes merge require
+ // substantial memory
.with_query("select * from t ORDER BY a , b DESC")
- // enough memory to sort if we don't try to merge it all at once
+ // enough memory to sort if we don't try to merge it all at once
.with_memory_limit(partition_size)
- // use a single partiton so only a sort is needed
- .with_scenario(Scenario::DictionaryStrings(1))
+ // use a single partiton so only a sort is needed
+ .with_scenario(scenario)
.with_disk_manager_config(DiskManagerConfig::NewOs)
.with_expected_plan(
// It is important that this plan only has a SortExec, not
// also merge, so we can ensure the sort could finish
// given enough merging memory
&[
- "+---------------+--------------------------------------------------------------------------------------------------------+",
- "| plan_type | plan |",
- "+---------------+--------------------------------------------------------------------------------------------------------+",
- "| logical_plan | Sort: t.a ASC NULLS LAST, t.b DESC NULLS FIRST |",
- "| | TableScan: t projection=[a, b] |",
- "| physical_plan | SortExec: expr=[a@0 ASC NULLS LAST,b@1 DESC] |",
- "| | MemoryExec: partitions=1, partition_sizes=[5], output_ordering=a@0 ASC NULLS LAST,b@1 ASC NULLS LAST |",
- "| | |",
- "+---------------+--------------------------------------------------------------------------------------------------------+",
+ "+---------------+----------------------------------------------------------------------------------------------------------+",
+ "| plan_type | plan |",
+ "+---------------+----------------------------------------------------------------------------------------------------------+",
+ "| logical_plan | Sort: t.a ASC NULLS LAST, t.b DESC NULLS FIRST |",
+ "| | TableScan: t projection=[a, b] |",
+ "| physical_plan | SortExec: expr=[a@0 ASC NULLS LAST,b@1 DESC] |",
+ "| | MemoryExec: partitions=1, partition_sizes=[245], output_ordering=a@0 ASC NULLS LAST,b@1 ASC NULLS LAST |",
+ "| | |",
+ "+---------------+----------------------------------------------------------------------------------------------------------+",
]
);
@@ -471,11 +475,48 @@ enum Scenario {
/// [`StreamingTable`]
AccessLogStreaming,
- /// N partitions of of sorted, dictionary encoded strings
- DictionaryStrings(usize),
+ /// N partitions of of sorted, dictionary encoded strings.
+ DictionaryStrings {
+ partitions: usize,
+ /// If true, splits all input batches into 1 row each
+ single_row_batches: bool,
+ },
}
impl Scenario {
+ /// Create a new DictionaryStrings scenario with the number of partitions
+ fn new_dictionary_strings(partitions: usize) -> Self {
+ Self::DictionaryStrings {
+ partitions,
+ single_row_batches: false,
+ }
+ }
+
+ /// Should the input be split into 1 row batches?
+ fn with_single_row_batches(mut self, val: bool) -> Self {
+ if let Self::DictionaryStrings {
+ single_row_batches, ..
+ } = &mut self
+ {
+ *single_row_batches = val;
+ } else {
+ panic!("Scenario does not support single row batches");
+ }
+ self
+ }
+
+ /// return the size, in bytes, of each partition
+ fn partition_size(&self) -> usize {
+ if let Self::DictionaryStrings {
+ single_row_batches, ..
+ } = self
+ {
+ batches_byte_size(&maybe_split_batches(dict_batches(), *single_row_batches))
+ } else {
+ panic!("Scenario does not support partition size");
+ }
+ }
+
/// return a TableProvider with data for the test
fn table(&self) -> Arc<dyn TableProvider> {
match self {
@@ -500,11 +541,17 @@ impl Scenario {
.with_infinite_table(true);
Arc::new(table)
}
- Self::DictionaryStrings(num_partitions) => {
+ Self::DictionaryStrings {
+ partitions,
+ single_row_batches,
+ } => {
use datafusion::physical_expr::expressions::col;
- let batches: Vec<Vec<_>> = std::iter::repeat(dict_batches())
- .take(*num_partitions)
- .collect();
+ let batches: Vec<Vec<_>> = std::iter::repeat(maybe_split_batches(
+ dict_batches(),
+ *single_row_batches,
+ ))
+ .take(*partitions)
+ .collect();
let schema = batches[0][0].schema();
let options = SortOptions {
@@ -544,7 +591,7 @@ impl Scenario {
// first
Some(vec![Arc::new(JoinSelection::new())])
}
- Self::DictionaryStrings(_) => {
+ Self::DictionaryStrings { .. } => {
// Use default rules
None
}
@@ -559,6 +606,29 @@ fn access_log_batches() -> Vec<RecordBatch> {
.collect()
}
+/// If `one_row_batches` is true, then returns new record batches that
+/// are one row in size
+fn maybe_split_batches(
+ batches: Vec<RecordBatch>,
+ one_row_batches: bool,
+) -> Vec<RecordBatch> {
+ if !one_row_batches {
+ return batches;
+ }
+
+ batches
+ .into_iter()
+ .flat_map(|mut batch| {
+ let mut batches = vec![];
+ while batch.num_rows() > 1 {
+ batches.push(batch.slice(0, 1));
+ batch = batch.slice(1, batch.num_rows() - 1);
+ }
+ batches
+ })
+ .collect()
+}
+
static DICT_BATCHES: OnceLock<Vec<RecordBatch>> = OnceLock::new();
/// Returns 5 sorted string dictionary batches each with 50 rows with
diff --git a/datafusion/physical-plan/Cargo.toml b/datafusion/physical-plan/Cargo.toml
index 02f1547627..8549ad6fdd 100644
--- a/datafusion/physical-plan/Cargo.toml
+++ b/datafusion/physical-plan/Cargo.toml
@@ -60,3 +60,4 @@ tempfile = "3"
#[dev-dependencies]
termtree = "0.4.1"
tokio = { version = "1.28", features = ["macros", "rt", "rt-multi-thread", "sync", "fs", "parking_lot"] }
+uuid = { version = "^1.2", features = ["v4"] }
diff --git a/datafusion/physical-plan/src/aggregates/group_values/row.rs b/datafusion/physical-plan/src/aggregates/group_values/row.rs
index 1e8688f9ec..346f15fd1f 100644
--- a/datafusion/physical-plan/src/aggregates/group_values/row.rs
+++ b/datafusion/physical-plan/src/aggregates/group_values/row.rs
@@ -16,9 +16,10 @@
// under the License.
use crate::aggregates::group_values::GroupValues;
+use crate::row_converter::CardinalityAwareRowConverter;
use ahash::RandomState;
use arrow::record_batch::RecordBatch;
-use arrow::row::{RowConverter, Rows, SortField};
+use arrow::row::{Rows, SortField};
use arrow_array::ArrayRef;
use arrow_schema::SchemaRef;
use datafusion_common::Result;
@@ -30,7 +31,7 @@ use hashbrown::raw::RawTable;
/// A [`GroupValues`] making use of [`Rows`]
pub struct GroupValuesRows {
/// Converter for the group values
- row_converter: RowConverter,
+ row_converter: CardinalityAwareRowConverter,
/// Logically maps group values to a group_index in
/// [`Self::group_values`] and in each accumulator
@@ -53,7 +54,7 @@ pub struct GroupValuesRows {
/// important for multi-column group keys.
///
/// [`Row`]: arrow::row::Row
- group_values: Rows,
+ group_values: Option<Rows>,
// buffer to be reused to store hashes
hashes_buffer: Vec<u64>,
@@ -64,7 +65,7 @@ pub struct GroupValuesRows {
impl GroupValuesRows {
pub fn try_new(schema: SchemaRef) -> Result<Self> {
- let row_converter = RowConverter::new(
+ let row_converter = CardinalityAwareRowConverter::new(
schema
.fields()
.iter()
@@ -73,13 +74,12 @@ impl GroupValuesRows {
)?;
let map = RawTable::with_capacity(0);
- let group_values = row_converter.empty_rows(0, 0);
Ok(Self {
row_converter,
map,
map_size: 0,
- group_values,
+ group_values: None,
hashes_buffer: Default::default(),
random_state: Default::default(),
})
@@ -93,6 +93,11 @@ impl GroupValues for GroupValuesRows {
let group_rows = self.row_converter.convert_columns(cols)?;
let n_rows = group_rows.num_rows();
+ let mut group_values = match self.group_values.take() {
+ Some(group_values) => group_values,
+ None => self.row_converter.empty_rows(0, 0)?,
+ };
+
// tracks to which group each of the input rows belongs
groups.clear();
@@ -107,7 +112,7 @@ impl GroupValues for GroupValuesRows {
// verify that a group that we are inserting with hash is
// actually the same key value as the group in
// existing_idx (aka group_values @ row)
- group_rows.row(row) == self.group_values.row(*group_idx)
+ group_rows.row(row) == group_values.row(*group_idx)
});
let group_idx = match entry {
@@ -116,8 +121,8 @@ impl GroupValues for GroupValuesRows {
// 1.2 Need to create new entry for the group
None => {
// Add new entry to aggr_state and save newly created index
- let group_idx = self.group_values.num_rows();
- self.group_values.push(group_rows.row(row));
+ let group_idx = group_values.num_rows();
+ group_values.push(group_rows.row(row));
// for hasher function, use precomputed hash value
self.map.insert_accounted(
@@ -131,12 +136,15 @@ impl GroupValues for GroupValuesRows {
groups.push(group_idx);
}
+ self.group_values = Some(group_values);
+
Ok(())
}
fn size(&self) -> usize {
+ let group_values_size = self.group_values.as_ref().map(|v| v.size()).unwrap_or(0);
self.row_converter.size()
- + self.group_values.size()
+ + group_values_size
+ self.map_size
+ self.hashes_buffer.allocated_size()
}
@@ -146,25 +154,34 @@ impl GroupValues for GroupValuesRows {
}
fn len(&self) -> usize {
- self.group_values.num_rows()
+ self.group_values
+ .as_ref()
+ .map(|group_values| group_values.num_rows())
+ .unwrap_or(0)
}
fn emit(&mut self, emit_to: EmitTo) -> Result<Vec<ArrayRef>> {
- Ok(match emit_to {
+ let mut group_values = self
+ .group_values
+ .take()
+ .expect("Can not emit from empty rows");
+
+ let output = match emit_to {
EmitTo::All => {
- // Eventually we may also want to clear the hash table here
- self.row_converter.convert_rows(&self.group_values)?
+ let output = self.row_converter.convert_rows(&group_values)?;
+ group_values.clear();
+ output
}
EmitTo::First(n) => {
- let groups_rows = self.group_values.iter().take(n);
+ let groups_rows = group_values.iter().take(n);
let output = self.row_converter.convert_rows(groups_rows)?;
// Clear out first n group keys by copying them to a new Rows.
// TODO file some ticket in arrow-rs to make this more efficent?
- let mut new_group_values = self.row_converter.empty_rows(0, 0);
- for row in self.group_values.iter().skip(n) {
+ let mut new_group_values = self.row_converter.empty_rows(0, 0)?;
+ for row in group_values.iter().skip(n) {
new_group_values.push(row);
}
- std::mem::swap(&mut new_group_values, &mut self.group_values);
+ std::mem::swap(&mut new_group_values, &mut group_values);
// SAFETY: self.map outlives iterator and is not modified concurrently
unsafe {
@@ -180,13 +197,18 @@ impl GroupValues for GroupValuesRows {
}
output
}
- })
+ };
+
+ self.group_values = Some(group_values);
+ Ok(output)
}
fn clear_shrink(&mut self, batch: &RecordBatch) {
let count = batch.num_rows();
- // FIXME: there is no good way to clear_shrink for self.group_values
- self.group_values = self.row_converter.empty_rows(count, 0);
+ self.group_values = self.group_values.take().map(|mut rows| {
+ rows.clear();
+ rows
+ });
self.map.clear();
self.map.shrink_to(count, |_| 0); // hasher does not matter since the map is cleared
self.map_size = self.map.capacity() * std::mem::size_of::<(u64, usize)>();
diff --git a/datafusion/physical-plan/src/aggregates/mod.rs b/datafusion/physical-plan/src/aggregates/mod.rs
index 2916786a80..7c7a593c48 100644
--- a/datafusion/physical-plan/src/aggregates/mod.rs
+++ b/datafusion/physical-plan/src/aggregates/mod.rs
@@ -1664,7 +1664,7 @@ mod tests {
))];
let task_ctx = if spill {
- new_spill_ctx(2, 2144)
+ new_spill_ctx(2, 2500)
} else {
Arc::new(TaskContext::default())
};
diff --git a/datafusion/physical-plan/src/aggregates/order/partial.rs b/datafusion/physical-plan/src/aggregates/order/partial.rs
index ff8a75b9b2..bd8688820d 100644
--- a/datafusion/physical-plan/src/aggregates/order/partial.rs
+++ b/datafusion/physical-plan/src/aggregates/order/partial.rs
@@ -15,7 +15,8 @@
// specific language governing permissions and limitations
// under the License.
-use arrow::row::{OwnedRow, RowConverter, Rows, SortField};
+use crate::row_converter::CardinalityAwareRowConverter;
+use arrow::row::{OwnedRow, Rows, SortField};
use arrow_array::ArrayRef;
use arrow_schema::Schema;
use datafusion_common::Result;
@@ -70,7 +71,7 @@ pub(crate) struct GroupOrderingPartial {
/// Converter for the sort key (used on the group columns
/// specified in `order_indexes`)
- row_converter: RowConverter,
+ row_converter: CardinalityAwareRowConverter,
}
#[derive(Debug, Default)]
@@ -124,7 +125,7 @@ impl GroupOrderingPartial {
Ok(Self {
state: State::Start,
order_indices: order_indices.to_vec(),
- row_converter: RowConverter::new(fields)?,
+ row_converter: CardinalityAwareRowConverter::new(fields)?,
})
}
@@ -141,7 +142,7 @@ impl GroupOrderingPartial {
.map(|&idx| group_values[idx].clone())
.collect();
- Ok(self.row_converter.convert_columns(&sort_values)?)
+ self.row_converter.convert_columns(&sort_values)
}
/// How many groups be emitted, or None if no data can be emitted
diff --git a/datafusion/physical-plan/src/lib.rs b/datafusion/physical-plan/src/lib.rs
index 9c116e73ea..c500ecde5d 100644
--- a/datafusion/physical-plan/src/lib.rs
+++ b/datafusion/physical-plan/src/lib.rs
@@ -348,6 +348,7 @@ pub mod memory;
pub mod metrics;
pub mod projection;
pub mod repartition;
+mod row_converter;
pub mod sorts;
pub mod stream;
pub mod streaming;
diff --git a/datafusion/physical-plan/src/row_converter.rs b/datafusion/physical-plan/src/row_converter.rs
new file mode 100644
index 0000000000..447cdaf4c1
--- /dev/null
+++ b/datafusion/physical-plan/src/row_converter.rs
@@ -0,0 +1,295 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! [`CardinalityAwareRowConverter`] for converting data to
+//! [`arrow::row`] format.
+
+use arrow::datatypes::DataType;
+use arrow::row::{Row, RowConverter};
+use arrow::row::{Rows, SortField};
+use arrow_array::cast::AsArray;
+use arrow_array::*;
+use datafusion_common::{internal_err, DataFusionError, Result};
+
+/// The threshold of the number of values at which to consider a
+/// [`DictionaryArray`] "high" cardinality.
+///
+/// Since [`RowConverter`] blindly generates a mapping for all values,
+/// regardless of if they appear in the keys, this value is compared
+/// to the length of values.
+///
+/// The assumption is that the number of potential distinct key values
+/// (aka the length of the values array) is a more robust predictor of
+/// being "high" cardinality than the actual number of keys used. The
+/// intuition for this is that other values in the dictionary could be
+/// used in subsequent batches.
+///
+/// While an argument can made for doing something more sophisticated,
+/// this would likely only really make sense if the dictionary
+/// interner itself followed a similar approach, which it did not at
+/// the time of this writing.
+const LOW_CARDINALITY_THRESHOLD: usize = 512;
+
+/// Wrapper around an [`arrow::row`] [`RowConverter`] that disables
+/// dictionary preservation for high cardinality columns, based on the
+/// observed cardinalities in the first columns converted.
+///
+/// ## Background
+///
+/// By default, the [`RowConverter`] interns (and keeps a copy of) all
+/// values from [`DictionaryArray`]s. For low cardinality columns
+/// (with few distinct values) this approach is both faster and more
+/// memory efficient. However for high cardinality coumns it is slower
+/// and requires more memory. In certain degenerate cases, such as
+/// columns of nearly unique values, the `RowConverter` will keep a
+/// copy of the entire column.
+///
+/// See <https://github.com/apache/arrow-datafusion/issues/7200> for
+/// more details
+#[derive(Debug)]
+pub enum CardinalityAwareRowConverter {
+ /// Converter is newly initialized, and hasn't yet seen data
+ New {
+ /// Defines the Row conversion
+ fields: Vec<SortField>,
+ },
+ /// Converter has seen data and can convert [`Array`]s to/from
+ /// [`Rows`]
+ Converting {
+ /// Underlying converter
+ converter: RowConverter,
+ /// if preserve_dictionaries is disabled, the output type can be
+ /// different than input.
+ output_types: Vec<DataType>,
+ },
+}
+
+impl CardinalityAwareRowConverter {
+ pub fn new(fields: Vec<SortField>) -> Result<Self> {
+ Ok(Self::New { fields })
+ }
+
+ /// Returns the memory size of the underlying [`RowConverter`] if
+ /// any.
+ pub fn size(&self) -> usize {
+ match self {
+ Self::New { .. } => 0,
+ Self::Converting { converter, .. } => converter.size(),
+ }
+ }
+
+ pub fn empty_rows(&self, row_capacity: usize, data_capacity: usize) -> Result<Rows> {
+ match self {
+ Self::New { .. } => internal_err!(
+ "CardinalityAwareRowConverter has not converted any rows yet"
+ ),
+ Self::Converting { converter, .. } => {
+ Ok(converter.empty_rows(row_capacity, data_capacity))
+ }
+ }
+ }
+
+ pub fn convert_rows<'a, I>(&self, rows: I) -> Result<Vec<ArrayRef>>
+ where
+ I: IntoIterator<Item = Row<'a>>,
+ {
+ match self {
+ Self::New { .. } => internal_err!(
+ "CardinalityAwareRowConverter has not converted any rows yet"
+ ),
+ Self::Converting {
+ converter,
+ output_types,
+ } => {
+ // Cast output type if needed. The input type and
+ // output type must match exactly (including
+ // encodings).
+ // https://github.com/apache/arrow-datafusion/discussions/7421
+ // could reduce the need for this.
+ let output = converter
+ .convert_rows(rows)?
+ .into_iter()
+ .zip(output_types.iter())
+ .map(|(arr, output_type)| {
+ if arr.data_type() != output_type {
+ Ok(arrow::compute::cast(&arr, output_type)?)
+ } else {
+ Ok(arr)
+ }
+ })
+ .collect::<Result<Vec<_>>>()?;
+
+ Ok(output)
+ }
+ }
+ }
+
+ /// Calls [`RowConverter::convert_columns`] after first
+ /// initializing the converter based on cardinalities
+ pub fn convert_columns(&mut self, columns: &[ArrayRef]) -> Result<Rows> {
+ Ok(self.converter_mut(columns)?.convert_columns(columns)?)
+ }
+
+ /// Return a mutable reference to the inner converter, creating it if needed
+ fn converter_mut(&mut self, columns: &[ArrayRef]) -> Result<&mut RowConverter> {
+ if let Self::New { fields } = self {
+ let mut updated_fields = fields.clone();
+ let mut output_types = vec![];
+ for (i, col) in columns.iter().enumerate() {
+ output_types.push(col.data_type().clone());
+ if let DataType::Dictionary(_, _) = col.data_type() {
+ // see comments on LOW_CARDINALITY_THRESHOLD for
+ // the rationale of this calculation
+ let cardinality = col.as_any_dictionary_opt().unwrap().values().len();
+ if cardinality >= LOW_CARDINALITY_THRESHOLD {
+ updated_fields[i] =
+ updated_fields[i].clone().preserve_dictionaries(false);
+ }
+ }
+ }
+ *self = Self::Converting {
+ converter: RowConverter::new(updated_fields)?,
+ output_types,
+ };
+ };
+
+ match self {
+ Self::New { .. } => {
+ unreachable!();
+ }
+ Self::Converting { converter, .. } => Ok(converter),
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use std::sync::Arc;
+
+ use rand::Rng;
+ use uuid::Uuid;
+
+ use arrow::datatypes::DataType;
+ use arrow_array::{
+ types::Int32Type, ArrayRef, DictionaryArray, Int32Array, RecordBatch, StringArray,
+ };
+ use arrow_schema::SortOptions;
+
+ use super::*;
+
+ /// Generate a record batch with two columns:
+ ///
+ /// `a_dict`: String Dictionary
+ /// `b_prim`: Int32Array with random values 0..20
+ enum Generator {
+ /// "High" cardinality (20 distinct values)
+ High,
+ /// "Low" cardinality (2 distinct values)
+ Low,
+ }
+
+ impl Generator {
+ fn build(&self) -> RecordBatch {
+ let (keys, values) = match self {
+ Self::High => {
+ let values: Vec<_> =
+ (0..1024).map(|_| Uuid::new_v4().to_string()).collect();
+ let values = StringArray::from(values);
+
+ let keys: Int32Array = (0..1024)
+ .map(|_| rand::thread_rng().gen_range(0..1024))
+ .collect();
+ (keys, values)
+ }
+ Self::Low => {
+ let values = StringArray::from_iter_values(["a", "b", "c"]);
+ let keys: Int32Array = (0..1024)
+ .map(|_| rand::thread_rng().gen_range(0..2))
+ .collect();
+ (keys, values)
+ }
+ };
+ let dict =
+ DictionaryArray::<Int32Type>::try_new(keys, Arc::new(values)).unwrap();
+ let col_a = Arc::new(dict) as ArrayRef;
+
+ // building column `b_prim`
+ let values: Int32Array = (0..1024)
+ .map(|_| rand::thread_rng().gen_range(0..20))
+ .collect();
+ let col_b: ArrayRef = Arc::new(values);
+
+ RecordBatch::try_from_iter(vec![("a_dict", col_a), ("b_prim", col_b)])
+ .unwrap()
+ }
+
+ fn sort_fields(&self) -> Vec<SortField> {
+ vec![
+ SortField::new_with_options(
+ DataType::Dictionary(
+ Box::new(DataType::Int32),
+ Box::new(DataType::Utf8),
+ ),
+ SortOptions::default(),
+ ),
+ SortField::new_with_options(DataType::Int32, SortOptions::default()),
+ ]
+ }
+ }
+
+ #[tokio::test]
+ async fn test_with_high_card() {
+ let generator = Generator::High;
+ let batch = generator.build();
+
+ let mut card_converter =
+ CardinalityAwareRowConverter::new(generator.sort_fields()).unwrap();
+ let rows = card_converter.convert_columns(batch.columns()).unwrap();
+ let converted_batch = card_converter.convert_rows(&rows).unwrap();
+ assert_eq!(converted_batch, batch.columns());
+
+ let mut row_converter = RowConverter::new(generator.sort_fields()).unwrap();
+ let rows = row_converter.convert_columns(batch.columns()).unwrap();
+ let converted_batch = row_converter.convert_rows(&rows).unwrap();
+ assert_eq!(converted_batch, batch.columns());
+
+ // with high cardinality the cardinality aware converter
+ // should be lower, as there is no interning of values
+ assert!(card_converter.size() < row_converter.size());
+ }
+
+ #[tokio::test]
+ async fn test_with_low_card() {
+ let generator = Generator::Low;
+ let batch = generator.build();
+
+ let mut card_converter =
+ CardinalityAwareRowConverter::new(generator.sort_fields()).unwrap();
+ let rows = card_converter.convert_columns(batch.columns()).unwrap();
+ let converted_batch = card_converter.convert_rows(&rows).unwrap();
+ assert_eq!(converted_batch, batch.columns());
+
+ let mut row_converter = RowConverter::new(generator.sort_fields()).unwrap();
+ let rows = row_converter.convert_columns(batch.columns()).unwrap();
+ let converted_batch = row_converter.convert_rows(&rows).unwrap();
+ assert_eq!(converted_batch, batch.columns());
+
+ // with low cardinality the row converters sizes should be
+ // equal as the same converter logic is used
+ assert_eq!(card_converter.size(), row_converter.size());
+ }
+}
diff --git a/datafusion/physical-plan/src/sorts/stream.rs b/datafusion/physical-plan/src/sorts/stream.rs
index a7f9e7380c..2ddce910ef 100644
--- a/datafusion/physical-plan/src/sorts/stream.rs
+++ b/datafusion/physical-plan/src/sorts/stream.rs
@@ -15,13 +15,14 @@
// specific language governing permissions and limitations
// under the License.
+use crate::row_converter::CardinalityAwareRowConverter;
use crate::sorts::cursor::{FieldArray, FieldCursor, RowCursor};
use crate::SendableRecordBatchStream;
use crate::{PhysicalExpr, PhysicalSortExpr};
use arrow::array::Array;
use arrow::datatypes::Schema;
use arrow::record_batch::RecordBatch;
-use arrow::row::{RowConverter, SortField};
+use arrow::row::SortField;
use datafusion_common::Result;
use datafusion_execution::memory_pool::MemoryReservation;
use futures::stream::{Fuse, StreamExt};
@@ -80,7 +81,7 @@ impl FusedStreams {
#[derive(Debug)]
pub struct RowCursorStream {
/// Converter to convert output of physical expressions
- converter: RowConverter,
+ converter: CardinalityAwareRowConverter,
/// The physical expressions to sort by
column_expressions: Vec<Arc<dyn PhysicalExpr>>,
/// Input streams
@@ -105,7 +106,7 @@ impl RowCursorStream {
.collect::<Result<Vec<_>>>()?;
let streams = streams.into_iter().map(|s| s.fuse()).collect();
- let converter = RowConverter::new(sort_fields)?;
+ let converter = CardinalityAwareRowConverter::new(sort_fields)?;
Ok(Self {
converter,
reservation,