Skip to content
Closed
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 @@ -73,6 +73,7 @@
import io.trino.sql.planner.plan.ValuesNode;
import io.trino.sql.planner.plan.WindowNode;
import io.trino.sql.tree.Cast;
import io.trino.sql.tree.CoalesceExpression;
import io.trino.sql.tree.ComparisonExpression;
import io.trino.sql.tree.DecimalLiteral;
import io.trino.sql.tree.Delete;
Expand Down Expand Up @@ -144,6 +145,7 @@
import static com.google.common.collect.Iterables.getOnlyElement;
import static io.trino.SystemSessionProperties.getMaxRecursionDepth;
import static io.trino.SystemSessionProperties.isSkipRedundantSort;
import static io.trino.spi.StandardErrorCode.INVALID_ARGUMENTS;
import static io.trino.spi.StandardErrorCode.INVALID_WINDOW_FRAME;
import static io.trino.spi.StandardErrorCode.MERGE_TARGET_ROW_MULTIPLE_MATCHES;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
Expand Down Expand Up @@ -543,8 +545,9 @@ 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);
Metadata metadata = plannerContext.getMetadata();
TableSchema tableSchema = metadata.getTableSchema(session, handle);
Map<String, ColumnHandle> columnMap = metadata.getColumnHandles(session, handle);
List<ColumnSchema> columnsSchemas = tableSchema.getColumns();

List<String> targetColumnNames = node.getAssignments().stream()
Expand Down Expand Up @@ -584,10 +587,32 @@ public UpdateNode plan(Update node)
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)));
TableMetadata tableMetadata = metadata.getTableMetadata(session, handle);
ImmutableMap<String, Integer> updatedColumnNameToIndex = IntStream.range(0, updatedColumnNames.size())
.boxed()
.collect(toImmutableMap(updatedColumnNames::get, i -> i));
ImmutableList.Builder<Symbol> updatedNonNullableColumnsValuesBuilder = ImmutableList.builder();
Assignments.Builder assignmentsBuilder = Assignments.builder();
assignmentsBuilder.putIdentities(builder.getRoot().getOutputSymbols());

for (ColumnMetadata column : tableMetadata.getColumns()) {
if (targetColumnNames.contains(column.getName())) {
Symbol output = symbolAllocator.newSymbol(column.getName(), column.getType());
updatedNonNullableColumnsValuesBuilder.add(output);
@SuppressWarnings("ConstantConditions") int columnIndex = updatedColumnNameToIndex.get(column.getName());
Expression expression = ((ProjectNode) builder.getRoot()).getAssignments().get(planAndMappings.get(orderedColumnValues.get(columnIndex)));
if (!column.isNullable()) {
expression = new CoalesceExpression(expression, new Cast(failFunction(metadata, session, INVALID_ARGUMENTS, format(
"NULL value not allowed for NOT NULL column: %s",
column.getName())), toSqlType(column.getType())));
}
assignmentsBuilder.put(output, expression);
}
}
ProjectNode projectNode = new ProjectNode(idAllocator.getNextId(), builder.getRoot(), assignmentsBuilder.build());
builder = builder.withNewRoot(projectNode);
Symbol rowId = builder.translate(analysis.getRowIdField(table));
updatedColumnValuesBuilder.add(rowId);
updatedNonNullableColumnsValuesBuilder.add(rowId);

List<Symbol> outputs = ImmutableList.of(
symbolAllocator.newSymbol("partialrows", BIGINT),
Expand All @@ -602,11 +627,11 @@ public UpdateNode plan(Update node)
builder.getRoot(),
new UpdateTarget(
Optional.empty(),
plannerContext.getMetadata().getTableMetadata(session, handle).getTable(),
tableMetadata.getTable(),
updatedColumnNames,
updatedColumnHandles),
rowId,
updatedColumnValuesBuilder.build(),
updatedNonNullableColumnsValuesBuilder.build(),
outputs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2824,6 +2824,27 @@ public void testInsertIntoNotNullColumn()
}
}

@Test
public void testUpdateNotNullColumn()
{
skipTestUnless(hasBehavior(SUPPORTS_CREATE_TABLE));
skipTestUnless(hasBehavior(SUPPORTS_UPDATE));

if (!hasBehavior(SUPPORTS_NOT_NULL_CONSTRAINT)) {
assertQueryFails(
"CREATE TABLE not_null_constraint (not_null_col INTEGER NOT NULL)",
format("line 1:35: Catalog '%s' does not support non-null column for column name 'not_null_col'", getSession().getCatalog().orElseThrow()));
return;
}

try (TestTable table = new TestTable(getQueryRunner()::execute, "update_not_null", "(nullable_col INTEGER, not_null_col INTEGER NOT NULL)")) {
assertUpdate(format("INSERT INTO %s (nullable_col, not_null_col) VALUES (1, 10)", table.getName()), 1);
assertQuery("SELECT * FROM " + table.getName(), "VALUES (1, 10)");
assertQueryFails("UPDATE " + table.getName() + " SET not_null_col = NULL WHERE nullable_col = 1", "NULL value not allowed for NOT NULL column: not_null_col");
assertQueryFails("UPDATE " + table.getName() + " SET not_null_col = TRY(5/0) where nullable_col = 1", "NULL value not allowed for NOT NULL column: not_null_col");
}
}

@Language("RegExp")
protected String errorMessageForInsertIntoNotNullColumn(String columnName)
{
Expand Down