Skip to content

Commit

Permalink
fix(sqlalchemy): quote struct field names
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed May 28, 2023
1 parent d8cbd14 commit f5c91fc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ibis/backends/base/sql/alchemy/datatypes.py
Expand Up @@ -47,8 +47,9 @@ def __init__(self, fields: Mapping[str, sat.TypeEngine]) -> None:

@compiles(StructType, "default")
def compiles_struct(element, compiler, **kw):
quote = compiler.dialect.identifier_preparer.quote
content = ", ".join(
f"{field} {compiler.process(typ, **kw)}"
f"{quote(field)} {compiler.process(typ, **kw)}"
for field, typ in element.fields.items()
)
return f"STRUCT({content})"
Expand Down
12 changes: 12 additions & 0 deletions ibis/backends/duckdb/tests/test_datatypes.py
@@ -1,8 +1,11 @@
import duckdb_engine
import pytest
import sqlalchemy as sa
import sqlglot as sg
from packaging.version import parse as vparse
from pytest import param

import ibis.backends.base.sql.alchemy.datatypes as sat
import ibis.common.exceptions as exc
import ibis.expr.datatypes as dt
from ibis.backends.duckdb.datatypes import parse
Expand Down Expand Up @@ -119,3 +122,12 @@ def test_parse_quoted_struct_field():
assert ddt.parse('STRUCT("a" INT, "a b c" INT)') == dt.Struct(
{"a": dt.int32, "a b c": dt.int32}
)


def test_generate_quoted_struct():
typ = sat.StructType(
{"in come": sa.TEXT(), "my count": sa.BIGINT(), "thing": sa.INT()}
)
result = typ.compile(dialect=duckdb_engine.Dialect())
expected = 'STRUCT("in come" TEXT, "my count" BIGINT, thing INTEGER)'
assert result == expected

0 comments on commit f5c91fc

Please sign in to comment.