Skip to content

Reserved words are not quoted correctly #602

@izzudinhafiz

Description

@izzudinhafiz

When using PyAthena with Sqlalchemy, reserved keywords aren't being quoted correctly. The test case is also failing to catch this as it's compiling with the default dialect instead of PyAthena's Dialect

I have a minimum reproducible snippet here

from pyathena.sqlalchemy.base import AthenaDialect
from sqlalchemy import Column, MetaData, String, Table

dialect = AthenaDialect()

# this is from the test code
test_case = Table("select", MetaData(), Column("current_timestamp", String()))
should_quote = Table("select", MetaData(), Column("current_timestamp", String()))

test_case_query = str(test_case.select().where(test_case.c.current_timestamp == "a"))
should_quote_query = (
    should_quote.select()
    .where(should_quote.c.current_timestamp == "a")
    .compile(dialect=dialect)
    .string
)

assert '"select"' in test_case_query, test_case_query
assert '"select"' in should_quote_query, should_quote_query

This will fail the assertion on should_quote_query

The query from the test case outputs to

SELECT "select"."current_timestamp" 
FROM "select" 
WHERE "select"."current_timestamp" = :current_timestamp_1

Where as when using the dialect

SELECT select.current_timestamp 
FROM select 
WHERE select.current_timestamp = %(current_timestamp_1)s

The impacted test_case is here

class TestSQLAlchemyAthena:
    def test_reserved_words(self):
        """Presto uses double quotes, not backticks"""
        fake_table = Table("select", MetaData(), Column("current_timestamp", types.String()))
        query = str(fake_table.select().where(fake_table.c.current_timestamp == "a"))
        assert '"select"' in query
        assert '"current_timestamp"' in query
        assert "`select`" not in query
        assert "`current_timestamp`" not in query

I'll be opening a PR to fix this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions