Search in sources :

Example 1 with Field

use of io.trino.spi.type.RowType.Field in project trino by trinodb.

the class HiveUpdateProcessor method getUpdateRowIdColumnHandle.

/**
 * Return the column UPDATE column handle, which depends on the 3 ACID columns as well as the non-updated columns.
 */
public static HiveColumnHandle getUpdateRowIdColumnHandle(List<HiveColumnHandle> nonUpdatedColumnHandles) {
    List<Field> allAcidFields = new ArrayList<>(ACID_READ_FIELDS);
    if (!nonUpdatedColumnHandles.isEmpty()) {
        RowType userColumnRowType = from(nonUpdatedColumnHandles.stream().map(column -> field(column.getName(), column.getType())).collect(toImmutableList()));
        allAcidFields.add(field(ACID_COLUMN_ROW_STRUCT, userColumnRowType));
    }
    RowType acidRowType = from(allAcidFields);
    return createBaseColumn(UPDATE_ROW_ID_COLUMN_NAME, UPDATE_ROW_ID_COLUMN_INDEX, toHiveType(acidRowType), acidRowType, SYNTHESIZED, Optional.empty());
}
Also used : Field(io.trino.spi.type.RowType.Field) ArrayList(java.util.ArrayList) RowType(io.trino.spi.type.RowType)

Example 2 with Field

use of io.trino.spi.type.RowType.Field in project trino by trinodb.

the class AcidSchema method createRowType.

public static Type createRowType(List<String> names, List<Type> types) {
    requireNonNull(names, "names is null");
    requireNonNull(types, "types is null");
    checkArgument(names.size() == types.size(), "names size %s differs from types size %s", names.size(), types.size());
    ImmutableList.Builder<Field> builder = ImmutableList.builder();
    for (int i = 0; i < names.size(); i++) {
        builder.add(new Field(Optional.of(names.get(i)), types.get(i)));
    }
    return RowType.from(builder.build());
}
Also used : Field(io.trino.spi.type.RowType.Field) ImmutableList(com.google.common.collect.ImmutableList)

Example 3 with Field

use of io.trino.spi.type.RowType.Field in project trino by trinodb.

the class TestPushDownDereferencesRules method testPushdownDereferencesThroughUnnest.

@Test
public void testPushdownDereferencesThroughUnnest() {
    ArrayType arrayType = new ArrayType(BIGINT);
    tester().assertThat(new PushDownDereferenceThroughUnnest(tester().getTypeAnalyzer())).on(p -> p.project(Assignments.of(p.symbol("x"), expression("msg[1]")), p.unnest(ImmutableList.of(p.symbol("msg", ROW_TYPE)), ImmutableList.of(new UnnestNode.Mapping(p.symbol("arr", arrayType), ImmutableList.of(p.symbol("field")))), Optional.empty(), INNER, Optional.empty(), p.values(p.symbol("msg", ROW_TYPE), p.symbol("arr", arrayType))))).matches(strictProject(ImmutableMap.of("x", PlanMatchPattern.expression("msg_x")), unnest(strictProject(ImmutableMap.of("msg_x", PlanMatchPattern.expression("msg[1]"), "msg", PlanMatchPattern.expression("msg"), "arr", PlanMatchPattern.expression("arr")), values("msg", "arr")))));
    // Test with dereferences on unnested column
    RowType rowType = rowType(field("f1", BIGINT), field("f2", BIGINT));
    ArrayType nestedColumnType = new ArrayType(rowType(field("f1", BIGINT), field("f2", rowType)));
    tester().assertThat(new PushDownDereferenceThroughUnnest(tester().getTypeAnalyzer())).on(p -> p.project(Assignments.of(p.symbol("deref_replicate", BIGINT), expression("replicate[2]"), p.symbol("deref_unnest", BIGINT), expression("unnested_row[2]")), p.unnest(ImmutableList.of(p.symbol("replicate", rowType)), ImmutableList.of(new UnnestNode.Mapping(p.symbol("nested", nestedColumnType), ImmutableList.of(p.symbol("unnested_bigint", BIGINT), p.symbol("unnested_row", rowType)))), p.values(p.symbol("replicate", rowType), p.symbol("nested", nestedColumnType))))).matches(strictProject(ImmutableMap.of("deref_replicate", PlanMatchPattern.expression("symbol"), "deref_unnest", // not pushed down
    PlanMatchPattern.expression("unnested_row[2]")), unnest(ImmutableList.of("replicate", "symbol"), ImmutableList.of(unnestMapping("nested", ImmutableList.of("unnested_bigint", "unnested_row"))), strictProject(ImmutableMap.of("symbol", PlanMatchPattern.expression("replicate[2]"), "replicate", PlanMatchPattern.expression("replicate"), "nested", PlanMatchPattern.expression("nested")), values("replicate", "nested")))));
}
Also used : ArrayType(io.trino.spi.type.ArrayType) ROW_NUMBER(io.trino.sql.planner.plan.TopNRankingNode.RankingType.ROW_NUMBER) CATALOG_ID(io.trino.sql.planner.iterative.rule.test.RuleTester.CATALOG_ID) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) Collections.singletonList(java.util.Collections.singletonList) PlanMatchPattern.assignUniqueId(io.trino.sql.planner.assertions.PlanMatchPattern.assignUniqueId) PlanMatchPattern.limit(io.trino.sql.planner.assertions.PlanMatchPattern.limit) CatalogName(io.trino.connector.CatalogName) PlanMatchPattern.markDistinct(io.trino.sql.planner.assertions.PlanMatchPattern.markDistinct) TpchTableHandle(io.trino.plugin.tpch.TpchTableHandle) TEST_SESSION(io.trino.SessionTestUtils.TEST_SESSION) ASCENDING(io.trino.sql.tree.SortItem.Ordering.ASCENDING) ExpressionMatcher(io.trino.sql.planner.assertions.ExpressionMatcher) TpchColumnHandle(io.trino.plugin.tpch.TpchColumnHandle) RowType(io.trino.spi.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) PlanMatchPattern.topNRanking(io.trino.sql.planner.assertions.PlanMatchPattern.topNRanking) Assignments(io.trino.sql.planner.plan.Assignments) ArrayType(io.trino.spi.type.ArrayType) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) SortItem(io.trino.sql.tree.SortItem) ASC_NULLS_FIRST(io.trino.spi.connector.SortOrder.ASC_NULLS_FIRST) FIRST(io.trino.sql.tree.SortItem.NullOrdering.FIRST) PlanMatchPattern.strictProject(io.trino.sql.planner.assertions.PlanMatchPattern.strictProject) BIGINT(io.trino.spi.type.BigintType.BIGINT) MetadataManager.createTestMetadataManager(io.trino.metadata.MetadataManager.createTestMetadataManager) WindowFrame(io.trino.sql.tree.WindowFrame) Optional(java.util.Optional) RowType.field(io.trino.spi.type.RowType.field) WindowNode(io.trino.sql.planner.plan.WindowNode) PlanMatchPattern.rowNumber(io.trino.sql.planner.assertions.PlanMatchPattern.rowNumber) PlanBuilder.expression(io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression) PlanMatchPattern.semiJoin(io.trino.sql.planner.assertions.PlanMatchPattern.semiJoin) INNER(io.trino.sql.planner.plan.JoinNode.Type.INNER) PlanMatchPattern.window(io.trino.sql.planner.assertions.PlanMatchPattern.window) Type(io.trino.spi.type.Type) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) PlanMatchPattern.filter(io.trino.sql.planner.assertions.PlanMatchPattern.filter) ImmutableList(com.google.common.collect.ImmutableList) RowType.rowType(io.trino.spi.type.RowType.rowType) PlanMatchPattern.sort(io.trino.sql.planner.assertions.PlanMatchPattern.sort) PlanMatchPattern.join(io.trino.sql.planner.assertions.PlanMatchPattern.join) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) PlanMatchPattern.unnest(io.trino.sql.planner.assertions.PlanMatchPattern.unnest) TestingTransactionHandle(io.trino.testing.TestingTransactionHandle) PlanMatchPattern.topN(io.trino.sql.planner.assertions.PlanMatchPattern.topN) TupleDomain(io.trino.spi.predicate.TupleDomain) OrderingScheme(io.trino.sql.planner.OrderingScheme) UnnestNode(io.trino.sql.planner.plan.UnnestNode) SortOrder(io.trino.spi.connector.SortOrder) PlanMatchPattern.functionCall(io.trino.sql.planner.assertions.PlanMatchPattern.functionCall) QualifiedName(io.trino.sql.tree.QualifiedName) TableHandle(io.trino.metadata.TableHandle) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) FrameBound(io.trino.sql.tree.FrameBound) UnnestMapping.unnestMapping(io.trino.sql.planner.assertions.PlanMatchPattern.UnnestMapping.unnestMapping) PlanMatchPattern.tableScan(io.trino.sql.planner.assertions.PlanMatchPattern.tableScan) UnnestNode(io.trino.sql.planner.plan.UnnestNode) RowType(io.trino.spi.type.RowType) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Example 4 with Field

use of io.trino.spi.type.RowType.Field in project trino by trinodb.

the class TypeCoercion method isTypeOnlyCoercion.

public boolean isTypeOnlyCoercion(Type source, Type result) {
    if (source.equals(result)) {
        return true;
    }
    if (!canCoerce(source, result)) {
        return false;
    }
    if (source instanceof VarcharType && result instanceof VarcharType) {
        return true;
    }
    if (source instanceof DecimalType && result instanceof DecimalType) {
        DecimalType sourceDecimal = (DecimalType) source;
        DecimalType resultDecimal = (DecimalType) result;
        boolean sameDecimalSubtype = (sourceDecimal.isShort() && resultDecimal.isShort()) || (!sourceDecimal.isShort() && !resultDecimal.isShort());
        boolean sameScale = sourceDecimal.getScale() == resultDecimal.getScale();
        boolean sourcePrecisionIsLessOrEqualToResultPrecision = sourceDecimal.getPrecision() <= resultDecimal.getPrecision();
        return sameDecimalSubtype && sameScale && sourcePrecisionIsLessOrEqualToResultPrecision;
    }
    if (source instanceof RowType && result instanceof RowType) {
        RowType sourceType = (RowType) source;
        RowType resultType = (RowType) result;
        List<Field> sourceFields = sourceType.getFields();
        List<Field> resultFields = resultType.getFields();
        if (sourceFields.size() != resultFields.size()) {
            return false;
        }
        for (int i = 0; i < sourceFields.size(); i++) {
            if (!isTypeOnlyCoercion(sourceFields.get(i).getType(), resultFields.get(i).getType())) {
                return false;
            }
        }
        return true;
    }
    String sourceTypeBase = source.getBaseName();
    String resultTypeBase = result.getBaseName();
    if (sourceTypeBase.equals(resultTypeBase) && isCovariantParametrizedType(source)) {
        List<Type> sourceTypeParameters = source.getTypeParameters();
        List<Type> resultTypeParameters = result.getTypeParameters();
        checkState(sourceTypeParameters.size() == resultTypeParameters.size());
        for (int i = 0; i < sourceTypeParameters.size(); i++) {
            if (!isTypeOnlyCoercion(sourceTypeParameters.get(i), resultTypeParameters.get(i))) {
                return false;
            }
        }
        return true;
    }
    return false;
}
Also used : Field(io.trino.spi.type.RowType.Field) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) TimeWithTimeZoneType.createTimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType.createTimeWithTimeZoneType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType) TimestampType(io.trino.spi.type.TimestampType) CharType.createCharType(io.trino.spi.type.CharType.createCharType) VarcharType(io.trino.spi.type.VarcharType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) SetDigestType(io.trino.type.setdigest.SetDigestType) RowType(io.trino.spi.type.RowType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) TimestampWithTimeZoneType.createTimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType) CharType(io.trino.spi.type.CharType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) DecimalType(io.trino.spi.type.DecimalType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) DecimalType(io.trino.spi.type.DecimalType) RowType(io.trino.spi.type.RowType)

Example 5 with Field

use of io.trino.spi.type.RowType.Field in project trino by trinodb.

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.trino.spi.type.RowType.Field) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) TimeWithTimeZoneType.createTimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType.createTimeWithTimeZoneType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType) TimestampType(io.trino.spi.type.TimestampType) CharType.createCharType(io.trino.spi.type.CharType.createCharType) VarcharType(io.trino.spi.type.VarcharType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) SetDigestType(io.trino.type.setdigest.SetDigestType) RowType(io.trino.spi.type.RowType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) TimestampWithTimeZoneType.createTimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType) CharType(io.trino.spi.type.CharType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) DecimalType(io.trino.spi.type.DecimalType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

RowType (io.trino.spi.type.RowType)7 Field (io.trino.spi.type.RowType.Field)6 ImmutableList (com.google.common.collect.ImmutableList)5 ArrayType (io.trino.spi.type.ArrayType)5 Type (io.trino.spi.type.Type)5 TupleDomain (io.trino.spi.predicate.TupleDomain)3 BIGINT (io.trino.spi.type.BigintType.BIGINT)3 BOOLEAN (io.trino.spi.type.BooleanType.BOOLEAN)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 TEST_SESSION (io.trino.SessionTestUtils.TEST_SESSION)2 CatalogName (io.trino.connector.CatalogName)2 MetadataManager.createTestMetadataManager (io.trino.metadata.MetadataManager.createTestMetadataManager)2 TableHandle (io.trino.metadata.TableHandle)2 CharType (io.trino.spi.type.CharType)2 CharType.createCharType (io.trino.spi.type.CharType.createCharType)2 DecimalType (io.trino.spi.type.DecimalType)2 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)2 MapType (io.trino.spi.type.MapType)2 TimeType (io.trino.spi.type.TimeType)2 TimeType.createTimeType (io.trino.spi.type.TimeType.createTimeType)2