Skip to content

fix(tool/list-tables): trim whitespace in postgres/cockroachdb table_… - #4306

Closed
beskvladimir-create wants to merge 3 commits into
googleapis:mainfrom
beskvladimir-create:fix/list-tables-trim-table-names
Closed

beskvladimir-create wants to merge 3 commits into
googleapis:mainfrom
beskvladimir-create:fix/list-tables-trim-table-names

Conversation

@beskvladimir-create

Copy link
Copy Markdown

Description

postgres-list-tables and cockroachdb-list-tables split table_names on comma via string_to_array without trimming, so a value like "t1, t2" became {'t1', ' t2'} and the second element never matched. The table was silently omitted with no error, which to an agent reads as "the table doesn't exist". This trims each split element before matching, consistent with mssql-list-tables (LTRIM/RTRIM) and spanner-list-tables (regexp split on \s*,\s* plus TRIM).

Verified against PostgreSQL 16: with the fix, "t1, t2" and messy input such as " t1 , t2 " both return all named tables, and the empty-string no-filter guard is unchanged. Added an integration test case to the shared Postgres list-tables test (runs for postgres, alloydb, cloudsql-pg). CockroachDB has no list-tables integration test today, so its identical fix is covered by review.

Security: no change to parameter binding. table_names is still passed as the bound $1 parameter; this only changes how the already-bound value is split and compared, so there is no new SQL/query-injection surface.

PR Checklist

Opened an issue first: yes (#4305). Reviewed the full diff: yes. Tests and linter: to be validated by CI. Code coverage: a test was added. Docs: not necessary. Breaking change: no.

Issue Reference

Fixes #4305 🦕

@google-cla

google-cla Bot commented Oct 22, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the SQL queries for listing tables in CockroachDB and PostgreSQL to handle surrounding whitespace in comma-separated table names, and adds a corresponding integration test. The reviewer suggested a more efficient and idiomatic approach using regexp_split_to_array to split and trim the table names in a single operation, avoiding the overhead of a subquery with unnest and btrim.

@beskvladimir-create

Copy link
Copy Markdown
Author

@googlebot I signed it!

…names filter

postgres-list-tables and cockroachdb-list-tables split table_names on comma
via string_to_array without trimming, so a value like "t1, t2" became
{'t1',' t2'} and the second element never matched, silently omitting the
table. Trim each split element before matching, consistent with the
mssql and spanner list-tables tools.

Fixes googleapis#4305
@beskvladimir-create
beskvladimir-create force-pushed the fix/list-tables-trim-table-names branch from 2c58512 to 3ee8e52 Compare October 22, 2026 04:56
…es table_names

Addresses review feedback on the postgres list-tables filter: replace the
unnest + btrim subquery with a single regexp_split_to_array(TRIM($1), '\s*,\s*')
array operation, which is more idiomatic and also trims any surrounding
whitespace, not only spaces. CockroachDB keeps string_to_array + btrim.
…achdb table_names

Apply the same single-array split to the CockroachDB query as the Postgres
one. regexp_split_to_array is a registered CockroachDB builtin, so this
drops the unnest + btrim subquery and trims all surrounding whitespace.
@Yuan325

Yuan325 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Hi @beskvladimir-create Thank you for your contributions.
After looking closely at the changes in this PR, I don't think we should clean the table name whitespace within the SQL code itself. If a user provides a table name with leading or trailing whitespace, it is generally considered a typo on their end. It is safer to let the underlying database driver return an error for the invalid table name rather than having the tool attempt to implicitly modify or "correct" the input. If the database driver is silently dropping this and it wasn't an expected behavior, this will probably need a fix on the database driver's end. I'll go ahead and close this PR.

@Yuan325 Yuan325 closed this Nov 6, 2026
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.

postgres-list-tables / cockroachdb-list-tables: table_names with surrounding whitespace are silently dropped

3 participants