Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature: Add custom flatten schema level support #12

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions meltano.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ plugins:
- name: extra_fields
- name: extra_fields_types
- name: partition_cols
- name: max_flatten_level
kind: integer
config:
start_date: '2010-01-01T00:00:00Z'
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "target-parquet"
version = "1.0.3"
version = "1.1.0"
description = "`target-parquet` is a Singer target for parquet, built with the Meltano Singer SDK."
readme = "README.md"
authors = ["Joao Amaral <joao.amaral@automattic.com>"]
Expand Down
3 changes: 1 addition & 2 deletions target_parquet/sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
class ParquetSink(BatchSink):
"""parquet target sink class."""

flatten_max_level = 100 # Max level of nesting to flatten

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.pyarrow_df = None
self.destination_path = os.path.join(
self.config.get("destination_path", "output"), self.stream_name
)
self.files_saved = 0
self.flatten_max_level = self.config.get("max_flatten_level", 100)

# Extra fields
self.extra_values = (
Expand Down
6 changes: 6 additions & 0 deletions target_parquet/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class TargetParquet(Target):
"It can control the memory usage of the target.",
default=10000,
),
th.Property(
"max_flatten_level",
th.IntegerType,
description="Max level of nesting to flatten",
default=100,
),
th.Property(
"extra_fields",
th.StringType,
Expand Down
36 changes: 36 additions & 0 deletions tests/utils/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,42 @@ def test_flatten_schema_to_pyarrow_schema():
assert pyarrow_schema == expected_pyarrow_schema


def test_no_flatten_schema_to_pyarrow():
schema = {
"type": "object",
"properties": {
"str": {"type": ["null", "string"]},
"int": {"type": ["null", "integer"]},
"decimal": {"type": ["null", "number"]},
"nested": {
"type": "object",
"properties": {
"nested_str": {"type": ["null", "string"]},
"nested_int": {"type": ["null", "integer"]},
"deep_nested": {
"type": "object",
"properties": {
"deep_str": {"type": ["null", "string"]},
},
},
},
},
},
}

flatten_schema_result = flatten_schema(schema, max_level=0)
pyarrow_schema = flatten_schema_to_pyarrow_schema(flatten_schema_result)
expected_pyarrow_schema = pa.schema(
[
pa.field("str", pa.string()),
pa.field("int", pa.int64()),
pa.field("decimal", pa.float64()),
pa.field("nested", pa.string()),
]
)

assert pyarrow_schema == expected_pyarrow_schema

@pytest.mark.parametrize(
"field_name, input_types, expected_result",
[
Expand Down