dbt-labs/dbt-core · error

Table.distinct: error selecting rows: {e}

Error message

Table.distinct: error selecting rows: {e}

What it means

Thrown when `Table.distinct()` fails to select the distinct rows from the underlying Arrow table. The internal `select_rows` operation on the table's representation (which materializes the deduplicated row indices into a new table) returned an error, which is wrapped as an InvalidOperation error. This indicates the row-selection step of the distinct computation failed, typically due to an internal state or schema problem.

Source

Thrown at crates/dbt-agate/src/table.rs:976

        let mut seen_groups = HashSet::new();
        let indices: Vec<u64> = grouper
            .iter()
            .enumerate()
            .filter_map(|(row_idx, group_id)| {
                if seen_groups.insert(group_id) {
                    Some(row_idx as u64)
                } else {
                    None
                }
            })
            .collect();
        let selection_vector = UInt64Array::new(indices.into(), None);

        let repr = self
            .repr
            .select_rows(&selection_vector, None)
            .map_err(|e| {
                Error::new(
                    ErrorKind::InvalidOperation,
                    format!("Table.distinct: error selecting rows: {e}"),
                )
            })?;
        Ok(AgateTable::from_repr(repr.into()))
    }
}

impl Default for AgateTable {
    fn default() -> Self {
        let batch = RecordBatch::new_empty(Arc::new(Schema::empty()));
        Self::from_record_batch(Arc::new(batch))
    }
}

// TODO(felipecrv): implement the AgateTable Python API
// https://github.com/wireservice/agate/blob/master/agate/table/__init__.py#L34
impl Object for AgateTable {

View on GitHub (pinned to 0267ce9170)

Solutions

  1. Inspect the wrapped inner error message (`Table.distinct: error selecting rows: {e}`) for the root cause from select_rows
  2. Verify the table's schema/columns are well-formed before calling distinct
  3. Rebuild the table from source data if its repr may be stale or corrupted
  4. If reproducible, file a bug with a minimal reproduction since this wraps an internal operation

Example fix

null
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

try:
    distinct_table = table.distinct(key)
except Exception as e:
    if 'error selecting rows' in str(e):
        # rebuild table or log internal failure
        distinct_table = table
    else:
        raise

Prevention

When it happens

Trigger: Calling `table.distinct(key)` (Python `Table.distinct`) where the computed distinct row indices cannot be applied via `repr.select_rows`, e.g. due to an internal select_rows failure on the table representation.

Common situations: Running dbt-agate table pipelines that deduplicate rows; usually surfaces when the underlying table repr is corrupted or the select implementation encounters an unsupported schema, rather than from bad user input.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of dbt-labs/dbt-core@0267ce9170 (2026-09-07). Data as JSON: /api/errors/4a984c742f38df22. Report an issue: GitHub.