Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jun 15, 2025

The SQL parser was incorrectly allowing qualified table names (with aliases or schema prefixes) in trigger body statements, when according to SQLite specifications, only unqualified table names should be permitted.

Problem

The following SQL was being parsed successfully when it should throw a syntax error:

CREATE TRIGGER IF NOT EXISTS "tr_log_dsn_removal" 
  AFTER DELETE ON "dsn" 
  FOR EACH ROW 
BEGIN
  DELETE FROM host h -- Aliases are INVALID inside trigger bodies
  WHERE h.host = OLD.host AND h.host NOT IN (
    SELECT DISTINCT d2.host
    FROM dsn d2 -- Same - aliases not allowed
    WHERE d2.id != OLD.id 
      AND d2.host = OLD.host
  );
END

According to SQLite documentation, UPDATE, DELETE, and INSERT statements in trigger bodies must use unqualified table names only.

Solution

  • Created specialized parsing functions parseTriggerBodyDeleteStatement() and parseTriggerBodyUpdateStatement() that only allow unqualified table names
  • Added validateUnqualifiedTableName() helper to detect and reject schema qualification (schema.table) and table aliases (table alias or table AS alias)
  • Modified parseTriggerBodyStatement() to use the specialized parsers for DELETE/UPDATE statements
  • INSERT statements continue to work as before (they already used simple identifier parsing)

Testing

Added comprehensive test cases that verify:

  • Table aliases are properly rejected with clear error message
  • Schema-qualified table names are properly rejected
  • Unqualified table names continue to work correctly
  • INSERT statements remain unaffected
  • All existing functionality is preserved

The parser now correctly rejects qualified table names in trigger bodies while maintaining full compatibility with valid SQL.

Fixes #74.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Error Parsing Qualified Tables in Triggers Fix qualified table name parsing in trigger bodies Jun 15, 2025
Copilot AI requested a review from otoolep June 15, 2025 03:55
@otoolep
Copy link
Member

otoolep commented Jun 17, 2025

Thanks @copilot -- went a different way, but your input was helpful.

@otoolep otoolep closed this Jun 17, 2025
@otoolep otoolep deleted the copilot/fix-74 branch June 17, 2025 02:30
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.

Error Parsing Qualified Tables in Triggers

2 participants