Search in sources :

Example 81 with ArrayType

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");
}
Also used : ArrayType(io.trino.spi.type.ArrayType) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Test(org.testng.annotations.Test)

Example 82 with ArrayType

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();
}
Also used : ArrayType(io.trino.spi.type.ArrayType) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Test(org.testng.annotations.Test)

Example 83 with ArrayType

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();
}
Also used : ArrayType(io.trino.spi.type.ArrayType) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Test(org.testng.annotations.Test)

Example 84 with ArrayType

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));
}
Also used : Arrays(java.util.Arrays) QuantileDigest(io.airlift.stats.QuantileDigest) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) TestingFunctionResolution(io.trino.metadata.TestingFunctionResolution) Page(io.trino.spi.Page) BlockAssertions.createLongSequenceBlock(io.trino.block.BlockAssertions.createLongSequenceBlock) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) Test(org.testng.annotations.Test) BlockAssertions.createDoubleSequenceBlock(io.trino.block.BlockAssertions.createDoubleSequenceBlock) BlockAssertions.createSequenceBlockOfReal(io.trino.block.BlockAssertions.createSequenceBlockOfReal) NaN(java.lang.Double.NaN) Block(io.trino.spi.block.Block) FloatingPointBitsConverterUtil.floatToSortableInt(io.trino.operator.aggregation.FloatingPointBitsConverterUtil.floatToSortableInt) BlockAssertions.createRLEBlock(io.trino.block.BlockAssertions.createRLEBlock) Integer.min(java.lang.Integer.min) QDIGEST_EQUALITY(io.trino.operator.aggregation.TestMergeQuantileDigestFunction.QDIGEST_EQUALITY) Floats(com.google.common.primitives.Floats) LongStream(java.util.stream.LongStream) BlockAssertions.createDoublesBlock(io.trino.block.BlockAssertions.createDoublesBlock) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StandardTypes(io.trino.spi.type.StandardTypes) ArrayType(io.trino.spi.type.ArrayType) FloatingPointBitsConverterUtil.doubleToSortableLong(io.trino.operator.aggregation.FloatingPointBitsConverterUtil.doubleToSortableLong) Collectors(java.util.stream.Collectors) AbstractTestFunctions(io.trino.operator.scalar.AbstractTestFunctions) BlockAssertions.createBlockOfReals(io.trino.block.BlockAssertions.createBlockOfReals) String.format(java.lang.String.format) QualifiedName(io.trino.sql.tree.QualifiedName) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) Integer.max(java.lang.Integer.max) SqlVarbinary(io.trino.spi.type.SqlVarbinary) List(java.util.List) AggregationTestUtils.assertAggregation(io.trino.operator.aggregation.AggregationTestUtils.assertAggregation) BIGINT(io.trino.spi.type.BigintType.BIGINT) BlockAssertions.createLongsBlock(io.trino.block.BlockAssertions.createLongsBlock) TypeSignatureProvider(io.trino.sql.analyzer.TypeSignatureProvider) Collections(java.util.Collections) REAL(io.trino.spi.type.RealType.REAL) Joiner(com.google.common.base.Joiner) ArrayType(io.trino.spi.type.ArrayType)

Example 85 with ArrayType

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));
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) StructuralTestUtil.mapType(io.trino.util.StructuralTestUtil.mapType) Block(io.trino.spi.block.Block) BlockAssertions.createLongsBlock(io.trino.block.BlockAssertions.createLongsBlock) BlockBuilder(io.trino.spi.block.BlockBuilder) Test(org.testng.annotations.Test)

Aggregations

ArrayType (io.trino.spi.type.ArrayType)289 Test (org.testng.annotations.Test)205 Type (io.trino.spi.type.Type)92 RowType (io.trino.spi.type.RowType)86 ImmutableList (com.google.common.collect.ImmutableList)66 List (java.util.List)62 ArrayList (java.util.ArrayList)59 MapType (io.trino.spi.type.MapType)43 Arrays.asList (java.util.Arrays.asList)36 Collections.singletonList (java.util.Collections.singletonList)34 VarcharType (io.trino.spi.type.VarcharType)32 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)32 BlockBuilder (io.trino.spi.block.BlockBuilder)31 MessageType (org.apache.parquet.schema.MessageType)31 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)30 MessageTypeParser.parseMessageType (org.apache.parquet.schema.MessageTypeParser.parseMessageType)27 DecimalType (io.trino.spi.type.DecimalType)26 StructuralTestUtil.mapType (io.trino.testing.StructuralTestUtil.mapType)24 Block (io.trino.spi.block.Block)23 Map (java.util.Map)23