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());
}
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());
}
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")))));
}
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;
}
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);
}
Aggregations