Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error for enums: blank string ("") or "mro" as option name #337

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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: 12 additions & 1 deletion clickhouse_sqlalchemy/drivers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,16 @@ def _parse_detetime_params(spec):

@staticmethod
def _parse_options(option_string):
options = dict()
def _fix_invalid_enum_option_name(option_name):
# Python Enum does not support blank string ("")
# or "mro" as option name
if option_name == '':
option_name = '_'
elif option_name == 'mro':
option_name = '__'
return option_name

options = {}
after_name = False
escaped = False
quote_character = None
Expand All @@ -363,6 +372,7 @@ def _parse_options(option_string):
if ch in (' ', '='):
pass
elif ch == ',':
name = _fix_invalid_enum_option_name(name)
options[name] = int(value)
after_name = False
name = ''
Expand All @@ -384,6 +394,7 @@ def _parse_options(option_string):
quote_character = ch

if after_name:
name = _fix_invalid_enum_option_name(name)
options.setdefault(name, int(value)) # Word after last comma

return options
Expand Down
15 changes: 15 additions & 0 deletions tests/drivers/test_clickhouse_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ def test_empty_set_expr(self):

self.assertEqual(len(rv), 0)

def test_enum_type_with_illegal_characters(self):
empty_string = ''
mro = 'mro'
try:
self.dialect._get_column_type(
'col_name',
f"Enum8('f' = -1, '{empty_string}' = 0, 'ok' = 1, '{mro}' = 2)"
)
self.dialect._get_column_type(
'col_name',
f"Enum8('f' = -1, '{mro}' = 0, 'ok' = 1, '{empty_string}' = 2)"
)
except ValueError as e:
self.fail(f"Enum options parsing failed: {e}")


class ClickHouseAsynchDialectTestCase(BaseAsynchTestCase):

Expand Down