From df4b14a2228b6bfad83e0aa777857673cc77b696 Mon Sep 17 00:00:00 2001 From: Dain Sundstrom Date: Wed, 13 Sep 2023 12:25:23 -0700 Subject: [PATCH] Test read FLAT to block builder --- .../java/io/trino/type/AbstractTestType.java | 6 ++ .../TestArrayOfMapOfBigintVarcharType.java | 85 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 core/trino-main/src/test/java/io/trino/type/TestArrayOfMapOfBigintVarcharType.java diff --git a/core/trino-main/src/test/java/io/trino/type/AbstractTestType.java b/core/trino-main/src/test/java/io/trino/type/AbstractTestType.java index 23e5dfc95b6e..15d3dc3b05e5 100644 --- a/core/trino-main/src/test/java/io/trino/type/AbstractTestType.java +++ b/core/trino-main/src/test/java/io/trino/type/AbstractTestType.java @@ -95,6 +95,7 @@ public abstract class AbstractTestType private final TypeOperators typeOperators; private final MethodHandle readBlockMethod; private final MethodHandle writeBlockMethod; + private final MethodHandle writeFlatToBlockMethod; private final MethodHandle readFlatMethod; private final MethodHandle writeFlatMethod; private final MethodHandle writeBlockToFlatMethod; @@ -127,6 +128,7 @@ protected AbstractTestType(Type type, Class objectValueType, Block testBlock, typeOperators = new TypeOperators(); readBlockMethod = typeOperators.getReadValueOperator(type, simpleConvention(FAIL_ON_NULL, BLOCK_POSITION_NOT_NULL)); writeBlockMethod = typeOperators.getReadValueOperator(type, simpleConvention(BLOCK_BUILDER, NEVER_NULL)); + writeFlatToBlockMethod = typeOperators.getReadValueOperator(type, simpleConvention(BLOCK_BUILDER, FLAT)); readFlatMethod = typeOperators.getReadValueOperator(type, simpleConvention(FAIL_ON_NULL, FLAT)); writeFlatMethod = typeOperators.getReadValueOperator(type, simpleConvention(FLAT_RETURN, NEVER_NULL)); writeBlockToFlatMethod = typeOperators.getReadValueOperator(type, simpleConvention(FLAT_RETURN, BLOCK_POSITION)); @@ -312,6 +314,10 @@ else if (type.getJavaType() == Block.class) { assertEquals(readFlatMethod.invoke(fixed, elementFixedOffset, variable), expectedStackValue); } + BlockBuilder blockBuilder = type.createBlockBuilder(null, 1); + writeFlatToBlockMethod.invokeExact(fixed, elementFixedOffset, variable, blockBuilder); + assertPositionEquals(testBlock, i, expectedStackValue, expectedObjectValues.get(i)); + if (type.isComparable()) { assertTrue((Boolean) flatFlatEqualOperator.invokeExact(fixed, elementFixedOffset, variable, fixed, elementFixedOffset, variable)); assertTrue((Boolean) flatBlockPositionEqualOperator.invokeExact(fixed, elementFixedOffset, variable, testBlock, i)); diff --git a/core/trino-main/src/test/java/io/trino/type/TestArrayOfMapOfBigintVarcharType.java b/core/trino-main/src/test/java/io/trino/type/TestArrayOfMapOfBigintVarcharType.java new file mode 100644 index 000000000000..e442fdec7904 --- /dev/null +++ b/core/trino-main/src/test/java/io/trino/type/TestArrayOfMapOfBigintVarcharType.java @@ -0,0 +1,85 @@ +/* + * Licensed 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 io.trino.type; + +import com.google.common.collect.ImmutableMap; +import io.trino.spi.block.Block; +import io.trino.spi.block.BlockBuilder; +import io.trino.spi.type.ArrayType; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static io.trino.spi.type.BigintType.BIGINT; +import static io.trino.spi.type.VarcharType.VARCHAR; +import static io.trino.util.StructuralTestUtil.arrayBlockOf; +import static io.trino.util.StructuralTestUtil.mapBlockOf; +import static io.trino.util.StructuralTestUtil.mapType; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class TestArrayOfMapOfBigintVarcharType + extends AbstractTestType +{ + private static final ArrayType TYPE = new ArrayType(mapType(BIGINT, VARCHAR)); + + public TestArrayOfMapOfBigintVarcharType() + { + super(TYPE, List.class, createTestBlock()); + } + + public static Block createTestBlock() + { + BlockBuilder blockBuilder = TYPE.createBlockBuilder(null, 4); + TYPE.writeObject(blockBuilder, arrayBlockOf(TYPE.getElementType(), + mapBlockOf(BIGINT, VARCHAR, ImmutableMap.of(1, "hi")), + mapBlockOf(BIGINT, VARCHAR, ImmutableMap.of(2, "bye")))); + TYPE.writeObject(blockBuilder, arrayBlockOf(TYPE.getElementType(), + mapBlockOf(BIGINT, VARCHAR, ImmutableMap.of(1, "2", 2, "hello")), + mapBlockOf(BIGINT, VARCHAR, ImmutableMap.of(3, "4", 4, "bye")))); + TYPE.writeObject(blockBuilder, arrayBlockOf(TYPE.getElementType(), + mapBlockOf(BIGINT, VARCHAR, ImmutableMap.of(100, "hundred")), + mapBlockOf(BIGINT, VARCHAR, ImmutableMap.of(200, "two hundred")))); + return blockBuilder.build(); + } + + @Override + protected Object getGreaterValue(Object value) + { + throw new UnsupportedOperationException(); + } + + @Test + public void testRange() + { + assertThat(type.getRange()) + .isEmpty(); + } + + @Test + public void testPreviousValue() + { + assertThatThrownBy(() -> type.getPreviousValue(getSampleValue())) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Type is not orderable: " + type); + } + + @Test + public void testNextValue() + { + assertThatThrownBy(() -> type.getPreviousValue(getSampleValue())) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Type is not orderable: " + type); + } +}