Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix column lineage for UPDATE statements with subqueries #18815

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3315,14 +3315,24 @@ protected Scope visitUpdate(Update update, Optional<Scope> scope)

ImmutableList.Builder<ExpressionAnalysis> analysesBuilder = ImmutableList.builder();
ImmutableList.Builder<Type> expressionTypesBuilder = ImmutableList.builder();
ImmutableMap.Builder<String, Set<SourceColumn>> sourceColumnsByColumnNameBuilder = ImmutableMap.builder();
for (UpdateAssignment assignment : update.getAssignments()) {
String targetColumnName = assignment.getName().getValue();
Expression expression = assignment.getValue();
ExpressionAnalysis analysis = analyzeExpression(expression, tableScope);
analysesBuilder.add(analysis);
expressionTypesBuilder.add(analysis.getType(expression));
ExpressionAnalysis expressionAnalysis = analyzeExpression(expression, tableScope);
analysesBuilder.add(expressionAnalysis);
expressionTypesBuilder.add(expressionAnalysis.getType(expression));

Set<SourceColumn> sourceColumns = expressionAnalysis.getSubqueries().stream()
.map(query -> analyze(query.getNode(), tableScope))
.flatMap(subqueryScope -> subqueryScope.getRelationType().getVisibleFields().stream())
.flatMap(field -> analysis.getSourceColumns(field).stream())
.collect(toImmutableSet());
sourceColumnsByColumnNameBuilder.put(targetColumnName, sourceColumns);
}
List<ExpressionAnalysis> analyses = analysesBuilder.build();
List<Type> expressionTypes = expressionTypesBuilder.build();
Map<String, Set<SourceColumn>> sourceColumnsByColumnName = sourceColumnsByColumnNameBuilder.buildOrThrow();

List<Type> tableTypes = update.getAssignments().stream()
.map(assignment -> requireNonNull(columns.get(assignment.getName().getValue())))
Expand Down Expand Up @@ -3353,7 +3363,9 @@ protected Scope visitUpdate(Update update, Optional<Scope> scope)
tableName,
Optional.of(table),
Optional.of(updatedColumnSchemas.stream()
.map(column -> new OutputColumn(new Column(column.getName(), column.getType().toString()), ImmutableSet.of()))
.map(column -> new OutputColumn(
new Column(column.getName(), column.getType().toString()),
sourceColumnsByColumnName.getOrDefault(column.getName(), ImmutableSet.of())))
.collect(toImmutableList())));

createMergeAnalysis(table, handle, tableSchema, tableScope, tableScope, ImmutableList.of(updatedColumnHandles));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,65 @@ public void testOutputColumnsForUpdatingSingleColumn()
.containsExactly(new OutputColumnMetadata("test_varchar", VARCHAR_TYPE, ImmutableSet.of()));
}

@Test
public void testOutputColumnsForUpdatingColumnWithSelectQuery()
throws Exception
{
QueryEvents queryEvents = runQueryAndWaitForEvents("UPDATE mock.default.table_for_output SET test_varchar = (SELECT name from nation LIMIT 1)").getQueryEvents();
QueryCompletedEvent event = queryEvents.getQueryCompletedEvent();
assertThat(event.getIoMetadata().getOutput().get().getColumns().get())
.containsExactly(new OutputColumnMetadata("test_varchar", VARCHAR_TYPE, ImmutableSet.of(new ColumnDetail("tpch", "tiny", "nation", "name"))));
}

@Test
public void testOutputColumnsForUpdatingColumnWithSelectQueryWithAliasedField()
throws Exception
{
QueryEvents queryEvents = runQueryAndWaitForEvents("UPDATE mock.default.table_for_output SET test_varchar = (SELECT name AS aliased_name from nation LIMIT 1)").getQueryEvents();
QueryCompletedEvent event = queryEvents.getQueryCompletedEvent();
assertThat(event.getIoMetadata().getOutput().get().getColumns().get())
.containsExactly(new OutputColumnMetadata("test_varchar", VARCHAR_TYPE, ImmutableSet.of(new ColumnDetail("tpch", "tiny", "nation", "name"))));
}

@Test
public void testOutputColumnsForUpdatingColumnsWithSelectQueries()
throws Exception
{
QueryEvents queryEvents = runQueryAndWaitForEvents("""
UPDATE mock.default.table_for_output SET test_varchar = (SELECT name AS aliased_name from nation LIMIT 1), test_bigint = (SELECT nationkey FROM nation LIMIT 1)
""").getQueryEvents();
QueryCompletedEvent event = queryEvents.getQueryCompletedEvent();
assertThat(event.getIoMetadata().getOutput().get().getColumns().get())
.containsExactlyInAnyOrder(
new OutputColumnMetadata("test_varchar", VARCHAR_TYPE, ImmutableSet.of(new ColumnDetail("tpch", "tiny", "nation", "name"))),
new OutputColumnMetadata("test_bigint", BIGINT_TYPE, ImmutableSet.of(new ColumnDetail("tpch", "tiny", "nation", "nationkey"))));
}

@Test
public void testOutputColumnsForUpdatingColumnsWithSelectQueryAndRawValue()
throws Exception
{
QueryEvents queryEvents = runQueryAndWaitForEvents("""
UPDATE mock.default.table_for_output SET test_varchar = (SELECT name AS aliased_name from nation LIMIT 1), test_bigint = 1
""").getQueryEvents();
QueryCompletedEvent event = queryEvents.getQueryCompletedEvent();
assertThat(event.getIoMetadata().getOutput().get().getColumns().get())
.containsExactlyInAnyOrder(
new OutputColumnMetadata("test_varchar", VARCHAR_TYPE, ImmutableSet.of(new ColumnDetail("tpch", "tiny", "nation", "name"))),
new OutputColumnMetadata("test_bigint", BIGINT_TYPE, ImmutableSet.of()));
}

@Test
public void testOutputColumnsForUpdatingColumnWithSelectQueryAndWhereClauseWithOuterColumn()
throws Exception
{
QueryEvents queryEvents = runQueryAndWaitForEvents("""
UPDATE mock.default.table_for_output SET test_varchar = (SELECT name from nation WHERE test_bigint = nationkey)""").getQueryEvents();
QueryCompletedEvent event = queryEvents.getQueryCompletedEvent();
assertThat(event.getIoMetadata().getOutput().get().getColumns().get())
.containsExactly(new OutputColumnMetadata("test_varchar", VARCHAR_TYPE, ImmutableSet.of(new ColumnDetail("tpch", "tiny", "nation", "name"))));
}

@Test
public void testCreateTable()
throws Exception
Expand Down