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

SNOW-1234216 Native Arrow structured types array support #1687

Merged
merged 32 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
84228c8
SNOW-1259709 - add mapping for basic types for arrays and maps
sfc-gh-pmotacki Mar 21, 2024
978e14a
SNOW-1259709 - add mapping for basic types for arrays and maps
sfc-gh-pmotacki Mar 21, 2024
9169852
SNOW-1259709 - add mapping for basic types for arrays and maps
sfc-gh-pmotacki Mar 21, 2024
1369a88
SNOW-1259709 - add mapping for basic types for arrays and maps
sfc-gh-pmotacki Mar 22, 2024
f531d93
SNOW-1259709 - add mapping for basic types for arrays and maps
sfc-gh-pmotacki Mar 22, 2024
c91f0b1
SNOW-1259709 - add mapping for basic types for arrays and maps
sfc-gh-pmotacki Mar 22, 2024
baf98e9
SNOW-1259709 - add mapping for basic types for arrays and maps
sfc-gh-pmotacki Mar 25, 2024
73c0f05
SNOW-1234214 Add support for maps in native arrow structured types
sfc-gh-dheyman Mar 27, 2024
155aaf7
Conflicts resolved
sfc-gh-dheyman Mar 28, 2024
49945a1
SNOW-1234216 Native Arrow structured types arrays support
sfc-gh-dheyman Mar 28, 2024
31a9f5a
Add internal arrow converter infra for varchars
sfc-gh-dheyman Mar 28, 2024
b463b02
Conflicts resolved
sfc-gh-dheyman Mar 28, 2024
c9cb74e
Fix map parsing
sfc-gh-dheyman Mar 28, 2024
62c4b8c
Mapping to json node
sfc-gh-pmotacki Mar 29, 2024
3b948af
Mapping to json node
sfc-gh-pmotacki Mar 29, 2024
11af0d4
Add support for structured types native arrow arrays
sfc-gh-dheyman Mar 29, 2024
6e0a26a
Conflicts resolved
sfc-gh-dheyman Mar 29, 2024
b1a7a3c
Add SnowflakeJdbcInternalApi annotation to converters
sfc-gh-dheyman Apr 1, 2024
9e326d8
Revert "Add SnowflakeJdbcInternalApi annotation to converters"
sfc-gh-dheyman Apr 2, 2024
8ac8dbb
Conclicts resolved
sfc-gh-dheyman Apr 2, 2024
2604eb3
Fix casting
sfc-gh-dheyman Apr 2, 2024
3ec0b41
Change package of converters class
sfc-gh-dheyman Apr 2, 2024
2b490b5
Fix japicmp
sfc-gh-dheyman Apr 2, 2024
ec1325f
reformat
sfc-gh-dheyman Apr 2, 2024
af14680
Add arrow with json structured types support
sfc-gh-dheyman Apr 2, 2024
9b26950
Add flag dependency on structured types
sfc-gh-dheyman Apr 2, 2024
34e0ee7
Remove proxy
sfc-gh-dheyman Apr 2, 2024
fbf69e2
Change message
sfc-gh-dheyman Apr 2, 2024
be0adbe
CR suggestions
sfc-gh-dheyman Apr 2, 2024
0c4b17b
Fix
sfc-gh-dheyman Apr 2, 2024
73aa58c
Fix duplicated timezone
sfc-gh-dheyman Apr 2, 2024
dfd48a4
Reformat
sfc-gh-dheyman Apr 2, 2024
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 @@ -16,7 +16,6 @@
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import net.snowflake.client.core.json.Converters;
import net.snowflake.client.core.structs.SQLDataCreationHelper;
import net.snowflake.client.jdbc.FieldMetadata;
import net.snowflake.client.util.ThrowingBiFunction;
Expand Down Expand Up @@ -187,11 +186,7 @@ public Timestamp readTimestamp(TimeZone tz) throws SQLException {
converters
.getStructuredTypeDateTimeConverter()
.getTimestamp(
(JsonStringHashMap<String, Object>) value,
columnType,
columnSubType,
tz,
scale));
(Map<String, Object>) value, columnType, columnSubType, tz, scale));
});
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/net/snowflake/client/core/BaseSqlInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.sql.SQLXML;
import java.sql.Timestamp;
import java.util.List;
import net.snowflake.client.core.json.Converters;
import net.snowflake.client.jdbc.FieldMetadata;
import net.snowflake.client.jdbc.SnowflakeLoggedFeatureNotSupportedException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.snowflake.client.core.json;
package net.snowflake.client.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -11,18 +11,20 @@
import java.util.Arrays;
import java.util.Map;
import java.util.TimeZone;
import net.snowflake.client.core.SFBaseSession;
import net.snowflake.client.core.SFException;
import net.snowflake.client.core.SnowflakeJdbcInternalApi;
import net.snowflake.client.core.SqlInputTimestampUtil;
import net.snowflake.client.core.arrow.StructuredTypeDateTimeConverter;
import net.snowflake.client.core.json.BooleanConverter;
import net.snowflake.client.core.json.BytesConverter;
import net.snowflake.client.core.json.DateTimeConverter;
import net.snowflake.client.core.json.NumberConverter;
import net.snowflake.client.core.json.StringConverter;
import net.snowflake.client.jdbc.ErrorCode;
import net.snowflake.client.jdbc.SnowflakeResultSetSerializableV1;
import net.snowflake.client.util.JsonStringToTypeConverter;
import net.snowflake.client.util.Converter;
import net.snowflake.common.core.SFBinaryFormat;
import net.snowflake.common.core.SFTimestamp;
import net.snowflake.common.core.SnowflakeDateTimeFormat;

@SnowflakeJdbcInternalApi
public class Converters {
private final BooleanConverter booleanConverter;
private final NumberConverter numberConverter;
Expand Down Expand Up @@ -122,47 +124,47 @@ public StructuredTypeDateTimeConverter getStructuredTypeDateTimeConverter() {
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter integerConverter(int columnType) {
public Converter integerConverter(int columnType) {
return value -> getNumberConverter().getInt(value, columnType);
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter smallIntConverter(int columnType) {
public Converter smallIntConverter(int columnType) {
return value -> getNumberConverter().getShort(value, columnType);
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter tinyIntConverter(int columnType) {
public Converter tinyIntConverter(int columnType) {
return value -> getNumberConverter().getByte(value);
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter bigIntConverter(int columnType) {
public Converter bigIntConverter(int columnType) {
return value -> getNumberConverter().getBigInt(value, columnType);
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter longConverter(int columnType) {
public Converter longConverter(int columnType) {
return value -> getNumberConverter().getLong(value, columnType);
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter bigDecimalConverter(int columnType) {
public Converter bigDecimalConverter(int columnType) {
return value -> getNumberConverter().getBigDecimal(value, columnType);
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter floatConverter(int columnType) {
public Converter floatConverter(int columnType) {
return value -> getNumberConverter().getBigDecimal(value, columnType);
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter doubleConverter(int columnType) {
public Converter doubleConverter(int columnType) {
return value -> getNumberConverter().getBigDecimal(value, columnType);
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter bytesConverter(int columnType, int scale) {
public Converter bytesConverter(int columnType, int scale) {
return value -> {
byte[] primitiveArray = getBytesConverter().getBytes(value, columnType, Types.BINARY, scale);
Byte[] newByteArray = new Byte[primitiveArray.length];
Expand All @@ -172,29 +174,34 @@ public JsonStringToTypeConverter bytesConverter(int columnType, int scale) {
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter varcharConverter(int columnType, int columnSubType, int scale) {
public Converter varcharConverter(int columnType, int columnSubType, int scale) {
return value -> getStringConverter().getString(value, columnType, columnSubType, scale);
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter booleanConverter(int columnType) {
public Converter booleanConverter(int columnType) {
return value -> getBooleanConverter().getBoolean(value, columnType);
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter dateConverter(SFBaseSession session) {
public Converter dateStringConverter(SFBaseSession session) {
return value -> {
SnowflakeDateTimeFormat formatter =
SnowflakeDateTimeFormat.fromSqlFormat(
(String) session.getCommonParameters().get("DATE_OUTPUT_FORMAT"));
SFTimestamp timestamp = formatter.parse(value);
SFTimestamp timestamp = formatter.parse((String) value);
return Date.valueOf(
Instant.ofEpochMilli(timestamp.getTime()).atZone(ZoneOffset.UTC).toLocalDate());
};
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter timeConverter(SFBaseSession session) {
public Converter dateFromIntConverter(TimeZone tz) {
return value -> structuredTypeDateTimeConverter.getDate((Integer) value, tz);
}

@SnowflakeJdbcInternalApi
public Converter timeFromStringConverter(SFBaseSession session) {
return value -> {
SnowflakeDateTimeFormat formatter =
SnowflakeDateTimeFormat.fromSqlFormat(
Expand All @@ -205,8 +212,12 @@ public JsonStringToTypeConverter timeConverter(SFBaseSession session) {
};
}

public Converter timeFromIntConverter(int scale) {
return value -> structuredTypeDateTimeConverter.getTime((Long) value, scale);
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter timestampConverter(
public Converter timestampFromStringConverter(
int columnSubType,
int columnType,
int scale,
Expand All @@ -225,22 +236,29 @@ public JsonStringToTypeConverter timestampConverter(
};
}

public Converter timestampFromStructConverter(
int columnType, int columnSubType, TimeZone tz, int scale) {
return value ->
structuredTypeDateTimeConverter.getTimestamp(
(Map<String, Object>) value, columnType, columnSubType, tz, scale);
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter structConverter(ObjectMapper objectMapper) {
public Converter structConverter(ObjectMapper objectMapper) {
return value -> {
try {
return objectMapper.readValue(value, Map.class);
return objectMapper.readValue((String) value, Map.class);
} catch (JsonProcessingException e) {
throw new SFException(e, ErrorCode.INVALID_STRUCT_DATA);
}
};
}

@SnowflakeJdbcInternalApi
public JsonStringToTypeConverter arrayConverter(ObjectMapper objectMapper) {
public Converter arrayConverter(ObjectMapper objectMapper) {
return value -> {
try {
return objectMapper.readValue(value, Map[].class);
return objectMapper.readValue((String) value, Map[].class);
} catch (JsonProcessingException e) {
throw new SFException(e, ErrorCode.INVALID_STRUCT_DATA);
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/net/snowflake/client/core/JsonSqlInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;
import net.snowflake.client.core.json.Converters;
import net.snowflake.client.core.structs.SQLDataCreationHelper;
import net.snowflake.client.jdbc.FieldMetadata;
import net.snowflake.client.jdbc.SnowflakeLoggedFeatureNotSupportedException;
Expand Down
136 changes: 133 additions & 3 deletions src/main/java/net/snowflake/client/core/SFArrowResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@
import java.sql.Array;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.stream.Stream;
import net.snowflake.client.core.arrow.ArrowVectorConverter;
import net.snowflake.client.core.arrow.StructConverter;
import net.snowflake.client.core.arrow.VarCharConverter;
import net.snowflake.client.core.json.Converters;
import net.snowflake.client.core.structs.StructureTypeHelper;
import net.snowflake.client.jdbc.ArrowResultChunk;
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 All @@ -38,6 +42,7 @@
import net.snowflake.client.jdbc.telemetry.TelemetryUtil;
import net.snowflake.client.log.SFLogger;
import net.snowflake.client.log.SFLoggerFactory;
import net.snowflake.client.util.Converter;
import net.snowflake.common.core.SFBinaryFormat;
import net.snowflake.common.core.SnowflakeDateTimeFormat;
import net.snowflake.common.core.SqlState;
Expand Down Expand Up @@ -364,16 +369,29 @@ public Converters getConverters() {
}

@Override
@SnowflakeJdbcInternalApi
public SQLInput createSqlInputForColumn(Object input, int columnIndex, SFBaseSession session) {
return new ArrowSqlInput(
(Map<String, Object>) input,
session,
converters,
resultSetMetaData.getColumnMetadata().get(columnIndex - 1).getFields());
}

@Override
@SnowflakeJdbcInternalApi
public Date convertToDate(Object object, TimeZone tz) throws SFException {
return converters.getStructuredTypeDateTimeConverter().getDate((int) object, tz);
}

@Override
@SnowflakeJdbcInternalApi
public Time convertToTime(Object object, int scale) throws SFException {
return converters.getStructuredTypeDateTimeConverter().getTime((long) object, scale);
}

@Override
@SnowflakeJdbcInternalApi
public Timestamp convertToTimestamp(
Object object, int columnType, int columnSubType, TimeZone tz, int scale) throws SFException {
return converters
Expand Down Expand Up @@ -566,8 +584,120 @@ private Object createArrowSqlInput(int columnIndex, Map<String, Object> input) {

@Override
public Array getArray(int columnIndex) throws SFException {
// TODO: handleArray SNOW-969794
throw new SFException(ErrorCode.FEATURE_UNSUPPORTED, "data type ARRAY");
ArrowVectorConverter converter = currentChunkIterator.getCurrentConverter(columnIndex - 1);
int index = currentChunkIterator.getCurrentRowInRecordBatch();
wasNull = converter.isNull(index);
Object obj = converter.toObject(index);
return getArrayInternal((List<Object>) obj, columnIndex);
}

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

int columnSubType = fieldMetadata.getType();
int columnType = ColumnTypeHelper.getColumnType(columnSubType, session);
int scale = fieldMetadata.getScale();

switch (columnSubType) {
case Types.INTEGER:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, converters.integerConverter(columnType))
.toArray(Integer[]::new));
case Types.SMALLINT:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, converters.smallIntConverter(columnType))
.toArray(Short[]::new));
case Types.TINYINT:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, converters.tinyIntConverter(columnType))
.toArray(Byte[]::new));
case Types.BIGINT:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, converters.bigIntConverter(columnType)).toArray(Long[]::new));
case Types.DECIMAL:
case Types.NUMERIC:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, converters.bigDecimalConverter(columnType))
.toArray(BigDecimal[]::new));
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGNVARCHAR:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, converters.varcharConverter(columnType, columnSubType, scale))
.toArray(String[]::new));
case Types.BINARY:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, converters.bytesConverter(columnType, scale))
.toArray(Byte[][]::new));
case Types.FLOAT:
case Types.REAL:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, converters.floatConverter(columnType)).toArray(Float[]::new));
case Types.DOUBLE:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, converters.doubleConverter(columnType))
.toArray(Double[]::new));
case Types.DATE:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, converters.dateFromIntConverter(sessionTimezone))
.toArray(Date[]::new));
case Types.TIME:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, converters.timeFromIntConverter(scale)).toArray(Time[]::new));
case Types.TIMESTAMP:
return new SfSqlArray(
columnSubType,
mapAndConvert(
elements,
converters.timestampFromStructConverter(
columnType, columnSubType, sessionTimezone, scale))
.toArray(Timestamp[]::new));
case Types.BOOLEAN:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, converters.booleanConverter(columnType))
.toArray(Boolean[]::new));
case Types.STRUCT:
return new SfSqlArray(columnSubType, mapAndConvert(elements, e -> e).toArray(Map[]::new));
case Types.ARRAY:
return new SfSqlArray(
columnSubType,
mapAndConvert(elements, e -> ((List) e).stream().toArray(Map[]::new))
.toArray(Map[][]::new));
default:
throw new SFException(
ErrorCode.FEATURE_UNSUPPORTED,
"Can't construct array for data type: " + columnSubType);
}
} catch (RuntimeException e) {
throw new SFException(e, ErrorCode.INVALID_STRUCT_DATA);
}
}

private <T> Stream<T> mapAndConvert(List<Object> elements, Converter<T> converter) {
return elements.stream()
.map(
obj -> {
try {
return converter.convert(obj);
} catch (SFException e) {
throw new RuntimeException(e);
}
});
}

@Override
Expand Down
Loading
Loading