Search in sources :

Example 46 with ArrayType

use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.

the class TestRowExpressionFormatter method testConstants.

@Test
public void testConstants() {
    // null
    RowExpression constantExpression = constantNull(UNKNOWN);
    assertEquals(format(constantExpression), "null");
    // boolean
    constantExpression = constant(true, BOOLEAN);
    assertEquals(format(constantExpression), "BOOLEAN'true'");
    // double
    constantExpression = constant(1.1, DOUBLE);
    assertEquals(format(constantExpression), "DOUBLE'1.1'");
    constantExpression = constant(Double.NaN, DOUBLE);
    assertEquals(format(constantExpression), "DOUBLE'NaN'");
    constantExpression = constant(Double.POSITIVE_INFINITY, DOUBLE);
    assertEquals(format(constantExpression), "DOUBLE'Infinity'");
    // real
    constantExpression = constant((long) floatToIntBits(1.1f), REAL);
    assertEquals(format(constantExpression), "REAL'1.1'");
    constantExpression = constant((long) floatToIntBits(Float.NaN), REAL);
    assertEquals(format(constantExpression), "REAL'NaN'");
    constantExpression = constant((long) floatToIntBits(Float.POSITIVE_INFINITY), REAL);
    assertEquals(format(constantExpression), "REAL'Infinity'");
    // string
    constantExpression = constant(utf8Slice("abcde"), VARCHAR);
    assertEquals(format(constantExpression), "VARCHAR'abcde'");
    constantExpression = constant(utf8Slice("fgh"), createCharType(3));
    assertEquals(format(constantExpression), "CHAR'fgh'");
    // integer
    constantExpression = constant(1L, TINYINT);
    assertEquals(format(constantExpression), "TINYINT'1'");
    constantExpression = constant(1L, SMALLINT);
    assertEquals(format(constantExpression), "SMALLINT'1'");
    constantExpression = constant(1L, INTEGER);
    assertEquals(format(constantExpression), "INTEGER'1'");
    constantExpression = constant(1L, BIGINT);
    assertEquals(format(constantExpression), "BIGINT'1'");
    // varbinary
    Slice value = Slices.wrappedBuffer(BaseEncoding.base16().decode("123456AB"));
    constantExpression = constant(value, VARBINARY);
    assertEquals(format(constantExpression), "X'12 34 56 ab'");
    // color
    constantExpression = constant(256L, COLOR);
    assertEquals(format(constantExpression), "COLOR'256'");
    // long and short decimals
    constantExpression = constant(decimal("1.2345678910"), DecimalType.createDecimalType(11, 10));
    assertEquals(format(constantExpression), "DECIMAL'1.2345678910'");
    constantExpression = constant(decimal("1.281734081274028174012432412423134"), DecimalType.createDecimalType(34, 33));
    assertEquals(format(constantExpression), "DECIMAL'1.281734081274028174012432412423134'");
    // time
    constantExpression = constant(662727600000L, TIMESTAMP);
    assertEquals(format(constantExpression), "TIMESTAMP'1991-01-01 00:00:00.000'");
    constantExpression = constant(7670L, DATE);
    assertEquals(format(constantExpression), "DATE'1991-01-01'");
    // interval
    constantExpression = constant(24L, INTERVAL_DAY_TIME);
    assertEquals(format(constantExpression), "INTERVAL DAY TO SECOND'0 00:00:00.024'");
    constantExpression = constant(25L, INTERVAL_YEAR_MONTH);
    assertEquals(format(constantExpression), "INTERVAL YEAR TO MONTH'2-1'");
    // block
    constantExpression = constant(new LongArrayBlockBuilder(null, 4).writeLong(1L).writeLong(2).build(), new ArrayType(BIGINT));
    assertEquals(format(constantExpression), "[Block: position count: 2; size: 96 bytes]");
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) RowExpression(com.facebook.presto.spi.relation.RowExpression) LongArrayBlockBuilder(com.facebook.presto.common.block.LongArrayBlockBuilder) Test(org.testng.annotations.Test)

Example 47 with ArrayType

use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.

the class TestRowExpressionFormatter method testComplex.

@Test
public void testComplex() {
    RowExpression complexExpression;
    RowExpression expression = createCallExpression(ADD);
    complexExpression = call(SUBTRACT.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(SUBTRACT, fromTypes(BIGINT, BIGINT)), BIGINT, C_BIGINT, expression);
    assertEquals(format(complexExpression), "(c_bigint) - ((c_bigint) + (BIGINT'5'))");
    RowExpression expression1 = createCallExpression(ADD);
    RowExpression expression2 = call(MULTIPLY.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(MULTIPLY, fromTypes(BIGINT, BIGINT)), BIGINT, expression1, C_BIGINT);
    RowExpression expression3 = createCallExpression(GREATER_THAN);
    complexExpression = new SpecialFormExpression(OR, BOOLEAN, expression2, expression3);
    assertEquals(format(complexExpression), "(((c_bigint) + (BIGINT'5')) * (c_bigint)) OR ((c_bigint) > (BIGINT'5'))");
    ArrayType arrayType = (ArrayType) C_BIGINT_ARRAY.getType();
    Type elementType = arrayType.getElementType();
    expression1 = call(SUBSCRIPT.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(SUBSCRIPT, fromTypes(arrayType, elementType)), elementType, ImmutableList.of(C_BIGINT_ARRAY, constant(5L, INTEGER)));
    expression2 = call(NEGATION.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(NEGATION, fromTypes(expression1.getType())), expression1.getType(), expression1);
    expression3 = call(ADD.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(ADD, fromTypes(expression2.getType(), BIGINT)), BIGINT, expression2, constant(5L, BIGINT));
    assertEquals(format(expression3), "(-(c_bigint_array[INTEGER'5'])) + (BIGINT'5')");
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) DecimalType(com.facebook.presto.common.type.DecimalType) CastType(com.facebook.presto.metadata.CastType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) RowExpression(com.facebook.presto.spi.relation.RowExpression) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) Test(org.testng.annotations.Test)

Example 48 with ArrayType

use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.

the class TestRowExpressionFormatter method testCalls.

@Test
public void testCalls() {
    RowExpression callExpression;
    // arithmetic
    callExpression = createCallExpression(ADD);
    assertEquals(format(callExpression), "(c_bigint) + (BIGINT'5')");
    callExpression = createCallExpression(SUBTRACT);
    assertEquals(format(callExpression), "(c_bigint) - (BIGINT'5')");
    callExpression = createCallExpression(MULTIPLY);
    assertEquals(format(callExpression), "(c_bigint) * (BIGINT'5')");
    callExpression = createCallExpression(DIVIDE);
    assertEquals(format(callExpression), "(c_bigint) / (BIGINT'5')");
    callExpression = createCallExpression(MODULUS);
    assertEquals(format(callExpression), "(c_bigint) % (BIGINT'5')");
    // comparison
    callExpression = createCallExpression(GREATER_THAN);
    assertEquals(format(callExpression), "(c_bigint) > (BIGINT'5')");
    callExpression = createCallExpression(LESS_THAN);
    assertEquals(format(callExpression), "(c_bigint) < (BIGINT'5')");
    callExpression = createCallExpression(GREATER_THAN_OR_EQUAL);
    assertEquals(format(callExpression), "(c_bigint) >= (BIGINT'5')");
    callExpression = createCallExpression(LESS_THAN_OR_EQUAL);
    assertEquals(format(callExpression), "(c_bigint) <= (BIGINT'5')");
    callExpression = createCallExpression(EQUAL);
    assertEquals(format(callExpression), "(c_bigint) = (BIGINT'5')");
    callExpression = createCallExpression(NOT_EQUAL);
    assertEquals(format(callExpression), "(c_bigint) <> (BIGINT'5')");
    callExpression = createCallExpression(IS_DISTINCT_FROM);
    assertEquals(format(callExpression), "(c_bigint) IS DISTINCT FROM (BIGINT'5')");
    // negation
    RowExpression expression = createCallExpression(ADD);
    callExpression = call(NEGATION.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(NEGATION, fromTypes(expression.getType())), expression.getType(), expression);
    assertEquals(format(callExpression), "-((c_bigint) + (BIGINT'5'))");
    // subscript
    ArrayType arrayType = (ArrayType) C_BIGINT_ARRAY.getType();
    Type elementType = arrayType.getElementType();
    RowExpression subscriptExpression = call(SUBSCRIPT.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(SUBSCRIPT, fromTypes(arrayType, elementType)), elementType, ImmutableList.of(C_BIGINT_ARRAY, constant(0L, INTEGER)));
    callExpression = subscriptExpression;
    assertEquals(format(callExpression), "c_bigint_array[INTEGER'0']");
    // cast
    callExpression = call(CAST.name(), FUNCTION_AND_TYPE_MANAGER.lookupCast(CastType.CAST, TINYINT.getTypeSignature(), BIGINT.getTypeSignature()), BIGINT, constant(1L, TINYINT));
    assertEquals(format(callExpression), "CAST(TINYINT'1' AS bigint)");
    // between
    callExpression = call(BETWEEN.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(BETWEEN, fromTypes(BIGINT, BIGINT, BIGINT)), BOOLEAN, subscriptExpression, constant(1L, BIGINT), constant(5L, BIGINT));
    assertEquals(format(callExpression), "c_bigint_array[INTEGER'0'] BETWEEN (BIGINT'1') AND (BIGINT'5')");
    // other
    callExpression = call(HASH_CODE.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(HASH_CODE, fromTypes(BIGINT)), BIGINT, constant(1L, BIGINT));
    assertEquals(format(callExpression), "HASH_CODE(BIGINT'1')");
    // like
    callExpression = call("LIKE", FUNCTION_RESOLUTION.likeVarcharFunction(), BOOLEAN, C_VARCHAR, call(CAST.name(), FUNCTION_AND_TYPE_MANAGER.lookupCast(CastType.CAST, VARCHAR.getTypeSignature(), LIKE_PATTERN.getTypeSignature()), LIKE_PATTERN, constant(utf8Slice("prefix%"), VARCHAR)));
    assertEquals(format(callExpression), "c_varchar LIKE VARCHAR'prefix%'");
    callExpression = OPTIMIZER.optimize(callExpression, OPTIMIZED, SESSION);
    assertTrue(format(callExpression).startsWith("c_varchar LIKE LIKEPATTERN'io.airlift.joni.Regex@"));
    // like escape
    callExpression = call("LIKE", FUNCTION_RESOLUTION.likeVarcharFunction(), BOOLEAN, C_VARCHAR, call("LIKE_PATTERN", FUNCTION_RESOLUTION.likePatternFunction(), LIKE_PATTERN, constant(utf8Slice("%escaped$_"), VARCHAR), constant(utf8Slice("$"), VARCHAR)));
    assertEquals(format(callExpression), "c_varchar LIKE VARCHAR'%escaped$_' ESCAPE VARCHAR'$'");
    callExpression = OPTIMIZER.optimize(callExpression, OPTIMIZED, SESSION);
    assertTrue(format(callExpression).startsWith("c_varchar LIKE LIKEPATTERN'io.airlift.joni.Regex@"));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) DecimalType(com.facebook.presto.common.type.DecimalType) CastType(com.facebook.presto.metadata.CastType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) RowExpression(com.facebook.presto.spi.relation.RowExpression) Test(org.testng.annotations.Test)

Example 49 with ArrayType

use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.

the class BigQueryResultPageSource method writeBlock.

private void writeBlock(BlockBuilder output, Type type, Object value) {
    if (type instanceof ArrayType && value instanceof List<?>) {
        BlockBuilder builder = output.beginBlockEntry();
        for (Object element : (List<?>) value) {
            appendTo(type.getTypeParameters().get(0), element, builder);
        }
        output.closeEntry();
        return;
    }
    if (type instanceof RowType && value instanceof GenericRecord) {
        GenericRecord record = (GenericRecord) value;
        BlockBuilder builder = output.beginBlockEntry();
        List<String> fieldNames = new ArrayList<>();
        for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
            TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
            fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
        }
        checkState(fieldNames.size() == type.getTypeParameters().size(), "fieldName doesn't match with type size : %s", type);
        for (int index = 0; index < type.getTypeParameters().size(); index++) {
            appendTo(type.getTypeParameters().get(index), record.get(fieldNames.get(index)), builder);
        }
        output.closeEntry();
        return;
    }
    throw new BigQueryException(BIGQUERY_UNSUPPORTED_TYPE_FOR_BLOCK, "Unhandled type for Block: " + type.getTypeSignature());
}
Also used : ArrayList(java.util.ArrayList) RowType(com.facebook.presto.common.type.RowType) ArrayType(com.facebook.presto.common.type.ArrayType) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) GenericRecord(org.apache.avro.generic.GenericRecord) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 50 with ArrayType

use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.

the class TestTypeConversions method testConvertStringArrayColumn.

@Test
public void testConvertStringArrayColumn() {
    BigQueryColumnHandle column = new BigQueryColumnHandle("test", BigQueryType.STRING, REPEATED, ImmutableList.of(), null);
    ColumnMetadata metadata = column.getColumnMetadata();
    assertThat(metadata.getType()).isEqualTo(new ArrayType(VarcharType.VARCHAR));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) Test(org.testng.annotations.Test)

Aggregations

ArrayType (com.facebook.presto.common.type.ArrayType)287 Test (org.testng.annotations.Test)219 Type (com.facebook.presto.common.type.Type)99 RowType (com.facebook.presto.common.type.RowType)79 ArrayList (java.util.ArrayList)58 ImmutableList (com.google.common.collect.ImmutableList)54 List (java.util.List)51 MapType (com.facebook.presto.common.type.MapType)39 DecimalType.createDecimalType (com.facebook.presto.common.type.DecimalType.createDecimalType)36 Arrays.asList (java.util.Arrays.asList)34 Collections.singletonList (java.util.Collections.singletonList)33 MessageType (org.apache.parquet.schema.MessageType)30 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)28 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)27 MessageTypeParser.parseMessageType (org.apache.parquet.schema.MessageTypeParser.parseMessageType)27 InternalAggregationFunction (com.facebook.presto.operator.aggregation.InternalAggregationFunction)26 PrimitiveType (org.apache.parquet.schema.PrimitiveType)26 StructuralTestUtil.mapType (com.facebook.presto.tests.StructuralTestUtil.mapType)24 Block (com.facebook.presto.common.block.Block)23 DecimalType (com.facebook.presto.common.type.DecimalType)17