mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(rds-data): return typed array result fields (#5665)
* fix(rds-data): return typed array result fields * fix(rds-data): reject PostgreSQL bit-string array coercion
This commit is contained in:
1 parent
025af9d215
commit
bb06acdeb1
6 files changed
+177
-9
No files matched your search
+3
-3
@@ -18,9 +18,9 @@ import java.sql.SQLException;
|
||||
*
|
||||
* <p>{@code type} is a {@link java.sql.Types} code and {@code typeName} is the
|
||||
* engine's own type name, exactly as AWS reports them (for example {@code 4}
|
||||
* and {@code int4} for a PostgreSQL integer). {@code arrayBaseColumnType} is
|
||||
* always {@code 0} because array columns are not mapped to {@code arrayValue}
|
||||
* fields yet.
|
||||
* and {@code int4} for a PostgreSQL integer). {@code arrayBaseColumnType} starts
|
||||
* at {@code 0}: the result mapper fills it from non-null JDBC array values,
|
||||
* because {@code ResultSetMetaData} does not expose the array element type.
|
||||
*/
|
||||
final class RdsDataColumnMetadata {
|
||||
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package io.github.hectorvent.floci.services.rdsdata;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Array;
|
||||
import java.sql.Blob;
|
||||
import java.sql.Date;
|
||||
import java.sql.SQLException;
|
||||
@@ -67,11 +70,49 @@ final class RdsDataFieldMapper {
|
||||
case Types.DATE -> field.put("stringValue", formatDate(value));
|
||||
case Types.TIME -> field.put("stringValue", formatTime(value));
|
||||
case Types.TIMESTAMP, Types.TIMESTAMP_WITH_TIMEZONE -> field.put("stringValue", formatTimestamp(value));
|
||||
case Types.ARRAY -> field.set("arrayValue", mapArray(objectMapper, (Array) value));
|
||||
default -> field.put("stringValue", value.toString());
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
private static ObjectNode mapArray(ObjectMapper objectMapper, Array array) throws SQLException {
|
||||
try {
|
||||
int baseType = array.getBaseType();
|
||||
// PostgreSQL reports both bool and bit strings as JDBC BIT; decoding bit strings as booleans loses bits.
|
||||
if (baseType == Types.BIT && "bit".equalsIgnoreCase(array.getBaseTypeName())) {
|
||||
throw new AwsException("UnsupportedResultException", "Bit-string arrays are not supported.", 400);
|
||||
}
|
||||
String fieldName = switch (baseType) {
|
||||
case Types.BIGINT, Types.INTEGER, Types.SMALLINT, Types.TINYINT -> "longValue";
|
||||
case Types.BOOLEAN, Types.BIT -> "booleanValue";
|
||||
case Types.FLOAT, Types.REAL, Types.DOUBLE -> "doubleValue";
|
||||
case Types.ARRAY -> throw new AwsException("UnsupportedResultException",
|
||||
"Multidimensional arrays are not supported.", 400);
|
||||
case Types.BINARY, Types.VARBINARY, Types.LONGVARBINARY, Types.BLOB ->
|
||||
throw new AwsException("UnsupportedResultException", "Binary arrays are not supported.", 400);
|
||||
default -> "stringValue";
|
||||
};
|
||||
Object values = array.getArray();
|
||||
ObjectNode arrayValue = objectMapper.createObjectNode();
|
||||
ArrayNode elements = arrayValue.putArray(fieldName + "s");
|
||||
// Qualify the reflection helper to distinguish it from JDBC's java.sql.Array.
|
||||
for (int index = 0; index < java.lang.reflect.Array.getLength(values); index++) {
|
||||
Object element = java.lang.reflect.Array.get(values, index);
|
||||
if (element == null) {
|
||||
throw new AwsException("UnsupportedResultException", "Null array elements are not supported.", 400);
|
||||
}
|
||||
if (element instanceof Array || element.getClass().isArray()) {
|
||||
throw new AwsException("UnsupportedResultException", "Multidimensional arrays are not supported.", 400);
|
||||
}
|
||||
elements.add(toField(objectMapper, element, baseType).get(fieldName));
|
||||
}
|
||||
return arrayValue;
|
||||
} finally {
|
||||
array.free();
|
||||
}
|
||||
}
|
||||
|
||||
private static String formatDate(Object value) {
|
||||
if (value instanceof Date date) {
|
||||
return date.toLocalDate().toString();
|
||||
|
||||
@@ -19,6 +19,7 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Array;
|
||||
import java.sql.Blob;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -502,10 +503,12 @@ public class RdsDataService implements Resettable {
|
||||
try (ResultSet rs = statement.getResultSet()) {
|
||||
ResultSetMetaData meta = rs.getMetaData();
|
||||
rejectUnsupportedResultTypes(meta, engine);
|
||||
if (includeMetadata) {
|
||||
response.set("columnMetadata", RdsDataColumnMetadata.toColumnMetadata(objectMapper, meta));
|
||||
ArrayNode columnMetadata = includeMetadata
|
||||
? RdsDataColumnMetadata.toColumnMetadata(objectMapper, meta) : null;
|
||||
if (columnMetadata != null) {
|
||||
response.set("columnMetadata", columnMetadata);
|
||||
}
|
||||
response.set("records", records(rs, meta));
|
||||
response.set("records", records(rs, meta, columnMetadata));
|
||||
}
|
||||
response.put("numberOfRecordsUpdated", 0L);
|
||||
} else {
|
||||
@@ -561,7 +564,7 @@ public class RdsDataService implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayNode records(ResultSet rs, ResultSetMetaData meta) throws SQLException {
|
||||
private ArrayNode records(ResultSet rs, ResultSetMetaData meta, ArrayNode columnMetadata) throws SQLException {
|
||||
ArrayNode records = objectMapper.createArrayNode();
|
||||
int columnCount = meta.getColumnCount();
|
||||
// Serialized size of the records array: its two brackets plus one comma between rows.
|
||||
@@ -571,6 +574,9 @@ public class RdsDataService implements Resettable {
|
||||
long rawRowBytes = 0;
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
Object value = rs.getObject(i);
|
||||
if (columnMetadata != null && value instanceof Array array) {
|
||||
((ObjectNode) columnMetadata.get(i - 1)).put("arrayBaseColumnType", array.getBaseType());
|
||||
}
|
||||
rawRowBytes += valueBytes(value);
|
||||
row.add(RdsDataFieldMapper.toField(objectMapper, value, meta.getColumnType(i)));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user