-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e3ec8d
commit 05ae3f1
Showing
5 changed files
with
68 additions
and
1 deletion.
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.ARRAY: | ||
"""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