Skip to content

Commit a4a1dc9

Browse files
authored
Support unsetting transaction-bound SchemaTemplate in a Transaction (#3496)
This PR add support for "unsetting" a transaction-bound SchemaTemplate from the ongoing transaction. This is essential if we want to revert the execution to using the SchemaTemplate from the catalog thereby undoing all the temporary function definitions at once. Note that this could be more efficient than the `DROP TEMPORARY FUNCTION ...` DDL introduced in the previous PR and have the wider scope that is beyond `TEMPORARY FUNCTIONS` definitions. Also added a test for testing it in the context of `TransactionBoundDatabase`.
1 parent acc9549 commit a4a1dc9

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

fdb-relational-core/src/main/java/com/apple/foundationdb/relational/api/Transaction.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public interface Transaction extends AutoCloseable {
5858
*/
5959
void setBoundSchemaTemplate(@Nonnull SchemaTemplate schemaTemplate);
6060

61+
/**
62+
* Unsets the bound schema template, if one exists.
63+
*/
64+
void unsetBoundSchemaTemplate();
65+
6166
@Override
6267
void close() throws RelationalException;
6368

fdb-relational-core/src/main/java/com/apple/foundationdb/relational/recordlayer/RecordContextTransaction.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,15 @@ public Optional<SchemaTemplate> getBoundSchemaTemplateMaybe() {
9191

9292
@Override
9393
public void setBoundSchemaTemplate(@Nonnull final SchemaTemplate schemaTemplate) {
94-
context.removeFromSession(SchemaTemplate.class.toString(), SchemaTemplate.class);
94+
unsetBoundSchemaTemplate();
9595
context.putInSessionIfAbsent(SchemaTemplate.class.toString(), schemaTemplate);
9696
}
9797

98+
@Override
99+
public void unsetBoundSchemaTemplate() {
100+
context.removeFromSession(SchemaTemplate.class.toString(), SchemaTemplate.class);
101+
}
102+
98103
@Override
99104
public void close() throws RelationalException {
100105
abort();

fdb-relational-core/src/main/java/com/apple/foundationdb/relational/recordlayer/RecordStoreAndRecordContextTransaction.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public void setBoundSchemaTemplate(@Nonnull final SchemaTemplate schemaTemplate)
8282
transaction.setBoundSchemaTemplate(schemaTemplate);
8383
}
8484

85+
@Override
86+
public void unsetBoundSchemaTemplate() {
87+
transaction.unsetBoundSchemaTemplate();
88+
}
89+
8590
@Override
8691
public void close() throws RelationalException {
8792
transaction.close();

fdb-relational-core/src/test/java/com/apple/foundationdb/relational/recordlayer/TransactionBoundDatabaseTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,32 @@ void dropTemporaryIfExistFunction() throws RelationalException, SQLException {
224224
}
225225
}
226226

227+
@Test
228+
void unsetTransactionBoundSchemaTemplate() throws RelationalException, SQLException {
229+
final var embeddedConnection = connRule.getUnderlyingEmbeddedConnection();
230+
final var store = getStore(embeddedConnection);
231+
final var schemaTemplate = getSchemaTemplate(embeddedConnection);
232+
233+
try (FDBRecordContext context = createNewContext(embeddedConnection)) {
234+
final var newStore = store.asBuilder().setMetaDataProvider(store.getMetaDataProvider()).setContext(context).open();
235+
try (Transaction transaction = new RecordStoreAndRecordContextTransaction(newStore, context, schemaTemplate)) {
236+
EmbeddedRelationalDriver driver = new EmbeddedRelationalDriver(new TransactionBoundEmbeddedRelationalEngine());
237+
try (RelationalConnection conn = driver.connect(dbRule.getConnectionUri(), transaction, Options.NONE)) {
238+
conn.setSchema("TEST_SCHEMA");
239+
try (RelationalStatement statement = conn.createStatement()) {
240+
statement.executeUpdate("CREATE TEMPORARY FUNCTION REST_FUNC() ON COMMIT DROP FUNCTION AS SELECT * FROM RESTAURANT WHERE REST_NO > 1000");
241+
}
242+
Assertions.assertThat(transaction.getBoundSchemaTemplateMaybe()).isPresent();
243+
Assertions.assertThat(transaction.getBoundSchemaTemplateMaybe().get().getInvokedRoutines())
244+
.hasSize(1)
245+
.anyMatch(routine -> routine.getName().equals("REST_FUNC"));
246+
transaction.unsetBoundSchemaTemplate();
247+
Assertions.assertThat(transaction.getBoundSchemaTemplateMaybe()).isEmpty();
248+
}
249+
}
250+
}
251+
}
252+
227253
static FDBRecordStore getStore(EmbeddedRelationalConnection connection) throws RelationalException, SQLException {
228254
connection.setAutoCommit(false);
229255
connection.createNewTransaction();

fdb-relational-core/src/test/java/com/apple/foundationdb/relational/utils/InMemoryTransactionManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public void setBoundSchemaTemplate(@Nonnull final SchemaTemplate schemaTemplate)
8484
throw new NotImplementedException("method is not implemented");
8585
}
8686

87+
@Override
88+
public void unsetBoundSchemaTemplate() {
89+
throw new NotImplementedException("method is not implemented");
90+
}
91+
8792
@Override
8893
public void close() {
8994
//no-op

0 commit comments

Comments
 (0)