Skip to content

Commit

Permalink
Add tests for nested datatype
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-alhuang committed Nov 5, 2024
1 parent 3fed4f1 commit 527e7d9
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static net.snowflake.ingest.utils.Constants.ROLE;
import static net.snowflake.ingest.utils.ParameterProvider.BDEC_PARQUET_COMPRESSION_ALGORITHM;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -630,14 +631,18 @@ protected void verifyMultipleColumns(
}
}

private void assertEqualValues(Object expectedValue, Object actualValue) {
private void assertEqualValues(Object expectedValue, Object actualValue)
throws JsonProcessingException {
if (expectedValue instanceof BigDecimal) {
Assertions.assertThat(actualValue)
.usingComparatorForType(BigDecimal::compareTo, BigDecimal.class)
.usingRecursiveComparison()
.isEqualTo(expectedValue);
} else if (expectedValue instanceof Timestamp) {
Assertions.assertThat(actualValue.toString()).isEqualTo(expectedValue.toString());
} else if (expectedValue instanceof Map) {
Assertions.assertThat(objectMapper.readTree((String) actualValue))
.isEqualTo(objectMapper.valueToTree(expectedValue));
} else {
Assertions.assertThat(actualValue).isEqualTo(expectedValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,106 @@ public void testPrimitiveColumns() throws Exception {
verifyMultipleColumns(
tableName, Collections.singletonList(newValue), Arrays.asList(value, newValue), "id");
}

@Test
public void testStructType() throws Exception {
String tableName = createIcebergTableWithColumns("id int, object_col object(a int)");
Map<String, Object> value = new HashMap<>();
value.put("id", 0L);
value.put("object_col", Collections.singletonMap("a", 1));
verifyMultipleColumns(
tableName, Collections.singletonList(value), Collections.singletonList(value), "id");

conn.createStatement()
.execute(
String.format(
"ALTER ICEBERG TABLE %s ALTER COLUMN object_col SET DATA TYPE object(a int, b int)",
tableName));
value.put(
"object_col",
new HashMap<String, Object>() {
{
put("a", 1);
put("b", null);
}
});

Map<String, Object> newValue = new HashMap<>();
newValue.put("id", 1L);
newValue.put(
"object_col",
new HashMap<String, Object>() {
{
put("a", 2);
put("b", 3);
}
});
verifyMultipleColumns(
tableName, Collections.singletonList(newValue), Arrays.asList(value, newValue), "id");
}

@Test
public void testNestedDataType() throws Exception {
String tableName =
createIcebergTableWithColumns(
"id int, object_col object(map_col map(string, array(object(a int))))");
Map<String, Object> value = new HashMap<>();
value.put("id", 0L);
value.put(
"object_col",
Collections.singletonMap(
"map_col",
Collections.singletonMap(
"key", Collections.singletonList(Collections.singletonMap("a", 1)))));
verifyMultipleColumns(
tableName, Collections.singletonList(value), Collections.singletonList(value), "id");

conn.createStatement()
.execute(
String.format(
"ALTER ICEBERG TABLE %s ALTER COLUMN object_col SET DATA TYPE object(map_col"
+ " map(string, array(object(a int, b int))), map_col_2 map(string, int))",
tableName));
value.put(
"object_col",
new HashMap<String, Object>() {
{
put(
"map_col",
Collections.singletonMap(
"key",
Collections.singletonList(
new HashMap<String, Object>() {
{
put("a", 1);
put("b", null);
}
})));
put("map_col_2", null);
}
});

Map<String, Object> newValue = new HashMap<>();
newValue.put("id", 1L);
newValue.put(
"object_col",
new HashMap<String, Object>() {
{
put(
"map_col",
Collections.singletonMap(
"key",
Collections.singletonList(
new HashMap<String, Object>() {
{
put("a", 2);
put("b", 3);
}
})));
put("map_col_2", Collections.singletonMap("key", 4));
}
});
verifyMultipleColumns(
tableName, Collections.singletonList(newValue), Arrays.asList(value, newValue), "id");
}
}

0 comments on commit 527e7d9

Please sign in to comment.