Skip to content

Commit

Permalink
SNOW-1479614: Fix conversion of OBJECT column nested fields metadata (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-dheyman authored Jul 18, 2024
1 parent b9dcc47 commit 1c0f40a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/main/java/net/snowflake/client/jdbc/SnowflakeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,13 @@ static List<FieldMetadata> createFieldsMetadata(
throws SnowflakeSQLLoggedException {
List<FieldMetadata> fields = new ArrayList<>();
for (JsonNode node : fieldsJson) {
String colName = node.path("name").asText();
String colName;
if (!node.path("fieldType").isEmpty()) {
colName = node.path("fieldName").asText();
node = node.path("fieldType");
} else {
colName = node.path("name").asText();
}
int scale = node.path("scale").asInt();
int precision = node.path("precision").asInt();
String internalColTypeName = node.path("type").asText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
package net.snowflake.client.jdbc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Expand All @@ -32,4 +35,27 @@ public void testGetObjectNotSupported() throws SQLException {
}
}
}

/** Added in > 3.17.0 */
@Test
public void testObjectColumn() throws SQLException {
try (Connection connection = getConnection();
Statement statement = connection.createStatement()) {
statement.execute(
"CREATE OR REPLACE TABLE TABLEWITHOBJECTCOLUMN ("
+ " col OBJECT("
+ " str VARCHAR,"
+ " num NUMBER(38,0)"
+ " )"
+ " )");
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet resultSet =
metaData.getColumns(
connection.getCatalog(), connection.getSchema(), "TABLEWITHOBJECTCOLUMN", null)) {
assertTrue(resultSet.next());
assertEquals("OBJECT", resultSet.getObject("TYPE_NAME"));
assertFalse(resultSet.next());
}
}
}
}
38 changes: 37 additions & 1 deletion src/test/java/net/snowflake/client/jdbc/SnowflakeUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import static net.snowflake.client.jdbc.SnowflakeUtil.getSnowflakeType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -33,7 +35,7 @@ public void testCreateMetadata() throws Throwable {
fields.add(fieldOne);
JsonNode fieldTwo = createFieldNode("name2", 5, 128, 2, "real", true, "collation", 256);
fields.add(fieldTwo);
rootNode.put("fields", fields);
rootNode.putIfAbsent("fields", fields);
SnowflakeColumnMetadata expectedColumnMetadata =
createExpectedMetadata(rootNode, fieldOne, fieldTwo);
// when
Expand All @@ -46,6 +48,40 @@ public void testCreateMetadata() throws Throwable {
OBJECT_MAPPER.writeValueAsString(columnMetadata));
}

@Test
public void testCreateFieldsMetadataForObject() throws Throwable {
// given
ObjectNode rootNode = createRootNode();
ArrayNode fields = OBJECT_MAPPER.createArrayNode();
fields.add(
OBJECT_MAPPER.readTree(
"{\"fieldName\":\"name1\", \"fieldType\": {\"type\":\"text\",\"precision\":null,\"length\":256,\"scale\":null,\"nullable\":false}}"));
fields.add(
OBJECT_MAPPER.readTree(
"{\"fieldName\":\"name2\", \"fieldType\": {\"type\":\"real\",\"precision\":5,\"length\":128,\"scale\":null,\"nullable\":true}}"));
rootNode.putIfAbsent("fields", fields);

// when
SnowflakeColumnMetadata columnMetadata =
SnowflakeUtil.extractColumnMetadata(rootNode, false, null);
// then
assertNotNull(columnMetadata);
assertEquals("OBJECT", columnMetadata.getTypeName());

FieldMetadata firstField = columnMetadata.getFields().get(0);
assertEquals("name1", firstField.getName());
assertEquals(SnowflakeType.TEXT, firstField.getBase());
assertEquals(256, firstField.getByteLength());
assertFalse(firstField.isNullable());

FieldMetadata secondField = columnMetadata.getFields().get(1);
assertEquals("name2", secondField.getName());
assertEquals(SnowflakeType.REAL, secondField.getBase());
assertEquals(128, secondField.getByteLength());
assertEquals(5, secondField.getPrecision());
assertTrue(secondField.isNullable());
}

private static SnowflakeColumnMetadata createExpectedMetadata(
JsonNode rootNode, JsonNode fieldOne, JsonNode fieldTwo) throws SnowflakeSQLLoggedException {
ColumnTypeInfo columnTypeInfo =
Expand Down

0 comments on commit 1c0f40a

Please sign in to comment.