Search in sources :

Example 6 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class TypeCoercion method typeCompatibilityForRow.

private TypeCompatibility typeCompatibilityForRow(RowType firstType, RowType secondType) {
    List<Field> firstFields = firstType.getFields();
    List<Field> secondFields = secondType.getFields();
    if (firstFields.size() != secondFields.size()) {
        return TypeCompatibility.incompatible();
    }
    ImmutableList.Builder<Field> fields = ImmutableList.builder();
    boolean coercible = true;
    for (int i = 0; i < firstFields.size(); i++) {
        Type firstFieldType = firstFields.get(i).getType();
        Type secondFieldType = secondFields.get(i).getType();
        TypeCompatibility typeCompatibility = compatibility(firstFieldType, secondFieldType);
        if (!typeCompatibility.isCompatible()) {
            return TypeCompatibility.incompatible();
        }
        Type commonParameterType = typeCompatibility.getCommonSuperType();
        Optional<String> firstParameterName = firstFields.get(i).getName();
        Optional<String> secondParameterName = secondFields.get(i).getName();
        Optional<String> commonName = firstParameterName.equals(secondParameterName) ? firstParameterName : Optional.empty();
        // ignore parameter name for coercible
        coercible &= typeCompatibility.isCoercible();
        fields.add(new Field(commonName, commonParameterType));
    }
    return TypeCompatibility.compatible(RowType.from(fields.build()), coercible);
}
Also used : Field(io.prestosql.spi.type.RowType.Field) UnknownType(io.prestosql.spi.type.UnknownType) CharType(io.prestosql.spi.type.CharType) DecimalType(io.prestosql.spi.type.DecimalType) MapType(io.prestosql.spi.type.MapType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) LikePatternType(io.prestosql.spi.type.LikePatternType) ArrayType(io.prestosql.spi.type.ArrayType) CharType.createCharType(io.prestosql.spi.type.CharType.createCharType) SetDigestType(io.prestosql.type.setdigest.SetDigestType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.prestosql.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList)

Example 7 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class OperatorAssertion method toRow.

public static Block toRow(List<Type> parameterTypes, Object... values) {
    checkArgument(parameterTypes.size() == values.length, "parameterTypes.size(" + parameterTypes.size() + ") does not equal to values.length(" + values.length + ")");
    RowType rowType = RowType.anonymous(parameterTypes);
    BlockBuilder blockBuilder = new RowBlockBuilder(parameterTypes, 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 : RowBlockBuilder(io.prestosql.spi.block.RowBlockBuilder) RowType(io.prestosql.spi.type.RowType) BlockBuilder(io.prestosql.spi.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(io.prestosql.util.StructuralTestUtil.appendToBlockBuilder) RowBlockBuilder(io.prestosql.spi.block.RowBlockBuilder)

Example 8 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class TestTypeUtil method testParametricType.

@Test
public void testParametricType() {
    Type type = parseType(typeManager, "decimal(10,2)");
    assertTrue(type instanceof DecimalType);
    type = parseType(typeManager, "char(100)");
    assertTrue(type instanceof CharType);
    type = parseType(typeManager, "varchar(100)");
    assertTrue(type instanceof VarcharType);
    type = parseType(typeManager, "array(varchar(10))");
    assertTrue(type instanceof ArrayType);
    type = parseType(typeManager, "row(street varchar(10))");
    assertTrue(type instanceof RowType);
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) BigintType(io.prestosql.spi.type.BigintType) UnknownType(io.prestosql.spi.type.UnknownType) CharType(io.prestosql.spi.type.CharType) DecimalType(io.prestosql.spi.type.DecimalType) HyperLogLogType(io.prestosql.spi.type.HyperLogLogType) DoubleType(io.prestosql.spi.type.DoubleType) P4HyperLogLogType(io.prestosql.spi.type.P4HyperLogLogType) RowType(io.prestosql.spi.type.RowType) TimestampWithTimeZoneType(io.prestosql.spi.type.TimestampWithTimeZoneType) Type(io.prestosql.spi.type.Type) SmallintType(io.prestosql.spi.type.SmallintType) TimeWithTimeZoneType(io.prestosql.spi.type.TimeWithTimeZoneType) IntegerType(io.prestosql.spi.type.IntegerType) TimeType(io.prestosql.spi.type.TimeType) RealType(io.prestosql.spi.type.RealType) ArrayType(io.prestosql.spi.type.ArrayType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) TimestampType(io.prestosql.spi.type.TimestampType) DateType(io.prestosql.spi.type.DateType) BooleanType(io.prestosql.spi.type.BooleanType) TypeUtil.parseType(io.prestosql.client.util.TypeUtil.parseType) VarcharType(io.prestosql.spi.type.VarcharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) RowType(io.prestosql.spi.type.RowType) CharType(io.prestosql.spi.type.CharType) Test(org.testng.annotations.Test)

Example 9 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class RelationPlanner method planCrossJoinUnnest.

private RelationPlan planCrossJoinUnnest(RelationPlan leftPlan, Join joinNode, Unnest node) {
    RelationType unnestOutputDescriptor = analysis.getOutputDescriptor(node);
    // Create symbols for the result of unnesting
    ImmutableList.Builder<Symbol> unnestedSymbolsBuilder = ImmutableList.builder();
    for (Field field : unnestOutputDescriptor.getVisibleFields()) {
        Symbol symbol = planSymbolAllocator.newSymbol(field);
        unnestedSymbolsBuilder.add(symbol);
    }
    ImmutableList<Symbol> unnestedSymbols = unnestedSymbolsBuilder.build();
    // Add a projection for all the unnest arguments
    PlanBuilder planBuilder = initializePlanBuilder(leftPlan);
    planBuilder = planBuilder.appendProjections(node.getExpressions(), planSymbolAllocator, idAllocator);
    TranslationMap translations = planBuilder.getTranslations();
    ProjectNode projectNode = (ProjectNode) planBuilder.getRoot();
    ImmutableMap.Builder<Symbol, List<Symbol>> unnestSymbols = ImmutableMap.builder();
    UnmodifiableIterator<Symbol> unnestedSymbolsIterator = unnestedSymbols.iterator();
    for (Expression expression : node.getExpressions()) {
        Type type = analysis.getType(expression);
        Symbol inputSymbol = translations.get(expression);
        if (type instanceof ArrayType) {
            Type elementType = ((ArrayType) type).getElementType();
            if (elementType instanceof RowType) {
                ImmutableList.Builder<Symbol> unnestSymbolBuilder = ImmutableList.builder();
                for (int i = 0; i < ((RowType) elementType).getFields().size(); i++) {
                    unnestSymbolBuilder.add(unnestedSymbolsIterator.next());
                }
                unnestSymbols.put(inputSymbol, unnestSymbolBuilder.build());
            } else {
                unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next()));
            }
        } else if (type instanceof MapType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next(), unnestedSymbolsIterator.next()));
        } else {
            throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
        }
    }
    Optional<Symbol> ordinalitySymbol = node.isWithOrdinality() ? Optional.of(unnestedSymbolsIterator.next()) : Optional.empty();
    checkState(!unnestedSymbolsIterator.hasNext(), "Not all output symbols were matched with input symbols");
    UnnestNode unnestNode = new UnnestNode(idAllocator.getNextId(), projectNode, leftPlan.getFieldMappings(), unnestSymbols.build(), ordinalitySymbol);
    return new RelationPlan(unnestNode, analysis.getScope(joinNode), unnestNode.getOutputSymbols());
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Symbol(io.prestosql.spi.plan.Symbol) RowType(io.prestosql.spi.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) MapType(io.prestosql.spi.type.MapType) ArrayType(io.prestosql.spi.type.ArrayType) Field(io.prestosql.sql.analyzer.Field) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) RelationType(io.prestosql.sql.analyzer.RelationType) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) UnnestNode(io.prestosql.sql.planner.plan.UnnestNode) RelationType(io.prestosql.sql.analyzer.RelationType) ProjectNode(io.prestosql.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)

Example 10 with RowType

use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.

the class TestSerDeUtils method testMapBlock.

@Test
public void testMapBlock() {
    MapHolder holder = new MapHolder();
    holder.map = new TreeMap<>();
    holder.map.put("twelve", new InnerStruct(13, 14L));
    holder.map.put("fifteen", new InnerStruct(16, 17L));
    RowType rowType = RowType.anonymous(ImmutableList.of(INTEGER, BIGINT));
    RowType rowOfMapOfVarcharRowType = RowType.anonymous(ImmutableList.of(HiveTestUtils.mapType(VARCHAR, rowType)));
    Block actual = toBinaryBlock(rowOfMapOfVarcharRowType, holder, getInspector(MapHolder.class));
    Block mapBlock = mapBlockOf(VARCHAR, rowType, new Object[] { utf8Slice("fifteen"), utf8Slice("twelve") }, new Object[] { rowBlockOf(rowType.getTypeParameters(), 16, 17L), rowBlockOf(rowType.getTypeParameters(), 13, 14L) });
    Block expected = StructuralTestUtil.rowBlockOf(ImmutableList.of(HiveTestUtils.mapType(VARCHAR, rowType)), mapBlock);
    assertBlockEquals(actual, expected);
}
Also used : RowType(io.prestosql.spi.type.RowType) Block(io.prestosql.spi.block.Block) Test(org.testng.annotations.Test)

Aggregations

RowType (io.prestosql.spi.type.RowType)76 ArrayType (io.prestosql.spi.type.ArrayType)38 Type (io.prestosql.spi.type.Type)35 MapType (io.prestosql.spi.type.MapType)29 Test (org.testng.annotations.Test)25 List (java.util.List)19 ImmutableList (com.google.common.collect.ImmutableList)18 VarcharType (io.prestosql.spi.type.VarcharType)17 Block (io.prestosql.spi.block.Block)16 BlockBuilder (io.prestosql.spi.block.BlockBuilder)15 Map (java.util.Map)12 BigintType (io.prestosql.spi.type.BigintType)11 DecimalType (io.prestosql.spi.type.DecimalType)11 DoubleType (io.prestosql.spi.type.DoubleType)11 BooleanType (io.prestosql.spi.type.BooleanType)10 IntegerType (io.prestosql.spi.type.IntegerType)10 RealType (io.prestosql.spi.type.RealType)10 SmallintType (io.prestosql.spi.type.SmallintType)10 TimestampType (io.prestosql.spi.type.TimestampType)10 VarbinaryType (io.prestosql.spi.type.VarbinaryType)10