Skip to content

Commit 8d4ac2f

Browse files
authored
feat: DH-22378: add QST array type ColumnDefinition constructors (#7931)
1 parent 759721c commit 8d4ac2f

36 files changed

Lines changed: 443 additions & 140 deletions

engine/api/src/main/java/io/deephaven/engine/table/ColumnDefinition.java

Lines changed: 83 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,27 @@ public static ColumnDefinition<?> of(String name, Type<?> type) {
107107
}
108108

109109
public static ColumnDefinition<?> of(String name, PrimitiveType<?> type) {
110-
final PrimitiveType.Visitor<ColumnDefinition<?>> adapter = new Adapter(name);
111-
return type.walk(adapter);
110+
return type.walk((PrimitiveType.Visitor<ColumnDefinition<?>>) new Adapter(name));
112111
}
113112

114113
public static ColumnDefinition<?> of(String name, GenericType<?> type) {
115-
final GenericType.Visitor<ColumnDefinition<?>> adapter = new Adapter(name);
116-
return type.walk(adapter);
114+
return type.walk((GenericType.Visitor<ColumnDefinition<?>>) new Adapter(name));
115+
}
116+
117+
public static <T> ColumnDefinition<T> of(String name, ArrayType<T, ?> type) {
118+
return type.walk(new ArrayAdapter<>(name));
119+
}
120+
121+
public static <T> ColumnDefinition<T> of(String name, PrimitiveVectorType<T, ?> type) {
122+
return new ColumnDefinition<>(name, type.clazz(), type.componentType().clazz(), ColumnType.Normal);
123+
}
124+
125+
public static <T> ColumnDefinition<T> of(String name, GenericVectorType<T, ?> type) {
126+
return new ColumnDefinition<>(name, type.clazz(), type.componentType().clazz(), ColumnType.Normal);
127+
}
128+
129+
public static <T> ColumnDefinition<T> of(String name, NativeArrayType<T, ?> type) {
130+
return new ColumnDefinition<>(name, type.clazz(), type.componentType().clazz(), ColumnType.Normal);
117131
}
118132

119133
public static <T extends Vector<?>> ColumnDefinition<T> ofVector(
@@ -122,6 +136,14 @@ public static <T extends Vector<?>> ColumnDefinition<T> ofVector(
122136
return new ColumnDefinition<>(name, vectorType, baseComponentTypeForVector(vectorType), ColumnType.Normal);
123137
}
124138

139+
public static <T extends Vector<?>> ColumnDefinition<T> ofVector(
140+
@NotNull final String name,
141+
@NotNull final Class<T> vectorType,
142+
@Nullable final Class<?> componentType) {
143+
return new ColumnDefinition<>(name, vectorType,
144+
checkAndMaybeInferVectorComponentType(vectorType, componentType), ColumnType.Normal);
145+
}
146+
125147
public static <T> ColumnDefinition<T> fromGenericType(
126148
@NotNull final String name,
127149
@NotNull final Class<T> dataType) {
@@ -175,30 +197,6 @@ private static Class<?> baseComponentTypeForVector(@NotNull final Class<? extend
175197
throw new IllegalArgumentException("Unrecognized Vector type " + vectorType);
176198
}
177199

178-
private static void assertComponentTypeValid(
179-
@NotNull final Class<?> dataType, @Nullable final Class<?> componentType) {
180-
if (!Vector.class.isAssignableFrom(dataType) && !dataType.isArray()) {
181-
return;
182-
}
183-
if (componentType == null) {
184-
throw new IllegalArgumentException("Required component type not specified for data type " + dataType);
185-
}
186-
if (dataType.isArray()) {
187-
final Class<?> arrayComponentType = dataType.getComponentType();
188-
if (!arrayComponentType.isAssignableFrom(componentType)) {
189-
throw new IllegalArgumentException(
190-
"Invalid component type " + componentType + " for array data type " + dataType);
191-
}
192-
return;
193-
}
194-
// noinspection unchecked
195-
final Class<?> baseComponentType = baseComponentTypeForVector((Class<? extends Vector<?>>) dataType);
196-
if (!baseComponentType.isAssignableFrom(componentType)) {
197-
throw new IllegalArgumentException(
198-
"Invalid component type " + componentType + " for Vector data type " + dataType);
199-
}
200-
}
201-
202200
private static Class<?> checkAndMaybeInferComponentType(
203201
@NotNull final Class<?> dataType, @Nullable final Class<?> inputComponentType) {
204202
if (dataType.isArray()) {
@@ -214,21 +212,33 @@ private static Class<?> checkAndMaybeInferComponentType(
214212
}
215213
if (Vector.class.isAssignableFrom(dataType)) {
216214
// noinspection unchecked
217-
final Class<?> vectorComponentType =
218-
baseComponentTypeForVector((Class<? extends Vector<?>>) dataType);
219-
if (inputComponentType == null) {
220-
/*
221-
* TODO (https://github.com/deephaven/deephaven-core/issues/817): Allow formula results returning Vector
222-
* to know component type if (Vector.class.isAssignableFrom(dataType)) { throw new
223-
* IllegalArgumentException("Missing required component type for Vector data type " + dataType); }
224-
*/
225-
return vectorComponentType;
226-
}
227-
if (!vectorComponentType.isAssignableFrom(inputComponentType)) {
228-
throw new IllegalArgumentException(
229-
"Invalid component type " + inputComponentType + " for Vector data type " + dataType);
230-
}
231-
return inputComponentType;
215+
return checkAndMaybeInferVectorComponentType((Class<? extends Vector<?>>) dataType, inputComponentType);
216+
}
217+
// Note: some testing currently depends on being able to create Collection + componentType definitions:
218+
// io.deephaven.server.jetty.BarrageChunkFactoryTest.testNotAListDestinationPropagation
219+
// if (inputComponentType != null) {
220+
// throw new IllegalArgumentException(String.format(
221+
// "Invalid componentType %s for non-array, non-Vector dataType %s", inputComponentType, dataType));
222+
// }
223+
// return null;
224+
return inputComponentType;
225+
}
226+
227+
private static Class<?> checkAndMaybeInferVectorComponentType(
228+
@NotNull final Class<? extends Vector<?>> dataType,
229+
@Nullable final Class<?> inputComponentType) {
230+
final Class<?> vectorComponentType = baseComponentTypeForVector(dataType);
231+
if (inputComponentType == null) {
232+
/*
233+
* TODO (https://github.com/deephaven/deephaven-core/issues/817): Allow formula results returning Vector to
234+
* know component type if (Vector.class.isAssignableFrom(dataType)) { throw new
235+
* IllegalArgumentException("Missing required component type for Vector data type " + dataType); }
236+
*/
237+
return vectorComponentType;
238+
}
239+
if (!vectorComponentType.isAssignableFrom(inputComponentType)) {
240+
throw new IllegalArgumentException(
241+
"Invalid component type " + inputComponentType + " for Vector data type " + dataType);
232242
}
233243
return inputComponentType;
234244
}
@@ -314,23 +324,7 @@ public ColumnDefinition<?> visit(InstantType instantType) {
314324

315325
@Override
316326
public ColumnDefinition<?> visit(ArrayType<?, ?> arrayType) {
317-
return arrayType.walk(new ArrayType.Visitor<>() {
318-
@Override
319-
public ColumnDefinition<?> visit(NativeArrayType<?, ?> nativeArrayType) {
320-
return fromGenericType(name, nativeArrayType.clazz(), nativeArrayType.componentType().clazz());
321-
}
322-
323-
@Override
324-
public ColumnDefinition<?> visit(PrimitiveVectorType<?, ?> vectorPrimitiveType) {
325-
// noinspection unchecked
326-
return ofVector(name, (Class<? extends Vector<?>>) vectorPrimitiveType.clazz());
327-
}
328-
329-
@Override
330-
public ColumnDefinition<?> visit(GenericVectorType<?, ?> genericVectorType) {
331-
return fromGenericType(name, ObjectVector.class, genericVectorType.componentType().clazz());
332-
}
333-
});
327+
return of(name, arrayType);
334328
}
335329

336330
@Override
@@ -339,6 +333,33 @@ public ColumnDefinition<?> visit(CustomType<?> customType) {
339333
}
340334
}
341335

336+
private static class ArrayAdapter<T> implements ArrayType.Visitor<ColumnDefinition<T>> {
337+
338+
private final String name;
339+
340+
public ArrayAdapter(String name) {
341+
this.name = Objects.requireNonNull(name);
342+
}
343+
344+
@Override
345+
public ColumnDefinition<T> visit(NativeArrayType<?, ?> nativeArrayType) {
346+
// noinspection unchecked
347+
return of(name, (NativeArrayType<T, ?>) nativeArrayType);
348+
}
349+
350+
@Override
351+
public ColumnDefinition<T> visit(PrimitiveVectorType<?, ?> vectorPrimitiveType) {
352+
// noinspection unchecked
353+
return of(name, (PrimitiveVectorType<T, ?>) vectorPrimitiveType);
354+
}
355+
356+
@Override
357+
public ColumnDefinition<T> visit(GenericVectorType<?, ?> genericVectorType) {
358+
// noinspection unchecked
359+
return of(name, (GenericVectorType<T, ?>) genericVectorType);
360+
}
361+
}
362+
342363
@NotNull
343364
private final String name;
344365
@NotNull
@@ -397,7 +418,8 @@ public <Other> ColumnDefinition<Other> withDataType(@NotNull final Class<Other>
397418
// noinspection unchecked
398419
return dataType == newDataType
399420
? (ColumnDefinition<Other>) this
400-
: fromGenericType(name, newDataType, componentType, columnType);
421+
// Do not pass along the existing component-type; it will be inferred
422+
: fromGenericType(name, newDataType, null, columnType);
401423
}
402424

403425
public <Other> ColumnDefinition<Other> withDataType(

0 commit comments

Comments
 (0)