Skip to content
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 @@ -2798,15 +2798,17 @@ protected Scope visitMerge(Merge merge, Optional<Scope> scope)
{
Relation relation = merge.getTarget();
Table table = getMergeTargetTable(relation);
QualifiedObjectName tableName = createQualifiedObjectName(session, table, table.getName());
if (metadata.isMaterializedView(session, tableName)) {
QualifiedObjectName originalTableName = createQualifiedObjectName(session, table, table.getName());
if (metadata.isMaterializedView(session, originalTableName)) {
throw semanticException(NOT_SUPPORTED, merge, "Merging into materialized views is not supported");
}
if (metadata.getView(session, tableName).isPresent()) {
if (metadata.isView(session, originalTableName)) {
throw semanticException(NOT_SUPPORTED, merge, "Merging into views is not supported");
}

TableHandle targetTableHandle = metadata.getTableHandle(session, tableName)
RedirectionAwareTableHandle redirection = metadata.getRedirectionAwareTableHandle(session, originalTableName);
QualifiedObjectName tableName = redirection.getRedirectedTableName().orElse(originalTableName);
TableHandle targetTableHandle = redirection.getTableHandle()
.orElseThrow(() -> semanticException(TABLE_NOT_FOUND, table, "Table '%s' does not exist", tableName));

StatementAnalyzer analyzer = statementAnalyzerFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,33 @@ public void testUpdate()
onTrino().executeQuery("DROP TABLE " + icebergTableName);
}

@Test(groups = {HIVE_ICEBERG_REDIRECTIONS, PROFILE_SPECIFIC_TESTS})
public void testMerge()
{
String sourceTableName = "iceberg_merge_source_" + randomTableSuffix();
String targetTableName = "iceberg_merge_target_" + randomTableSuffix();
String hiveSourceTableName = "hive.default." + sourceTableName;
String hiveTargetTableName = "hive.default." + targetTableName;
String icebergSourceTableName = "iceberg.default." + sourceTableName;
String icebergTargetTableName = "iceberg.default." + targetTableName;

createIcebergTable(icebergSourceTableName, true, true);
createIcebergTable(icebergTargetTableName, true, false);

assertThat(onTrino().executeQuery("" +
"MERGE INTO " + hiveTargetTableName + " t USING " + hiveSourceTableName + " s ON t.nationkey = s.nationkey " +
"WHEN NOT MATCHED " +
" THEN INSERT (nationkey, name, regionkey, comment) " +
" VALUES (s.nationkey, s.name, s.regionkey, s.comment)"))
.updatedRowsCountIsEqualTo(25);
assertResultsEqual(
onTrino().executeQuery("TABLE " + icebergSourceTableName),
onTrino().executeQuery("TABLE " + icebergTargetTableName));

onTrino().executeQuery("DROP TABLE " + icebergSourceTableName);
onTrino().executeQuery("DROP TABLE " + icebergTargetTableName);
}

@Test(groups = {HIVE_ICEBERG_REDIRECTIONS, PROFILE_SPECIFIC_TESTS})
public void testDropTable()
{
Expand Down