Skip to content

[sqlalchemy-spanner]: _get_table_filter_query([]) generates invalid SQL '() AND ' when filter_names is empty #18838

Open

Description

@rocksnow1942

Determine this is the right repository

  • I determined this is the correct repository in which to report this bug.

Summary of the issue

When filter_names is an empty collection ([] or ()), SpannerDialect._get_table_filter_query(filter_names, ...) returns "() AND ", causing get_multi_columns, get_multi_pk_constraint, get_multi_foreign_keys, and get_multi_indexes to execute invalid SQL (WHERE () AND ...).

Environment details

  • OS: Linux
  • Python version: 3.13
  • sqlalchemy-spanner version: 1.17.2 (also reproduced on main / 1.20.1 at eb8a76628adf10093959600071a18f50285c2ecf)
  • sqlalchemy version: 2.0.52

Problem description

In SpannerDialect._get_table_filter_query:

    def _get_table_filter_query(
        self, filter_names, info_schema_table, append_query=False
    ):
        table_filter_query = ""
        if filter_names is not None:
            for table_name in filter_names:
                query = f"{info_schema_table}.table_name = '{table_name}'"
                if table_filter_query != "":
                    table_filter_query = table_filter_query + " OR " + query
                else:
                    table_filter_query = query
            table_filter_query = "(" + table_filter_query + ") "
            if append_query:
                table_filter_query = table_filter_query + " AND "

        return table_filter_query

When filter_names is [] (not None, which means "reflect all tables", whereas [] means "reflect zero tables"), for table_name in filter_names: does not execute, and _get_table_filter_query([], "col", append_query=True) returns "() AND ".

In contrast, SQLAlchemy's get_multi_* contract returns {} when filter_names is an empty collection (e.g., when reflecting a schema with no matching tables).

Suggested fix

Either short-circuit and return {} in get_multi_* when filter_names is not None and not filter_names, or emit "FALSE " / "FALSE AND " in _get_table_filter_query when filter_names is empty (or bind @filter_names with IN UNNEST(@filter_names) as in #18205).

Reproduction steps: code

from google.cloud.sqlalchemy_spanner.sqlalchemy_spanner import SpannerDialect

dialect = SpannerDialect()
print(repr(dialect._get_table_filter_query([], "col", append_query=True)))

Actual output

'()  AND '

Expected output

Either get_multi_* short-circuits to {} on empty filter_names, or _get_table_filter_query([], ...) returns a valid SQL predicate such as 'FALSE AND '.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: spannerIssues related to the Spanner API.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions