Skip to content

Commit

Permalink
SNOW-1234214 Fix for json Maps (#1688)
Browse files Browse the repository at this point in the history
* Mapping to json node
  • Loading branch information
sfc-gh-pmotacki authored Apr 2, 2024
1 parent 0c54d6f commit 6a209b0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
9 changes: 4 additions & 5 deletions src/main/java/net/snowflake/client/core/SFJsonResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package net.snowflake.client.core;

import static net.snowflake.client.jdbc.SnowflakeUtil.getJsonNodeStringValue;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -420,11 +422,8 @@ private Stream getStream(Iterator nodeElements, JsonStringToTypeConverter conver

private static Object convert(JsonStringToTypeConverter converter, JsonNode node)
throws SFException {
if (node.isValueNode()) {
return converter.convert(node.asText());
} else {
return converter.convert(node.toString());
}
String nodeValue = getJsonNodeStringValue(node);
return converter.convert(nodeValue);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public JsonStringToTypeConverter timeConverter(SFBaseSession session) {
SnowflakeDateTimeFormat formatter =
SnowflakeDateTimeFormat.fromSqlFormat(
(String) session.getCommonParameters().get("TIME_OUTPUT_FORMAT"));
SFTimestamp timestamp = formatter.parse((String) value);
SFTimestamp timestamp = formatter.parse(value);
return Time.valueOf(
Instant.ofEpochMilli(timestamp.getTime()).atZone(ZoneOffset.UTC).toLocalTime());
};
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/net/snowflake/client/jdbc/SnowflakeBaseResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
Expand All @@ -43,6 +44,7 @@
import net.snowflake.client.core.ObjectMapperFactory;
import net.snowflake.client.core.SFBaseResultSet;
import net.snowflake.client.core.SFBaseSession;
import net.snowflake.client.core.SFException;
import net.snowflake.client.core.structs.SQLDataCreationHelper;
import net.snowflake.client.log.SFLogger;
import net.snowflake.client.log.SFLoggerFactory;
Expand Down Expand Up @@ -1550,16 +1552,8 @@ public <T> Map<String, T> getMap(int columnIndex, Class<T> type) throws SQLExcep
int scale = valueFieldMetadata.getScale();
TimeZone tz = sfBaseResultSet.getSessionTimeZone();
Object object = getObject(columnIndex);
Map<String, Object> map;
if (object instanceof JsonSqlInput) {
map = new HashMap<>();
JsonNode jsonNode = ((JsonSqlInput) object).getInput();
jsonNode
.fieldNames()
.forEachRemaining(node -> map.put(node.toString(), jsonNode.get(node.toString())));
} else {
map = (Map<String, Object>) object;
}
Map<String, Object> map =
mapSFExceptionToSQLException(() -> prepareMapWithValues(object, type));
Map<String, T> resultMap = new HashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (SQLData.class.isAssignableFrom(type)) {
Expand Down Expand Up @@ -1748,4 +1742,25 @@ public boolean isWrapperFor(Class<?> iface) throws SQLException {

return iface.isInstance(this);
}

private <T> Map<String, Object> prepareMapWithValues(Object object, Class<T> type)
throws SFException {
if (object instanceof JsonSqlInput) {
Map map = new HashMap<>();
JsonNode jsonNode = ((JsonSqlInput) object).getInput();
for (Iterator<String> it = jsonNode.fieldNames(); it.hasNext(); ) {
String name = it.next();
map.put(
name,
SQLData.class.isAssignableFrom(type)
? jsonNode.get(name)
: SnowflakeUtil.getJsonNodeStringValue(jsonNode.get(name)));
}
return map;
} else if (object instanceof Map) {
return (Map<String, Object>) object;
} else {
throw new SFException(ErrorCode.INVALID_STRUCT_DATA, "Object couldn't be converted to map");
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/net/snowflake/client/jdbc/SnowflakeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -800,4 +800,8 @@ public static <T> T mapSFExceptionToSQLException(ThrowingCallable<T, SFException
throw new SQLException(e);
}
}

public static String getJsonNodeStringValue(JsonNode node) throws SFException {
return node.isValueNode() ? node.asText() : node.toString();
}
}

0 comments on commit 6a209b0

Please sign in to comment.