|
| 1 | +import logging |
| 2 | + |
1 | 3 | from open_data_contract_standard.model import OpenDataContractStandard, SchemaObject |
2 | 4 |
|
3 | 5 | from datacontract.export.exporter import Exporter, _check_schema_name_for_export, _determine_sql_server_type |
@@ -113,6 +115,9 @@ def _to_sql_table( |
113 | 115 | server_type: str = "snowflake", |
114 | 116 | **kwargs, |
115 | 117 | ) -> str: |
| 118 | + if (model.physicalType or "").lower() == "view": |
| 119 | + return _to_sql_view(model_name, model, server_type) |
| 120 | + |
116 | 121 | if server_type == "databricks": |
117 | 122 | # Databricks recommends to use the CREATE OR REPLACE statement for unity managed tables |
118 | 123 | # https://docs.databricks.com/en/sql/language-manual/sql-ref-syntax-ddl-create-table-using.html |
@@ -184,6 +189,68 @@ def _to_sql_table( |
184 | 189 | return result |
185 | 190 |
|
186 | 191 |
|
| 192 | +def _to_sql_view(model_name: str, model: SchemaObject, server_type: str) -> str: |
| 193 | + """Select the properties from their transformSourceObjects. |
| 194 | +
|
| 195 | + ODCS does not say how several source objects are joined, so the view is then a draft with a |
| 196 | + `<TODO: join condition>` that the user has to fill in before it can be created. Without any source object, |
| 197 | + it is a draft selecting `FROM <TODO: source object>`. |
| 198 | + """ |
| 199 | + properties = model.properties or [] |
| 200 | + column_names = [prop.physicalName or prop.name for prop in properties] |
| 201 | + |
| 202 | + # An entry of transformSourceObjects whose last part is the property's name is a column of the object |
| 203 | + # before it; any other entry is an object. |
| 204 | + select = [] |
| 205 | + all_sources = [] |
| 206 | + for prop, column_name in zip(properties, column_names): |
| 207 | + names = {n.lower() for n in (prop.name, prop.physicalName) if n} |
| 208 | + column = column_name |
| 209 | + for entry in prop.transformSourceObjects or []: |
| 210 | + obj, _, col = entry.rpartition(".") |
| 211 | + if obj and col.lower() in names: |
| 212 | + all_sources.append(obj) |
| 213 | + column = col |
| 214 | + else: |
| 215 | + all_sources.append(entry) |
| 216 | + select.append(column if column == column_name else f"{column} AS {column_name}") |
| 217 | + sources = list(dict.fromkeys(all_sources)) |
| 218 | + if len(sources) != 1: |
| 219 | + logging.getLogger(__name__).warning( |
| 220 | + "Schema '%s' is a view with %s source objects; the exported view is a draft with TODOs to fill in.", |
| 221 | + model.name, |
| 222 | + len(sources), |
| 223 | + ) |
| 224 | + |
| 225 | + from_clause = sources[0] if sources else "<TODO: source object>" |
| 226 | + for source in sources[1:]: |
| 227 | + from_clause += f"\nJOIN {source} ON <TODO: join condition>" |
| 228 | + |
| 229 | + if server_type == "sqlserver": |
| 230 | + result = f"CREATE OR ALTER VIEW {model_name}" |
| 231 | + elif server_type == "clickhouse": |
| 232 | + result = f"CREATE OR REPLACE VIEW `{model_name}`" |
| 233 | + else: |
| 234 | + result = f"CREATE OR REPLACE VIEW {model_name}" |
| 235 | + |
| 236 | + # Snowflake and Databricks take column comments in the view's column list |
| 237 | + if server_type in ("snowflake", "databricks"): |
| 238 | + quote = '"' if server_type == "databricks" else "'" |
| 239 | + columns = [] |
| 240 | + for prop, column_name in zip(properties, column_names): |
| 241 | + if prop.description is not None: |
| 242 | + column_name += f" COMMENT {quote}{_escape(prop.description)}{quote}" |
| 243 | + columns.append(f" {column_name}") |
| 244 | + result += " (\n" + ",\n".join(columns) + "\n)" |
| 245 | + if model.description is not None: |
| 246 | + separator = "=" if server_type == "snowflake" else " " |
| 247 | + result += f" COMMENT{separator}{quote}{_escape(model.description)}{quote}" |
| 248 | + |
| 249 | + result += " AS\nSELECT\n" + ",\n".join(f" {c}" for c in select) |
| 250 | + result += f"\nFROM {from_clause};\n" |
| 251 | + return result |
| 252 | + |
| 253 | + |
187 | 254 | def _get_clickhouse_order_by(pk_props: list) -> str | None: |
188 | 255 | """Build the ORDER BY clause for ClickHouse from primary key columns.""" |
189 | 256 | if not pk_props: |
|
0 commit comments