-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support conforming singer property names to target identifier c…
…onstraints in SQL sinks (#1039) Co-authored-by: Edgar R. M. <[email protected]> Co-authored-by: Edgar R. M <[email protected]>
- Loading branch information
1 parent
2721dc5
commit 937acf3
Showing
8 changed files
with
271 additions
and
11 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,3 @@ | ||
"""A sample tap for testing SQL target property name transformations.""" | ||
|
||
from .hostile_tap import SampleTapHostile |
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,40 @@ | ||
from __future__ import annotations | ||
|
||
import random | ||
import string | ||
from typing import Iterable | ||
|
||
from singer_sdk import typing as th | ||
from singer_sdk.streams import Stream | ||
|
||
|
||
class HostilePropertyNamesStream(Stream): | ||
""" | ||
A stream with property names that are not compatible as unescaped identifiers | ||
in common DBMS systems. | ||
""" | ||
|
||
name = "hostile_property_names_stream" | ||
schema = th.PropertiesList( | ||
th.Property("name with spaces", th.StringType), | ||
th.Property("NameIsCamelCase", th.StringType), | ||
th.Property("name-with-dashes", th.StringType), | ||
th.Property("Name-with-Dashes-and-Mixed-cases", th.StringType), | ||
th.Property("5name_starts_with_number", th.StringType), | ||
th.Property("6name_starts_with_number", th.StringType), | ||
th.Property("7name_starts_with_number", th.StringType), | ||
th.Property("name_with_emoji_😈", th.StringType), | ||
).to_dict() | ||
|
||
@staticmethod | ||
def get_random_lowercase_string(): | ||
return "".join(random.choice(string.ascii_lowercase) for _ in range(10)) | ||
|
||
def get_records(self, context: dict | None) -> Iterable[dict | tuple[dict, dict]]: | ||
return ( | ||
{ | ||
key: self.get_random_lowercase_string() | ||
for key in self.schema["properties"].keys() | ||
} | ||
for _ in range(10) | ||
) |
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,24 @@ | ||
"""A sample tap for testing SQL target property name transformations.""" | ||
|
||
from typing import List | ||
|
||
from samples.sample_tap_hostile.hostile_streams import HostilePropertyNamesStream | ||
from singer_sdk import Stream, Tap | ||
from singer_sdk.typing import PropertiesList | ||
|
||
|
||
class SampleTapHostile(Tap): | ||
"""Sample tap for for testing SQL target property name transformations.""" | ||
|
||
name: str = "sample-tap-hostile" | ||
config_jsonschema = PropertiesList().to_dict() | ||
|
||
def discover_streams(self) -> List[Stream]: | ||
"""Return a list of discovered streams.""" | ||
return [ | ||
HostilePropertyNamesStream(tap=self), | ||
] | ||
|
||
|
||
if __name__ == "__main__": | ||
SampleTapHostile.cli() |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Helper functions for conforming identifiers.""" | ||
import re | ||
from string import ascii_lowercase, digits | ||
|
||
|
||
def snakecase(string: str) -> str: | ||
"""Convert string into snake case. | ||
Args: | ||
string: String to convert. | ||
Returns: | ||
string: Snake cased string. | ||
""" | ||
string = re.sub(r"[\-\.\s]", "_", string) | ||
string = ( | ||
string[0].lower() | ||
+ re.sub( | ||
r"[A-Z]", lambda matched: "_" + str(matched.group(0).lower()), string[1:] | ||
) | ||
if string | ||
else string | ||
) | ||
return re.sub(r"_{2,}", "_", string).rstrip("_") | ||
|
||
|
||
def replace_leading_digit(string: str) -> str: | ||
"""Replace leading numeric character with equivalent letter. | ||
Args: | ||
string: String to process. | ||
Returns: | ||
A modified string if original starts with a number, | ||
else the unmodified original. | ||
""" | ||
if string[0] in digits: | ||
letters = list(ascii_lowercase) | ||
numbers = [int(d) for d in digits] | ||
digit_map = {n: letters[n] for n in numbers} | ||
return digit_map[int(string[0])] + string[1:] | ||
return string |
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