datafusion icon indicating copy to clipboard operation
datafusion copied to clipboard

Regression: `DataFrameWriteOptions::with_single_file_output` produces a directory

Open sergiimk opened this issue 2 years ago • 18 comments

Describe the bug

Consider a snippet like this:

df.write_parquet(
  "dir/data",
  DataFrameWriteOptions::new().with_single_file_output(true),
  None
).await

Before v43 this would write a single file called data, but in v43 this is creating data as a directory with a randomly named file(s) in it.

This seems to be related to #13079 (cc @dhegberg) that added an extension-based heuristic.

I see this as a regression, as single file output is requested explicitly, and I don't want a heuristics to be applied.

We are using Parquet files with a content-addressable file system and our files don't have extensions.

To Reproduce

See above

Expected behavior

Considering the introduction of the extension-based heuristic I would suggest the following behavior:

  • with_single_file_output is not called (single_file_output == None) - apply the heuristic
  • with_single_file_output(true) - produce a single file at the exact path specified
  • with_single_file_output(false) - create directory under specified path if doesn't exist and write one or many files

Additional context

sergiimk avatar Nov 09 '24 01:11 sergiimk

I agree this is a regression. Thank you for the callout @sergiimk

I think this is a pretty good first issue for someone as the description is clear and the need is well defined.

alamb avatar Nov 13 '24 06:11 alamb

take

irenjj avatar Nov 13 '24 07:11 irenjj

It seems hard to control the behavior of write_parquet by single_file_output(and I've noticed that It's never used), what really controls whether to generate a single file output is determining the suffix(in start_demuxer_task()), there are several methods I can think of to handle this issue:

  1. We can add a suffix like .single to the paths that require generating a single file, and then recognize this suffix in start_demuxer_task().
  2. Give up single_file_output in DataFrameWriteOptions, use FileSinkConfig instead to control single file behavior.

cc @alamb @sergiimk @dhegberg

irenjj avatar Nov 18 '24 14:11 irenjj

Did some digging and found this old PR #9041 (cc @yyy1000) that seems to have removed single_file_output flag from FileSinkConfig - worth looking into it to understand the reasoning, not to undo the changes.

Looking at v42 code it does indeed seem that DataFrameWriteOptions::single_file_output is not read anywhere and the logic relies on just this condition in demux:

let single_file_output = !base_output_path.is_collection();

which in v43 became:

let single_file_output = !base_output_path.is_collection() && base_output_path.file_extension().is_some();

The DataFrameWriteOptions::new().with_single_file_output(true) is used in a bunch of tests though, so it's just a lucky coincidence that all tests give file a proper test.parquet name and not just test.

Personally I think that all kinds of extension-based heuristics don't belong in such low level code like start_demuxer_task and perhaps better left at the DataFrame level.

Whichever heuristic version (pre v36, pre v43, or post v43) is the right one - I don't really mind, but I think there should be a way to skip it and specify explicitly.

sergiimk avatar Nov 19 '24 02:11 sergiimk

It seems that the previous PR intentionally removed the single_file_output option, but later introduced a heuristic.

irenjj avatar Nov 19 '24 11:11 irenjj

I feel like i reviewed a PR recently related to this issue but could not find it. I wonder if it is still valid

  • I am collecting regressions we should try and fix for the next release https://github.com/apache/datafusion/issues/14008

alamb avatar Jan 13 '25 18:01 alamb

@alamb , is this issue still valid?

nssalian avatar Jul 21 '25 04:07 nssalian

@alamb , is this issue still valid?

I am not sure @nssalian -- could you try reproducing the description and see if it still happens?

alamb avatar Jul 21 '25 17:07 alamb

This still is happening with DataFusion 49. Using .parquet suffix in output str, aligns to expected behavior of this issue.

use datafusion::{
    dataframe::DataFrameWriteOptions,
    prelude::{ParquetReadOptions, SessionContext},
};

#[tokio::main]
async fn main() -> datafusion::error::Result<()> {
    let ctx = SessionContext::new();
    let options = ParquetReadOptions::new();
    let df = ctx
        .read_parquet("./data/yellow_tripdata_2025-01.parquet", options)
        .await?;

    let clone_df = df.clone();

    df.write_parquet(
        "./data/new_yellow_caps.parquet", // pre-serve the shape of the issue
        DataFrameWriteOptions::new().with_single_file_output(true),
        None,
    )
    .await?;

    clone_df
        .write_parquet(
            "./data/new_yellow_caps", // pre-serve the shape of the issue
            DataFrameWriteOptions::new().with_single_file_output(true),
            None,
        )
        .await?;

    Ok(())
}
hknlof@localhost data_samples % find data -type f | sort
data/new_yellow_caps.parquet
data/new_yellow_caps/kWLvaFapgf6OoXk5_0.parquet
data/new_yellow_caps/kWLvaFapgf6OoXk5_1.parquet
data/new_yellow_caps/kWLvaFapgf6OoXk5_2.parquet
data/new_yellow_caps/kWLvaFapgf6OoXk5_3.parquet
data/yellow_tripdata_2025-01.parquet
# Cargo.toml
[package]
name = "data_samples"
version = "0.1.0"
edition = "2024"

[dependencies]
datafusion = "49.0.0"
tokio = { version = "1.47.0", features = ["rt-multi-thread", "tokio-macros"] }

hknlof avatar Jul 29 '25 07:07 hknlof

Thanks for checking @hknlof

alamb avatar Jul 29 '25 19:07 alamb

I will try to have a go at it, this evening. First impressions: @sergiimk @irenjj observations still hold true. We do not read single_file_output of DataFrameWriteOptions.

Gut feeling without having to have spend too much time:

  1. Write an option into LogicalPlan, when field is set true. Not sure if the options are passed into ExecutionPlan during the build.
  2. Build task_ctx, ExecutionPlan, path, and writer_properties within DF::write_parquet and pass to plan_to_parquet.
  3. Ideally, this becomes part of the FileSinkConfig, but might go overboard here.

https://github.com/apache/datafusion/blob/branch-49/datafusion/datasource-parquet/src/writer.rs#L31

hknlof avatar Jul 30 '25 07:07 hknlof

I have a working solution with the introduction of .single suffix. Feels slightly nasty. Will look into using FileSinkConfig. Will open a PR later.

The problem occurs with partitioned data and no suffix. It does not occur on single partition no suffix.

hknlof avatar Jul 31 '25 10:07 hknlof

Not sure, whom to ping on the PR for triggering the jobs. I had to re-commit with clippy suggestions implemented.

hknlof avatar Aug 07 '25 11:08 hknlof

So my understanding of this is as follows:

  • #9041 removed the single_file_output from FileSinkConfig in order to have a more intuitive SQL API; that is, for SQL COPY TO statements it would now infer from the path provided if it should write to a single file or a directory based on the trailing / if present
  • #13079 extended this to include checking if it ends with a valid extension; that is, if the path ends with .parquet then it writes to single file, otherwise if it ends with / (including .parquet/) then it writes into a directory

Now we are running into the problem of the current DataFrameWriteOptions::with_single_file_output not being respected because it clashes with the above heuristics.

Way forward

I think trying to fix this issue by plugging in another heuristic in addition to the above is a bit hacky and not a long term solution (#17009).

It seems it would cause more confusion and make things harder to maintain. I think the suggestion in the original post has merit:

Considering the introduction of the extension-based heuristic I would suggest the following behavior:

  • with_single_file_output is not called (single_file_output == None) - apply the heuristic
  • with_single_file_output(true) - produce a single file at the exact path specified
  • with_single_file_output(false) - create directory under specified path if doesn't exist and write one or many files

Whether we use this existing config or name it something else, I like this way of specifying if it should use the default heuristic behaviour (more intuitive) or respecting the users choice if they explicitly want a single file or directory, especially in cases where they might want to write a single file that doesn't end in a valid extension (e.g. parquet) for whatever reason.

I admit I don't know how this would work in terms of the DataFusion internals and plumbing required, I haven't looked that far, but I think we should agree on this external API so we can then work our way inwards to implement it correctly.

Jefffrey avatar Dec 03 '25 03:12 Jefffrey

I like the proposed API. I will look into the plumbing and report back in the PR. Thanks @Jefffrey

hknlof avatar Dec 05 '25 10:12 hknlof

Is someone working on this?

RSAgr avatar Jan 02 '26 17:01 RSAgr

I am working on this.

kumarUjjawal avatar Jan 21 '26 15:01 kumarUjjawal

take

kumarUjjawal avatar Jan 21 '26 15:01 kumarUjjawal