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
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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.

List<String> itemList = new ArrayList<>();
ArrayNode arrayNode = (ArrayNode) columnNode;
for (JsonNode e : arrayNode) {
itemList.add(e.isTextual() ? e.textValue() : MAPPER.writeValueAsString(e));
}
columnValue = itemList;
} else if (columnNode.isTextual()) {
if (columnNode.isTextual()) {
columnValue = columnNode.textValue();
} else if (columnNode.isNull()) {
columnValue = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,24 @@ public void testSchematizationStringField() throws JsonProcessingException {
assert got.get("\"ANSWER\"").equals("42");
}

@Test
public void testSchematizationArrayOfObject() throws JsonProcessingException {
RecordService service = new RecordService();
SnowflakeJsonConverter jsonConverter = new SnowflakeJsonConverter();

service.setEnableSchematization(true);
String value = "{\"players\":[{\"name\":\"John Doe\",\"age\":30},{\"name\":\"Jane Doe\",\"age\":30}]}";
byte[] valueContents = (value).getBytes(StandardCharsets.UTF_8);
SchemaAndValue sv = jsonConverter.toConnectData(topic, valueContents);

SinkRecord record =
new SinkRecord(
topic, partition, Schema.STRING_SCHEMA, "string", sv.schema(), sv.value(), partition);

Map<String, Object> got = service.getProcessedRecordForStreamingIngest(record);
assert got.get("\"PLAYERS\"").equals("[{\"name\":\"John Doe\",\"age\":30},{\"name\":\"Jane Doe\",\"age\":30}]");
}

@Test
public void testColumnNameFormatting() throws JsonProcessingException {
RecordService service = new RecordService();
Expand Down