Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect query results for array type column in Bigquery with bigquery.arrow-serialization.enabled enabled #23982

Merged
merged 2 commits into from
Oct 30, 2024
Merged
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 @@ -29,6 +29,7 @@
import io.trino.spi.type.Type;
import io.trino.spi.type.VarbinaryType;
import io.trino.spi.type.VarcharType;
import org.apache.arrow.memory.ArrowBuf;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.vector.BigIntVector;
import org.apache.arrow.vector.BitVector;
Expand All @@ -48,6 +49,7 @@
import org.apache.arrow.vector.complex.StructVector;
import org.apache.arrow.vector.ipc.message.ArrowRecordBatch;
import org.apache.arrow.vector.types.pojo.Schema;
import org.apache.arrow.vector.util.TransferPair;

import java.math.BigDecimal;
import java.util.List;
Expand Down Expand Up @@ -75,6 +77,7 @@
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static org.apache.arrow.compression.CommonsCompressionFactory.INSTANCE;
import static org.apache.arrow.vector.complex.BaseRepeatedValueVector.OFFSET_WIDTH;
import static org.apache.arrow.vector.types.Types.MinorType.DECIMAL256;

public class BigQueryArrowToPageConverter
Expand All @@ -84,10 +87,12 @@ public class BigQueryArrowToPageConverter
private final VectorSchemaRoot root;
private final VectorLoader loader;
private final List<BigQueryColumnHandle> columns;
private final BufferAllocator allocator;

public BigQueryArrowToPageConverter(BigQueryTypeManager typeManager, BufferAllocator allocator, Schema schema, List<BigQueryColumnHandle> columns)
{
this.typeManager = requireNonNull(typeManager, "typeManager is null");
this.allocator = requireNonNull(allocator, "allocator is null");
this.columns = ImmutableList.copyOf(requireNonNull(columns, "columns is null"));
List<FieldVector> vectors = schema.getFields().stream()
.map(field -> field.createVector(allocator))
Expand Down Expand Up @@ -172,7 +177,7 @@ else if (javaType == LongTimestampWithTimeZone.class) {
writeVectorValues(output, vector, index -> writeObjectTimestampWithTimezone(output, type, vector, index), offset, length);
}
else if (type instanceof ArrayType arrayType) {
writeVectorValues(output, vector, _ -> writeArrayBlock(output, arrayType, vector), offset, length);
writeVectorValues(output, vector, index -> writeArrayBlock(output, arrayType, vector, index), offset, length);
}
else if (type instanceof RowType rowType) {
writeVectorValues(output, vector, index -> writeRowBlock(output, rowType, vector, index), offset, length);
Expand Down Expand Up @@ -241,11 +246,23 @@ private void writeObjectTimestampWithTimezone(BlockBuilder output, Type type, Fi
type.writeObject(output, fromEpochMillisAndFraction(floorDiv(epochMicros, MICROSECONDS_PER_MILLISECOND), picosOfMillis, UTC_KEY));
}

private void writeArrayBlock(BlockBuilder output, ArrayType arrayType, FieldVector vector)
private void writeArrayBlock(BlockBuilder output, ArrayType arrayType, FieldVector vector, int index)
{
Type elementType = arrayType.getElementType();
FieldVector innerVector = ((ListVector) vector).getDataVector();
((ArrayBlockBuilder) output).buildEntry(elementBuilder -> convertType(elementBuilder, elementType, innerVector, 0, innerVector.getValueCount()));
((ArrayBlockBuilder) output).buildEntry(elementBuilder -> {
ArrowBuf offsetBuffer = vector.getOffsetBuffer();

int start = offsetBuffer.getInt((long) index * OFFSET_WIDTH);
int end = offsetBuffer.getInt((long) (index + 1) * OFFSET_WIDTH);

FieldVector innerVector = ((ListVector) vector).getDataVector();

TransferPair transferPair = innerVector.getTransferPair(allocator);
transferPair.splitAndTransfer(start, end - start);
try (FieldVector sliced = (FieldVector) transferPair.getTo()) {
convertType(elementBuilder, elementType, sliced, 0, sliced.getValueCount());
}
});
}

private void writeRowBlock(BlockBuilder output, RowType rowType, FieldVector vector, int index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import static io.trino.type.JsonType.JSON;
import static java.lang.String.format;
import static java.time.ZoneOffset.UTC;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
Expand Down Expand Up @@ -734,6 +735,18 @@ public void testArray()
.execute(getQueryRunner(), bigqueryViewCreateAndInsert("test.array"));
}

@Test
public void testArrayType()
{
try (TestTable table = new TestTable(getQueryRunner()::execute, "test_array_", "(a BIGINT, b ARRAY<DOUBLE>, c ARRAY<BIGINT>)")) {
ebyhr marked this conversation as resolved.
Show resolved Hide resolved
assertUpdate("INSERT INTO " + table.getName() + " (a, b, c) VALUES (5, ARRAY[1.23E1], ARRAY[15]), (6, ARRAY[1.24E1, 1.27E1, 2.23E1], ARRAY[25, 26, 36])", 2);
assertThat(query("SELECT * FROM " + table.getName()))
.matches("VALUES " +
"(BIGINT '5', ARRAY[DOUBLE '12.3'], ARRAY[BIGINT '15']), " +
"(BIGINT '6', ARRAY[DOUBLE '12.4', DOUBLE '12.7', DOUBLE '22.3'], ARRAY[BIGINT '25', BIGINT '26', BIGINT '36'])");
}
}

@Test
public void testUnsupportedNullArray()
{
Expand Down