NoahKusaba commented on code in PR #160:
URL:
https://github.com/apache/datafusion-iceberg/pull/160#discussion_r4791077016
##########
crates/datafusion/src/write/exec.rs:
##########
@@ -124,17 +126,27 @@ impl IcebergWriteExec {
.write_datafusion_fanout_enabled()
.map_err(to_datafusion_error)?;
if metadata.default_partition_spec().is_unpartitioned() {
- return Ok(None);
+ return Ok((Distribution::UnspecifiedDistribution, None));
}
let partition = col(PROJECTED_PARTITION_VALUE_COLUMN,
&input.schema()).map_err(|_| {
plan_datafusion_err!(
"Writing partitioned table {} requires a
'{PROJECTED_PARTITION_VALUE_COLUMN}' column; add it with
project_with_partition",
table.identifier()
)
})?;
- Ok((!fanout).then(|| {
+ let distribution = if metadata
+ .properties()
+ .get("write.distribution-mode")
+ .is_some_and(|mode| mode.eq_ignore_ascii_case("hash"))
+ {
+ Distribution::KeyPartitioned(vec![Arc::clone(&partition)])
Review Comment:
Hashing on `_partition` can't use the input's existing distribution.
`_partition` is a new column, so an input already hashed on `cat` (after `GROUP
BY cat` or a join on `cat`) gets a second full shuffle.
Suggestion: require `KeyPartitioned` on the source columns of identity
fields, and add `_partition` only when some field isn't an identity transform.
`KeyPartitioned([region, cat])` is met by an upstream `Hash([region])`, and an
inserted hash still groups rows the same way. The ordering requirement has the
same problem.
##########
crates/datafusion/src/write/exec.rs:
##########
@@ -124,17 +126,27 @@ impl IcebergWriteExec {
.write_datafusion_fanout_enabled()
.map_err(to_datafusion_error)?;
if metadata.default_partition_spec().is_unpartitioned() {
- return Ok(None);
+ return Ok((Distribution::UnspecifiedDistribution, None));
}
let partition = col(PROJECTED_PARTITION_VALUE_COLUMN,
&input.schema()).map_err(|_| {
plan_datafusion_err!(
"Writing partitioned table {} requires a
'{PROJECTED_PARTITION_VALUE_COLUMN}' column; add it with
project_with_partition",
table.identifier()
)
})?;
- Ok((!fanout).then(|| {
+ let distribution = if metadata
+ .properties()
+ .get("write.distribution-mode")
+ .is_some_and(|mode| mode.eq_ignore_ascii_case("hash"))
Review Comment:
Unknown values are silently ignored here. A typo like `"hsah"`, or
`"range"`, falls back to no distribution. The fanout property in this same
function raises a plan error, and Java's `DistributionMode.fromName` throws on
unknown values. Can we parse this into an enum and either reject `range` or
treat it as a hash on the partition key?
##########
crates/datafusion/tests/insert_into.rs:
##########
@@ -652,6 +664,90 @@ async fn sorts_each_input_partition_without_fanout() ->
Result<(), Box<dyn Error
Ok(())
}
+/// Each input task supplies a row for both truncated keys. Hash distribution
+/// lets one writer collect the two rows for each key into one data file.
+#[tokio::test]
+async fn hash_distributes_writes_by_transformed_partition() -> Result<(),
Box<dyn Error>>
+{
+ let fixtures = FixtureCatalog::try_new().await?;
+ let harness = insert_harness(
+ &fixtures,
+ Some(Transform::Truncate(10)),
+ &[
+ ("write.distribution-mode", "hash"),
+ ("write.datafusion.fanout.enabled", "false"),
+ ],
+ )
+ .await?;
+
+ let (plan, _) = harness
+ .query("INSERT INTO iceberg.default.t VALUES (1, 11), (2, 21) UNION
ALL VALUES (3, 19), (4, 29)")
+ .await?;
+
+ let table = fixtures
+ .catalog()
+ .load_table(&TableIdent::from_strs(["default", "t"])?)
+ .await?;
+ let files: Vec<_> = table
+ .scan()
+ .build()?
+ .plan_files()
+ .await?
+ .map_ok(|task| (task.partition().cloned(), task.record_count()))
+ .try_collect()
+ .await?;
+ let file = |key| (Some(Struct::from_iter([Some(Literal::int(key))])),
Some(2));
+ assert_eq!(files.len(), 2);
+ assert_eq!(
+ HashSet::from_iter(files),
+ HashSet::from([file(10), file(20)])
+ );
Review Comment:
Nit: compare the sorted `Vec` of files in one assertion instead of `len()`
plus a `HashSet`. It would also be good to test an invalid value, `range`, and
`hash` on an unpartitioned table.
--
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]