-
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: Developers can now more easily override the mapping from SQL co…
…lumn type to JSON schema (#2618) * feat: (WIP) Let developers more easily override SQL column type to JSON schema mapping * Rename `SQLToJSONSchemaMap` -> `SQLToJSONSchema` * Remove annotation from example * Move class reference docs * Fix example in docs
- Loading branch information
1 parent
efdc735
commit 7ea1422
Showing
9 changed files
with
283 additions
and
9 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.SQLToJSONSchema | ||
========================================= | ||
|
||
.. currentmodule:: singer_sdk.connectors.sql | ||
|
||
.. autoclass:: SQLToJSONSchema | ||
: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
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 |
---|---|---|
|
@@ -9,4 +9,5 @@ porting | |
pagination-classes | ||
custom-clis | ||
config-schema | ||
sql-tap | ||
``` |
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,54 @@ | ||
# Building SQL taps | ||
|
||
## Default type mapping | ||
|
||
The Singer SDK automatically handles the most common SQLAlchemy column types, using [`functools.singledispatchmethod`](inv:python:py:class:#functools.singledispatchmethod) to process each type. See the [`SQLToJSONSchema`](connectors.sql.SQLToJSONSchema) reference documentation for details. | ||
|
||
## Custom type mapping | ||
|
||
If the class above doesn't cover all the types supported by the SQLAlchemy dialect in your tap, you can subclass it and override or extend with a new method for the type you need to support: | ||
|
||
```python | ||
import functools | ||
|
||
from sqlalchemy import Numeric | ||
from singer_sdk import typing as th | ||
from singer_sdk.connectors import SQLConnector | ||
from singer_sdk.connectors.sql import SQLToJSONSchema | ||
|
||
from my_sqlalchemy_dialect import VectorType | ||
|
||
|
||
class CustomSQLToJSONSchema(SQLToJSONSchema): | ||
@SQLToJSONSchema.to_jsonschema.register | ||
def custom_number_to_jsonschema(self, column_type: Numeric): | ||
"""Override the default mapping for NUMERIC columns. | ||
For example, a scale of 4 translates to a multipleOf 0.0001. | ||
""" | ||
return {"type": ["number"], "multipleOf": 10**-column_type.scale} | ||
|
||
@SQLToJSONSchema.to_jsonschema.register(VectorType) | ||
def vector_to_json_schema(self, column_type): | ||
"""Custom vector to JSON schema.""" | ||
return th.ArrayType(th.NumberType()).to_dict() | ||
``` | ||
|
||
````{tip} | ||
You can also use a type annotation to specify the type of the column when registering a new method: | ||
```python | ||
@SQLToJSONSchema.to_jsonschema.register | ||
def vector_to_json_schema(self, column_type: VectorType): | ||
return th.ArrayType(th.NumberType()).to_dict() | ||
``` | ||
```` | ||
|
||
Then, you need to use your custom type mapping in your connector: | ||
|
||
```python | ||
class MyConnector(SQLConnector): | ||
@functools.cached_property | ||
def type_mapping(self): | ||
return CustomSQLToJSONSchema() | ||
``` |
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
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.