dbt-labs/dbt-core · error

Table.group_by_key: error selecting table rows

Error message

Table.group_by_key: error selecting table rows: {e}

What it means

After grouping, group_by_key selects the rows belonging to each group to build the sub-tables. If the row-selection take operation fails at the Arrow level, the error is wrapped as 'error selecting table rows'.

Solutions

  1. Check the inner Arrow error message for the concrete take failure (e.g. out-of-bounds index).
  2. Sanitize the key column before grouping (replace nulls, cast to a simple type like Utf8 or Int64).
  3. Reproduce with a small table to isolate which column or key values break selection.

Example fix

// before
let groups = table.group_by_key("id", None, None)?; // id is List<Utf8>
// after
let groups = table
    .with_column("id_str", cast(col("id"), Utf8))?
    .group_by_key("id_str", None, None)?;
Defensive patterns

Strategy: try-catch

Try / catch

match table.group_by_key(key, None, None) {
    Ok(groups) => groups,
    Err(e) if e.to_string().contains("error selecting table rows") => {
        eprintln!("arrow take failed while building groups: {e}");
        // sanitize/cast key column and retry
        return Err(e);
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Internal failure while taking rows per group: out-of-bounds indices produced by the grouper, or an Arrow compute error (e.g. type/kernel mismatch) during the take, surfaced via the collect of per-group tables.

Common situations: Custom or unusual column types whose take kernel fails; edge-case grouping inputs (all-null keys, very large indices); running with an Arrow version whose take behavior differs.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


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

Appendix: source

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

                let indices = UInt64Array::new(indices.into(), None);
                let table = self
                    .repr
                    .select_rows(
                        &indices,
                        Some(TakeOptions {
                            check_bounds: false, // groups only contain valid indices
                            ..Default::default()
                        }),
                    )
                    .map(|repr| {
                        let table = AgateTable::from_repr(Arc::new(repr));
                        Arc::new(table)
                    })?;
                Ok(table) as Result<Arc<AgateTable>, ArrowError>
            })
            .collect::<Result<Vec<Arc<AgateTable>>, ArrowError>>()
            .map_err(|e| {
                Error::new(
                    ErrorKind::InvalidOperation,
                    format!("Table.group_by_key: error selecting table rows: {e}"),
                )
            })?;

        let key_name = Some(key_name.to_string());
        let is_fork = true; // skip validations
        let repr = TableSetRepr::try_new(tables, keys, key_name, key_type, is_fork)?;
        Ok(TableSet::from_repr(repr))
    }

    fn distinct(&self, key: Option<Vec<String>>) -> Result<AgateTable, Error> {
        let column_names = match key {
            Some(keys) => self
                .column_names()
                .iter()
                .filter_map(|key| {
                    if keys.contains(key) {

View on GitHub (pinned to 0267ce9170)