Skip to content
Closed
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
33 changes: 33 additions & 0 deletions datacontract/export/sql_type_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ def _get_format(field: Union[SchemaProperty, FieldLike]) -> Optional[str]:
return field.format


def _get_max_length(field: Union[SchemaProperty, FieldLike]) -> Optional[int]:
"""Get maxLength from a field's logicalTypeOptions or customProperties."""
if isinstance(field, SchemaProperty):
if field.logicalTypeOptions and field.logicalTypeOptions.get("maxLength"):
return field.logicalTypeOptions.get("maxLength")
val = _get_config_value(field, "maxLength")
if val:
return int(val)
return None
return None


def _get_items(field: Union[SchemaProperty, FieldLike]) -> Optional[Union[SchemaProperty, FieldLike]]:
"""Get items from an array field."""
if isinstance(field, SchemaProperty):
Expand All @@ -98,6 +110,10 @@ def convert_to_sql_type(field: Union[SchemaProperty, FieldLike], server_type: st
if physical_type:
return physical_type

# Respect the physicalType set directly on an ODCS SchemaProperty as-is.
if isinstance(field, SchemaProperty) and field.physicalType:
return field.physicalType

if server_type == "snowflake":
return convert_to_snowflake(field)
elif server_type == "postgres":
Expand Down Expand Up @@ -137,6 +153,9 @@ def convert_to_snowflake(field: Union[SchemaProperty, FieldLike]) -> None | str:
if type is None:
return None
if type.lower() in ["string", "varchar", "text"]:
max_length = _get_max_length(field)
if max_length:
return f"varchar({max_length})"
return type.upper() # STRING, TEXT, VARCHAR are all the same in snowflake
if type.lower() in ["timestamp", "timestamp_tz"]:
return "TIMESTAMP_TZ"
Expand Down Expand Up @@ -178,6 +197,9 @@ def convert_type_to_postgres(field: Union[SchemaProperty, FieldLike]) -> None |
if type.lower() in ["string", "varchar", "text"]:
if format == "uuid":
return "uuid"
max_length = _get_max_length(field)
if max_length:
return f"varchar({max_length})"
return "text" # STRING does not exist, TEXT and VARCHAR are all the same in postrges
if type.lower() in ["timestamp", "timestamp_tz"]:
return "timestamptz"
Expand Down Expand Up @@ -364,7 +386,12 @@ def convert_to_duckdb(field: Union[SchemaProperty, FieldLike]) -> None | str:
}

# Convert simple mappings
_varchar_types = {"nvarchar", "varchar", "string", "text"}
if type_lower in type_mapping:
if type_lower in _varchar_types:
max_length = _get_max_length(field)
if max_length:
return f"VARCHAR({max_length})"
return type_mapping[type_lower]

# convert decimal numbers with precision and scale
Expand Down Expand Up @@ -421,6 +448,9 @@ def convert_type_to_sqlserver(field: Union[SchemaProperty, FieldLike]) -> None |
if field_type in ["string", "varchar", "text"]:
if format == "uuid":
return "uniqueidentifier"
max_length = _get_max_length(field)
if max_length:
return f"varchar({max_length})"
return "varchar"
if field_type in ["timestamp", "timestamp_tz"]:
return "datetimeoffset"
Expand Down Expand Up @@ -499,6 +529,9 @@ def convert_type_to_trino(field: Union[SchemaProperty, FieldLike]) -> None | str
return None
field_type = field_type.lower()
if field_type in ["string", "text", "varchar"]:
max_length = _get_max_length(field)
if max_length:
return f"varchar({max_length})"
return "varchar"
# tinyint, smallint not supported by data contract
if field_type in ["number", "decimal", "numeric"]:
Expand Down
31 changes: 31 additions & 0 deletions tests/fixtures/postgres-export-string-types/datacontract.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
dataProduct: string-types-test
version: 1.0.0
status: current
id: string-types-postgres-test

kind: DataContract
apiVersion: v3.0.0

servers:
- server: production
type: postgres
host: localhost
port: 5432
database: mydb
schema: public

schema:
- name: my_table
physicalType: table
properties:
- name: field_physical_type
logicalType: string
physicalType: VARCHAR(255)
- name: field_max_length
logicalType: string
logicalTypeOptions:
maxLength: 100
- name: field_plain_string
logicalType: string
- name: field_integer
logicalType: integer
16 changes: 16 additions & 0 deletions tests/test_sql_type_converter.py
Copy link
Copy Markdown
Contributor

@jochenchrist jochenchrist Mar 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot The test is too complicated for me. I want an Sql Exporter test that is simple to understand.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced in 4bdfdc4. The test is now a single, simple SQL exporter test (matching test_export_sql.py style) with a fixture YAML at tests/fixtures/postgres-export-string-types/datacontract.yaml — asserting the full DDL output covering physicalType pass-through (VARCHAR(255)), maxLength (varchar(100)), plain string (text), and integer.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from datacontract.data_contract import DataContract


def test_to_sql_ddl_postgres_string_types():
actual = DataContract(data_contract_file="fixtures/postgres-export-string-types/datacontract.yaml").export("sql")
expected = """
-- Data Contract: string-types-postgres-test
-- SQL Dialect: postgres
CREATE TABLE my_table (
field_physical_type VARCHAR(255),
field_max_length varchar(100),
field_plain_string text,
field_integer integer
);
""".strip()
assert actual == expected
Loading