Skip to content

fix(external-function): manage signatures and grants - #90

Merged
noel merged 5 commits into
datacoves:mainfrom
GClunies:fix/external-function-signature
Nov 12, 2026
Merged

noel merged 5 commits into
datacoves:mainfrom
GClunies:fix/external-function-signature

Conversation

@GClunies

@GClunies GClunies commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Closes #85.

Summary

Snowcap now manages an external function by its full signature, name(arg types). Snowflake accepts EXTERNAL FUNCTION only in CREATE. Every other statement names the object FUNCTION.

ExternalFunction  DB.SCH.ECHO(NUMBER, VARCHAR)
├── fetch         DESC FUNCTION DB.SCH.ECHO(NUMBER, VARCHAR)
│                 + the INFORMATION_SCHEMA.FUNCTIONS row with the same signature
├── list          SHOW EXTERNAL FUNCTIONS IN ACCOUNT → one FQN per overload
├── CREATE        CREATE [SECURE] EXTERNAL FUNCTION ...
├── ALTER / DROP  ... FUNCTION DB.SCH.ECHO(NUMBER, VARCHAR)
├── OWNERSHIP     GRANT OWNERSHIP ON FUNCTION ...
└── Grant         GRANT USAGE ON FUNCTION ...  (still depends on the ExternalFunction)

Other behavior:

  • A function with no arguments has the empty signature ().
  • exclude_resources: [external_function] excludes grants on external functions. The check reads Grant.target_type, the declared type, not the SQL type.
  • A ResourcePointer to a function, procedure, or external function includes its argument types in its FQN, equality, and hash.

Review fixes (commit bee8d0d)

 ResourcePointer.__init__
-  parse arg types if "(" in name
+  parse arg types if type in {FUNCTION, PROCEDURE, EXTERNAL_FUNCTION} and "(" in name

 fetch_external_function
-  data = next(row for row in functions if signature matches)        # StopIteration
+  data = next((row for row in functions if signature matches), None)
+  if data is None: return None
-  "owner": quote(FUNCTION_OWNER)                                    # TypeError on NULL
+  "owner": quote(FUNCTION_OWNER) if FUNCTION_OWNER else ""

Name parsing fixes (commit ed5ab7a)

 parse_identifier
-  signature starts at the first "("                      # "T(X)" → name "T", args [X]
+  signature starts at the first "(" outside quotes       # "T(X)" → name "T(X)"
+  or the last part matches SHOW GRANTS "FN(ARGS):RETURNS"

 ResourcePointer
-  NamedResource parses the name, then the pointer parses it again   # raises on {{ var.db }}.SCH.FN(VARCHAR)
+  NamedResource._parse_name parses once; the pointer hook keeps arg types

Evidence

  • Before: the 3 new tests fail on the PR head before the fix.

    ResourcePointer('"MY(POOL)"', WAREHOUSE).fqn      → '"MY(POOL)"(POOL)'   FAIL
    fetch_external_function, no matching row          → StopIteration       FAIL
    fetch_external_function, FUNCTION_OWNER = NULL    → TypeError           FAIL
    

    After: all pass.

    ResourcePointer('"MY(POOL)"', WAREHOUSE).fqn      → '"MY(POOL)"'         PASS
    fetch_external_function, no matching row          → None                PASS
    fetch_external_function, FUNCTION_OWNER = NULL    → owner ""            PASS
    FUNCTION/PROCEDURE/EXTERNAL_FUNCTION pointers keep their arg types       PASS
    
  • The name parsing tests fail before commit ed5ab7a and pass after it:

    parse_identifier('DB.SCH."T(X)"')            → name '"T(X)"', arg types None        PASS
    parse_identifier('DB.SCH."my(fn"(VARCHAR)')  → name '"my(fn"', [VARCHAR]            PASS  (was ParseException)
    parse_identifier('DB.SCH."FN(X VARCHAR):OBJECT"') → name '"FN"', [X VARCHAR]        unchanged
    ResourcePointer(FUNCTION) calls parse_identifier once for its name                  PASS  (was 2)
    ResourcePointer('{{ var.db }}.SCH.FN(VARCHAR)', FUNCTION) constructs                PASS  (was ParseException)
    
  • The CI checks pass locally on the branch merged with current main:

    • make lint: black, codespell, and ruff pass.
    • make typecheck: Success: no issues found in 35 source files.
    • uv run pytest --ignore=tests/integration: 2345 passed, 1 skipped, 1 xfailed.
  • The function and grant integration tests pass against the test account. test_blueprint_external_function_lifecycle reads a function grant back from SHOW GRANTS and plans no changes.

  • Two integration tests fail on this branch and fail the same way on main:

    • test_fetch[PythonStoredProcedure]: Python runtime version 3.9 is decommissioned.
    • test_show_future_grants_skipped_when_no_future_grants_in_manifest: assert 8 == 0.

Merge Danger

Door: two-way

The change adds no migration and no destructive statement. A revert restores the old behavior.

Blast Radius: grants

Grants on external functions now render ON FUNCTION <name>(<arg types>). Before this PR, Snowflake rejected those statements. A quoted name that holds ( now keeps its full name. Before, Snowcap cut the name at the (.

🤖 Generated with Claude Code

https://claude.ai/code/session_011dXsGKk6RCfX9bMFY9NLNG

@noel

noel commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Reviewed this PR (full external-function signature/grant handling). Found three real issues via code-review, verified each by running the actual code in a worktree:

  1. Most severe: ResourcePointer.__init__ (snowcap/resource.py, around the new _arg_types line) treats any string name containing ( as a function-style signature, regardless of resource type. This isn't scoped to external functions, it's a general regression. Reproduced:

    >>> rp = ResourcePointer(name='"MY(POOL)"', resource_type=ResourceType.WAREHOUSE)
    >>> rp._arg_types
    ['POOL']
    >>> str(rp.fqn)
    '"MY(POOL)"(POOL)'

    A quoted identifier that legitimately contains a literal ( (rare, but valid Snowflake) gets its FQN, equality, and hash silently corrupted. Worth gating the signature-parsing on resource_type being function-like before parsing ( out of the name.

  2. Confirmed: fetch_external_function (snowcap/data_provider.py) uses next() with no default to find the matching INFORMATION_SCHEMA.FUNCTIONS row. If DESC FUNCTION succeeds but nothing in INFORMATION_SCHEMA.FUNCTIONS matches the signature (race between the two queries, or a signature-format mismatch), this raises a raw uncaught StopIteration instead of returning None/a clear error like every other fetch_* in the file. Reproduced by feeding it an empty INFORMATION_SCHEMA result.

  3. Plausible, not yet reproduced against a live account: data["FUNCTION_OWNER"] is passed straight into _quote_snowflake_identifier with no None guard, unlike the codebase's existing _get_owner_identifier helper. If that column is ever NULL, this would raise TypeError instead of degrading.

Minor: ResourcePointer.__init__ re-parses the identifier with pyparsing a second time just to recover arg_types, duplicating the parse NamedResource.__init__ already did one line above, on every function/procedure/external-function grant target.

Ponytail/complexity pass: lean, nothing to cut, this diff even removes speculative code (the isinstance(on, ResourcePointer) branch in Grant.__init__), and necessary size for the problem it solves.

Separate heads-up, not a code issue: this PR and #84 both independently add resource_type_is_integration/_OWNERSHIP_OBJECT_TYPES/_ownership_object_type to lifecycle.py/enums.py. Whichever merges second will conflict with the other's version of those files.

@noel

noel commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

@GClunies checking in on this one, it's been quiet since my review on Oct 13 and it's the oldest branch open.

Two things in your favour, both verified just now rather than assumed:

So the only thing standing between this and merge is finding 1, the ResourcePointer.__init__ signature parsing not being gated on resource_type. That's the one I'd want fixed since it's a general regression rather than anything external-function specific. 2 and 3 are small, and the minor double-parse note is optional.

Are you planning to pick this back up? If you'd rather not, I'm happy to push the fix to your branch or take it over, just say which.

GClunies and others added 3 commits November 10, 2026 18:00
…nto lineup/adhoc-36-get-https-github-com-datacoves-snowcap-p
- ResourcePointer parses argument types only for functions, procedures,
  and external functions. A quoted name of another type can hold a `(`.
- fetch_external_function returns None when no INFORMATION_SCHEMA row
  matches the signature that DESC FUNCTION resolved.
- fetch_external_function returns an empty owner when FUNCTION_OWNER is NULL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011dXsGKk6RCfX9bMFY9NLNG
@GClunies

Copy link
Copy Markdown
Contributor Author

@noel thanks for the review. Commit bee8d0d fixes all three findings, and the branch now includes current main.

Finding Fix Test
1. ResourcePointer parses ( for every type Parses argument types only for FUNCTION, PROCEDURE, and EXTERNAL_FUNCTION test_quoted_name_with_parenthesis_is_not_a_signature: a warehouse "MY(POOL)" keeps the FQN "MY(POOL)"
2. next() raises StopIteration next(..., None), and the function returns None test_no_information_schema_row_for_the_signature_returns_none
3. A NULL FUNCTION_OWNER raises TypeError Returns "", the same as _get_owner_identifier test_null_owner_degrades_to_empty

The minor double parse stays. It now runs only for function, procedure, and external function pointers. To remove it, NamedResource must keep a field that only pointers read.

Checks: make lint and make typecheck pass. The unit tests give 2332 passed, 1 skipped, 1 xfailed. The external function integration test passes against the test account.

A separate bug exists on main, outside this PR: parse_identifier splits at ( inside quotes. A table named "T(X)" gets the name "T".

- parse_identifier starts a signature at the first ( outside quotes. A
  quoted name such as "T(X)" keeps its parenthesis. The SHOW GRANTS form
  "FN(X VARCHAR):OBJECT" still parses as a signature.
- ResourcePointer reads argument types from the parse that NamedResource
  already does. A function pointer whose name holds a var no longer
  raises ParseException.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011dXsGKk6RCfX9bMFY9NLNG
@GClunies

Copy link
Copy Markdown
Contributor Author

@noel commit ed5ab7a also fixes the optional double parse and a related bug that is already on main.

  • ResourcePointer now parses its name once. NamedResource._parse_name does the parse, and the pointer override keeps the argument types. This also fixes a bug from this PR: a function pointer with a var name, for example {{ var.db }}.SCH.FN(VARCHAR), raised ParseException.
  • parse_identifier starts a signature at the first ( outside quotes. Before, a table named "T(X)" came out as "T". The SHOW GRANTS form DB.SCH."FN(X VARCHAR):OBJECT" still parses the same way.

The unit tests give 2345 passed. The external function lifecycle integration test passes, and it reads a function grant back from SHOW GRANTS.

@noel
noel merged commit 6ce5c12 into datacoves:main Nov 12, 2026
6 checks passed
GClunies added a commit to GClunies/snowcap that referenced this pull request Nov 21, 2026
Rerun live_audit.py on every object at snowcap 6ce5c12, so FINDINGS.md
reflects current main, as the review asked.

main changed three things that the inventory had to follow:
- RESOURCE MONITOR: triggers is a field now (datacoves#86). The truth query joins
  SHOW's notify_at, suspend_at, and suspend_immediately_at columns, and
  the row has samples. It passes L2 to L5.
- SECURITY INTEGRATION:saml2: SAML2SecurityIntegration models it now.
  The object has create_args, truth queries, and samples for 11 rows.
  The rows without a sample say why. The x509 samples are self-signed
  test certificates with no private key.
- EXTERNAL FUNCTION: fqn now carries the argument signature (datacoves#90), so
  the DESCRIBE in the truth query doubled it and failed. The query names
  the function without a signature now.

NOTEBOOK now fails L2, because Snowflake disabled new Legacy Notebooks
on the test account (BCR: no Legacy Notebook activity in the 60 days
before November 1, 2026). snowcap's Notebook renders only the legacy
CREATE NOTEBOOK, so the finding is correct for current snowcap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011dXsGLBEG2amgh6CdUmCP5
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.

ExternalFunction names leave out argument types, so Snowflake rejects GRANT OWNERSHIP on them

2 participants