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 @@ -2051,10 +2051,12 @@ public void testMetadataDelete()
assertEquals(e.getMessage(), "This connector only supports delete where one or more partitions are deleted entirely");
}

// Test successful metadata delete on partition columns
assertUpdate("DELETE FROM test_metadata_delete WHERE LINE_STATUS='O'");

assertQuery("SELECT * from test_metadata_delete", "SELECT orderkey, linenumber, linestatus FROM lineitem WHERE linestatus<>'O' and linenumber<>3");

// Test delete on non-partition column - should fail
try {
getQueryRunner().execute("DELETE FROM test_metadata_delete WHERE ORDER_KEY=1");
fail("expected exception");
Expand All @@ -2065,6 +2067,17 @@ public void testMetadataDelete()

assertQuery("SELECT * from test_metadata_delete", "SELECT orderkey, linenumber, linestatus FROM lineitem WHERE linestatus<>'O' and linenumber<>3");

// Test delete with partition column AND RAND() - should fail because RAND() requires row-level filtering
try {
getQueryRunner().execute("DELETE FROM test_metadata_delete WHERE LINE_STATUS='F' AND rand() <= 0.1");
fail("expected exception");
}
catch (RuntimeException e) {
assertEquals(e.getMessage(), "This connector only supports delete where one or more partitions are deleted entirely");
}

assertQuery("SELECT * from test_metadata_delete", "SELECT orderkey, linenumber, linestatus FROM lineitem WHERE linestatus<>'O' and linenumber<>3");

assertUpdate("DROP TABLE test_metadata_delete");

assertFalse(getQueryRunner().tableExists(getSession(), "test_metadata_delete"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.facebook.presto.Session;
import com.facebook.presto.metadata.Metadata;
import com.facebook.presto.metadata.TableLayout;
import com.facebook.presto.spi.VariableAllocator;
import com.facebook.presto.spi.WarningCollector;
import com.facebook.presto.spi.plan.DeleteNode;
Expand All @@ -23,6 +24,7 @@
import com.facebook.presto.spi.plan.PlanNodeIdAllocator;
import com.facebook.presto.spi.plan.TableFinishNode;
import com.facebook.presto.spi.plan.TableScanNode;
import com.facebook.presto.spi.relation.RowExpression;
import com.facebook.presto.sql.planner.TypeProvider;
import com.facebook.presto.sql.planner.plan.ExchangeNode;
import com.facebook.presto.sql.planner.plan.SimplePlanRewriter;
Expand All @@ -31,6 +33,7 @@
import java.util.List;
import java.util.Optional;

import static com.facebook.presto.expressions.LogicalRowExpressions.TRUE_CONSTANT;
import static java.util.Objects.requireNonNull;

/**
Expand Down Expand Up @@ -101,6 +104,13 @@ public PlanNode visitTableFinish(TableFinishNode node, RewriteContext<Void> cont
return context.defaultRewrite(node);
}

// Check for remaining predicates that require row-level filtering.
// The remainingPredicate contains filters that couldn't be pushed down to the connector,
// such as non-deterministic functions (RAND(), UUID(), etc.) or complex expressions.
if (hasRemainingPredicates(tableScanNode)) {
return context.defaultRewrite(node);
}

planChanged = true;
return new MetadataDeleteNode(
node.getSourceLocation(),
Expand All @@ -109,6 +119,22 @@ public PlanNode visitTableFinish(TableFinishNode node, RewriteContext<Void> cont
Iterables.getOnlyElement(node.getOutputVariables()));
}

private boolean hasRemainingPredicates(TableScanNode tableScanNode)
{
if (!tableScanNode.getTable().getLayout().isPresent()) {
return false;
}

TableLayout tableLayout = metadata.getLayout(session, tableScanNode.getTable());

Optional<RowExpression> remainingPredicate = tableLayout.getRemainingPredicate();
if (remainingPredicate.isPresent() && !TRUE_CONSTANT.equals(remainingPredicate.get())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Direct comparison to TRUE_CONSTANT may miss logical equivalence.

Using object equality may not detect logically equivalent predicates. Use a normalization or equivalence utility to ensure correct predicate handling.

Suggested implementation:

            Optional<RowExpression> remainingPredicate = tableLayout.getRemainingPredicate();
            if (remainingPredicate.isPresent() && !isTruePredicate(remainingPredicate.get())) {
                return true;
            }

You will need to ensure that a utility method isTruePredicate(RowExpression expression) exists and correctly checks logical equivalence to "true". If it does not exist, you should implement it, for example:

public static boolean isTruePredicate(RowExpression expression) {
    // Implement logical equivalence check, e.g.:
    // return expression instanceof ConstantExpression && ((ConstantExpression) expression).getValue().equals(Boolean.TRUE);
    // Or use an existing utility if available.
}

If your codebase already has a utility for this (e.g., in RowExpressionUtils), use that instead.

return true;
}

return false;
}

private static <T> Optional<T> findNode(PlanNode source, Class<T> clazz)
{
while (true) {
Expand Down
Loading