Skip to content

Commit

Permalink
split getJsonString in SfSqlArray into getJsonString for writes and g…
Browse files Browse the repository at this point in the history
…et(predefined)Text for reads, extract isVarcharConvertedStruct
  • Loading branch information
sfc-gh-mkubik committed Nov 26, 2024
1 parent b2e1136 commit 5dff05b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,19 @@ public Object getObject(int columnIndex) throws SFException {
converter.setSessionTimeZone(sessionTimeZone);
Object obj = converter.toObject(index);
boolean isStructuredType = resultSetMetaData.isStructuredTypeColumn(columnIndex);
if (type == Types.STRUCT && isStructuredType && converter instanceof VarCharConverter) {
if (isVarcharConvertedStruct(type, isStructuredType, converter)) {
if (obj != null) {
return new StructObjectWrapper((String) obj, createJsonSqlInput(columnIndex, obj));
}
}
return obj;
}

private boolean isVarcharConvertedStruct(
int type, boolean isStructuredType, ArrowVectorConverter converter) {
return type == Types.STRUCT && isStructuredType && converter instanceof VarCharConverter;
}

private Object createJsonSqlInput(int columnIndex, Object obj) throws SFException {
try {
if (obj == null) {
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/net/snowflake/client/core/SfSqlArray.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.snowflake.client.core;

import static net.snowflake.client.core.FieldSchemaCreator.buildBindingSchemaForType;
import static net.snowflake.client.core.FieldSchemaCreator.logger;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.sql.Array;
Expand All @@ -19,6 +20,7 @@ public class SfSqlArray implements Array {
private String text;
private int baseType;
private Object elements;
private String jsonStringFromElements;

public SfSqlArray(String text, int baseType, Object elements) {
this.text = text;
Expand Down Expand Up @@ -87,13 +89,21 @@ public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map)
@Override
public void free() throws SQLException {}

public String getJsonString() throws SQLException {
public String getText() {
if (text == null) {
text = buildJsonStringFromElements(elements);
logger.warn("Text field wasn't initialized. Should never happen.");
}
return text;
}

public String getJsonString() throws SQLException {
if (jsonStringFromElements == null) {
jsonStringFromElements = buildJsonStringFromElements(elements);
}

return jsonStringFromElements;
}

private static String buildJsonStringFromElements(Object elements) throws SQLException {
try {
return SnowflakeUtil.mapJson(elements);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public Object getObject(int columnIndex) throws SQLException {
} else if (object instanceof StructObjectWrapper) {
return ((StructObjectWrapper) object).getJsonString();
} else if (object instanceof SfSqlArray) {
return ((SfSqlArray) object).getJsonString();
return ((SfSqlArray) object).getText();
} else if (object instanceof ArrowSqlInput) {
throw new SQLException(
"Arrow native struct couldn't be converted to String. To map to SqlData the method getObject(int columnIndex, Class type) should be used");
Expand Down

0 comments on commit 5dff05b

Please sign in to comment.