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

Simplify array functions #18094

Merged
merged 4 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add READ_VALUE operator to types
  • Loading branch information
dain committed Jun 30, 2023
commit 10bff931a3f25a75afe54a0d396ab3e5f912512b
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
import io.trino.operator.scalar.GenericIndeterminateOperator;
import io.trino.operator.scalar.GenericLessThanOperator;
import io.trino.operator.scalar.GenericLessThanOrEqualOperator;
import io.trino.operator.scalar.GenericReadValueOperator;
import io.trino.operator.scalar.GenericXxHash64Operator;
import io.trino.operator.scalar.HmacFunctions;
import io.trino.operator.scalar.HyperLogLogFunctions;
Expand Down Expand Up @@ -569,6 +570,7 @@ public static FunctionBundle create(FeaturesConfig featuresConfig, TypeOperators
.functions(MAP_FILTER_FUNCTION, new MapTransformKeysFunction(blockTypeOperators), MAP_TRANSFORM_VALUES_FUNCTION)
.function(FORMAT_FUNCTION)
.function(TRY_CAST)
.function(new GenericReadValueOperator(typeOperators))
.function(new GenericEqualOperator(typeOperators))
.function(new GenericHashCodeOperator(typeOperators))
.function(new GenericXxHash64Operator(typeOperators))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import static io.trino.spi.function.OperatorType.IS_DISTINCT_FROM;
import static io.trino.spi.function.OperatorType.LESS_THAN;
import static io.trino.spi.function.OperatorType.LESS_THAN_OR_EQUAL;
import static io.trino.spi.function.OperatorType.READ_VALUE;
import static io.trino.spi.function.OperatorType.XX_HASH_64;
import static io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature;
import static java.lang.String.CASE_INSENSITIVE_ORDER;
Expand Down Expand Up @@ -119,6 +120,9 @@ else if (ORDERABLE_TYPE_OPERATORS.contains(operator)) {
verifyTypeSignatureDoesNotContainAnyTypeParameters(typeSignature, typeSignature, typeParameterNames);
}
}
else if (operator == READ_VALUE) {
verifyOperatorSignature(operator, argumentTypes);
}
else {
throw new IllegalArgumentException("Operator dependency on " + operator + " is not allowed");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.operator.scalar;

import io.trino.metadata.SqlScalarFunction;
import io.trino.spi.function.BoundSignature;
import io.trino.spi.function.FunctionMetadata;
import io.trino.spi.function.ScalarFunctionImplementation;
import io.trino.spi.function.Signature;
import io.trino.spi.type.Type;
import io.trino.spi.type.TypeOperators;
import io.trino.spi.type.TypeSignature;

import java.lang.invoke.MethodHandle;

import static io.trino.spi.function.OperatorType.READ_VALUE;
import static java.util.Objects.requireNonNull;

public class GenericReadValueOperator
extends SqlScalarFunction
{
private final TypeOperators typeOperators;

public GenericReadValueOperator(TypeOperators typeOperators)
{
super(FunctionMetadata.scalarBuilder()
.signature(Signature.builder()
.operatorType(READ_VALUE)
.typeVariable("T")
.returnType(new TypeSignature("T"))
.argumentType(new TypeSignature("T"))
.build())
.build());
this.typeOperators = requireNonNull(typeOperators, "typeOperators is null");
}

@Override
protected SpecializedSqlScalarFunction specialize(BoundSignature boundSignature)
{
Type type = boundSignature.getArgumentType(0);
return invocationConvention -> {
MethodHandle methodHandle = typeOperators.getReadValueOperator(type, invocationConvention);
return ScalarFunctionImplementation.builder()
.methodHandle(methodHandle)
.build();
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static void validateOperator(OperatorType operatorType, TypeSignature ret
case IS_DISTINCT_FROM:
case XX_HASH_64:
case INDETERMINATE:
case READ_VALUE:
// TODO
}
}
Expand Down
48 changes: 43 additions & 5 deletions core/trino-main/src/test/java/io/trino/type/AbstractTestType.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.trino.type.BlockTypeOperators.BlockPositionXxHash64;
import org.testng.annotations.Test;

import java.lang.invoke.MethodHandle;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -56,7 +57,9 @@
import static io.trino.spi.connector.SortOrder.DESC_NULLS_FIRST;
import static io.trino.spi.connector.SortOrder.DESC_NULLS_LAST;
import static io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.BLOCK_POSITION;
import static io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.BLOCK_POSITION_NOT_NULL;
import static io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL;
import static io.trino.spi.function.InvocationConvention.InvocationReturnConvention.BLOCK_BUILDER;
import static io.trino.spi.function.InvocationConvention.InvocationReturnConvention.DEFAULT_ON_NULL;
import static io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL;
import static io.trino.spi.function.InvocationConvention.InvocationReturnConvention.NULLABLE_RETURN;
Expand Down Expand Up @@ -84,7 +87,11 @@ public abstract class AbstractTestType
private final Class<?> objectValueType;
private final Block testBlock;
protected final Type type;

private final TypeOperators typeOperators;
private final MethodHandle readBlockMethod;
private final MethodHandle writeBlockMethod;

protected final BlockTypeOperators blockTypeOperators;
private final BlockPositionEqual equalOperator;
private final BlockPositionHashCode hashCodeOperator;
Expand All @@ -103,6 +110,9 @@ protected AbstractTestType(Type type, Class<?> objectValueType, Block testBlock,
{
this.type = requireNonNull(type, "type is null");
typeOperators = new TypeOperators();
readBlockMethod = typeOperators.getReadValueOperator(type, simpleConvention(FAIL_ON_NULL, BLOCK_POSITION_NOT_NULL));
writeBlockMethod = typeOperators.getReadValueOperator(type, simpleConvention(BLOCK_BUILDER, NEVER_NULL));

blockTypeOperators = new BlockTypeOperators(typeOperators);
if (type.isComparable()) {
equalOperator = blockTypeOperators.getEqualOperator(type);
Expand Down Expand Up @@ -182,6 +192,7 @@ protected PlannerContext createPlannerContext()

@Test
public void testBlock()
throws Throwable
{
for (Entry<Integer, Object> entry : expectedStackValues.entrySet()) {
assertPositionEquals(testBlock, entry.getKey(), entry.getValue(), expectedObjectValues.get(entry.getKey()));
Expand Down Expand Up @@ -233,6 +244,7 @@ protected Object getSampleValue()
}

protected void assertPositionEquals(Block block, int position, Object expectedStackValue, Object expectedObjectValue)
throws Throwable
{
long hash = 0;
if (type.isComparable()) {
Expand All @@ -247,9 +259,16 @@ protected void assertPositionEquals(Block block, int position, Object expectedSt
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
type.appendTo(block, position, blockBuilder);
assertPositionValue(blockBuilder.build(), 0, expectedStackValue, hash, expectedObjectValue);

if (expectedStackValue != null) {
blockBuilder = type.createBlockBuilder(null, 1);
writeBlockMethod.invoke(expectedStackValue, blockBuilder);
assertPositionValue(blockBuilder.build(), 0, expectedStackValue, hash, expectedObjectValue);
}
}

private void assertPositionValue(Block block, int position, Object expectedStackValue, long expectedHash, Object expectedObjectValue)
throws Throwable
{
assertEquals(block.isNull(position), expectedStackValue == null);

Expand Down Expand Up @@ -330,45 +349,56 @@ private void assertPositionValue(Block block, int position, Object expectedStack
assertThatThrownBy(() -> type.getLong(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getDouble(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getObject(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertEquals((boolean) readBlockMethod.invokeExact(block, position), expectedStackValue);
}
else if (type.getJavaType() == long.class) {
assertEquals(type.getLong(block, position), expectedStackValue);
assertThatThrownBy(() -> type.getBoolean(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getDouble(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getObject(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertEquals((long) readBlockMethod.invokeExact(block, position), expectedStackValue);
}
else if (type.getJavaType() == double.class) {
assertEquals(type.getDouble(block, position), expectedStackValue);
assertThatThrownBy(() -> type.getBoolean(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getLong(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getObject(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertEquals((double) readBlockMethod.invokeExact(block, position), expectedStackValue);
}
else if (type.getJavaType() == Slice.class) {
assertEquals(type.getSlice(block, position), expectedStackValue);
assertEquals(type.getObject(block, position), expectedStackValue);
assertThatThrownBy(() -> type.getBoolean(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getLong(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getDouble(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertEquals((Slice) readBlockMethod.invokeExact(block, position), expectedStackValue);
}
else if (type.getJavaType() == Block.class) {
SliceOutput actualSliceOutput = new DynamicSliceOutput(100);
writeBlock(blockEncodingSerde, actualSliceOutput, (Block) type.getObject(block, position));
SliceOutput expectedSliceOutput = new DynamicSliceOutput(actualSliceOutput.size());
writeBlock(blockEncodingSerde, expectedSliceOutput, (Block) expectedStackValue);
assertEquals(actualSliceOutput.slice(), expectedSliceOutput.slice());
assertBlockEquals((Block) type.getObject(block, position), (Block) expectedStackValue);
assertThatThrownBy(() -> type.getBoolean(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getLong(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getDouble(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getSlice(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertBlockEquals((Block) readBlockMethod.invokeExact(block, position), (Block) expectedStackValue);
}
else {
assertEquals(type.getObject(block, position), expectedStackValue);
assertThatThrownBy(() -> type.getBoolean(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getLong(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertThatThrownBy(() -> type.getDouble(block, position)).isInstanceOf(UnsupportedOperationException.class);
assertEquals(readBlockMethod.invoke(block, position), expectedStackValue);
}
}

private void assertBlockEquals(Block actualValue, Block expectedValue)
{
SliceOutput actualSliceOutput = new DynamicSliceOutput(100);
writeBlock(blockEncodingSerde, actualSliceOutput, actualValue);
SliceOutput expectedSliceOutput = new DynamicSliceOutput(actualSliceOutput.size());
writeBlock(blockEncodingSerde, expectedSliceOutput, expectedValue);
assertEquals(actualSliceOutput.slice(), expectedSliceOutput.slice());
}

private void verifyInvalidPositionHandling(Block block)
{
assertThatThrownBy(() -> type.getObjectValue(SESSION, block, -1))
Expand Down Expand Up @@ -427,6 +457,14 @@ private void verifyInvalidPositionHandling(Block block)
.hasMessage("Invalid position %d in block with %d positions", block.getPositionCount(), block.getPositionCount());
}

assertThatThrownBy(() -> readBlockMethod.invoke(block, -1))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid position -1 in block with %d positions", block.getPositionCount());

assertThatThrownBy(() -> readBlockMethod.invoke(block, block.getPositionCount()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid position %d in block with %d positions", block.getPositionCount(), block.getPositionCount());

if (type.getJavaType() == boolean.class) {
assertThatThrownBy(() -> type.getBoolean(block, -1))
.isInstanceOf(IllegalArgumentException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public enum OperatorType
SATURATED_FLOOR_CAST("SATURATED FLOOR CAST", 1),
IS_DISTINCT_FROM("IS DISTINCT FROM", 2),
XX_HASH_64("XX HASH 64", 1),
INDETERMINATE("INDETERMINATE", 1);
INDETERMINATE("INDETERMINATE", 1),
READ_VALUE("READ VALUE", 1),
/**/;

private final String operator;
private final int argumentCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public final class TypeOperatorDeclaration
{
public static final TypeOperatorDeclaration NO_TYPE_OPERATOR_DECLARATION = builder(boolean.class).build();

private final Collection<OperatorMethodHandle> readValueOperators;
private final Collection<OperatorMethodHandle> equalOperators;
private final Collection<OperatorMethodHandle> hashCodeOperators;
private final Collection<OperatorMethodHandle> xxHash64Operators;
Expand All @@ -65,6 +66,7 @@ public final class TypeOperatorDeclaration
private final Collection<OperatorMethodHandle> lessThanOrEqualOperators;

private TypeOperatorDeclaration(
Collection<OperatorMethodHandle> readValueOperators,
Collection<OperatorMethodHandle> equalOperators,
Collection<OperatorMethodHandle> hashCodeOperators,
Collection<OperatorMethodHandle> xxHash64Operators,
Expand All @@ -75,6 +77,7 @@ private TypeOperatorDeclaration(
Collection<OperatorMethodHandle> lessThanOperators,
Collection<OperatorMethodHandle> lessThanOrEqualOperators)
{
this.readValueOperators = List.copyOf(requireNonNull(readValueOperators, "readValueOperators is null"));
this.equalOperators = List.copyOf(requireNonNull(equalOperators, "equalOperators is null"));
this.hashCodeOperators = List.copyOf(requireNonNull(hashCodeOperators, "hashCodeOperators is null"));
this.xxHash64Operators = List.copyOf(requireNonNull(xxHash64Operators, "xxHash64Operators is null"));
Expand All @@ -96,6 +99,11 @@ public boolean isOrderable()
return !comparisonUnorderedLastOperators.isEmpty();
}

public Collection<OperatorMethodHandle> getReadValueOperators()
{
return readValueOperators;
}

public Collection<OperatorMethodHandle> getEqualOperators()
{
return equalOperators;
Expand Down Expand Up @@ -157,6 +165,7 @@ public static class Builder
{
private final Class<?> typeJavaType;

private final Collection<OperatorMethodHandle> readValueOperators = new ArrayList<>();
private final Collection<OperatorMethodHandle> equalOperators = new ArrayList<>();
private final Collection<OperatorMethodHandle> hashCodeOperators = new ArrayList<>();
private final Collection<OperatorMethodHandle> xxHash64Operators = new ArrayList<>();
Expand All @@ -175,6 +184,7 @@ private Builder(Class<?> typeJavaType)

public Builder addOperators(TypeOperatorDeclaration operatorDeclaration)
{
operatorDeclaration.getReadValueOperators().forEach(this::addReadValueOperator);
operatorDeclaration.getEqualOperators().forEach(this::addEqualOperator);
operatorDeclaration.getHashCodeOperators().forEach(this::addHashCodeOperator);
operatorDeclaration.getXxHash64Operators().forEach(this::addXxHash64Operator);
Expand All @@ -187,6 +197,13 @@ public Builder addOperators(TypeOperatorDeclaration operatorDeclaration)
return this;
}

public Builder addReadValueOperator(OperatorMethodHandle readValueOperator)
{
verifyMethodHandleSignature(1, typeJavaType, readValueOperator);
this.readValueOperators.add(readValueOperator);
return this;
}

public Builder addEqualOperator(OperatorMethodHandle equalOperator)
{
verifyMethodHandleSignature(2, boolean.class, equalOperator);
Expand Down Expand Up @@ -350,6 +367,9 @@ public Builder addOperators(Class<?> operatorsClass, Lookup lookup)
}

switch (operatorType) {
case READ_VALUE:
addReadValueOperator(new OperatorMethodHandle(parseInvocationConvention(operatorType, typeJavaType, method, typeJavaType), methodHandle));
break;
case EQUAL:
addEqualOperator(new OperatorMethodHandle(parseInvocationConvention(operatorType, typeJavaType, method, boolean.class), methodHandle));
break;
Expand Down Expand Up @@ -585,6 +605,7 @@ public TypeOperatorDeclaration build()
}

return new TypeOperatorDeclaration(
readValueOperators,
equalOperators,
hashCodeOperators,
xxHash64Operators,
Expand Down
Loading