Skip to content

Conversation

@iPeluwa
Copy link
Contributor

@iPeluwa iPeluwa commented Dec 15, 2025

Fixes #159

The codebase was using starts_with() string checks to detect statement types (SET, SHOW, INSERT, etc.) which is fragile - it can break with leading whitespace, comments, or case variations.

Since we already have the parsed Statement AST from sqlparser, this switches to using pattern matching directly on the Statement enum.

Changes in permissions.rs:

  • Renamed check_query_permission to check_statement_permission, now takes &Statement instead of &str
  • Permission detection (SELECT/INSERT/UPDATE/DELETE/CREATE/DROP/ALTER) now uses match on Statement variants
  • Added should_skip_permission_check() helper using matches!() macro for SET, SHOW, and transaction statements
  • Removed all the to_lowercase().starts_with() chains

Changes in handlers.rs:

  • INSERT detection now uses matches!(statement, Statement::Insert(_))
  • Removed unnecessary query_lower variable construction
  • Fixed extended query handler to properly destructure statement from the portal tuple

All 12 unit tests pass. The existing integration test failures (dbeaver, metabase, psql) are unrelated - they fail due to missing DataFusion functions like array_length and array_contains.

…tement detection

Fixes datafusion-contrib#159

The codebase was using starts_with() string checks to detect statement
types (SET, SHOW, INSERT, etc.) which is fragile - it can break with
leading whitespace, comments, or case variations.

Since we already have the parsed Statement AST from sqlparser, this
switches to using pattern matching directly on the Statement enum.

Changes in permissions.rs:
- Renamed check_query_permission to check_statement_permission, now
  takes &Statement instead of &str
- Permission detection (SELECT/INSERT/UPDATE/DELETE/CREATE/DROP/ALTER)
  now uses match on Statement variants
- Added should_skip_permission_check() helper using matches!() macro
  for SET, SHOW, and transaction statements
- Removed all the to_lowercase().starts_with() chains

Changes in handlers.rs:
- INSERT detection now uses matches!(statement, Statement::Insert(_))
- Removed unnecessary query_lower variable construction
- Fixed extended query handler to properly destructure statement from
  the portal tuple

All 12 unit tests pass. The existing integration test failures
(dbeaver, metabase, psql) are unrelated - they fail due to missing
DataFusion functions like array_length and array_contains.
Adds a test file to verify pgAdmin startup queries work correctly.
The test covers:
- SELECT version() query
- The CASE expression checking pg_extension for 'bdr' extension
  and pg_replication_slots for replication slot count

Both pg_extension and pg_replication_slots tables are already
implemented in pg_catalog, so these queries now pass.
Use reference instead of calling .to_string() on query string
as suggested by mjgarton - the query is only used for logging
and doesn't need to be owned.
Remove comments that don't fully describe what's being skipped.
The function name should_skip_permission_check is self-documenting.

Suggested by mjgarton.
Copy link
Collaborator

@mjgarton mjgarton left a comment

Choose a reason for hiding this comment

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

LGTM

@mjgarton mjgarton merged commit 0f69e19 into datafusion-contrib:master Dec 15, 2025
7 checks passed
@sunng87
Copy link
Member

sunng87 commented Dec 16, 2025

Thank you @iPeluwa @mjgarton ! The implementation feels much cleaner now.

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.

Refactor statement matching

3 participants