Skip to content

Commit

Permalink
Field writers for numerical arrays (#14900)
Browse files Browse the repository at this point in the history
Row-based frames, and by extension, MSQ now supports numeric array types. This means that all queries consuming or producing arrays would also work with MSQ. Numeric arrays can also be ingested via MSQ. Post this patch, queries like, SELECT [1, 2] would work with MSQ since they consume a numeric array, instead of failing with an unsupported column type exception.
  • Loading branch information
LakshSingla authored Oct 4, 2023
1 parent 88476e0 commit 30cf76d
Show file tree
Hide file tree
Showing 49 changed files with 2,916 additions and 510 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public static DimensionSchema createDimensionSchema(
switch (type.getElementType().getType()) {
case STRING:
return new StringDimensionSchema(column, DimensionSchema.MultiValueHandling.ARRAY, null);
case LONG:
case FLOAT:
case DOUBLE:
return new AutoTypeColumnSchema(column);
default:
throw new ISE("Cannot create dimension for type [%s]", type.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.druid.msq.indexing.error.TooManyColumnsFault;
import org.apache.druid.msq.indexing.error.TooManyInputFilesFault;
import org.apache.druid.msq.indexing.error.TooManyPartitionsFault;
import org.apache.druid.msq.indexing.error.UnknownFault;
import org.apache.druid.msq.test.MSQTestBase;
import org.apache.druid.msq.test.MSQTestFileUtils;
import org.apache.druid.segment.column.ColumnType;
Expand Down Expand Up @@ -220,31 +219,6 @@ public void testInsertWithTooManySegments() throws IOException

}

@Test
public void testInsertWithUnsupportedColumnType()
{
RowSignature dummyRowSignature = RowSignature.builder().add("__time", ColumnType.LONG).build();

testIngestQuery()
.setSql(StringUtils.format(
" insert into foo1 SELECT\n"
+ " floor(TIME_PARSE(\"timestamp\") to day) AS __time,\n"
+ " col1\n"
+ "FROM TABLE(\n"
+ " EXTERN(\n"
+ " '{ \"files\": [\"ignored\"],\"type\":\"local\"}',\n"
+ " '{\"type\": \"json\"}',\n"
+ " '[{\"name\": \"timestamp\", \"type\": \"string\"},{\"name\": \"col1\", \"type\": \"long_array\"} ]'\n"
+ " )\n"
+ ") PARTITIONED by day"
))
.setExpectedDataSource("foo1")
.setExpectedRowSignature(dummyRowSignature)
.setExpectedMSQFault(UnknownFault.forMessage(
"org.apache.druid.java.util.common.ISE: Cannot create dimension for type [ARRAY<LONG>]"))
.verifyResults();
}

@Test
public void testInsertWithManyColumns()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -1463,6 +1464,193 @@ public void testInsertArraysAutoType() throws IOException
.verifyResults();
}

@Test
public void testInsertArrays() throws IOException
{
List<Object[]> expectedRows = Arrays.asList(
new Object[]{
1672531200000L,
null,
null,
new Object[]{1L, 2L, 3L},
new Object[]{},
new Object[]{1.1d, 2.2d, 3.3d},
null
},
new Object[]{
1672531200000L,
null,
Arrays.asList("a", "b"),
null,
new Object[]{2L, 3L},
null,
new Object[]{null}
},
new Object[]{
1672531200000L,
Arrays.asList("a", "b"),
null,
null,
new Object[]{null, 2L, 9L},
null,
new Object[]{999.0d, 5.5d, null}
},
new Object[]{
1672531200000L,
Arrays.asList("a", "b"),
Arrays.asList("a", "b"),
new Object[]{1L, 2L, 3L},
new Object[]{1L, null, 3L},
new Object[]{1.1d, 2.2d, 3.3d},
new Object[]{1.1d, 2.2d, null}
},
new Object[]{
1672531200000L,
Arrays.asList("a", "b", "c"),
Arrays.asList(null, "b"),
new Object[]{2L, 3L},
null,
new Object[]{3.3d, 4.4d, 5.5d},
new Object[]{999.0d, null, 5.5d}
},
new Object[]{
1672531200000L,
Arrays.asList("b", "c"),
Arrays.asList("d", null, "b"),
new Object[]{1L, 2L, 3L, 4L},
new Object[]{1L, 2L, 3L},
new Object[]{1.1d, 3.3d},
new Object[]{null, 2.2d, null}
},
new Object[]{
1672531200000L,
Arrays.asList("d", "e"),
Arrays.asList("b", "b"),
new Object[]{1L, 4L},
new Object[]{1L},
new Object[]{2.2d, 3.3d, 4.0d},
null
},
new Object[]{
1672617600000L,
null,
null,
new Object[]{1L, 2L, 3L},
null,
new Object[]{1.1d, 2.2d, 3.3d},
new Object[]{}
},
new Object[]{
1672617600000L,
null,
Arrays.asList("a", "b"),
null,
new Object[]{2L, 3L},
null,
new Object[]{null, 1.1d}
},
new Object[]{
1672617600000L,
Arrays.asList("a", "b"),
null,
null,
new Object[]{null, 2L, 9L},
null,
new Object[]{999.0d, 5.5d, null}
},
new Object[]{
1672617600000L,
Arrays.asList("a", "b"),
Collections.emptyList(),
new Object[]{1L, 2L, 3L},
new Object[]{1L, null, 3L},
new Object[]{1.1d, 2.2d, 3.3d},
new Object[]{1.1d, 2.2d, null}
},
new Object[]{
1672617600000L,
Arrays.asList("a", "b", "c"),
Arrays.asList(null, "b"),
new Object[]{2L, 3L},
null,
new Object[]{3.3d, 4.4d, 5.5d},
new Object[]{999.0d, null, 5.5d}
},
new Object[]{
1672617600000L,
Arrays.asList("b", "c"),
Arrays.asList("d", null, "b"),
new Object[]{1L, 2L, 3L, 4L},
new Object[]{1L, 2L, 3L},
new Object[]{1.1d, 3.3d},
new Object[]{null, 2.2d, null}
},
new Object[]{
1672617600000L,
Arrays.asList("d", "e"),
Arrays.asList("b", "b"),
new Object[]{1L, 4L},
new Object[]{null},
new Object[]{2.2d, 3.3d, 4.0},
null
}
);

RowSignature rowSignatureWithoutTimeAndStringColumns =
RowSignature.builder()
.add("arrayLong", ColumnType.LONG_ARRAY)
.add("arrayLongNulls", ColumnType.LONG_ARRAY)
.add("arrayDouble", ColumnType.DOUBLE_ARRAY)
.add("arrayDoubleNulls", ColumnType.DOUBLE_ARRAY)
.build();


RowSignature fileSignature = RowSignature.builder()
.add("timestamp", ColumnType.STRING)
.add("arrayString", ColumnType.STRING_ARRAY)
.add("arrayStringNulls", ColumnType.STRING_ARRAY)
.addAll(rowSignatureWithoutTimeAndStringColumns)
.build();

// MSQ writes strings instead of string arrays
RowSignature rowSignature = RowSignature.builder()
.add("__time", ColumnType.LONG)
.add("arrayString", ColumnType.STRING)
.add("arrayStringNulls", ColumnType.STRING)
.addAll(rowSignatureWithoutTimeAndStringColumns)
.build();

final Map<String, Object> adjustedContext = new HashMap<>(context);
final File tmpFile = temporaryFolder.newFile();
final InputStream resourceStream = NestedDataTestUtils.class.getClassLoader().getResourceAsStream(NestedDataTestUtils.ARRAY_TYPES_DATA_FILE);
final InputStream decompressing = CompressionUtils.decompress(resourceStream, NestedDataTestUtils.ARRAY_TYPES_DATA_FILE);
Files.copy(decompressing, tmpFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
decompressing.close();

final String toReadFileNameAsJson = queryFramework().queryJsonMapper().writeValueAsString(tmpFile);

testIngestQuery().setSql(" INSERT INTO foo1 SELECT\n"
+ " TIME_PARSE(\"timestamp\") as __time,\n"
+ " arrayString,\n"
+ " arrayStringNulls,\n"
+ " arrayLong,\n"
+ " arrayLongNulls,\n"
+ " arrayDouble,\n"
+ " arrayDoubleNulls\n"
+ "FROM TABLE(\n"
+ " EXTERN(\n"
+ " '{ \"files\": [" + toReadFileNameAsJson + "],\"type\":\"local\"}',\n"
+ " '{\"type\": \"json\"}',\n"
+ " '" + queryFramework().queryJsonMapper().writeValueAsString(fileSignature) + "'\n"
+ " )\n"
+ ") PARTITIONED BY day")
.setQueryContext(adjustedContext)
.setExpectedResultRows(expectedRows)
.setExpectedDataSource("foo1")
.setExpectedRowSignature(rowSignature)
.verifyResults();
}

@Nonnull
private List<Object[]> expectedFooRows()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
/**
* Runs {@link CalciteArraysQueryTest} but with MSQ engine
*/
public class CalciteArraysSelectQueryMSQTest extends CalciteArraysQueryTest
public class CalciteArraysQueryMSQTest extends CalciteArraysQueryTest
{
private TestGroupByBuffers groupByBuffers;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.druid.frame.field;

import org.apache.datasketches.memory.Memory;
import org.apache.druid.segment.ColumnValueSelector;

import javax.annotation.Nullable;

/**
* Reader for fields written by {@link NumericArrayFieldWriter#getDoubleArrayFieldWriter}
*/
public class DoubleArrayFieldReader extends NumericArrayFieldReader
{
@Override
public ColumnValueSelector<?> makeColumnValueSelector(
Memory memory,
ReadableFieldPointer fieldPointer
)
{
return new NumericArrayFieldSelector<Double>(memory, fieldPointer)
{
final SettableFieldPointer fieldPointer = new SettableFieldPointer();
final ColumnValueSelector<?> columnValueSelector =
DoubleFieldReader.forArray().makeColumnValueSelector(memory, fieldPointer);

@Nullable
@Override
public Double getIndividualValueAtMemory(long position)
{
fieldPointer.setPosition(position);
if (columnValueSelector.isNull()) {
return null;
}
return columnValueSelector.getDouble();
}

@Override
public int getIndividualFieldSize()
{
return Byte.BYTES + Double.BYTES;
}
};
}
}
Loading

0 comments on commit 30cf76d

Please sign in to comment.