Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ public Object getObject(int columnIndex) throws SQLException {
ColumnMetaData metaData = columnMetaDataList.get(columnIndex - 1);
if (metaData.type.id == Types.TIMESTAMP_WITH_TIMEZONE) {
return accessor.getTimestamp(localCalendar);
} else {
return AvaticaSite.get(accessor, metaData.type.id, true, localCalendar);
}
if (metaData.type.id == Types.STRUCT) {
// Keep named field access for callers of the untyped getObject.
return accessor.getObject();
}
return AvaticaSite.get(accessor, metaData.type.id, true, localCalendar);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.arrow.driver.jdbc.accessor.impl.complex;

import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Struct;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -53,6 +55,14 @@ public Object getObject() {
return object;
}

@Override
public <T> T getObject(Class<T> type) throws SQLException {
if (type == Struct.class) {
return type.cast(getStruct());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Support an empty JDBC type map on the new Struct return value. I extended testStructMetadataAndObjectAccess with assertArrayEquals(new Object[] {12}, result.getObject(1, Struct.class).getAttributes(Map.of())). On JDK 21 this throws UnsupportedOperationException from Avatica’s StructImpl; the no-arg call directly above it returns [12]. An empty map should use the default attribute mappings. Could the returned Struct delegate that case to getAttributes() and report unsupported custom mappings as a SQLException subtype?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in aecdef0. Empty and null type maps now return the same attributes as getAttributes(). A nonempty map throws SQLFeatureNotSupportedException. The regression test covers all three cases. I also checked nested structs and null rows over a local Flight SQL connection on JDK 21. The JDBC core Maven build and its dependencies passed (4,697 tests passed, 74 skipped).

}
return super.getObject(type);
}

@Override
public Struct getStruct() {
int currentRow = getCurrentRow();
Expand All @@ -68,6 +78,14 @@ public Struct getStruct() {
.map(vector -> vector.getObject(currentRow))
.collect(Collectors.toList());

return new StructImpl(attributes);
return new StructImpl(attributes) {
@Override
public Object[] getAttributes(Map<String, Class<?>> map) throws SQLException {
if (map != null && !map.isEmpty()) {
throw new SQLFeatureNotSupportedException("Custom type maps are not supported");
}
return getAttributes();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public static List<ColumnMetaData> convertArrowFieldsToColumnMetaDataList(
.setLabel(field.getName());

setOnColumnMetaDataBuilder(builder, field.getMetadata());
if (fieldType instanceof ArrowType.Struct) {
builder.setColumnClassName(Map.class.getName());
}

builder.setType(
Common.AvaticaType.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public static int getSqlTypeIdFromArrowType(ArrowType arrowType) {
case LargeList:
return Types.ARRAY;
case Struct:
return Types.STRUCT;
case Duration:
case Interval:
case Map:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLTimeoutException;
import java.sql.Statement;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
Expand All @@ -66,9 +69,11 @@
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.IntVector;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.complex.StructVector;
import org.apache.arrow.vector.types.Types;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.FieldType;
import org.apache.arrow.vector.types.pojo.Schema;
import org.apache.arrow.vector.util.UuidUtility;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -103,6 +108,41 @@ private static void resultSetNextUntilDone(ResultSet resultSet) throws SQLExcept
}
}

@Test
public void testStructMetadataAndObjectAccess() throws SQLException {
Field child = Field.nullable("count", new ArrowType.Int(32, true));
Field field = new Field("value", FieldType.nullable(ArrowType.Struct.INSTANCE), List.of(child));
try (BufferAllocator allocator = new RootAllocator();
VectorSchemaRoot root = VectorSchemaRoot.create(new Schema(List.of(field)), allocator)) {
root.allocateNew();
StructVector values = (StructVector) root.getVector("value");
values.getChild("count", IntVector.class).setSafe(0, 12);
values.setIndexDefined(0);
values.setNull(1);
root.setRowCount(2);
try (ResultSet result = ArrowFlightJdbcVectorSchemaRootResultSet.fromVectorSchemaRoot(root)) {
ResultSetMetaData metadata = result.getMetaData();
assertEquals(java.sql.Types.STRUCT, metadata.getColumnType(1));
assertEquals("STRUCT", metadata.getColumnTypeName(1));
assertEquals(Map.class.getName(), metadata.getColumnClassName(1));
assertTrue(result.next());
assertEquals(Map.of("count", 12), result.getObject("value"));
assertEquals(Map.of("count", 12), result.getObject(1, Map.class));
Struct value = result.getObject(1, Struct.class);
Object[] expected = {12};
assertArrayEquals(expected, value.getAttributes());
assertArrayEquals(expected, value.getAttributes(Map.of()));
assertArrayEquals(expected, value.getAttributes(null));
assertThrows(
SQLFeatureNotSupportedException.class,
() -> value.getAttributes(Map.of("STRUCT", Object.class)));
assertTrue(result.next());
assertNull(result.getObject(1, Struct.class));
assertTrue(result.wasNull());
}
}
}

/**
* Tests whether the {@link ArrowFlightJdbcDriver} can run a query successfully.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void testGetSqlTypeIdFromArrowType() {
assertEquals(Types.ARRAY, getSqlTypeIdFromArrowType(new ArrowType.LargeList()));
assertEquals(Types.ARRAY, getSqlTypeIdFromArrowType(new ArrowType.FixedSizeList(10)));

assertEquals(Types.JAVA_OBJECT, getSqlTypeIdFromArrowType(new ArrowType.Struct()));
assertEquals(Types.STRUCT, getSqlTypeIdFromArrowType(new ArrowType.Struct()));
assertEquals(
Types.JAVA_OBJECT, getSqlTypeIdFromArrowType(new ArrowType.Duration(TimeUnit.MILLISECOND)));
assertEquals(
Expand Down Expand Up @@ -132,7 +132,7 @@ public void testGetSqlTypeNameFromArrowType() {
assertEquals("ARRAY", getSqlTypeNameFromArrowType(new ArrowType.LargeList()));
assertEquals("ARRAY", getSqlTypeNameFromArrowType(new ArrowType.FixedSizeList(10)));

assertEquals("JAVA_OBJECT", getSqlTypeNameFromArrowType(new ArrowType.Struct()));
assertEquals("STRUCT", getSqlTypeNameFromArrowType(new ArrowType.Struct()));

assertEquals(
"JAVA_OBJECT", getSqlTypeNameFromArrowType(new ArrowType.Duration(TimeUnit.MILLISECOND)));
Expand Down
Loading