Skip to content

Commit 7d7b16c

Browse files
authoredMar 27, 2024··
SNOW-1232333 - add mapping for basic types form arrays and maps (#1679)
* SNOW-1259709 - add mapping for basic types for arrays and maps
1 parent 63b654d commit 7d7b16c

File tree

6 files changed

+388
-54
lines changed

6 files changed

+388
-54
lines changed
 

‎src/main/java/net/snowflake/client/core/ArrowSqlInput.java

+19-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package net.snowflake.client.core;
66

7-
import static net.snowflake.client.jdbc.SnowflakeUtil.mapExceptions;
7+
import static net.snowflake.client.jdbc.SnowflakeUtil.mapSFExceptionToSQLException;
88

99
import java.math.BigDecimal;
1010
import java.sql.Date;
@@ -43,7 +43,7 @@ public String readString() throws SQLException {
4343
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
4444
int columnSubType = fieldMetadata.getType();
4545
int scale = fieldMetadata.getScale();
46-
return mapExceptions(
46+
return mapSFExceptionToSQLException(
4747
() ->
4848
converters
4949
.getStringConverter()
@@ -56,7 +56,7 @@ public boolean readBoolean() throws SQLException {
5656
return withNextValue(
5757
(value, fieldMetadata) -> {
5858
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
59-
return mapExceptions(
59+
return mapSFExceptionToSQLException(
6060
() -> converters.getBooleanConverter().getBoolean(value, columnType));
6161
});
6262
}
@@ -65,15 +65,16 @@ public boolean readBoolean() throws SQLException {
6565
public byte readByte() throws SQLException {
6666
return withNextValue(
6767
(value, fieldMetadata) ->
68-
mapExceptions(() -> converters.getNumberConverter().getByte(value)));
68+
mapSFExceptionToSQLException(() -> converters.getNumberConverter().getByte(value)));
6969
}
7070

7171
@Override
7272
public short readShort() throws SQLException {
7373
return withNextValue(
7474
(value, fieldMetadata) -> {
7575
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
76-
return mapExceptions(() -> converters.getNumberConverter().getShort(value, columnType));
76+
return mapSFExceptionToSQLException(
77+
() -> converters.getNumberConverter().getShort(value, columnType));
7778
});
7879
}
7980

@@ -82,7 +83,8 @@ public int readInt() throws SQLException {
8283
return withNextValue(
8384
(value, fieldMetadata) -> {
8485
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
85-
return mapExceptions(() -> converters.getNumberConverter().getInt(value, columnType));
86+
return mapSFExceptionToSQLException(
87+
() -> converters.getNumberConverter().getInt(value, columnType));
8688
});
8789
}
8890

@@ -91,7 +93,8 @@ public long readLong() throws SQLException {
9193
return withNextValue(
9294
(value, fieldMetadata) -> {
9395
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
94-
return mapExceptions(() -> converters.getNumberConverter().getLong(value, columnType));
96+
return mapSFExceptionToSQLException(
97+
() -> converters.getNumberConverter().getLong(value, columnType));
9598
});
9699
}
97100

@@ -100,7 +103,8 @@ public float readFloat() throws SQLException {
100103
return withNextValue(
101104
(value, fieldMetadata) -> {
102105
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
103-
return mapExceptions(() -> converters.getNumberConverter().getFloat(value, columnType));
106+
return mapSFExceptionToSQLException(
107+
() -> converters.getNumberConverter().getFloat(value, columnType));
104108
});
105109
}
106110

@@ -109,7 +113,8 @@ public double readDouble() throws SQLException {
109113
return withNextValue(
110114
(value, fieldMetadata) -> {
111115
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
112-
return mapExceptions(() -> converters.getNumberConverter().getDouble(value, columnType));
116+
return mapSFExceptionToSQLException(
117+
() -> converters.getNumberConverter().getDouble(value, columnType));
113118
});
114119
}
115120

@@ -118,7 +123,7 @@ public BigDecimal readBigDecimal() throws SQLException {
118123
return withNextValue(
119124
(value, fieldMetadata) -> {
120125
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
121-
return mapExceptions(
126+
return mapSFExceptionToSQLException(
122127
() -> converters.getNumberConverter().getBigDecimal(value, columnType));
123128
});
124129
}
@@ -130,7 +135,7 @@ public byte[] readBytes() throws SQLException {
130135
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
131136
int columnSubType = fieldMetadata.getType();
132137
int scale = fieldMetadata.getScale();
133-
return mapExceptions(
138+
return mapSFExceptionToSQLException(
134139
() ->
135140
converters.getBytesConverter().getBytes(value, columnType, columnSubType, scale));
136141
});
@@ -140,7 +145,7 @@ public byte[] readBytes() throws SQLException {
140145
public Date readDate() throws SQLException {
141146
return withNextValue(
142147
(value, fieldMetadata) ->
143-
mapExceptions(
148+
mapSFExceptionToSQLException(
144149
() ->
145150
converters
146151
.getStructuredTypeDateTimeConverter()
@@ -151,7 +156,7 @@ public Date readDate() throws SQLException {
151156
public Time readTime() throws SQLException {
152157
return withNextValue(
153158
(value, fieldMetadata) ->
154-
mapExceptions(
159+
mapSFExceptionToSQLException(
155160
() -> {
156161
int scale = fieldMetadata.getScale();
157162
return converters
@@ -168,7 +173,7 @@ public Timestamp readTimestamp(TimeZone tz) throws SQLException {
168173
return null;
169174
}
170175
int scale = fieldMetadata.getScale();
171-
return mapExceptions(
176+
return mapSFExceptionToSQLException(
172177
() ->
173178
converters
174179
.getStructuredTypeDateTimeConverter()

‎src/main/java/net/snowflake/client/core/JsonSqlInput.java

+22-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
package net.snowflake.client.core;
55

6-
import static net.snowflake.client.jdbc.SnowflakeUtil.mapExceptions;
6+
import static net.snowflake.client.jdbc.SnowflakeUtil.mapSFExceptionToSQLException;
77

88
import com.fasterxml.jackson.databind.JsonNode;
99
import java.math.BigDecimal;
@@ -55,7 +55,7 @@ public String readString() throws SQLException {
5555
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
5656
int columnSubType = fieldMetadata.getType();
5757
int scale = fieldMetadata.getScale();
58-
return mapExceptions(
58+
return mapSFExceptionToSQLException(
5959
() ->
6060
converters
6161
.getStringConverter()
@@ -68,7 +68,7 @@ public boolean readBoolean() throws SQLException {
6868
return withNextValue(
6969
(value, jsonNode, fieldMetadata) -> {
7070
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
71-
return mapExceptions(
71+
return mapSFExceptionToSQLException(
7272
() -> converters.getBooleanConverter().getBoolean(value, columnType));
7373
});
7474
}
@@ -77,15 +77,16 @@ public boolean readBoolean() throws SQLException {
7777
public byte readByte() throws SQLException {
7878
return withNextValue(
7979
(value, jsonNode, fieldMetadata) ->
80-
mapExceptions(() -> converters.getNumberConverter().getByte(value)));
80+
mapSFExceptionToSQLException(() -> converters.getNumberConverter().getByte(value)));
8181
}
8282

8383
@Override
8484
public short readShort() throws SQLException {
8585
return withNextValue(
8686
(value, jsonNode, fieldMetadata) -> {
8787
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
88-
return mapExceptions(() -> converters.getNumberConverter().getShort(value, columnType));
88+
return mapSFExceptionToSQLException(
89+
() -> converters.getNumberConverter().getShort(value, columnType));
8990
});
9091
}
9192

@@ -94,7 +95,8 @@ public int readInt() throws SQLException {
9495
return withNextValue(
9596
(value, jsonNode, fieldMetadata) -> {
9697
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
97-
return mapExceptions(() -> converters.getNumberConverter().getInt(value, columnType));
98+
return mapSFExceptionToSQLException(
99+
() -> converters.getNumberConverter().getInt(value, columnType));
98100
});
99101
}
100102

@@ -103,7 +105,8 @@ public long readLong() throws SQLException {
103105
return withNextValue(
104106
(value, jsonNode, fieldMetadata) -> {
105107
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
106-
return mapExceptions(() -> converters.getNumberConverter().getLong(value, columnType));
108+
return mapSFExceptionToSQLException(
109+
() -> converters.getNumberConverter().getLong(value, columnType));
107110
});
108111
}
109112

@@ -112,7 +115,8 @@ public float readFloat() throws SQLException {
112115
return withNextValue(
113116
(value, jsonNode, fieldMetadata) -> {
114117
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
115-
return mapExceptions(() -> converters.getNumberConverter().getFloat(value, columnType));
118+
return mapSFExceptionToSQLException(
119+
() -> converters.getNumberConverter().getFloat(value, columnType));
116120
});
117121
}
118122

@@ -121,7 +125,8 @@ public double readDouble() throws SQLException {
121125
return withNextValue(
122126
(value, jsonNode, fieldMetadata) -> {
123127
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
124-
return mapExceptions(() -> converters.getNumberConverter().getDouble(value, columnType));
128+
return mapSFExceptionToSQLException(
129+
() -> converters.getNumberConverter().getDouble(value, columnType));
125130
});
126131
}
127132

@@ -130,7 +135,7 @@ public BigDecimal readBigDecimal() throws SQLException {
130135
return withNextValue(
131136
(value, jsonNode, fieldMetadata) -> {
132137
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
133-
return mapExceptions(
138+
return mapSFExceptionToSQLException(
134139
() -> converters.getNumberConverter().getBigDecimal(value, columnType));
135140
});
136141
}
@@ -142,7 +147,7 @@ public byte[] readBytes() throws SQLException {
142147
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
143148
int columnSubType = fieldMetadata.getType();
144149
int scale = fieldMetadata.getScale();
145-
return mapExceptions(
150+
return mapSFExceptionToSQLException(
146151
() ->
147152
converters.getBytesConverter().getBytes(value, columnType, columnSubType, scale));
148153
});
@@ -170,6 +175,11 @@ public Time readTime() throws SQLException {
170175
});
171176
}
172177

178+
@Override
179+
public Timestamp readTimestamp() throws SQLException {
180+
return readTimestamp(null);
181+
}
182+
173183
@Override
174184
public Timestamp readTimestamp(TimeZone tz) throws SQLException {
175185
return withNextValue(
@@ -186,7 +196,7 @@ public Timestamp readTimestamp(TimeZone tz) throws SQLException {
186196
if (result != null) {
187197
return result;
188198
}
189-
return mapExceptions(
199+
return mapSFExceptionToSQLException(
190200
() ->
191201
converters
192202
.getDateTimeConverter()

‎src/main/java/net/snowflake/client/core/SFBaseResultSet.java

+5
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,9 @@ public Converters getConverters() {
208208
logger.debug("Json converters weren't created");
209209
return null;
210210
}
211+
212+
@SnowflakeJdbcInternalApi
213+
public TimeZone getSessionTimeZone() {
214+
return resultSetSerializable.getTimeZone();
215+
}
211216
}

‎src/main/java/net/snowflake/client/jdbc/SnowflakeBaseResultSet.java

+284-27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package net.snowflake.client.jdbc;
66

7+
import static net.snowflake.client.jdbc.SnowflakeUtil.mapSFExceptionToSQLException;
8+
79
import com.fasterxml.jackson.core.type.TypeReference;
810
import com.fasterxml.jackson.databind.JsonNode;
911
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -35,6 +37,7 @@
3537
import java.util.List;
3638
import java.util.Map;
3739
import java.util.TimeZone;
40+
import net.snowflake.client.core.ColumnTypeHelper;
3841
import net.snowflake.client.core.JsonSqlInput;
3942
import net.snowflake.client.core.ObjectMapperFactory;
4043
import net.snowflake.client.core.SFBaseResultSet;
@@ -1372,8 +1375,6 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
13721375
return (T) (Double) getDouble(columnIndex);
13731376
} else if (Date.class.isAssignableFrom(type)) {
13741377
return (T) getDate(columnIndex);
1375-
} else if (Date.class.isAssignableFrom(type)) {
1376-
return (T) getDate(columnIndex);
13771378
} else if (Time.class.isAssignableFrom(type)) {
13781379
return (T) getTime(columnIndex);
13791380
} else if (Timestamp.class.isAssignableFrom(type)) {
@@ -1395,43 +1396,299 @@ public <T> List<T> getList(int columnIndex, Class<T> type) throws SQLException {
13951396
}
13961397

13971398
public <T> T[] getArray(int columnIndex, Class<T> type) throws SQLException {
1398-
Map[] jsonMaps = (Map[]) getArray(columnIndex).getArray();
1399-
T[] arr = (T[]) java.lang.reflect.Array.newInstance(type, jsonMaps.length);
1399+
int columnSubType = resultSetMetaData.getInternalColumnType(columnIndex);
1400+
int columnType = ColumnTypeHelper.getColumnType(columnSubType, session);
1401+
;
1402+
int scale = resultSetMetaData.getScale(columnIndex);
1403+
TimeZone tz = sfBaseResultSet.getSessionTimeZone();
1404+
Object[] objects = (Object[]) getArray(columnIndex).getArray();
1405+
T[] arr = (T[]) java.lang.reflect.Array.newInstance(type, objects.length);
14001406
int counter = 0;
1401-
for (Map map : jsonMaps) {
1402-
SQLData instance = (SQLData) SQLDataCreationHelper.create(type);
1403-
SQLInput sqlInput =
1404-
new JsonSqlInput(
1405-
OBJECT_MAPPER.convertValue(map, JsonNode.class),
1406-
session,
1407-
sfBaseResultSet.getConverters(),
1408-
sfBaseResultSet.getMetaData().getColumnMetadata().get(columnIndex - 1).getFields(),
1409-
sfBaseResultSet.getSessionTimezone());
1410-
instance.readSQL(sqlInput, null);
1411-
arr[counter++] = (T) instance;
1407+
for (Object value : objects) {
1408+
if (SQLData.class.isAssignableFrom(type)) {
1409+
Map data = (Map) value;
1410+
SQLData instance = (SQLData) SQLDataCreationHelper.create(type);
1411+
SQLInput sqlInput =
1412+
new JsonSqlInput(
1413+
OBJECT_MAPPER.convertValue(data, JsonNode.class),
1414+
session,
1415+
sfBaseResultSet.getConverters(),
1416+
sfBaseResultSet.getMetaData().getColumnMetadata().get(columnIndex - 1).getFields(),
1417+
sfBaseResultSet.getSessionTimezone());
1418+
instance.readSQL(sqlInput, null);
1419+
arr[counter++] = (T) instance;
1420+
} else if (String.class.isAssignableFrom(type)) {
1421+
arr[counter++] =
1422+
mapSFExceptionToSQLException(
1423+
() ->
1424+
(T)
1425+
sfBaseResultSet
1426+
.getConverters()
1427+
.getStringConverter()
1428+
.getString(value, columnType, columnSubType, scale));
1429+
} else if (Boolean.class.isAssignableFrom(type)) {
1430+
arr[counter++] =
1431+
mapSFExceptionToSQLException(
1432+
() ->
1433+
(T)
1434+
sfBaseResultSet
1435+
.getConverters()
1436+
.getBooleanConverter()
1437+
.getBoolean(value, columnType));
1438+
} else if (Byte.class.isAssignableFrom(type)) {
1439+
arr[counter++] =
1440+
mapSFExceptionToSQLException(
1441+
() ->
1442+
(T)
1443+
sfBaseResultSet
1444+
.getConverters()
1445+
.getBytesConverter()
1446+
.getBytes(value, columnType, columnSubType, scale));
1447+
} else if (Short.class.isAssignableFrom(type)) {
1448+
arr[counter++] =
1449+
mapSFExceptionToSQLException(
1450+
() ->
1451+
(T)
1452+
Short.valueOf(
1453+
sfBaseResultSet
1454+
.getConverters()
1455+
.getNumberConverter()
1456+
.getShort(value, columnType)));
1457+
} else if (Integer.class.isAssignableFrom(type)) {
1458+
arr[counter++] =
1459+
mapSFExceptionToSQLException(
1460+
() ->
1461+
(T)
1462+
Integer.valueOf(
1463+
sfBaseResultSet
1464+
.getConverters()
1465+
.getNumberConverter()
1466+
.getInt(value, columnType)));
1467+
} else if (Long.class.isAssignableFrom(type)) {
1468+
arr[counter++] =
1469+
mapSFExceptionToSQLException(
1470+
() ->
1471+
(T)
1472+
Long.valueOf(
1473+
sfBaseResultSet
1474+
.getConverters()
1475+
.getNumberConverter()
1476+
.getLong(value, columnType)));
1477+
} else if (Float.class.isAssignableFrom(type)) {
1478+
arr[counter++] =
1479+
mapSFExceptionToSQLException(
1480+
() ->
1481+
(T)
1482+
Float.valueOf(
1483+
sfBaseResultSet
1484+
.getConverters()
1485+
.getNumberConverter()
1486+
.getFloat(value, columnType)));
1487+
} else if (Double.class.isAssignableFrom(type)) {
1488+
arr[counter++] =
1489+
mapSFExceptionToSQLException(
1490+
() ->
1491+
(T)
1492+
Double.valueOf(
1493+
sfBaseResultSet
1494+
.getConverters()
1495+
.getNumberConverter()
1496+
.getDouble(value, columnType)));
1497+
} else if (Date.class.isAssignableFrom(type)) {
1498+
arr[counter++] =
1499+
mapSFExceptionToSQLException(
1500+
() ->
1501+
(T)
1502+
sfBaseResultSet
1503+
.getConverters()
1504+
.getDateTimeConverter()
1505+
.getDate(value, columnType, columnSubType, tz, scale));
1506+
} else if (Time.class.isAssignableFrom(type)) {
1507+
arr[counter++] =
1508+
mapSFExceptionToSQLException(
1509+
() ->
1510+
(T)
1511+
sfBaseResultSet
1512+
.getConverters()
1513+
.getDateTimeConverter()
1514+
.getTime(value, columnType, columnSubType, tz, scale));
1515+
} else if (Timestamp.class.isAssignableFrom(type)) {
1516+
mapSFExceptionToSQLException(
1517+
() ->
1518+
(T)
1519+
sfBaseResultSet
1520+
.getConverters()
1521+
.getDateTimeConverter()
1522+
.getTimestamp(value, columnType, columnSubType, tz, scale));
1523+
} else if (BigDecimal.class.isAssignableFrom(type)) {
1524+
arr[counter++] = (T) getBigDecimal(columnIndex);
1525+
} else {
1526+
logger.warn(
1527+
"Unsupported type passed to getArray(int columnIndex, Class<T> type): "
1528+
+ type.getName());
1529+
throw new SQLException(
1530+
"Type passed to 'getObject(int columnIndex,Class<T> type)' is unsupported. Type: "
1531+
+ type.getName());
1532+
}
14121533
}
1413-
14141534
return arr;
14151535
}
14161536

14171537
public <T> Map<String, T> getMap(int columnIndex, Class<T> type) throws SQLException {
1538+
int columnType = resultSetMetaData.getInternalColumnType(columnIndex);
1539+
int columnSubType = resultSetMetaData.getInternalColumnType(columnIndex);
1540+
int scale = resultSetMetaData.getScale(columnIndex);
1541+
TimeZone tz = sfBaseResultSet.getSessionTimeZone();
14181542
Object object = getObject(columnIndex);
14191543
JsonNode jsonNode = ((JsonSqlInput) object).getInput();
14201544
Map<String, Object> map =
14211545
OBJECT_MAPPER.convertValue(jsonNode, new TypeReference<Map<String, Object>>() {});
14221546
Map<String, T> resultMap = new HashMap<>();
1423-
14241547
for (Map.Entry<String, Object> entry : map.entrySet()) {
1425-
SQLData instance = (SQLData) SQLDataCreationHelper.create(type);
1426-
SQLInput sqlInput =
1427-
new JsonSqlInput(
1428-
jsonNode.get(entry.getKey()),
1429-
session,
1430-
sfBaseResultSet.getConverters(),
1431-
sfBaseResultSet.getMetaData().getColumnMetadata().get(columnIndex - 1).getFields(),
1432-
sfBaseResultSet.getSessionTimezone());
1433-
instance.readSQL(sqlInput, null);
1434-
resultMap.put(entry.getKey(), (T) instance);
1548+
if (SQLData.class.isAssignableFrom(type)) {
1549+
SQLData instance = (SQLData) SQLDataCreationHelper.create(type);
1550+
SQLInput sqlInput =
1551+
new JsonSqlInput(
1552+
jsonNode.get(entry.getKey()),
1553+
session,
1554+
sfBaseResultSet.getConverters(),
1555+
sfBaseResultSet.getMetaData().getColumnMetadata().get(columnIndex - 1).getFields(),
1556+
sfBaseResultSet.getSessionTimezone());
1557+
instance.readSQL(sqlInput, null);
1558+
resultMap.put(entry.getKey(), (T) instance);
1559+
} else if (String.class.isAssignableFrom(type)) {
1560+
resultMap.put(
1561+
entry.getKey(),
1562+
mapSFExceptionToSQLException(
1563+
() ->
1564+
(T)
1565+
sfBaseResultSet
1566+
.getConverters()
1567+
.getStringConverter()
1568+
.getString(entry.getValue(), columnType, columnSubType, scale)));
1569+
} else if (Boolean.class.isAssignableFrom(type)) {
1570+
resultMap.put(
1571+
entry.getKey(),
1572+
mapSFExceptionToSQLException(
1573+
() ->
1574+
(T)
1575+
sfBaseResultSet
1576+
.getConverters()
1577+
.getBooleanConverter()
1578+
.getBoolean(entry.getValue(), columnType)));
1579+
} else if (Byte.class.isAssignableFrom(type)) {
1580+
resultMap.put(
1581+
entry.getKey(),
1582+
mapSFExceptionToSQLException(
1583+
() ->
1584+
(T)
1585+
sfBaseResultSet
1586+
.getConverters()
1587+
.getBytesConverter()
1588+
.getBytes(entry.getValue(), columnType, columnSubType, scale)));
1589+
} else if (Short.class.isAssignableFrom(type)) {
1590+
resultMap.put(
1591+
entry.getKey(),
1592+
mapSFExceptionToSQLException(
1593+
() ->
1594+
(T)
1595+
(Short)
1596+
sfBaseResultSet
1597+
.getConverters()
1598+
.getNumberConverter()
1599+
.getShort(entry.getValue(), columnType)));
1600+
} else if (Integer.class.isAssignableFrom(type)) {
1601+
resultMap.put(
1602+
entry.getKey(),
1603+
mapSFExceptionToSQLException(
1604+
() ->
1605+
(T)
1606+
(Integer)
1607+
sfBaseResultSet
1608+
.getConverters()
1609+
.getNumberConverter()
1610+
.getInt(entry.getValue(), columnType)));
1611+
} else if (Long.class.isAssignableFrom(type)) {
1612+
resultMap.put(
1613+
entry.getKey(),
1614+
mapSFExceptionToSQLException(
1615+
() ->
1616+
(T)
1617+
(Long)
1618+
sfBaseResultSet
1619+
.getConverters()
1620+
.getNumberConverter()
1621+
.getLong(entry.getValue(), columnType)));
1622+
} else if (Float.class.isAssignableFrom(type)) {
1623+
resultMap.put(
1624+
entry.getKey(),
1625+
mapSFExceptionToSQLException(
1626+
() ->
1627+
(T)
1628+
(Float)
1629+
sfBaseResultSet
1630+
.getConverters()
1631+
.getNumberConverter()
1632+
.getFloat(entry.getValue(), columnType)));
1633+
} else if (Double.class.isAssignableFrom(type)) {
1634+
resultMap.put(
1635+
entry.getKey(),
1636+
mapSFExceptionToSQLException(
1637+
() ->
1638+
(T)
1639+
(Double)
1640+
sfBaseResultSet
1641+
.getConverters()
1642+
.getNumberConverter()
1643+
.getDouble(entry.getValue(), columnType)));
1644+
} else if (BigDecimal.class.isAssignableFrom(type)) {
1645+
resultMap.put(
1646+
entry.getKey(),
1647+
mapSFExceptionToSQLException(
1648+
() ->
1649+
(T)
1650+
sfBaseResultSet
1651+
.getConverters()
1652+
.getNumberConverter()
1653+
.getBigDecimal(entry.getValue(), columnType)));
1654+
} else if (Date.class.isAssignableFrom(type)) {
1655+
resultMap.put(
1656+
entry.getKey(),
1657+
mapSFExceptionToSQLException(
1658+
() ->
1659+
(T)
1660+
sfBaseResultSet
1661+
.getConverters()
1662+
.getDateTimeConverter()
1663+
.getDate(entry.getValue(), columnType, columnSubType, tz, scale)));
1664+
} else if (Time.class.isAssignableFrom(type)) {
1665+
resultMap.put(
1666+
entry.getKey(),
1667+
mapSFExceptionToSQLException(
1668+
() ->
1669+
(T)
1670+
sfBaseResultSet
1671+
.getConverters()
1672+
.getDateTimeConverter()
1673+
.getTime(entry.getValue(), columnType, columnSubType, tz, scale)));
1674+
} else if (Timestamp.class.isAssignableFrom(type)) {
1675+
resultMap.put(
1676+
entry.getKey(),
1677+
mapSFExceptionToSQLException(
1678+
() ->
1679+
(T)
1680+
sfBaseResultSet
1681+
.getConverters()
1682+
.getDateTimeConverter()
1683+
.getTimestamp(entry.getValue(), columnType, columnSubType, tz, scale)));
1684+
} else {
1685+
logger.debug(
1686+
"Unsupported type passed to getObject(int columnIndex,Class<T> type): "
1687+
+ type.getName());
1688+
throw new SQLException(
1689+
"Type passed to 'getObject(int columnIndex,Class<T> type)' is unsupported. Type: "
1690+
+ type.getName());
1691+
}
14351692
}
14361693

14371694
return resultMap;

‎src/main/java/net/snowflake/client/jdbc/SnowflakeUtil.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,9 @@ public static boolean convertSystemPropertyToBooleanValue(
791791
return defaultValue;
792792
}
793793

794-
public static <T> T mapExceptions(ThrowingCallable<T, SFException> action) throws SQLException {
794+
@SnowflakeJdbcInternalApi
795+
public static <T> T mapSFExceptionToSQLException(ThrowingCallable<T, SFException> action)
796+
throws SQLException {
795797
try {
796798
return action.call();
797799
} catch (SFException e) {

‎src/test/java/net/snowflake/client/jdbc/ResultSetStructuredTypesLatestIT.java

+55
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,34 @@ public void testReturnAsArrayOfSqlData() throws SQLException {
204204
});
205205
}
206206

207+
@Test
208+
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
209+
public void testReturnAsArrayOfString() throws SQLException {
210+
withFirstRow(
211+
"SELECT ARRAY_CONSTRUCT('one', 'two','three')::ARRAY(VARCHAR)",
212+
(resultSet) -> {
213+
String[] resultArray =
214+
resultSet.unwrap(SnowflakeBaseResultSet.class).getArray(1, String.class);
215+
assertEquals("one", resultArray[0]);
216+
assertEquals("two", resultArray[1]);
217+
assertEquals("three", resultArray[2]);
218+
});
219+
}
220+
221+
@Test
222+
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
223+
public void testReturnAsListOfIntegers() throws SQLException {
224+
withFirstRow(
225+
"SELECT ARRAY_CONSTRUCT(1,2,3)::ARRAY(INTEGER)",
226+
(resultSet) -> {
227+
List<Integer> resultList =
228+
resultSet.unwrap(SnowflakeBaseResultSet.class).getList(1, Integer.class);
229+
assertEquals(Integer.valueOf(1), resultList.get(0));
230+
assertEquals(Integer.valueOf(2), resultList.get(1));
231+
assertEquals(Integer.valueOf(3), resultList.get(2));
232+
});
233+
}
234+
207235
@Test
208236
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
209237
public void testReturnAsMap() throws SQLException {
@@ -220,6 +248,33 @@ public void testReturnAsMap() throws SQLException {
220248
});
221249
}
222250

251+
@Test
252+
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
253+
public void testReturnAsMapOfLong() throws SQLException {
254+
withFirstRow(
255+
"SELECT {'x':1, 'y':2, 'z':3}::MAP(VARCHAR, BIGINT)",
256+
(resultSet) -> {
257+
Map<String, Long> map =
258+
resultSet.unwrap(SnowflakeBaseResultSet.class).getMap(1, Long.class);
259+
assertEquals(Long.valueOf(1), map.get("x"));
260+
assertEquals(Long.valueOf(2), map.get("y"));
261+
assertEquals(Long.valueOf(3), map.get("z"));
262+
});
263+
}
264+
265+
@Test
266+
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
267+
public void testReturnAsMapOfBoolean() throws SQLException {
268+
withFirstRow(
269+
"SELECT {'x':'true', 'y':0}::MAP(VARCHAR, BOOLEAN)",
270+
(resultSet) -> {
271+
Map<String, Boolean> map =
272+
resultSet.unwrap(SnowflakeBaseResultSet.class).getMap(1, Boolean.class);
273+
assertEquals(Boolean.TRUE, map.get("x"));
274+
assertEquals(Boolean.FALSE, map.get("y"));
275+
});
276+
}
277+
223278
@Test
224279
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
225280
public void testReturnAsList() throws SQLException {

0 commit comments

Comments
 (0)
Please sign in to comment.