use of io.trino.spi.type.ArrayType in project trino by trinodb.
the class TestSignatureBinder method testVariadic.
@Test
public void testVariadic() {
Signature rowVariadicBoundFunction = functionSignature().returnType(BIGINT.getTypeSignature()).argumentTypes(new TypeSignature("T")).typeVariableConstraints(ImmutableList.of(withVariadicBound("T", "row"))).build();
assertThat(rowVariadicBoundFunction).boundTo(RowType.anonymous(ImmutableList.of(BIGINT, BIGINT))).produces(new BoundVariables().setTypeVariable("T", RowType.anonymous(ImmutableList.of(BIGINT, BIGINT))));
assertThat(rowVariadicBoundFunction).boundTo(new ArrayType(BIGINT)).fails();
assertThat(rowVariadicBoundFunction).boundTo(new ArrayType(BIGINT)).withCoercion().fails();
assertThatThrownBy(() -> withVariadicBound("T", "array")).isInstanceOf(IllegalArgumentException.class).hasMessage("variadicBound must be row but is array");
assertThatThrownBy(() -> withVariadicBound("T", "map")).isInstanceOf(IllegalArgumentException.class).hasMessage("variadicBound must be row but is map");
assertThatThrownBy(() -> withVariadicBound("T", "decimal")).isInstanceOf(IllegalArgumentException.class).hasMessage("variadicBound must be row but is decimal");
}
use of io.trino.spi.type.ArrayType in project trino by trinodb.
the class TestSignatureBinder method testNonParametric.
@Test
public void testNonParametric() {
Signature function = functionSignature().returnType(BOOLEAN.getTypeSignature()).argumentTypes(BIGINT.getTypeSignature()).build();
assertThat(function).boundTo(BIGINT).succeeds();
assertThat(function).boundTo(VARCHAR).withCoercion().fails();
assertThat(function).boundTo(VARCHAR, BIGINT).withCoercion().fails();
assertThat(function).boundTo(new ArrayType(BIGINT)).withCoercion().fails();
}
use of io.trino.spi.type.ArrayType in project trino by trinodb.
the class TestSignatureBinder method testArray.
@Test
public void testArray() {
Signature getFunction = functionSignature().returnType(new TypeSignature("T")).argumentTypes(arrayType(new TypeSignature("T"))).typeVariableConstraints(ImmutableList.of(typeVariable("T"))).build();
assertThat(getFunction).boundTo(new ArrayType(BIGINT)).produces(new BoundVariables().setTypeVariable("T", BIGINT));
assertThat(getFunction).boundTo(BIGINT).withCoercion().fails();
assertThat(getFunction).boundTo(RowType.anonymous(ImmutableList.of(BIGINT))).withCoercion().fails();
Signature containsFunction = functionSignature().returnType(new TypeSignature("T")).argumentTypes(arrayType(new TypeSignature("T")), new TypeSignature("T")).typeVariableConstraints(ImmutableList.of(comparableTypeParameter("T"))).build();
assertThat(containsFunction).boundTo(new ArrayType(BIGINT), BIGINT).produces(new BoundVariables().setTypeVariable("T", BIGINT));
assertThat(containsFunction).boundTo(new ArrayType(BIGINT), VARCHAR).withCoercion().fails();
assertThat(containsFunction).boundTo(new ArrayType(HYPER_LOG_LOG), HYPER_LOG_LOG).withCoercion().fails();
Signature castFunction = functionSignature().returnType(arrayType(new TypeSignature("T2"))).argumentTypes(arrayType(new TypeSignature("T1")), arrayType(new TypeSignature("T2"))).typeVariableConstraints(ImmutableList.of(typeVariable("T1"), typeVariable("T2"))).build();
assertThat(castFunction).boundTo(new ArrayType(UNKNOWN), new ArrayType(createDecimalType(2, 1))).withCoercion().produces(new BoundVariables().setTypeVariable("T1", UNKNOWN).setTypeVariable("T2", createDecimalType(2, 1)));
Signature fooFunction = functionSignature().returnType(new TypeSignature("T")).argumentTypes(arrayType(new TypeSignature("T")), arrayType(new TypeSignature("T"))).typeVariableConstraints(ImmutableList.of(typeVariable("T"))).build();
assertThat(fooFunction).boundTo(new ArrayType(BIGINT), new ArrayType(BIGINT)).produces(new BoundVariables().setTypeVariable("T", BIGINT));
assertThat(fooFunction).boundTo(new ArrayType(BIGINT), new ArrayType(VARCHAR)).withCoercion().fails();
}
use of io.trino.spi.type.ArrayType in project trino by trinodb.
the class TestQuantileDigestAggregationFunction method assertPercentilesWithinError.
private void assertPercentilesWithinError(String type, SqlVarbinary binary, double error, List<? extends Number> rows, double[] percentiles) {
List<Double> boxedPercentiles = Arrays.stream(percentiles).sorted().boxed().collect(toImmutableList());
List<Number> lowerBounds = boxedPercentiles.stream().map(percentile -> getLowerBound(error, rows, percentile)).collect(toImmutableList());
List<Number> upperBounds = boxedPercentiles.stream().map(percentile -> getUpperBound(error, rows, percentile)).collect(toImmutableList());
// Ensure that the lower bound of each item in the distribution is not greater than the chosen quantiles
functionAssertions.assertFunction(format("zip_with(values_at_quantiles(CAST(X'%s' AS qdigest(%s)), ARRAY[%s]), ARRAY[%s], (value, lowerbound) -> value >= lowerbound)", binary.toString().replaceAll("\\s+", " "), type, ARRAY_JOINER.join(boxedPercentiles), ARRAY_JOINER.join(lowerBounds)), new ArrayType(BOOLEAN), Collections.nCopies(percentiles.length, true));
// Ensure that the upper bound of each item in the distribution is not less than the chosen quantiles
functionAssertions.assertFunction(format("zip_with(values_at_quantiles(CAST(X'%s' AS qdigest(%s)), ARRAY[%s]), ARRAY[%s], (value, upperbound) -> value <= upperbound)", binary.toString().replaceAll("\\s+", " "), type, ARRAY_JOINER.join(boxedPercentiles), ARRAY_JOINER.join(upperBounds)), new ArrayType(BOOLEAN), Collections.nCopies(percentiles.length, true));
}
use of io.trino.spi.type.ArrayType in project trino by trinodb.
the class TestStateCompiler method testComplexSerialization.
@Test
public void testComplexSerialization() {
Type arrayType = new ArrayType(BIGINT);
Type mapType = mapType(BIGINT, VARCHAR);
Map<String, Type> fieldMap = ImmutableMap.of("Block", arrayType, "AnotherBlock", mapType);
AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap);
AccumulatorStateSerializer<TestComplexState> serializer = StateCompiler.generateStateSerializer(TestComplexState.class, fieldMap);
TestComplexState singleState = factory.createSingleState();
TestComplexState deserializedState = factory.createSingleState();
singleState.setBoolean(true);
singleState.setLong(1);
singleState.setDouble(2.0);
singleState.setByte((byte) 3);
singleState.setInt(4);
singleState.setSlice(utf8Slice("test"));
singleState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
singleState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
singleState.setBlock(array);
singleState.setAnotherBlock(mapBlockOf(BIGINT, VARCHAR, ImmutableMap.of(123L, "testBlock")));
BlockBuilder builder = RowType.anonymous(ImmutableList.of(mapType, VARBINARY, arrayType, BOOLEAN, TINYINT, DOUBLE, INTEGER, BIGINT, VARBINARY, VARBINARY)).createBlockBuilder(null, 1);
serializer.serialize(singleState, builder);
Block block = builder.build();
serializer.deserialize(block, 0, deserializedState);
assertEquals(deserializedState.getBoolean(), singleState.getBoolean());
assertEquals(deserializedState.getLong(), singleState.getLong());
assertEquals(deserializedState.getDouble(), singleState.getDouble());
assertEquals(deserializedState.getByte(), singleState.getByte());
assertEquals(deserializedState.getInt(), singleState.getInt());
assertEquals(deserializedState.getSlice(), singleState.getSlice());
assertEquals(deserializedState.getAnotherSlice(), singleState.getAnotherSlice());
assertEquals(deserializedState.getYetAnotherSlice(), singleState.getYetAnotherSlice());
assertEquals(deserializedState.getBlock().getLong(0, 0), singleState.getBlock().getLong(0, 0));
assertEquals(deserializedState.getAnotherBlock().getLong(0, 0), singleState.getAnotherBlock().getLong(0, 0));
assertEquals(deserializedState.getAnotherBlock().getSlice(1, 0, 9), singleState.getAnotherBlock().getSlice(1, 0, 9));
}
Aggregations