-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(targets): SQL target developers can now more easily override the…
… mapping from JSON schema to SQL column type (#2732) * feat(target): SQL target developers can now more easily override the mapping from JSON schema to SQL column type * Add failing test for complex type case * Correctly handle multiple types * Make mypy happy * Test more cases * Add docs * Fix annotation in docs * Support custom fallbacks * Add a way to customize raw string handling
- Loading branch information
1 parent
2ad977e
commit de7d1dc
Showing
9 changed files
with
505 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
singer_sdk.connectors.sql.JSONSchemaToSQL | ||
========================================= | ||
|
||
.. currentmodule:: singer_sdk.connectors.sql | ||
|
||
.. autoclass:: JSONSchemaToSQL | ||
:members: | ||
:special-members: __init__, __call__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ pagination-classes | |
custom-clis | ||
config-schema | ||
sql-tap | ||
sql-target | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Building SQL targets | ||
|
||
## Mapping JSON Schema to SQL types | ||
|
||
Starting with version `0.42.0`, the Meltano Singer SDK provides a clean way to map JSON Schema to SQL types. This is useful when the SQL dialect needs to do special handling for certain JSON Schema types. | ||
|
||
### Custom JSON Schema mapping | ||
|
||
If the default [`JSONSchemaToSQL`](connectors.sql.JSONSchemaToSQL) instance doesn't cover all the types supported by the SQLAlchemy dialect in your target, you can override the {attr}`SQLConnector.jsonschema_to_sql <singer_sdk.SQLConnector.jsonschema_to_sql>` property and register a new type handler for the type you need to support: | ||
|
||
```python | ||
import functools | ||
|
||
import sqlalchemy as sa | ||
from singer_sdk import typing as th | ||
from singer_sdk.connectors import JSONSchemaToSQL, SQLConnector | ||
|
||
from my_sqlalchemy_dialect import VectorType | ||
|
||
|
||
def custom_array_to_sql(jsonschema: dict) -> VectorType | sa.types.VARCHAR: | ||
"""Custom mapping for arrays of numbers.""" | ||
if items := jsonschema.get("items"): | ||
if items.get("type") == "number": | ||
return VectorType() | ||
|
||
return sa.types.VARCHAR() | ||
|
||
|
||
class MyConnector(SQLConnector): | ||
@functools.cached_property | ||
def jsonschema_to_sql(self): | ||
to_sql = JSONSchemaToSQL() | ||
to_sql.register_type_handler("array", custom_array_to_sql) | ||
return to_sql | ||
``` | ||
|
||
### Custom string format mapping | ||
|
||
You can also register a new format handler for custom string formats: | ||
|
||
```python | ||
from my_sqlalchemy_dialect import URI | ||
|
||
|
||
class MyConnector(SQLConnector): | ||
@functools.cached_property | ||
def jsonschema_to_sql(self): | ||
to_sql = JSONSchemaToSQL() | ||
to_sql.register_format_handler("uri", URI) | ||
return to_sql | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,3 +152,4 @@ Other | |
:template: class.rst | ||
|
||
connectors.sql.SQLToJSONSchema | ||
connectors.sql.JSONSchemaToSQL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.