Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions kylinpy/sqla_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@


class KylinIdentifierPreparer(compiler.IdentifierPreparer):
compiler.IdentifierPreparer.reserved_words = \
set(itertools.chain(*[[e.lower(), e] for e in CALCITE_KEYWORDS]))
compiler.IdentifierPreparer.reserved_words.update(SUPERSET_KEYWORDS)
# Assign to this class's own attribute, not the base
# sqlalchemy.sql.compiler.IdentifierPreparer.reserved_words. That base
# attribute is the shared default every other SQLAlchemy dialect falls
# back on when it defines no reserved_words of its own; mutating it here
# (as this used to do) corrupted identifier quoting for every other
# dialect loaded in the same process for as long as kylinpy stayed
# imported, since common words like "name" ended up reserved globally.
reserved_words = \
set(itertools.chain(*[[e.lower(), e] for e in CALCITE_KEYWORDS])) | \
SUPERSET_KEYWORDS

def __init__(self, dialect, initial_quote='"',
final_quote=None, escape_quote='"', omit_schema=True):
Expand Down
48 changes: 48 additions & 0 deletions tests/test_reserved_words.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import subprocess
import sys


class TestReservedWords(object):
def test_importing_dialect_does_not_mutate_shared_base_reserved_words(self):
"""KylinIdentifierPreparer must not leak its reserved-word set onto
sqlalchemy.sql.compiler.IdentifierPreparer.reserved_words, the shared
default every other SQLAlchemy dialect without its own reserved_words
falls back on. Run in a subprocess so the check reflects the state
right after import, regardless of what else this test session has
already imported.
"""
script = (
"from sqlalchemy.sql import compiler\n"
"before = set(compiler.IdentifierPreparer.reserved_words)\n"
"import kylinpy.sqla_dialect\n"
"after = compiler.IdentifierPreparer.reserved_words\n"
"assert after == before, "
"'importing kylinpy.sqla_dialect mutated the shared '"
"'compiler.IdentifierPreparer.reserved_words default'\n"
"assert 'name' not in after\n"
"print('OK')\n"
)
result = subprocess.run(
[sys.executable, "-c", script],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
assert result.returncode == 0, result.stdout.decode()
assert b"OK" in result.stdout

def test_kylin_identifier_preparer_keeps_its_own_reserved_words(self):
from sqlalchemy.sql import compiler

import kylinpy.sqla_dialect as m

assert m.KylinIdentifierPreparer.reserved_words is not (
compiler.IdentifierPreparer.reserved_words
)
assert "name" in m.KylinIdentifierPreparer.reserved_words
assert "__timestamp" in m.KylinIdentifierPreparer.reserved_words