Skip to content

Commit

Permalink
SNOW-1313555 - make fields metadata visible for customers (#1709)
Browse files Browse the repository at this point in the history
* SNOW-1313555 - make fields metadata visible for customers
  • Loading branch information
sfc-gh-pmotacki authored Apr 10, 2024
1 parent ebae0bd commit 402a90a
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 27 deletions.
22 changes: 11 additions & 11 deletions src/main/java/net/snowflake/client/core/SFArrowResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import net.snowflake.client.jdbc.ArrowResultChunk.ArrowChunkIterator;
import net.snowflake.client.jdbc.ErrorCode;
import net.snowflake.client.jdbc.FieldMetadata;
import net.snowflake.client.jdbc.SnowflakeColumnMetadata;
import net.snowflake.client.jdbc.SnowflakeResultSetSerializableV1;
import net.snowflake.client.jdbc.SnowflakeSQLException;
import net.snowflake.client.jdbc.SnowflakeSQLLoggedException;
Expand Down Expand Up @@ -586,19 +585,17 @@ private Object createJsonSqlInput(int columnIndex, Object obj) throws SFExceptio
jsonNode,
session,
converters,
resultSetMetaData.getColumnMetadata().get(columnIndex - 1).getFields(),
resultSetMetaData.getColumnFields(columnIndex),
sessionTimeZone);
} catch (JsonProcessingException e) {
throw new SFException(e, ErrorCode.INVALID_STRUCT_DATA);
}
}

private Object createArrowSqlInput(int columnIndex, Map<String, Object> input) {
private Object createArrowSqlInput(int columnIndex, Map<String, Object> input)
throws SFException {
return new ArrowSqlInput(
input,
session,
converters,
resultSetMetaData.getColumnMetadata().get(columnIndex - 1).getFields());
input, session, converters, resultSetMetaData.getColumnFields(columnIndex));
}

@Override
Expand All @@ -621,10 +618,13 @@ public Array getArray(int columnIndex) throws SFException {

private SfSqlArray getArrowArray(List<Object> elements, int columnIndex) throws SFException {
try {
SnowflakeColumnMetadata arrayMetadata =
resultSetMetaData.getColumnMetadata().get(columnIndex - 1);
FieldMetadata fieldMetadata = arrayMetadata.getFields().get(0);

List<FieldMetadata> fieldMetadataList = resultSetMetaData.getColumnFields(columnIndex);
if (fieldMetadataList.size() != 1) {
throw new SFException(
ErrorCode.INVALID_STRUCT_DATA,
"Wrong size of fields for array type " + fieldMetadataList.size());
}
FieldMetadata fieldMetadata = fieldMetadataList.get(0);
int columnSubType = fieldMetadata.getType();
int columnType = ColumnTypeHelper.getColumnType(columnSubType, session);
int scale = fieldMetadata.getScale();
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/net/snowflake/client/core/SFBaseResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import net.snowflake.client.core.json.Converters;
import net.snowflake.client.jdbc.ErrorCode;
import net.snowflake.client.jdbc.FieldMetadata;
import net.snowflake.client.jdbc.SnowflakeColumnMetadata;
import net.snowflake.client.jdbc.SnowflakeResultSetSerializable;
import net.snowflake.client.jdbc.SnowflakeResultSetSerializableV1;
import net.snowflake.client.jdbc.SnowflakeSQLException;
Expand Down Expand Up @@ -275,9 +274,14 @@ protected SQLInput createJsonSqlInputForColumn(
@SnowflakeJdbcInternalApi
protected SfSqlArray getJsonArray(String obj, int columnIndex) throws SFException {
try {
SnowflakeColumnMetadata arrayMetadata =
resultSetMetaData.getColumnMetadata().get(columnIndex - 1);
FieldMetadata fieldMetadata = arrayMetadata.getFields().get(0);
List<FieldMetadata> fieldMetadataList = resultSetMetaData.getColumnFields(columnIndex);
if (fieldMetadataList.size() != 1) {
throw new SFException(
ErrorCode.FEATURE_UNSUPPORTED,
"Wrong size of fields for array type " + fieldMetadataList.size());
}

FieldMetadata fieldMetadata = fieldMetadataList.get(0);

int columnSubType = fieldMetadata.getType();
int columnType = ColumnTypeHelper.getColumnType(columnSubType, session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ private Object getSqlInput(String input, int columnIndex) throws SFException {
jsonNode,
session,
converters,
resultSetMetaData.getColumnMetadata().get(columnIndex - 1).getFields(),
resultSetMetaData.getColumnFields(columnIndex),
sessionTimeZone);
} catch (JsonProcessingException e) {
throw new SFException(e, ErrorCode.INVALID_STRUCT_DATA);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/net/snowflake/client/core/SFResultSetMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Map;
import java.util.TimeZone;
import net.snowflake.client.jdbc.ErrorCode;
import net.snowflake.client.jdbc.FieldMetadata;
import net.snowflake.client.jdbc.SnowflakeColumnMetadata;
import net.snowflake.client.jdbc.SnowflakeUtil;
import net.snowflake.client.log.SFLogger;
Expand Down Expand Up @@ -474,7 +475,15 @@ public List<Boolean> getIsAutoIncrementList() {
}

@SnowflakeJdbcInternalApi
public List<SnowflakeColumnMetadata> getColumnMetadata() {
return columnMetadata;
public List<FieldMetadata> getColumnFields(int column) throws SFException {
if (column < 1 || column > columnMetadata.size()) {
throw new SFException(ErrorCode.COLUMN_DOES_NOT_EXIST, column);
}

if (columnMetadata.get(column - 1) == null) {
throw new SFException(ErrorCode.INTERNAL_ERROR, "Missing column fields for column " + column);
}

return columnMetadata.get(column - 1).getFields();
}
}
2 changes: 0 additions & 2 deletions src/main/java/net/snowflake/client/jdbc/FieldMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
package net.snowflake.client.jdbc;

import java.util.List;
import net.snowflake.client.core.SnowflakeJdbcInternalApi;

@SnowflakeJdbcInternalApi
public class FieldMetadata {

private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1419,8 +1419,11 @@ public <T> T[] getArray(int columnIndex, Class<T> type) throws SQLException {
if (!StructureTypeHelper.isStructureTypeEnabled()) {
throw new SnowflakeLoggedFeatureNotSupportedException(session);
}
FieldMetadata fieldMetadata =
sfBaseResultSet.getMetaData().getColumnMetadata().get(columnIndex - 1).getFields().get(0);
List<FieldMetadata> fieldMetadataList = resultSetMetaData.getColumnFields(columnIndex);
if (fieldMetadataList.size() != 1) {
throw new SQLException("Wrong size of fields for array type " + fieldMetadataList.size());
}
FieldMetadata fieldMetadata = fieldMetadataList.get(0);
int columnSubType = fieldMetadata.getType();
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
int scale = fieldMetadata.getScale();
Expand Down Expand Up @@ -1569,8 +1572,12 @@ public <T> Map<String, T> getMap(int columnIndex, Class<T> type) throws SQLExcep
if (!StructureTypeHelper.isStructureTypeEnabled()) {
throw new SnowflakeLoggedFeatureNotSupportedException(session);
}
FieldMetadata valueFieldMetadata =
sfBaseResultSet.getMetaData().getColumnMetadata().get(columnIndex - 1).getFields().get(1);
List<FieldMetadata> fieldMetadataList = resultSetMetaData.getColumnFields(columnIndex);
if (fieldMetadataList.size() != 2) {
throw new SQLException(
"Wrong size of fields metadata for map type " + fieldMetadataList.size());
}
FieldMetadata valueFieldMetadata = fieldMetadataList.get(1);
int columnSubType = valueFieldMetadata.getType();
int columnType = ColumnTypeHelper.getColumnType(valueFieldMetadata.getType(), session);
int scale = valueFieldMetadata.getScale();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface SnowflakeResultSetMetaData {
int getColumnIndex(String columnName) throws SQLException;

int getInternalColumnType(int column) throws SQLException;

List<FieldMetadata> getColumnFields(int column) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public int getInternalColumnType(int column) throws SQLException {
}
}

@Override
public List<FieldMetadata> getColumnFields(int column) throws SQLException {
return SnowflakeUtil.mapSFExceptionToSQLException(
() -> resultSetMetaData.getColumnFields(column));
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
logger.debug("public <T> T unwrap(Class<T> iface)", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.snowflake.client.core.structs.StructureTypeHelper;
import net.snowflake.client.jdbc.BaseJDBCTest;
import net.snowflake.client.jdbc.SnowflakeBaseResultSet;
import net.snowflake.client.jdbc.SnowflakeResultSetMetaData;
import net.snowflake.client.jdbc.structuredtypes.sqldata.AllTypesClass;
import net.snowflake.client.jdbc.structuredtypes.sqldata.FewTypesSqlData;
import net.snowflake.client.jdbc.structuredtypes.sqldata.SimpleClass;
Expand All @@ -43,6 +44,7 @@

@RunWith(Parameterized.class)
@Category(TestCategoryStructuredType.class)
@Ignore
public class ResultSetStructuredTypesLatestIT extends BaseJDBCTest {

@Parameterized.Parameters(name = "format={0}")
Expand Down Expand Up @@ -650,11 +652,11 @@ public void testMapArrayOfArrays() throws SQLException {
}

@Test
public void testColumnTypeWhenStructureTypeIsDisabled() throws Exception {
public void testColumnTypeWhenStructureTypeIsNotReturned() throws Exception {
withStructureTypeTemporaryDisabled(
() -> {
withFirstRow(
"SELECT {'string':'a'}::OBJECT(string VARCHAR)",
"SELECT {'string':'a'}",
resultSet -> {
assertEquals(Types.VARCHAR, resultSet.getMetaData().getColumnType(1));
});
Expand All @@ -663,13 +665,36 @@ public void testColumnTypeWhenStructureTypeIsDisabled() throws Exception {

@Test
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
public void testColumnTypeWhenStructureTypeIsEnabled() throws Exception {
public void testColumnTypeAndFieldsWhenStructureTypeIsReturned() throws Exception {
withStructureTypeTemporaryEnabled(
() -> {
withFirstRow(
"SELECT {'string':'a'}::OBJECT(string VARCHAR)",
resultSet -> {
assertEquals(Types.STRUCT, resultSet.getMetaData().getColumnType(1));
assertEquals(
1,
resultSet
.getMetaData()
.unwrap(SnowflakeResultSetMetaData.class)
.getColumnFields(1)
.size());
assertEquals(
"VARCHAR",
resultSet
.getMetaData()
.unwrap(SnowflakeResultSetMetaData.class)
.getColumnFields(1)
.get(0)
.getTypeName());
assertEquals(
"string",
resultSet
.getMetaData()
.unwrap(SnowflakeResultSetMetaData.class)
.getColumnFields(1)
.get(0)
.getName());
});
});
}
Expand Down

0 comments on commit 402a90a

Please sign in to comment.