-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add sql-datatype to the SDK discovery and catalog #1872
Open
BuzzCutNorman
wants to merge
8
commits into
meltano:main
Choose a base branch
from
BuzzCutNorman:1323-add-sql-datatype-to-discovery-and-catalog
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
380ce4e
working version
BuzzCutNorman fbf875e
Merge branch 'main' into 1323-add-sql-datatype-to-discovery-and-catalog
edgarrmondragon d2f8f6c
Merge branch 'main' into 1323-add-sql-datatype-to-discovery-and-catalog
BuzzCutNorman 8bff56d
Merge branch 'main' into 1323-add-sql-datatype-to-discovery-and-catalog
BuzzCutNorman 4a70048
captured warnings and returned expanded message
BuzzCutNorman 7e8929c
Merge branch 'main' into 1323-add-sql-datatype-to-discovery-and-catalog
BuzzCutNorman 2f02791
updated returned message
BuzzCutNorman 0f3841a
Merge branch 'main' into 1323-add-sql-datatype-to-discovery-and-catalog
BuzzCutNorman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -396,6 +396,33 @@ | |
view_names = [] | ||
return [(t, False) for t in table_names] + [(v, True) for v in view_names] | ||
|
||
def discover_catalog_entry_sql_datatype( | ||
self, | ||
data_type: sqlalchemy.types.TypeEngine, | ||
) -> str: | ||
"""Retrun SQL Datatype as a string to utilize in the catalog. | ||
|
||
Args: | ||
data_type: given data type as sqlalchemy.types.TypeEngine | ||
|
||
Returns: | ||
A string description the given data type example "VARCHAR(length=15)". | ||
""" | ||
datatype_attributes = ("length", "scale", "precision") | ||
|
||
catalog_format = f"{type(data_type).__name__}(" | ||
|
||
for attribute in datatype_attributes: | ||
if hasattr(data_type, attribute) and getattr(data_type, attribute): | ||
catalog_format += f"{attribute}={(getattr(data_type, attribute))}, " | ||
|
||
if catalog_format.endswith(", "): | ||
catalog_format = catalog_format[:-2] | ||
|
||
catalog_format += ")" | ||
|
||
return catalog_format | ||
|
||
# TODO maybe should be splitted into smaller parts? | ||
def discover_catalog_entry( | ||
self, | ||
|
@@ -441,21 +468,34 @@ | |
|
||
# Initialize columns list | ||
table_schema = th.PropertiesList() | ||
for column_def in inspected.get_columns(table_name, schema=schema_name): | ||
column_name = column_def["name"] | ||
is_nullable = column_def.get("nullable", False) | ||
jsonschema_type: dict = self.to_jsonschema_type( | ||
t.cast(sqlalchemy.types.TypeEngine, column_def["type"]), | ||
) | ||
table_schema.append( | ||
th.Property( | ||
name=column_name, | ||
wrapped=th.CustomType(jsonschema_type), | ||
required=not is_nullable, | ||
), | ||
) | ||
with warnings.catch_warnings(record=True) as inspection_warnings: | ||
for column_def in inspected.get_columns(table_name, schema=schema_name): | ||
column_name = column_def["name"] | ||
is_nullable = column_def.get("nullable", False) | ||
jsonschema_type: dict = self.to_jsonschema_type( | ||
t.cast(sqlalchemy.types.TypeEngine, column_def["type"]), | ||
) | ||
table_schema.append( | ||
th.Property( | ||
name=column_name, | ||
wrapped=th.CustomType(jsonschema_type), | ||
required=not is_nullable, | ||
), | ||
) | ||
if len(inspection_warnings) > 0: | ||
for line in inspection_warnings: | ||
expanded_msg: str = ( | ||
f"Discovery warning: {line.message} in '{unique_stream_id}'" | ||
) | ||
self.logger.info(expanded_msg) | ||
schema = table_schema.to_dict() | ||
|
||
sql_datatypes = {} | ||
for column_def in inspected.get_columns(table_name, schema=schema_name): | ||
sql_datatypes[ | ||
str(column_def["name"]) | ||
] = self.discover_catalog_entry_sql_datatype(column_def["type"]) | ||
Comment on lines
+494
to
+497
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you feel comfortable adding tests for this? A good place might be in |
||
|
||
# Initialize available replication methods | ||
addl_replication_methods: list[str] = [""] # By default an empty list. | ||
# Notes regarding replication methods: | ||
|
@@ -480,6 +520,7 @@ | |
replication_method=replication_method, | ||
key_properties=key_properties, | ||
valid_replication_keys=None, # Must be defined by user | ||
sql_datatypes=sql_datatypes, | ||
), | ||
database=None, # Expects single-database context | ||
row_count=None, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this bits, wdyt about using the builtin
logging.captureWarnings
?We'd probably want to call it whenever a plugin's CLI is invoked, so in the body of
sdk/singer_sdk/plugin_base.py
Lines 513 to 531 in 046044f