Skip to content
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

File-based CDK: allow null values for all inferred columns #28847

Merged
merged 2 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ def conforms_to_schema(record: Mapping[str, Any], schema: Mapping[str, Any]) ->
value = record.get(column)

if value is not None:
if expected_type == "object":
if isinstance(expected_type, list):
return any(is_equal_or_narrower_type(value, e) for e in expected_type)
elif expected_type == "object":
return isinstance(value, dict)
elif expected_type == "array":
if not isinstance(value, list):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,25 @@ def list_files(self) -> List[RemoteFile]:

def infer_schema(self, files: List[RemoteFile]) -> Mapping[str, Any]:
loop = asyncio.get_event_loop()
return loop.run_until_complete(self._infer_schema(files))
schema = loop.run_until_complete(self._infer_schema(files))
return self._fill_nulls(schema)

@staticmethod
def _fill_nulls(schema: Mapping[str, Any]) -> Mapping[str, Any]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it might be worth unit testing this method instead of only relying on the scenarios since it's not trivial

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, added.

if isinstance(schema, dict):
for k, v in schema.items():
if k == "type":
if isinstance(v, list):
if "null" not in v:
schema[k] = ["null"] + v
elif v != "null":
schema[k] = ["null", v]
else:
DefaultFileBasedStream._fill_nulls(v)
elif isinstance(schema, list):
for item in schema:
DefaultFileBasedStream._fill_nulls(item)
return schema

async def _infer_schema(self, files: List[RemoteFile]) -> Mapping[str, Any]:
"""
Expand All @@ -208,7 +226,10 @@ async def _infer_schema(self, files: List[RemoteFile]) -> Mapping[str, Any]:
# number of concurrent tasks drops below the number allowed.
done, pending_tasks = await asyncio.wait(pending_tasks, return_when=asyncio.FIRST_COMPLETED)
for task in done:
base_schema = merge_schemas(base_schema, task.result())
try:
base_schema = merge_schemas(base_schema, task.result())
except Exception as exc:
self.logger.error(f"An error occurred inferring the schema. \n {traceback.format_exc()}", exc_info=exc)

return base_schema

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@
id="test_decimal_missing_precision"),
pytest.param(_default_avro_format, {"type": "bytes", "logicalType": "decimal", "precision": 9}, None, ValueError,
id="test_decimal_missing_scale"),
pytest.param(_default_avro_format, {"type": "bytes", "logicalType": "uuid"}, {"type": "string"}, None, id="test_uuid"),
pytest.param(_default_avro_format, {"type": "int", "logicalType": "date"}, {"type": "string", "format": "date"}, None,
pytest.param(_default_avro_format, {"type": "bytes", "logicalType": "uuid"}, {"type": ["null", "string"]}, None, id="test_uuid"),
pytest.param(_default_avro_format, {"type": "int", "logicalType": "date"}, {"type": ["null", "string"], "format": "date"}, None,
id="test_date"),
pytest.param(_default_avro_format, {"type": "int", "logicalType": "time-millis"}, {"type": "integer"}, None, id="test_time_millis"),
pytest.param(_default_avro_format, {"type": "long", "logicalType": "time-micros"}, {"type": "integer"}, None,
pytest.param(_default_avro_format, {"type": "int", "logicalType": "time-millis"}, {"type": ["null", "integer"]}, None, id="test_time_millis"),
pytest.param(_default_avro_format, {"type": "long", "logicalType": "time-micros"}, {"type": ["null", "integer"]}, None,
id="test_time_micros"),
pytest.param(
_default_avro_format,
{"type": "long", "logicalType": "timestamp-millis"}, {"type": "string", "format": "date-time"}, None, id="test_timestamp_millis"
{"type": "long", "logicalType": "timestamp-millis"}, {"type": ["null", "string"], "format": "date-time"}, None, id="test_timestamp_millis"
),
pytest.param(_default_avro_format, {"type": "long", "logicalType": "timestamp-micros"}, {"type": "string"}, None,
pytest.param(_default_avro_format, {"type": "long", "logicalType": "timestamp-micros"}, {"type": ["null", "string"]}, None,
id="test_timestamp_micros"),
pytest.param(
_default_avro_format,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@
"json_schema": {
"type": "object",
"properties": {
"col1": {"type": "string"},
"col2": {"type": "integer"},
"col1": {"type": ["null", "string"]},
"col2": {"type": ["null", "integer"]},
"_ab_source_file_last_modified": {"type": "string"},
"_ab_source_file_url": {"type": "string"},
},
Expand Down Expand Up @@ -327,19 +327,19 @@
"json_schema": {
"type": "object",
"properties": {
"col_double": {"type": "string"},
"col_string": {"type": "string"},
"col_double": {"type": ["null", "string"]},
"col_string": {"type": ["null", "string"]},
"col_album": {
"properties": {
"album": {"type": "string"},
"album": {"type": ["null", "string"]},
},
"type": "object",
"type": ["null", "object"],
},
"col_song": {
"properties": {
"title": {"type": "string"},
"title": {"type": ["null", "string"]},
},
"type": "object",
"type": ["null", "object"],
},
"_ab_source_file_last_modified": {"type": "string"},
"_ab_source_file_url": {"type": "string"},
Expand Down Expand Up @@ -422,28 +422,28 @@
"json_schema": {
"type": "object",
"properties": {
"col_array": {"items": {"type": "string"}, "type": "array"},
"col_bool": {"type": "boolean"},
"col_bytes": {"type": "string"},
"col_double": {"type": "string"},
"col_enum": {"enum": ["POP_ROCK", "INDIE_ROCK", "ALTERNATIVE_ROCK"], "type": "string"},
"col_fixed": {"pattern": "^[0-9A-Fa-f]{8}$", "type": "string"},
"col_float": {"type": "number"},
"col_int": {"type": "integer"},
"col_long": {"type": "integer"},
"col_map": {"additionalProperties": {"type": "string"}, "type": "object"},
"col_array": {"items": {"type": ["null", "string"]}, "type": ["null", "array"]},
"col_bool": {"type": ["null", "boolean"]},
"col_bytes": {"type": ["null", "string"]},
"col_double": {"type": ["null", "string"]},
"col_enum": {"enum": ["POP_ROCK", "INDIE_ROCK", "ALTERNATIVE_ROCK"], "type": ["null", "string"]},
"col_fixed": {"pattern": "^[0-9A-Fa-f]{8}$", "type": ["null", "string"]},
"col_float": {"type": ["null", "number"]},
"col_int": {"type": ["null", "integer"]},
"col_long": {"type": ["null", "integer"]},
"col_map": {"additionalProperties": {"type": ["null", "string"]}, "type": ["null", "object"]},
"col_record": {
"properties": {"artist": {"type": "string"}, "song": {"type": "string"}, "year": {"type": "integer"}},
"type": "object",
"properties": {"artist": {"type": ["null", "string"]}, "song": {"type": ["null", "string"]}, "year": {"type": ["null", "integer"]}},
"type": ["null", "object"],
},
"col_string": {"type": "string"},
"col_decimal": {"pattern": "^-?\\d{(1, 5)}(?:\\.\\d(1, 5))?$", "type": "string"},
"col_uuid": {"type": "string"},
"col_date": {"format": "date", "type": "string"},
"col_time_millis": {"type": "integer"},
"col_time_micros": {"type": "integer"},
"col_timestamp_millis": {"format": "date-time", "type": "string"},
"col_timestamp_micros": {"type": "string"},
"col_string": {"type": ["null", "string"]},
"col_decimal": {"pattern": "^-?\\d{(1, 5)}(?:\\.\\d(1, 5))?$", "type": ["null", "string"]},
"col_uuid": {"type": ["null", "string"]},
"col_date": {"format": "date", "type": ["null", "string"]},
"col_time_millis": {"type": ["null", "integer"]},
"col_time_micros": {"type": ["null", "integer"]},
"col_timestamp_millis": {"format": "date-time", "type": ["null", "string"]},
"col_timestamp_micros": {"type": ["null", "string"]},
"_ab_source_file_last_modified": {"type": "string"},
"_ab_source_file_url": {"type": "string"},
},
Expand Down Expand Up @@ -587,10 +587,10 @@
"json_schema": {
"type": "object",
"properties": {
"col_title": {"type": "string"},
"col_album": {"type": "string", "enum": ["SUMMERS_GONE", "IN_RETURN", "A_MOMENT_APART", "THE_LAST_GOODBYE"]},
"col_year": {"type": "integer"},
"col_vocals": {"type": "boolean"},
"col_title": {"type": ["null", "string"]},
"col_album": {"type": ["null", "string"], "enum": ["SUMMERS_GONE", "IN_RETURN", "A_MOMENT_APART", "THE_LAST_GOODBYE"]},
"col_year": {"type": ["null", "integer"]},
"col_vocals": {"type": ["null", "boolean"]},
"_ab_source_file_last_modified": {"type": "string"},
"_ab_source_file_url": {"type": "string"},
},
Expand All @@ -604,12 +604,12 @@
"json_schema": {
"type": "object",
"properties": {
"col_name": {"type": "string"},
"col_name": {"type": ["null", "string"]},
"col_location": {
"properties": {"country": {"type": "string"}, "state": {"type": "string"}, "city": {"type": "string"}},
"type": "object",
"properties": {"country": {"type": ["null", "string"]}, "state": {"type": ["null", "string"]}, "city": {"type": ["null", "string"]}},
"type": ["null", "object"],
},
"col_attendance": {"type": "integer"},
"col_attendance": {"type": ["null", "integer"]},
"_ab_source_file_last_modified": {"type": "string"},
"_ab_source_file_url": {"type": "string"},
},
Expand Down Expand Up @@ -698,19 +698,19 @@
"json_schema": {
"type": "object",
"properties": {
"col_double": {"type": "number"},
"col_string": {"type": "string"},
"col_double": {"type": ["null", "number"]},
"col_string": {"type": ["null", "string"]},
"col_album": {
"properties": {
"album": {"type": "string"},
"album": {"type": ["null", "string"]},
},
"type": "object",
"type": ["null", "object"],
},
"col_song": {
"properties": {
"title": {"type": "string"},
"title": {"type": ["null", "string"]},
},
"type": "object",
"type": ["null", "object"],
},
"_ab_source_file_last_modified": {"type": "string"},
"_ab_source_file_url": {"type": "string"},
Expand Down
Loading