Search in sources :

Example 11 with RowType

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

the class StructuralTestUtil method rowBlockOf.

public static Block rowBlockOf(List<Type> parameterTypes, Object... values) {
    RowType rowType = RowType.anonymous(parameterTypes);
    BlockBuilder blockBuilder = rowType.createBlockBuilder(null, 1);
    BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry();
    for (int i = 0; i < values.length; i++) {
        appendToBlockBuilder(parameterTypes.get(i), values[i], singleRowBlockWriter);
    }
    blockBuilder.closeEntry();
    return rowType.getObject(blockBuilder, 0);
}
Also used : RowType(com.facebook.presto.common.type.RowType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)

Example 12 with RowType

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

the class AbstractTestHiveClient method assertValueTypes.

private static void assertValueTypes(MaterializedRow row, List<ColumnMetadata> schema) {
    for (int columnIndex = 0; columnIndex < schema.size(); columnIndex++) {
        ColumnMetadata column = schema.get(columnIndex);
        Object value = row.getField(columnIndex);
        if (value != null) {
            if (BOOLEAN.equals(column.getType())) {
                assertInstanceOf(value, Boolean.class);
            } else if (TINYINT.equals(column.getType())) {
                assertInstanceOf(value, Byte.class);
            } else if (SMALLINT.equals(column.getType())) {
                assertInstanceOf(value, Short.class);
            } else if (INTEGER.equals(column.getType())) {
                assertInstanceOf(value, Integer.class);
            } else if (BIGINT.equals(column.getType())) {
                assertInstanceOf(value, Long.class);
            } else if (DOUBLE.equals(column.getType())) {
                assertInstanceOf(value, Double.class);
            } else if (REAL.equals(column.getType())) {
                assertInstanceOf(value, Float.class);
            } else if (isVarcharType(column.getType())) {
                assertInstanceOf(value, String.class);
            } else if (isCharType(column.getType())) {
                assertInstanceOf(value, String.class);
            } else if (VARBINARY.equals(column.getType())) {
                assertInstanceOf(value, SqlVarbinary.class);
            } else if (TIMESTAMP.equals(column.getType())) {
                assertInstanceOf(value, SqlTimestamp.class);
            } else if (DATE.equals(column.getType())) {
                assertInstanceOf(value, SqlDate.class);
            } else if (column.getType() instanceof ArrayType || column.getType() instanceof RowType) {
                assertInstanceOf(value, List.class);
            } else if (column.getType() instanceof MapType) {
                assertInstanceOf(value, Map.class);
            } else {
                fail("Unknown primitive type " + columnIndex);
            }
        }
    }
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) RowType(com.facebook.presto.common.type.RowType) OptionalDouble(java.util.OptionalDouble) Constraint(com.facebook.presto.spi.Constraint) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) SqlDate(com.facebook.presto.common.type.SqlDate) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 13 with RowType

use of com.facebook.presto.common.type.RowType 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 14 with RowType

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

the class TestTypeConversions method testConvertTwoLevelsRecordField.

@Test
public void testConvertTwoLevelsRecordField() {
    Field field = Field.of("rec", RECORD, Field.of("sub_rec", RECORD, Field.of("sub_sub_s", STRING), Field.of("sub_sub_i", INTEGER)), Field.of("sub_s", STRING), Field.of("sub_i", INTEGER));
    ColumnMetadata metadata = Conversions.toColumnMetadata(field);
    RowType targetType = RowType.from(ImmutableList.of(RowType.field("sub_rec", RowType.from(ImmutableList.of(RowType.field("sub_sub_s", VarcharType.VARCHAR), RowType.field("sub_sub_i", BigintType.BIGINT)))), RowType.field("sub_s", VarcharType.VARCHAR), RowType.field("sub_i", BigintType.BIGINT)));
    assertThat(metadata.getType()).isEqualTo(targetType);
}
Also used : Field(com.google.cloud.bigquery.Field) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) RowType(com.facebook.presto.common.type.RowType) Test(org.testng.annotations.Test)

Example 15 with RowType

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

the class RelationPlanner method planCrossJoinUnnest.

private RelationPlan planCrossJoinUnnest(RelationPlan leftPlan, Join joinNode, Unnest node) {
    RelationType unnestOutputDescriptor = analysis.getOutputDescriptor(node);
    // Create variables for the result of unnesting
    ImmutableList.Builder<VariableReferenceExpression> unnestedVariablesBuilder = ImmutableList.builder();
    for (Field field : unnestOutputDescriptor.getVisibleFields()) {
        VariableReferenceExpression variable = variableAllocator.newVariable(field);
        unnestedVariablesBuilder.add(variable);
    }
    ImmutableList<VariableReferenceExpression> unnestedVariables = unnestedVariablesBuilder.build();
    // Add a projection for all the unnest arguments
    PlanBuilder planBuilder = initializePlanBuilder(leftPlan);
    planBuilder = planBuilder.appendProjections(node.getExpressions(), variableAllocator, idAllocator);
    TranslationMap translations = planBuilder.getTranslations();
    ProjectNode projectNode = (ProjectNode) planBuilder.getRoot();
    ImmutableMap.Builder<VariableReferenceExpression, List<VariableReferenceExpression>> unnestVariables = ImmutableMap.builder();
    UnmodifiableIterator<VariableReferenceExpression> unnestedVariablesIterator = unnestedVariables.iterator();
    for (Expression expression : node.getExpressions()) {
        Type type = analysis.getType(expression);
        VariableReferenceExpression inputVariable = new VariableReferenceExpression(getSourceLocation(expression), translations.get(expression).getName(), type);
        if (type instanceof ArrayType) {
            Type elementType = ((ArrayType) type).getElementType();
            if (!SystemSessionProperties.isLegacyUnnest(session) && elementType instanceof RowType) {
                ImmutableList.Builder<VariableReferenceExpression> unnestVariableBuilder = ImmutableList.builder();
                for (int i = 0; i < ((RowType) elementType).getFields().size(); i++) {
                    unnestVariableBuilder.add(unnestedVariablesIterator.next());
                }
                unnestVariables.put(inputVariable, unnestVariableBuilder.build());
            } else {
                unnestVariables.put(inputVariable, ImmutableList.of(unnestedVariablesIterator.next()));
            }
        } else if (type instanceof MapType) {
            unnestVariables.put(inputVariable, ImmutableList.of(unnestedVariablesIterator.next(), unnestedVariablesIterator.next()));
        } else {
            throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
        }
    }
    Optional<VariableReferenceExpression> ordinalityVariable = node.isWithOrdinality() ? Optional.of(unnestedVariablesIterator.next()) : Optional.empty();
    checkState(!unnestedVariablesIterator.hasNext(), "Not all output variables were matched with input variables");
    UnnestNode unnestNode = new UnnestNode(getSourceLocation(node), idAllocator.getNextId(), projectNode, leftPlan.getFieldMappings(), unnestVariables.build(), ordinalityVariable);
    return new RelationPlan(unnestNode, analysis.getScope(joinNode), unnestNode.getOutputVariables());
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowType(com.facebook.presto.common.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Field(com.facebook.presto.sql.analyzer.Field) TypeUtils.isEnumType(com.facebook.presto.common.type.TypeUtils.isEnumType) ArrayType(com.facebook.presto.common.type.ArrayType) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) ExpressionTreeUtils.isEqualComparisonExpression(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.isEqualComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) UnnestNode(com.facebook.presto.sql.planner.plan.UnnestNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RelationType(com.facebook.presto.sql.analyzer.RelationType) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

RowType (com.facebook.presto.common.type.RowType)61 ArrayType (com.facebook.presto.common.type.ArrayType)37 Type (com.facebook.presto.common.type.Type)32 MapType (com.facebook.presto.common.type.MapType)28 ImmutableList (com.google.common.collect.ImmutableList)19 ArrayList (java.util.ArrayList)18 DecimalType (com.facebook.presto.common.type.DecimalType)16 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)15 Test (org.testng.annotations.Test)15 List (java.util.List)14 VarcharType (com.facebook.presto.common.type.VarcharType)12 Block (com.facebook.presto.common.block.Block)11 CharType (com.facebook.presto.common.type.CharType)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 PrestoException (com.facebook.presto.spi.PrestoException)8 Map (java.util.Map)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)6 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)6 TimestampType (com.facebook.presto.common.type.TimestampType)5