Skip to content

Commit

Permalink
Test read FLAT to block builder
Browse files Browse the repository at this point in the history
  • Loading branch information
dain committed Sep 14, 2023
1 parent 55089e9 commit df4b14a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit df4b14a

Please sign in to comment.