Henning Holgersen
10/31/2022, 3:10 PMHenning Holgersen
10/31/2022, 3:10 PMaaronsteers
10/31/2022, 3:30 PMaaronsteers
10/31/2022, 3:32 PMHenning Holgersen
10/31/2022, 3:52 PMaaronsteers
10/31/2022, 4:04 PMautomatically add columns that aren’t present in the record but are in the schema💯 - This is something I definitely think we should expect. Would be good to log an issue on this so we're clear on requirements and add to our SQL milestone.
create row hash if there is no keyAwesome. I think this would be really valuable to a lot of folks. Do you mind logging an SDK issue or a new GitHub discussion with your thoughts on this?
add a separate method for cleaning up column namesI think @ken_payne recently added this (or is doing so presently). 🙂
jake_gillberg
11/19/2022, 3:07 PMautomatically add columns that aren’t present in the record but are in the schemaI recently implemented this at the tap level (but agree that target level would probably be better), by creating an "empty record" and merging it with the tap generated record in the
post_process step. Feel free to steal this code:
def empty_record(self) -> dict:
"""An empty record in schema shape up to flatten depth"""
empty_record = self._empty_record(
self.schema, 0, self.config.get("flattening_max_depth", 0)
)
return empty_record if empty_record is not None else {}
@staticmethod
def _empty_record(
schema_node: dict, level: int = 0, max_level: int = 0
) -> dict | None:
"""recursive helper of empty_record"""
if level > max_level or "properties" not in schema_node:
return None
return {
k: SlingStream._empty_record(v, level + 1, max_level)
for k, v in schema_node["properties"].items()
}
@staticmethod
def merge_records(record_1: dict, record_2: dict) -> dict:
"""deep merge of two records, record_2 takes precedence"""
result: dict = deepcopy(record_1)
for k, value in record_2.items():
if isinstance(value, dict) and k in result and isinstance(result[k], dict):
result[k] = SlingStream.merge_records(result[k], value)
else:
result[k] = value
return result
def post_process(self, row: dict, context: dict | None = None) -> dict:
"""adds empty nodes matching the schema to flatten depth"""
return self.merge_records(self.empty_record(), row)