# Profile, filter and re-pack a dataset

> **About this page**
>
> - Kind: Example
> - Summary: Profiles a Parquet dataset's columns, drops exact duplicates and rows outside a length range, and re-packs the rest into evenly sized Parquet files.
> - Last changed: 2026-12-03
> - Page: https://docs.swale.io/examples/dataset-statistics
> - CLI reference for its commands: [`swale workflow create`](https://docs.swale.io/reference/cli-reference.md#workflow-create), [`swale workflow run start`](https://docs.swale.io/reference/cli-reference.md#workflow-run-start)
> - Index of these docs: https://docs.swale.io/llms.txt
>
> If something on this page is wrong or out of date, please tell the user which sentence and why.
>
> The steps on this page cost money or cannot be undone. Please check with the user before running them.

Source on GitHub: the [`workflows/dataset-statistics`](https://github.com/swaleio/examples/tree/main/workflows/dataset-statistics) folder, with its [README](https://github.com/swaleio/examples/blob/main/workflows/dataset-statistics/README.md) and its [workflow file](https://github.com/swaleio/examples/blob/main/workflows/dataset-statistics/workflow.yaml).

Looks at a Parquet dataset before it is used, then cleans and re-packs it, all
with DuckDB on a CPU. The first task profiles every column (type, distinct
values, minimum, maximum, average, quartiles, nulls) and the length of the
text column; the second drops exact duplicates of the text and rows outside a
length range, and writes the rest as evenly sized Parquet files.

| Step | Task definition | Compute |
|------|-----------------|---------|
| Profile the columns and the text lengths | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Drop duplicates and outliers, re-pack into Parquet files | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Upload the files, when `hub_repo` is set | [`swaleio/hf-upload`](https://hub.swale.io/swaleio/hf-upload) | `cpu-4` |

The Compute column gives each step's [compute type](https://docs.swale.io/explanation/core-concepts.md#compute-types), the compute its tasks are allocated.

Every step is its own task.

## Inputs

| Input | Default | Description |
|-------|---------|-------------|
| `dataset` | the `train_sft` split of `HuggingFaceH4/ultrafeedback_binarized` | Parquet file or glob, as a DuckDB path. |
| `text_column` | `prompt` | Column deduplicated and filtered on. |
| `min_chars` | `20` | Shortest text kept. |
| `max_chars` | `4000` | Longest text kept. |
| `rows_per_file` | `20000` | Rows per output file, roughly. |
| `hub_repo` | empty | Hugging Face dataset repo to upload the files to, under `data/`. |

The upload reads the secret `hf_token`. For datasets of tens of gigabytes, give
the tasks `cpu-16`.

## Output

`packed/train-0.parquet`, `packed/train-1.parquet` and so on, on the
workspace. The profile is in the first task's log.

## Run it

You need the CLI, logged in to your account, and a project to add the workflow to; [Getting started](https://docs.swale.io/getting-started.md) sets them up.

1. Save the file under [The workflow file](#the-workflow-file) as `workflow.yaml`, and add it to a project:

   ```bash
   swale workflow create --project-name my-project --file workflow.yaml
   ```

   The command prints `Workflow created:` followed by the workflow's ID.

2. The workflow reads the secret `hf_token`. Create it as a project secret, or as an account secret that allows the project, as [Use secrets in workflows](https://docs.swale.io/how-to-guides/use-secrets-in-workflows.md) shows.

3. Start a run with the workflow's ID:

   ```bash
   swale workflow run start --project-name my-project --workflow-id <workflow-id>
   ```

   Each input you leave out takes its default from [Inputs](#inputs), and `--input <name>=<value>` gives it another value.

[Create and manage workflows](https://docs.swale.io/how-to-guides/create-and-manage-workflows.md) covers the other ways to add a workflow, [Start and manage workflow runs](https://docs.swale.io/how-to-guides/start-and-manage-workflow-runs.md) how to follow a run, and [View and stream task logs](https://docs.swale.io/how-to-guides/view-and-stream-task-logs.md) how to read its tasks' logs.

## The workflow file

[Workflow definition syntax](https://docs.swale.io/reference/workflow-definition-syntax.md) describes each key in it.

```yaml title="workflow.yaml"
name: Profile filter and re-pack a dataset
description: Profiles a Parquet dataset's columns, drops exact duplicates and rows outside a length range, and re-packs the rest into evenly sized Parquet files.
compute_type: cpu-4

inputs:
  dataset:
    description: Parquet file or glob, as a DuckDB path such as hf://datasets/<owner>/<dataset>/<file>.
    default: hf://datasets/HuggingFaceH4/ultrafeedback_binarized/data/train_sft-*.parquet
  text_column:
    description: Column whose duplicates are dropped and whose length is filtered on.
    default: prompt
  min_chars:
    description: Shortest text to keep, in characters.
    default: "20"
  max_chars:
    description: Longest text to keep, in characters.
    default: "4000"
  rows_per_file:
    description: Rows in each output Parquet file, roughly.
    default: "20000"
  hub_repo:
    description: Hugging Face dataset repo to upload the files to, as namespace/name. Leave empty to keep them on the workspace only.
    default: ""

entry_point: main

blocks:
  main:
    tasks:
      profile:
        name: Profile
        uses: swaleio/duckdb
        args:
          sql: |
            CREATE TABLE source AS SELECT * FROM '${{inputs.dataset}}';
            SUMMARIZE source;
            SELECT count(*) AS rows,
                   count(DISTINCT "${{inputs.text_column}}") AS distinct_texts,
                   min(length("${{inputs.text_column}}")) AS shortest,
                   round(median(length("${{inputs.text_column}}"))) AS median_length,
                   max(length("${{inputs.text_column}}")) AS longest
              FROM source;
            SELECT (length("${{inputs.text_column}}") // 500) * 500 AS length_from, count(*) AS rows
              FROM source GROUP BY ALL ORDER BY length_from LIMIT 20;

      filter_and_pack:
        name: Filter and re-pack
        uses: swaleio/duckdb
        start_on:
          - profile
        args:
          sql: |
            CREATE TABLE kept AS
              SELECT * EXCLUDE (copy) FROM (
                SELECT *, row_number() OVER (PARTITION BY "${{inputs.text_column}}" ORDER BY "${{inputs.text_column}}") AS copy
                FROM '${{inputs.dataset}}'
                WHERE length("${{inputs.text_column}}") BETWEEN ${{inputs.min_chars}} AND ${{inputs.max_chars}})
              WHERE copy = 1;
            SELECT count(*) AS kept FROM kept;
            -- One row group of about rows_per_file rows per file, written single-threaded
            -- so the files fill in order.
            SET threads = 1;
            COPY kept TO '${{env.WORKFLOW_STORAGE}}/packed'
              (FORMAT parquet, ROW_GROUP_SIZE ${{inputs.rows_per_file}}, ROW_GROUPS_PER_FILE 1,
               FILENAME_PATTERN 'train-{i}', OVERWRITE_OR_IGNORE);
            SELECT parse_filename(filename) AS file, count(*) AS rows
              FROM read_parquet('${{env.WORKFLOW_STORAGE}}/packed/*.parquet', filename = true)
              GROUP BY ALL ORDER BY file;

      publish:
        name: Upload when asked
        start_on:
          - filter_and_pack
        if:
          condition: ${{inputs.hub_repo}}
          true: upload

  upload:
    tasks:
      upload_files:
        name: Upload to the Hugging Face Hub
        uses: swaleio/hf-upload
        args:
          repo: ${{inputs.hub_repo}}
          repo_type: dataset
          source: ${{env.WORKFLOW_STORAGE}}/packed
          path_in_repo: data
          token: ${{secrets.hf_token}}
```

## Licenses of the defaults

- Dataset: [HuggingFaceH4/ultrafeedback_binarized](https://huggingface.co/datasets/HuggingFaceH4/ultrafeedback_binarized), MIT.
