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

Fix array of objects serialization #727

Closed
wants to merge 1 commit into from

Conversation

cchandurkar
Copy link

@cchandurkar cchandurkar commented Oct 17, 2023

Currently, array of structs/objects are being written as an array of stringified json objects.

Here's a quick comparison:
Expected value in ARRAY column:

[
  null,
  {
    "xCategory": null,
    "xEntityId": null,
    "xEntityName": null,
    "xId": "89asda9s0a"
  }
]

Actual value:

[
  "null",
  "{\"xCategory\":null,\"xEntityId\":null,\"xId\":\"89asda9s0a\",\"xEntityName\":null}"
]

I think it's happening because those objects/structs are getting serialized twice. A simple fix here would be to serialize an entire ArrayNode as is instead of creating a List<String> out of it. This way, the value (Stringified array) is a valid JSON that is accepted by net.snowflake.ingest.streaming.SnowflakeStreamingIngestChannel.insertRow()

Issue: #724

@@ -287,14 +287,7 @@ private Map<String, Object> getMapFromJsonNodeForStreamingIngest(JsonNode node)
String columnName = columnNames.next();
JsonNode columnNode = node.get(columnName);
Object columnValue;
if (columnNode.isArray()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an interesting observation, I assume this was added for a reason, we will evaluate it to see if it will break anything, thanks!

Copy link
Author

@cchandurkar cchandurkar Oct 18, 2023

Choose a reason for hiding this comment

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

Looks like it was introduced in PR that enabled schematization and haven't been updated since.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, I tired some quick experiments and I think it makes sense to make the change you suggested, it allows the array to be ingested with its original data type (like we should ingest [1,2] but not ["1","2"]. But for json, I believe you still need to do parse_json in order to use it since it will be ingested as a string?

Copy link
Author

@cchandurkar cchandurkar Oct 21, 2023

Choose a reason for hiding this comment

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

If you serialize an entire array node, its JSON elements won't be stringified unless they are stringified in original data. They will appear as "expected" in description. And that won't require to use parse_json() as array elements are variants.

@sfc-gh-tzhang sfc-gh-tzhang self-assigned this Oct 18, 2023
sfc-gh-tzhang added a commit that referenced this pull request Nov 3, 2023
…r Schematization (#730)

Before this change, every element in the array will be added as a STRING, this change preserves the old data type in the source, for example when the input is [1, 2], the ingested value will be [1, 2] now instead of ["1", "2"]

Forked from #727, with additional tests
@sfc-gh-xhuang
Copy link
Collaborator

Closing as #730 has been merged

khsoneji pushed a commit to confluentinc/snowflake-kafka-connector that referenced this pull request Dec 4, 2023
…r Schematization (snowflakedb#730)

Before this change, every element in the array will be added as a STRING, this change preserves the old data type in the source, for example when the input is [1, 2], the ingested value will be [1, 2] now instead of ["1", "2"]

Forked from snowflakedb#727, with additional tests
khsoneji pushed a commit to confluentinc/snowflake-kafka-connector that referenced this pull request Dec 4, 2023
…r Schematization (snowflakedb#730)

Before this change, every element in the array will be added as a STRING, this change preserves the old data type in the source, for example when the input is [1, 2], the ingested value will be [1, 2] now instead of ["1", "2"]

Forked from snowflakedb#727, with additional tests
khsoneji pushed a commit to confluentinc/snowflake-kafka-connector that referenced this pull request Dec 4, 2023
…r Schematization (snowflakedb#730)

Before this change, every element in the array will be added as a STRING, this change preserves the old data type in the source, for example when the input is [1, 2], the ingested value will be [1, 2] now instead of ["1", "2"]

Forked from snowflakedb#727, with additional tests
EduardHantig pushed a commit to streamkap-com/snowflake-kafka-connector that referenced this pull request Feb 1, 2024
…r Schematization (snowflakedb#730)

Before this change, every element in the array will be added as a STRING, this change preserves the old data type in the source, for example when the input is [1, 2], the ingested value will be [1, 2] now instead of ["1", "2"]

Forked from snowflakedb#727, with additional tests
sudeshwasnik pushed a commit to confluentinc/snowflake-kafka-connector that referenced this pull request Feb 16, 2024
…r Schematization (snowflakedb#730)

Before this change, every element in the array will be added as a STRING, this change preserves the old data type in the source, for example when the input is [1, 2], the ingested value will be [1, 2] now instead of ["1", "2"]

Forked from snowflakedb#727, with additional tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants