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

Avro parser: return Decimal fields as strings #29182

Merged
merged 3 commits into from
Aug 8, 2023
Merged

Conversation

girarda
Copy link
Contributor

@girarda girarda commented Aug 7, 2023

What

  • Update the avro parser to always return Decimal data as strings
  • Rename the AvroFormat.decimal_as_float field to double_as_string to make it clearer that it is affecting double fields, not decimals
  • Update the parser to explicitly return dates and datetimes as iso strings instead of returning date and datetime objects

Recommended reading order

  1. airbyte-cdk/python/airbyte_cdk/sources/file_based/config/avro_format.py
  2. airbyte-cdk/python/airbyte_cdk/sources/file_based/file_types/avro_parser.py
  3. airbyte-cdk/python/unit_tests/sources/file_based/file_types/test_avro_parser.py
  4. airbyte-cdk/python/unit_tests/sources/file_based/scenarios/avro_scenarios.py
  5. airbyte-cdk/python/unit_tests/sources/file_based/test_scenarios.py
  6. airbyte-cdk/python/unit_tests/sources/file_based/scenarios/csv_scenarios.py

@octavia-squidington-iii octavia-squidington-iii added the CDK Connector Development Kit label Aug 7, 2023
@@ -157,5 +157,11 @@ def _to_output_value(avro_format: AvroFormat, record_type: Mapping[str, Any], re
return record_value
if record_type.get("logicalType") == "uuid":
return uuid.UUID(bytes=record_value)
elif record_type.get("logicalType") == "decimal":
return str(record_value)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -398,7 +398,7 @@
"drummer": "George Daniel",
},
"col_fixed": "\x12\x34\x56\x78",
"col_decimal": 1234.56789,
"col_decimal": "1234.56789",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

decimals should always be read as strings

return str(record_value)
return record_value
if record_type.get("logicalType") == "uuid":
return uuid.UUID(bytes=record_value)
elif record_type.get("logicalType") == "decimal":
return str(record_value)
elif record_type.get("logicalType") == "date":
Copy link
Contributor Author

Choose a reason for hiding this comment

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

explicitly convert the output to the format we want instead of returning a date/datetime

@girarda girarda changed the title update avro parsing Avro parser: return Decimal fields as strings Aug 7, 2023
@girarda girarda marked this pull request as ready for review August 7, 2023 20:35
@girarda girarda requested a review from a team as a code owner August 7, 2023 20:35
@girarda girarda requested review from clnoll and brianjlai August 7, 2023 20:36
Copy link
Contributor

@brianjlai brianjlai left a comment

Choose a reason for hiding this comment

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

lgtm!

double_as_string: bool = Field(
title="Convert Double Fields to Strings",
description="Whether to convert double fields to strings. There is a loss of precision when converting decimals to floats, so this is recommended.",
default=True,
Copy link
Contributor

Choose a reason for hiding this comment

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

And to confirm. For legacy S3 connectors, the adapter will by default set this to False if I understand correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, created an issue #29225

Copy link
Contributor

@clnoll clnoll left a comment

Choose a reason for hiding this comment

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

Looks great @girarda!

@girarda girarda merged commit 1b64288 into master Aug 8, 2023
@girarda girarda deleted the alex/avro_numbers branch August 8, 2023 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CDK Connector Development Kit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants