Skip to content

fix: register _fs_* macros for databases created via CREATE DATABASE#349

Open
mdz wants to merge 1 commit into
tekumara:mainfrom
mdz:claude/frosty-gauss-b8b96b
Open

fix: register _fs_* macros for databases created via CREATE DATABASE#349
mdz wants to merge 1 commit into
tekumara:mainfrom
mdz:claude/frosty-gauss-b8b96b

Conversation

@mdz

@mdz mdz commented Jun 29, 2026

Copy link
Copy Markdown

What

Register the internal _fs_* macros (_fs_flatten, _fs_object_construct, _fs_to_timestamp) when a database is created via a runtime CREATE DATABASE statement, not only in the connection constructor.

Why

These macros are what sqlglot's transforms rewrite to, but macros.creation_sql(catalog) was only invoked from FakeSnowflakeConnection.__init__. A database created later via CREATE DATABASE foo never had its macros registered.

This is invisible in library mode (you connect with the target database, so its macros get created), but bites server mode: a client connects once, then issues CREATE DATABASE per catalog. The first TO_TIMESTAMP_NTZ(...) / LATERAL FLATTEN(INPUT => ...) against such a catalog failed with:

Scalar Function with name _fs_to_timestamp does not exist!
Table Function with name _fs_flatten does not exist!

Fixes #347.

How

In cursor.py, the create_db_name branch already ran info_schema.per_db_creation_sql(...). This adds the matching macros.creation_sql(...) call right after it, mirroring conn.py exactly. The macros use CREATE OR REPLACE, so re-running on an existing catalog (e.g. CREATE DATABASE IF NOT EXISTS) is idempotent.

Test

test_create_database_registers_macros creates a database at runtime and verifies both TO_TIMESTAMP_NTZ (_fs_to_timestamp) and LATERAL FLATTEN (_fs_flatten) work against it.

🤖 Generated with Claude Code

The internal _fs_* macros (_fs_flatten, _fs_object_construct,
_fs_to_timestamp) that sqlglot transforms depend on were only
registered in the connection constructor, so databases created at
runtime via CREATE DATABASE lacked them. This bit server mode, where a
client connects once then issues CREATE DATABASE per catalog: the first
TO_TIMESTAMP_NTZ / LATERAL FLATTEN against such a catalog failed with
"_fs_to_timestamp does not exist" / "_fs_flatten does not exist".

Run macros.creation_sql(catalog) on CREATE DATABASE execution, mirroring
the connection constructor, so runtime-created catalogs get the same
treatment as connect-time ones.

Fixes tekumara#347

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

Server mode: internal _fs_* macros not registered for databases created via CREATE DATABASE

1 participant