Fix #3972 - Add dialects to LenFn and not use FQN in converter (#4262)

Fix #3972 - Add dialects to LenFn and not use FQN in converter (#4262)
This commit is contained in:
Pere Miquel Brull 2022-04-20 09:14:14 +02:00 committed by GitHub
parent 7958e81918
commit d30619a81f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 5 deletions

View File

@ -95,7 +95,6 @@ def ometa_to_orm(
We are building the class dynamically using We are building the class dynamically using
`type` and passing SQLAlchemy `Base` class `type` and passing SQLAlchemy `Base` class
as the bases tuple for inheritance. as the bases tuple for inheritance.
TODO: Remove the dialect once we solve the hierarchy service.db.schema.table. Check #3529
""" """
cols = { cols = {
@ -103,14 +102,17 @@ def ometa_to_orm(
for idx, col in enumerate(table.columns) for idx, col in enumerate(table.columns)
} }
schema_name = get_schema_name(schema)
orm_name = f"{schema_name}_{table.name}".replace(".", "_")
# Type takes positional arguments in the form of (name, bases, dict) # Type takes positional arguments in the form of (name, bases, dict)
orm = type( orm = type(
table.fullyQualifiedName.__root__.replace(".", "_"), # Output class name orm_name, # Output class name
(Base,), # SQLAlchemy declarative base (Base,), # SQLAlchemy declarative base
{ {
"__tablename__": str(table.name.__root__), "__tablename__": str(table.name.__root__),
"__table_args__": { "__table_args__": {
"schema": get_schema_name(schema), "schema": schema_name,
"extend_existing": True, # Recreates the table ORM object if it already exists. Useful for testing "extend_existing": True, # Recreates the table ORM object if it already exists. Useful for testing
}, },
**cols, **cols,

View File

@ -33,8 +33,10 @@ def _(element, compiler, **kw):
@compiles(LenFn, Dialects.SQLite) @compiles(LenFn, Dialects.SQLite)
@compiles(LenFn, Dialects.Vertica) @compiles(LenFn, Dialects.Vertica)
@compiles(LenFn, Dialects.Hive) # For some reason hive's dialect is in bytes... @compiles(LenFn, Dialects.Hive)
@compiles(LenFn, Dialects.Postgres) @compiles(LenFn, Dialects.Postgres)
@compiles(LenFn, Dialects.Databricks) @compiles(LenFn, Dialects.Databricks)
@compiles(LenFn, Dialects.MySQL)
@compiles(LenFn, Dialects.MariaDB)
def _(element, compiler, **kw): def _(element, compiler, **kw):
return "LENGTH(%s)" % compiler.process(element.clauses, **kw) return "LENGTH(%s)" % compiler.process(element.clauses, **kw)

View File

@ -34,7 +34,7 @@ class Dialects(Enum):
and profiling data. and profiling data.
""" """
Hive = b"hive" Hive = b"hive" # Hive requires bytes
Postgres = "postgresql" Postgres = "postgresql"
BigQuery = "bigquery" BigQuery = "bigquery"
MySQL = "mysql" MySQL = "mysql"