|
| 1 | +package net.snowflake.client.core; |
| 2 | + |
| 3 | +import net.snowflake.client.core.json.Converters; |
| 4 | +import net.snowflake.client.core.structs.SQLDataCreationHelper; |
| 5 | +import net.snowflake.client.jdbc.FieldMetadata; |
| 6 | +import net.snowflake.client.jdbc.SnowflakeLoggedFeatureNotSupportedException; |
| 7 | +import net.snowflake.client.util.ThrowingBiFunction; |
| 8 | +import net.snowflake.common.core.SFTimestamp; |
| 9 | +import net.snowflake.common.core.SnowflakeDateTimeFormat; |
| 10 | +import org.apache.arrow.vector.util.JsonStringHashMap; |
| 11 | + |
| 12 | +import java.io.InputStream; |
| 13 | +import java.io.Reader; |
| 14 | +import java.math.BigDecimal; |
| 15 | +import java.net.URL; |
| 16 | +import java.sql.*; |
| 17 | +import java.time.Instant; |
| 18 | +import java.time.ZoneOffset; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Iterator; |
| 21 | +import java.util.List; |
| 22 | +import java.util.TimeZone; |
| 23 | + |
| 24 | +import static net.snowflake.client.jdbc.SnowflakeUtil.mapExceptions; |
| 25 | + |
| 26 | +@SnowflakeJdbcInternalApi |
| 27 | +public class ArrowSqlInput implements SFSqlInput { |
| 28 | + |
| 29 | + private final JsonStringHashMap<String, Object> input; |
| 30 | + private final SFBaseSession session; |
| 31 | + private final Iterator<Object> elements; |
| 32 | + private final Converters converters; |
| 33 | + private final List<FieldMetadata> fields; |
| 34 | + |
| 35 | + private int currentIndex = 0; |
| 36 | + |
| 37 | + public ArrowSqlInput(JsonStringHashMap<String, Object> input, SFBaseSession session, Converters converters, List<FieldMetadata> fields) { |
| 38 | + this.input = input; |
| 39 | + this.elements = input.values().iterator(); |
| 40 | + this.session = session; |
| 41 | + this.converters = converters; |
| 42 | + this.fields = fields; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String readString() throws SQLException { |
| 47 | + return withNextValue( |
| 48 | + ((value, fieldMetadata) -> { |
| 49 | + int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session); |
| 50 | + int columnSubType = fieldMetadata.getType(); |
| 51 | + int scale = fieldMetadata.getScale(); |
| 52 | + return mapExceptions( |
| 53 | + () -> |
| 54 | + converters |
| 55 | + .getStringConverter() |
| 56 | + .getString(value, columnType, columnSubType, scale)); |
| 57 | + })); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean readBoolean() throws SQLException { |
| 62 | + return withNextValue((value, fieldMetadata) -> { |
| 63 | + int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session); |
| 64 | + return mapExceptions( |
| 65 | + () -> converters.getBooleanConverter().getBoolean(value, columnType)); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public byte readByte() throws SQLException { |
| 71 | + return withNextValue( |
| 72 | + (value, fieldMetadata) -> |
| 73 | + mapExceptions(() -> converters.getNumberConverter().getByte(value))); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public short readShort() throws SQLException { |
| 78 | + return withNextValue( |
| 79 | + (value, fieldMetadata) -> { |
| 80 | + int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session); |
| 81 | + return mapExceptions(() -> converters.getNumberConverter().getShort(value, columnType)); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public int readInt() throws SQLException { |
| 87 | + return withNextValue( |
| 88 | + (value, fieldMetadata) -> { |
| 89 | + int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session); |
| 90 | + return mapExceptions(() -> converters.getNumberConverter().getInt(value, columnType)); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public long readLong() throws SQLException { |
| 96 | + return withNextValue( |
| 97 | + (value, fieldMetadata) -> { |
| 98 | + int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session); |
| 99 | + return mapExceptions(() -> converters.getNumberConverter().getLong(value, columnType)); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public float readFloat() throws SQLException { |
| 105 | + return withNextValue( |
| 106 | + (value, fieldMetadata) -> { |
| 107 | + int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session); |
| 108 | + return mapExceptions(() -> converters.getNumberConverter().getFloat(value, columnType)); |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public double readDouble() throws SQLException { |
| 114 | + return withNextValue( |
| 115 | + (value, fieldMetadata) -> { |
| 116 | + int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session); |
| 117 | + return mapExceptions(() -> converters.getNumberConverter().getDouble(value, columnType)); |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public BigDecimal readBigDecimal() throws SQLException { |
| 123 | + return withNextValue( |
| 124 | + (value, fieldMetadata) -> { |
| 125 | + int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session); |
| 126 | + return mapExceptions( |
| 127 | + () -> converters.getNumberConverter().getBigDecimal(value, columnType)); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public byte[] readBytes() throws SQLException { |
| 133 | + return withNextValue( |
| 134 | + (value, fieldMetadata) -> { |
| 135 | + int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session); |
| 136 | + int columnSubType = fieldMetadata.getType(); |
| 137 | + int scale = fieldMetadata.getScale(); |
| 138 | + return mapExceptions( |
| 139 | + () -> |
| 140 | + converters.getBytesConverter().getBytes(value, columnType, columnSubType, scale)); |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public Date readDate() throws SQLException { |
| 146 | + return withNextValue( |
| 147 | + (value, fieldMetadata) -> { |
| 148 | + SnowflakeDateTimeFormat formatter = SqlInputTimestampUtil.extractDateTimeFormat(session, "DATE_OUTPUT_FORMAT"); |
| 149 | + SFTimestamp timestamp = formatter.parse((String) value); |
| 150 | + return Date.valueOf( |
| 151 | + Instant.ofEpochMilli(timestamp.getTime()).atZone(ZoneOffset.UTC).toLocalDate()); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public Time readTime() throws SQLException { |
| 157 | + return withNextValue( |
| 158 | + (value, fieldMetadata) -> { |
| 159 | + SnowflakeDateTimeFormat formatter = SqlInputTimestampUtil.extractDateTimeFormat(session, "TIME_OUTPUT_FORMAT"); |
| 160 | + SFTimestamp timestamp = formatter.parse((String) value); |
| 161 | + return Time.valueOf( |
| 162 | + Instant.ofEpochMilli(timestamp.getTime()).atZone(ZoneOffset.UTC).toLocalTime()); |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public Timestamp readTimestamp() throws SQLException { |
| 168 | + return readTimestamp(null); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public Timestamp readTimestamp(TimeZone tz) throws SQLException { |
| 173 | + return withNextValue( |
| 174 | + (value, fieldMetadata) -> { |
| 175 | + if (value == null) { |
| 176 | + return null; |
| 177 | + } |
| 178 | + int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session); |
| 179 | + int columnSubType = fieldMetadata.getType(); |
| 180 | + int scale = fieldMetadata.getScale(); |
| 181 | + // TODO structuredType what if not a string value? |
| 182 | + Timestamp result = SqlInputTimestampUtil.getTimestampFromType(columnSubType, (String) value, session); |
| 183 | + if (result != null) { |
| 184 | + return result; |
| 185 | + } |
| 186 | + return mapExceptions( |
| 187 | + () -> |
| 188 | + converters |
| 189 | + .getDateTimeConverter() |
| 190 | + .getTimestamp(value, columnType, columnSubType, tz, scale)); |
| 191 | + }); |
| 192 | + } |
| 193 | + |
| 194 | + |
| 195 | + @Override |
| 196 | + public Reader readCharacterStream() throws SQLException { |
| 197 | + throw new SnowflakeLoggedFeatureNotSupportedException(session, "readCharacterStream"); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public InputStream readAsciiStream() throws SQLException { |
| 202 | + throw new SnowflakeLoggedFeatureNotSupportedException(session, "readAsciiStream"); |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public InputStream readBinaryStream() throws SQLException { |
| 207 | + throw new SnowflakeLoggedFeatureNotSupportedException(session, "readBinaryStream"); |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public Object readObject() throws SQLException { |
| 212 | + return withNextValue((value, fieldMetadata) -> { |
| 213 | + if (!(value instanceof JsonStringHashMap)) { |
| 214 | + throw new SQLException("Invalid value passed to 'readObject()', expected Map; got: " + value.getClass()); |
| 215 | + } |
| 216 | + return value; |
| 217 | + }); |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public <T> T readObject(Class<T> type) throws SQLException { |
| 222 | + return withNextValue( |
| 223 | + (value, fieldMetadata) -> { |
| 224 | + SQLData instance = (SQLData) SQLDataCreationHelper.create(type); |
| 225 | + instance.readSQL( |
| 226 | + new ArrowSqlInput( |
| 227 | + (JsonStringHashMap<String, Object>) value, |
| 228 | + session, |
| 229 | + converters, |
| 230 | + Arrays.asList(fieldMetadata.getFields()) |
| 231 | + ), |
| 232 | + null |
| 233 | + ); |
| 234 | + return (T) instance; |
| 235 | + }); |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public Ref readRef() throws SQLException { |
| 240 | + throw new SnowflakeLoggedFeatureNotSupportedException(session, "readRef"); |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public Blob readBlob() throws SQLException { |
| 245 | + throw new SnowflakeLoggedFeatureNotSupportedException(session, "readBlob"); |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public Clob readClob() throws SQLException { |
| 250 | + throw new SnowflakeLoggedFeatureNotSupportedException(session, "readClob"); |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public Array readArray() throws SQLException { |
| 255 | + throw new SnowflakeLoggedFeatureNotSupportedException(session, "readArray"); |
| 256 | + } |
| 257 | + |
| 258 | + @Override |
| 259 | + public boolean wasNull() throws SQLException { |
| 260 | + return false; // nulls are not allowed in structure types |
| 261 | + } |
| 262 | + |
| 263 | + @Override |
| 264 | + public URL readURL() throws SQLException { |
| 265 | + throw new SnowflakeLoggedFeatureNotSupportedException(session, "readCharacterStream"); |
| 266 | + } |
| 267 | + |
| 268 | + @Override |
| 269 | + public NClob readNClob() throws SQLException { |
| 270 | + throw new SnowflakeLoggedFeatureNotSupportedException(session, "readNClob"); |
| 271 | + } |
| 272 | + |
| 273 | + @Override |
| 274 | + public String readNString() throws SQLException { |
| 275 | + throw new SnowflakeLoggedFeatureNotSupportedException(session, "readNString"); |
| 276 | + } |
| 277 | + |
| 278 | + @Override |
| 279 | + public SQLXML readSQLXML() throws SQLException { |
| 280 | + throw new SnowflakeLoggedFeatureNotSupportedException(session, "readSQLXML"); |
| 281 | + } |
| 282 | + |
| 283 | + @Override |
| 284 | + public RowId readRowId() throws SQLException { |
| 285 | + throw new SnowflakeLoggedFeatureNotSupportedException(session, "readRowId"); |
| 286 | + } |
| 287 | + |
| 288 | + private <T> T withNextValue( |
| 289 | + ThrowingBiFunction<Object, FieldMetadata, T, SQLException> action) |
| 290 | + throws SQLException { |
| 291 | + return action.apply(elements.next(), fields.get(currentIndex++)); |
| 292 | + } |
| 293 | +} |
0 commit comments