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 @@ -993,7 +993,13 @@ public OptionalLong delete(ConnectorSession session, JdbcTableHandle handle)
checkArgument(handle.getSortOrder().isEmpty(), "Unable to delete when sort order is set: %s", handle);
try (Connection connection = connectionFactory.openConnection(session)) {
verify(connection.getAutoCommit());
PreparedQuery preparedQuery = queryBuilder.prepareDeleteQuery(this, session, connection, handle.getRequiredNamedRelation(), handle.getConstraint());
PreparedQuery preparedQuery = queryBuilder.prepareDeleteQuery(
this,
session,
connection,
handle.getRequiredNamedRelation(),
handle.getConstraint(),
getAdditionalPredicate(handle.getConstraintExpressions(), Optional.empty()));
try (PreparedStatement preparedStatement = queryBuilder.prepareStatement(this, session, connection, preparedQuery)) {
return OptionalLong.of(preparedStatement.executeUpdate());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,20 @@ public PreparedQuery prepareDeleteQuery(
ConnectorSession session,
Connection connection,
JdbcNamedRelationHandle baseRelation,
TupleDomain<ColumnHandle> tupleDomain)
TupleDomain<ColumnHandle> tupleDomain,
Optional<String> additionalPredicate)
{
String sql = "DELETE FROM " + getRelation(client, baseRelation.getRemoteTableName());

ImmutableList.Builder<QueryParameter> accumulator = ImmutableList.builder();

List<String> clauses = toConjuncts(client, session, connection, tupleDomain, accumulator::add);
if (additionalPredicate.isPresent()) {
clauses = ImmutableList.<String>builder()
.addAll(clauses)
.add(additionalPredicate.get())
.build();
}
if (!clauses.isEmpty()) {
sql += " WHERE " + Joiner.on(" AND ").join(clauses);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ PreparedQuery prepareDeleteQuery(
ConnectorSession session,
Connection connection,
JdbcNamedRelationHandle baseRelation,
TupleDomain<ColumnHandle> tupleDomain);
TupleDomain<ColumnHandle> tupleDomain,
Optional<String> additionalPredicate);

PreparedStatement prepareStatement(
JdbcClient client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

// Single-threaded because H2 DDL operations can sometimes take a global lock, leading to apparent deadlocks
// like in https://github.com/trinodb/trino/issues/7209.
Expand Down Expand Up @@ -124,6 +125,13 @@ protected Optional<DataMappingTestSetup> filterDataMappingSmokeTestData(DataMapp
return Optional.of(dataMappingTestSetup);
}

@Override
public void testDeleteWithLike()
{
assertThatThrownBy(super::testDeleteWithLike)
.hasStackTraceContaining("TrinoException: Unsupported delete");
}

@Test
public void testUnknownTypeAsIgnored()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,13 @@ public void testDelete()
}
}

@Override
public void testDeleteWithLike()
{
assertThatThrownBy(super::testDeleteWithLike)
.hasStackTraceContaining("Delete without primary key or partition key is not supported");
}

@Override
public void testDeleteWithComplexPredicate()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ public void testDelete()
.hasStackTraceContaining("Deletes must match whole partitions for non-transactional tables");
}

@Override
public void testDeleteWithLike()
{
assertThatThrownBy(super::testDeleteWithLike)
.hasStackTraceContaining("Deletes must match whole partitions for non-transactional tables");
}

@Override
public void testDeleteWithComplexPredicate()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ public void testDelete()
.hasStackTraceContaining("This connector only supports delete where one or more identity-transformed partitions are deleted entirely");
}

@Override
public void testDeleteWithLike()
{
// Deletes are covered with testMetadataDelete test methods
assertThatThrownBy(super::testDeleteWithLike)
.hasStackTraceContaining("This connector only supports delete where one or more identity-transformed partitions are deleted entirely");
}

@Override
public void testDeleteWithComplexPredicate()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static java.util.stream.Collectors.joining;
import static java.util.stream.IntStream.range;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

Expand Down Expand Up @@ -196,6 +197,13 @@ public void testDropTable()
assertFalse(getQueryRunner().tableExists(getSession(), "test_drop"));
}

@Override
public void testDeleteWithLike()
{
assertThatThrownBy(super::testDeleteWithLike)
.hasStackTraceContaining("TrinoException: Unsupported delete");
}

@Test
public void testViews()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

Expand Down Expand Up @@ -306,6 +307,13 @@ public void testDropTable()
assertFalse(getQueryRunner().tableExists(getSession(), tableName));
}

@Override
public void testDeleteWithLike()
{
assertThatThrownBy(super::testDeleteWithLike)
.hasStackTraceContaining("TrinoException: Unsupported delete");
}

@Test
public void testViews()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@ public void testCountDistinctWithStringTypes()
}
}

@Override
public void testDeleteWithLike()
{
assertThatThrownBy(super::testDeleteWithLike)
.hasStackTraceContaining("TrinoException: Unsupported delete");
}

@Test
public void testSchemaOperations()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ public void testCountDistinctWithStringTypes()
}
}

@Override
public void testDeleteWithLike()
{
assertThatThrownBy(super::testDeleteWithLike)
.hasStackTraceContaining("TrinoException: Unsupported delete");
}

@Test
public void testSchemaOperations()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,13 @@ public OptionalLong delete(ConnectorSession session, JdbcTableHandle handle)
checkArgument(handle.getSortOrder().isEmpty(), "Unable to delete when sort order is set: %s", handle);
try (Connection connection = connectionFactory.openConnection(session)) {
verify(connection.getAutoCommit());
PreparedQuery preparedQuery = queryBuilder.prepareDeleteQuery(this, session, connection, handle.getRequiredNamedRelation(), handle.getConstraint());
PreparedQuery preparedQuery = queryBuilder.prepareDeleteQuery(
this,
session,
connection,
handle.getRequiredNamedRelation(),
handle.getConstraint(),
getAdditionalPredicate(handle.getConstraintExpressions(), Optional.empty()));
try (PreparedStatement preparedStatement = queryBuilder.prepareStatement(this, session, connection, preparedQuery)) {
int affectedRowsCount = preparedStatement.executeUpdate();
// In getPreparedStatement we set autocommit to false so here we need an explicit commit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ public void testDropTable()
assertFalse(getQueryRunner().tableExists(getSession(), "test_drop"));
}

@Override
public void testDeleteWithLike()
{
assertThatThrownBy(super::testDeleteWithLike)
.hasStackTraceContaining("TrinoException: Unsupported delete");
}

@Test
public void testReadFromView()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import static java.util.stream.Collectors.joining;
import static java.util.stream.IntStream.range;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

Expand Down Expand Up @@ -384,6 +385,13 @@ public void testShowCreateTable()
")");
}

@Override
public void testDeleteWithLike()
{
assertThatThrownBy(super::testDeleteWithLike)
.hasStackTraceContaining("TrinoException: Unsupported delete");
}

@Test(dataProvider = "dataCompression")
public void testCreateWithDataCompression(DataCompression dataCompression)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2336,6 +2336,17 @@ public void testDelete()
}
}

@Test
public void testDeleteWithLike()
{
skipTestUnlessSupportsDeletes();

try (TestTable table = new TestTable(getQueryRunner()::execute, "test_with_like_", "AS SELECT * FROM nation")) {
assertUpdate("DELETE FROM " + table.getName() + " WHERE name LIKE '%a%'", "VALUES 0");
assertUpdate("DELETE FROM " + table.getName() + " WHERE name LIKE '%A%'", "SELECT count(*) FROM nation WHERE name LIKE '%A%'");
}
}

@Test
public void testDeleteWithComplexPredicate()
{
Expand Down