Skip to content

adbc_scan_table: fix COUNT(*) on tables whose first column is not numeric - #14

Merged
rustyconover merged 3 commits into
Query-farm:mainfrom
prmoore77:upstream-fix/count-star-rowid-projection
Aug 27, 2026
Merged

adbc_scan_table: fix COUNT(*) on tables whose first column is not numeric#14
rustyconover merged 3 commits into
Query-farm:mainfrom
prmoore77:upstream-fix/count-star-rowid-projection

Conversation

@prmoore77

Copy link
Copy Markdown
Contributor

Problem

SELECT COUNT(*) FROM attached_db.schema.table fails with e.g.

Conversion Error: Could not convert string 'SYSTEM' to INT64

whenever the table's first column is not numeric (every Db2 SYSCAT.* table starts with TENANTNAME, for example). For COUNT(*) DuckDB asks the table function for only COLUMN_IDENTIFIER_ROW_ID; AdbcScanTableInitGlobal sees no valid column ids and falls back to SELECT *, while AdbcScanTableInitLocal maps Arrow column 0 onto the (BIGINT) row-id output slot. When column 0 happens to be numeric the count is right by accident; otherwise the cast fails.

Fix

When no real columns are requested but a table name is known, scan SELECT 1 FROM <qualified table> instead of SELECT *. That is cheap for the remote database (no wide rows shipped), always yields an INT column for the row-id slot, and produces exactly the row count. Filter pushdown is applied afterwards as before, so COUNT(*) ... WHERE ... still pushes the predicate down. The original fallback is kept for the no-table-name (raw query) case.

Repro

Any ADBC source with a string-first table, e.g. with adbc-driver-db2:

ATTACH 'db2://host:50000/SAMPLE' AS db2 (TYPE adbc);
SELECT COUNT(*) FROM db2.SYSCAT.SCHEMATA;   -- before: conversion error; after: 30

Not built locally — please run the extension test build.

🤖 Generated with Claude Code

…NT(*) on string-first tables)

DuckDB asks for COLUMN_IDENTIFIER_ROW_ID alone for COUNT(*); the scan fell
back to SELECT * and mapped Arrow column 0 onto the BIGINT row-id output,
failing with e.g. "Could not convert string 'SYSTEM' to INT64" whenever the
table's first column is not numeric (Db2 SYSCAT.TABLES, SYSCAT.SCHEMATA).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
(cherry picked from commit a844055)
prmoore77 and others added 2 commits August 26, 2026 18:46
Covers the fix for Query-farm#14 through the ATTACH scan path (the path DuckDB
actually uses for COUNT(*) on a catalog-qualified table -- unlike a direct
adbc_scan_table(...) call, which the optimizer projects a real column for
even when the query only needs a row count, so it never hits the buggy
fallback).

Verified this reproduces the original failure (Conversion Error: Could not
convert string 'alpha' to INT64) against the pre-fix code, and passes once
the fix in this PR is applied.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B5cXMpuhzmAxLF41Nt2T4L
@rustyconover
rustyconover merged commit 1b8f353 into Query-farm:main Aug 27, 2026
8 of 9 checks passed
@prmoore77

Copy link
Copy Markdown
Contributor Author

🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀

@prmoore77
prmoore77 deleted the upstream-fix/count-star-rowid-projection branch August 27, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants