use of io.trino.spi.connector.ColumnHandle in project trino by trinodb.
the class QueryPlanner method plan.
public UpdateNode plan(Update node) {
Table table = node.getTable();
TableHandle handle = analysis.getTableHandle(table);
TableSchema tableSchema = plannerContext.getMetadata().getTableSchema(session, handle);
Map<String, ColumnHandle> columnMap = plannerContext.getMetadata().getColumnHandles(session, handle);
List<ColumnSchema> columnsSchemas = tableSchema.getColumns();
List<String> targetColumnNames = node.getAssignments().stream().map(assignment -> assignment.getName().getValue()).collect(toImmutableList());
// Create lists of columnnames and SET expressions, in table column order
ImmutableList.Builder<String> updatedColumnNamesBuilder = ImmutableList.builder();
ImmutableList.Builder<ColumnHandle> updatedColumnHandlesBuilder = ImmutableList.builder();
ImmutableList.Builder<Expression> orderedColumnValuesBuilder = ImmutableList.builder();
for (ColumnSchema columnSchema : columnsSchemas) {
String name = columnSchema.getName();
int index = targetColumnNames.indexOf(name);
if (index >= 0) {
updatedColumnNamesBuilder.add(name);
updatedColumnHandlesBuilder.add(requireNonNull(columnMap.get(name), "columnMap didn't contain name"));
orderedColumnValuesBuilder.add(node.getAssignments().get(index).getValue());
}
}
List<String> updatedColumnNames = updatedColumnNamesBuilder.build();
List<ColumnHandle> updatedColumnHandles = updatedColumnHandlesBuilder.build();
List<Expression> orderedColumnValues = orderedColumnValuesBuilder.build();
// create table scan
RelationPlan relationPlan = new RelationPlanner(analysis, symbolAllocator, idAllocator, lambdaDeclarationToSymbolMap, plannerContext, outerContext, session, recursiveSubqueries).process(table, null);
PlanBuilder builder = newPlanBuilder(relationPlan, analysis, lambdaDeclarationToSymbolMap);
if (node.getWhere().isPresent()) {
builder = filter(builder, node.getWhere().get(), node);
}
builder = subqueryPlanner.handleSubqueries(builder, orderedColumnValues, analysis.getSubqueries(node));
builder = builder.appendProjections(orderedColumnValues, symbolAllocator, idAllocator);
PlanAndMappings planAndMappings = coerce(builder, orderedColumnValues, analysis, idAllocator, symbolAllocator, typeCoercion);
builder = planAndMappings.getSubPlan();
ImmutableList.Builder<Symbol> updatedColumnValuesBuilder = ImmutableList.builder();
orderedColumnValues.forEach(columnValue -> updatedColumnValuesBuilder.add(planAndMappings.get(columnValue)));
Symbol rowId = builder.translate(analysis.getRowIdField(table));
updatedColumnValuesBuilder.add(rowId);
List<Symbol> outputs = ImmutableList.of(symbolAllocator.newSymbol("partialrows", BIGINT), symbolAllocator.newSymbol("fragment", VARBINARY));
Optional<PlanNodeId> tableScanId = getIdForLeftTableScan(relationPlan.getRoot());
checkArgument(tableScanId.isPresent(), "tableScanId not present");
// create update node
return new UpdateNode(idAllocator.getNextId(), builder.getRoot(), new UpdateTarget(Optional.empty(), plannerContext.getMetadata().getTableMetadata(session, handle).getTable(), updatedColumnNames, updatedColumnHandles), rowId, updatedColumnValuesBuilder.build(), outputs);
}
use of io.trino.spi.connector.ColumnHandle in project trino by trinodb.
the class TestHiveProjectionPushdownIntoTableScan method testDereferencePushdown.
@Test
public void testDereferencePushdown() {
String testTable = "test_simple_projection_pushdown";
QualifiedObjectName completeTableName = new QualifiedObjectName(HIVE_CATALOG_NAME, SCHEMA_NAME, testTable);
getQueryRunner().execute(format("CREATE TABLE %s (col0, col1) AS" + " SELECT cast(row(5, 6) as row(x bigint, y bigint)) AS col0, 5 AS col1 WHERE false", testTable));
Session session = getQueryRunner().getDefaultSession();
Optional<TableHandle> tableHandle = getTableHandle(session, completeTableName);
assertTrue(tableHandle.isPresent(), "expected the table handle to be present");
Map<String, ColumnHandle> columns = getColumnHandles(session, completeTableName);
HiveColumnHandle column0Handle = (HiveColumnHandle) columns.get("col0");
HiveColumnHandle column1Handle = (HiveColumnHandle) columns.get("col1");
HiveColumnHandle columnX = createProjectedColumnHandle(column0Handle, ImmutableList.of(0));
HiveColumnHandle columnY = createProjectedColumnHandle(column0Handle, ImmutableList.of(1));
// Simple Projection pushdown
assertPlan("SELECT col0.x expr_x, col0.y expr_y FROM " + testTable, any(tableScan(((HiveTableHandle) tableHandle.get().getConnectorHandle()).withProjectedColumns(ImmutableSet.of(columnX, columnY))::equals, TupleDomain.all(), ImmutableMap.of("col0#x", columnX::equals, "col0#y", columnY::equals))));
// Projection and predicate pushdown
assertPlan(format("SELECT col0.x FROM %s WHERE col0.x = col1 + 3 and col0.y = 2", testTable), anyTree(filter("col0_y = BIGINT '2' AND (col0_x = cast((col1 + 3) as BIGINT))", tableScan(table -> {
HiveTableHandle hiveTableHandle = (HiveTableHandle) table;
return hiveTableHandle.getCompactEffectivePredicate().equals(TupleDomain.withColumnDomains(ImmutableMap.of(columnY, Domain.singleValue(BIGINT, 2L)))) && hiveTableHandle.getProjectedColumns().equals(ImmutableSet.of(column1Handle, columnX, columnY));
}, TupleDomain.all(), ImmutableMap.of("col0_y", columnY::equals, "col0_x", columnX::equals, "col1", column1Handle::equals)))));
// Projection and predicate pushdown with overlapping columns
assertPlan(format("SELECT col0, col0.y expr_y FROM %s WHERE col0.x = 5", testTable), anyTree(filter("col0_x = BIGINT '5'", tableScan(table -> {
HiveTableHandle hiveTableHandle = (HiveTableHandle) table;
return hiveTableHandle.getCompactEffectivePredicate().equals(TupleDomain.withColumnDomains(ImmutableMap.of(columnX, Domain.singleValue(BIGINT, 5L)))) && hiveTableHandle.getProjectedColumns().equals(ImmutableSet.of(column0Handle, columnX));
}, TupleDomain.all(), ImmutableMap.of("col0", column0Handle::equals, "col0_x", columnX::equals)))));
// Projection and predicate pushdown with joins
assertPlan(format("SELECT T.col0.x, T.col0, T.col0.y FROM %s T join %s S on T.col1 = S.col1 WHERE (T.col0.x = 2)", testTable, testTable), anyTree(project(ImmutableMap.of("expr_0_x", expression("expr_0[1]"), "expr_0", expression("expr_0"), "expr_0_y", expression("expr_0[2]")), join(INNER, ImmutableList.of(equiJoinClause("t_expr_1", "s_expr_1")), anyTree(filter("expr_0_x = BIGINT '2'", tableScan(table -> ((HiveTableHandle) table).getCompactEffectivePredicate().getDomains().get().equals(ImmutableMap.of(columnX, Domain.singleValue(BIGINT, 2L))), TupleDomain.all(), ImmutableMap.of("expr_0_x", columnX::equals, "expr_0", column0Handle::equals, "t_expr_1", column1Handle::equals)))), anyTree(tableScan(((HiveTableHandle) tableHandle.get().getConnectorHandle()).withProjectedColumns(ImmutableSet.of(column1Handle))::equals, TupleDomain.all(), ImmutableMap.of("s_expr_1", column1Handle::equals)))))));
}
use of io.trino.spi.connector.ColumnHandle in project trino by trinodb.
the class TestInformationSchemaMetadata method testInformationSchemaPredicatePushdown.
/**
* Tests information schema predicate pushdown when both schema and table name are specified.
*/
@Test
public void testInformationSchemaPredicatePushdown() {
TransactionId transactionId = transactionManager.beginTransaction(false);
ImmutableMap.Builder<ColumnHandle, Domain> domains = ImmutableMap.builder();
domains.put(new InformationSchemaColumnHandle("table_schema"), Domain.singleValue(VARCHAR, Slices.utf8Slice("test_schema")));
domains.put(new InformationSchemaColumnHandle("table_name"), Domain.singleValue(VARCHAR, Slices.utf8Slice("test_view")));
Constraint constraint = new Constraint(TupleDomain.withColumnDomains(domains.buildOrThrow()));
ConnectorSession session = createNewSession(transactionId);
ConnectorMetadata metadata = new InformationSchemaMetadata("test_catalog", this.metadata);
InformationSchemaTableHandle tableHandle = (InformationSchemaTableHandle) metadata.getTableHandle(session, new SchemaTableName("information_schema", "views"));
tableHandle = metadata.applyFilter(session, tableHandle, constraint).map(ConstraintApplicationResult::getHandle).map(InformationSchemaTableHandle.class::cast).orElseThrow(AssertionError::new);
assertEquals(tableHandle.getPrefixes(), ImmutableSet.of(new QualifiedTablePrefix("test_catalog", "test_schema", "test_view")));
}
use of io.trino.spi.connector.ColumnHandle in project trino by trinodb.
the class TestInformationSchemaMetadata method testInformationSchemaPredicatePushdownWithoutTablePredicate.
@Test
public void testInformationSchemaPredicatePushdownWithoutTablePredicate() {
TransactionId transactionId = transactionManager.beginTransaction(false);
// predicate without table name predicates should not cause table level prefixes to be evaluated
ImmutableMap.Builder<ColumnHandle, Domain> domains = ImmutableMap.builder();
domains.put(new InformationSchemaColumnHandle("table_schema"), Domain.singleValue(VARCHAR, Slices.utf8Slice("test_schema")));
Constraint constraint = new Constraint(TupleDomain.withColumnDomains(domains.buildOrThrow()));
ConnectorSession session = createNewSession(transactionId);
ConnectorMetadata metadata = new InformationSchemaMetadata("test_catalog", this.metadata);
InformationSchemaTableHandle tableHandle = (InformationSchemaTableHandle) metadata.getTableHandle(session, new SchemaTableName("information_schema", "views"));
tableHandle = metadata.applyFilter(session, tableHandle, constraint).map(ConstraintApplicationResult::getHandle).map(InformationSchemaTableHandle.class::cast).orElseThrow(AssertionError::new);
assertEquals(tableHandle.getPrefixes(), ImmutableSet.of(new QualifiedTablePrefix("test_catalog", "test_schema")));
}
use of io.trino.spi.connector.ColumnHandle in project trino by trinodb.
the class TestInformationSchemaMetadata method testInformationSchemaPredicatePushdownWithoutSchemaPredicate.
@Test
public void testInformationSchemaPredicatePushdownWithoutSchemaPredicate() {
TransactionId transactionId = transactionManager.beginTransaction(false);
// predicate without schema predicates should cause schemas to be enumerated when table predicates are present
ImmutableMap.Builder<ColumnHandle, Domain> domains = ImmutableMap.builder();
domains.put(new InformationSchemaColumnHandle("table_name"), Domain.singleValue(VARCHAR, Slices.utf8Slice("test_view")));
Constraint constraint = new Constraint(TupleDomain.withColumnDomains(domains.buildOrThrow()));
ConnectorSession session = createNewSession(transactionId);
ConnectorMetadata metadata = new InformationSchemaMetadata("test_catalog", this.metadata);
InformationSchemaTableHandle tableHandle = (InformationSchemaTableHandle) metadata.getTableHandle(session, new SchemaTableName("information_schema", "views"));
tableHandle = metadata.applyFilter(session, tableHandle, constraint).map(ConstraintApplicationResult::getHandle).map(InformationSchemaTableHandle.class::cast).orElseThrow(AssertionError::new);
// filter blindly applies filter to all visible schemas, so information_schema must be included
assertEquals(tableHandle.getPrefixes(), ImmutableSet.of(new QualifiedTablePrefix("test_catalog", "test_schema", "test_view"), new QualifiedTablePrefix("test_catalog", "information_schema", "test_view")));
}
Aggregations