Skip to content

Recognize MATERIALIZED as a keyword (issue752)#854

Open
gaoflow wants to merge 1 commit into
andialbrecht:masterfrom
gaoflow:fix-752-materialized-keyword
Open

Recognize MATERIALIZED as a keyword (issue752)#854
gaoflow wants to merge 1 commit into
andialbrecht:masterfrom
gaoflow:fix-752-materialized-keyword

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 16, 2026

Copy link
Copy Markdown

Fixes #752.

MATERIALIZED is absent from the KEYWORDS table, so in statements like CREATE MATERIALIZED VIEW the token is lexed as a Name instead of a Keyword. That leaves it untouched by keyword_case formatting, producing inconsistent output while every other DDL keyword is normalized:

import sqlparse

sqlparse.parse("create materialized view x.y.z as select 1")[0].tokens[2].is_keyword
# -> False   (expected True; it is a Name, not a Keyword)

sqlparse.format("create materialized view x.y.z as select 1", keyword_case="upper")
# -> 'CREATE materialized VIEW x.y.z AS SELECT 1'   <- 'materialized' left lowercase
# expected: 'CREATE MATERIALIZED VIEW x.y.z AS SELECT 1'

CREATE, VIEW, AS and SELECT are all uppercased; only materialized is left alone.

Fix

Add 'MATERIALIZED': tokens.Keyword to KEYWORDS, next to the existing VIEW keyword. MATERIALIZED is a standard SQL keyword used for CREATE/REFRESH/DROP MATERIALIZED VIEW across PostgreSQL, Oracle, BigQuery and Snowflake, so classifying it as a keyword matches both the spec and how sqlparse already treats VIEW.

After the change the token is a Keyword and keyword_case='upper' yields CREATE MATERIALIZED VIEW x.y.z AS SELECT 1.

Tests

Added test_materialized_view_issue752 in tests/test_regressions.py, asserting the token is a Keyword and that keyword_case='upper' normalizes it. The test is RED on the unpatched table and GREEN with the fix; the full suite stays green (488 passed, 2 xfailed, 1 xpassed). CHANGELOG and AUTHORS updated.

Disclosure: I prepared this fix with AI assistance under my direction; I reviewed and verified the change and the test myself.

MATERIALIZED was missing from the KEYWORDS table, so in statements like
CREATE MATERIALIZED VIEW the token was lexed as a Name rather than a
Keyword. This left it untouched by keyword_case formatting, producing
inconsistent output (e.g. 'CREATE materialized VIEW ...' under
keyword_case='upper') while every other DDL keyword was normalized.

Add MATERIALIZED to KEYWORDS, alongside the existing VIEW keyword, so it
parses and formats consistently.
Copilot AI review requested due to automatic review settings June 16, 2026 06:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR fixes parsing/formatting of CREATE MATERIALIZED VIEW statements by treating MATERIALIZED as a SQL keyword, and adds a regression test and changelog entry for issue752.

Changes:

  • Add MATERIALIZED to the keyword list so it tokenizes as Keyword.
  • Add a regression test ensuring parsing and keyword uppercasing behave correctly for materialized views.
  • Document the bug fix in CHANGELOG and add contributor to AUTHORS.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
tests/test_regressions.py Adds regression test for CREATE MATERIALIZED VIEW parsing/formatting.
sqlparse/keywords.py Registers MATERIALIZED as a keyword token.
CHANGELOG Documents the fix for consistent parsing/formatting (issue752).
AUTHORS Adds contributor attribution.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_regressions.py
Comment on lines +457 to +459
def test_materialized_view_issue752():
p = sqlparse.parse('CREATE MATERIALIZED VIEW v AS SELECT 1')[0]
assert p.tokens[2].ttype == T.Keyword
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.

In CREATE MATERIALIZED VIEW statements MATERIALIZED is not recognized as a keyword

2 participants