xtern commented on code in PR #2846: URL: https://github.com/apache/ignite-3/pull/2846#discussion_r1410304176
########## modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItSqlMultiStatementTxTest.java: ########## @@ -0,0 +1,301 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.sql.engine; + +import static org.apache.ignite.internal.sql.engine.util.SqlTestUtils.assertThrowsSqlException; +import static org.apache.ignite.internal.testframework.IgniteTestUtils.await; +import static org.apache.ignite.internal.testframework.IgniteTestUtils.waitForCondition; +import static org.apache.ignite.lang.ErrorGroups.Sql.RUNTIME_ERR; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import java.util.List; +import org.apache.ignite.internal.sql.BaseSqlMultiStatementTest; +import org.apache.ignite.internal.sql.engine.util.Commons; +import org.apache.ignite.internal.tx.InternalTransaction; +import org.apache.ignite.internal.util.AsyncCursor.BatchedResult; +import org.apache.ignite.sql.ExternalTransactionNotSupportedException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests to verify the execution of queries with transaction control statements. + * + * @see SqlQueryType#TX_CONTROL + */ +@SuppressWarnings("ThrowableNotThrown") +public class ItSqlMultiStatementTxTest extends BaseSqlMultiStatementTest { + /** Default number of rows in the big table. */ + private static final int BIG_TABLE_ROWS_COUNT = Commons.IN_BUFFER_SIZE * 6; + + /** Number of completed transactions before the test started. */ + private int numberOfFinishedTransactionsOnStart; + + @BeforeAll + void createTable() { + sql("CREATE TABLE test (id INT PRIMARY KEY)"); + + createTable("big", 1, 1); + sql("INSERT INTO big (id) SELECT x FROM TABLE(SYSTEM_RANGE(1, " + BIG_TABLE_ROWS_COUNT + "));"); + } + + @AfterEach + void clearTestTable() { + sql("DELETE FROM test"); + } + + @BeforeEach + void saveFinishedTxCount() { + numberOfFinishedTransactionsOnStart = txManager().finished(); + } + + @Test + void emptyTransactionControlStatement() { + List<AsyncSqlCursor<List<Object>>> cursors = fetchAllCursors( + runScript("START TRANSACTION; COMMIT")); + + assertThat(cursors, hasSize(2)); + + validateSingleResult(cursors.get(0)); + validateSingleResult(cursors.get(1)); + + verifyFinishedTxCount(1); + } + + @Test + void basicTxStatements() { + List<AsyncSqlCursor<List<Object>>> cursors = fetchAllCursors(runScript( + "START TRANSACTION;" + + "INSERT INTO test VALUES(0);" + + "INSERT INTO test VALUES(1);" + + "COMMIT;" + + + "START TRANSACTION;" + + "COMMIT;" + + + "START TRANSACTION;" + + "INSERT INTO test VALUES(2);" + + "COMMIT;" + )); + + verifyFinishedTxCount(3); + + assertQuery("select count(id) from test") + .returns(3L).check(); + + cursors.forEach(AsyncSqlCursor::closeAsync); + } + + @Test + void readMoreRowsThanCanBePrefetchedReadOnlyTx() { + String query = "START TRANSACTION READ ONLY;" + + "SELECT 1;" + + "SELECT id FROM big;" + + "COMMIT;"; + + List<AsyncSqlCursor<List<Object>>> cursors = fetchAllCursors(runScript(query)); + assertThat(cursors, hasSize(4)); + + BatchedResult<List<Object>> res = await(cursors.get(2).requestNextAsync(BIG_TABLE_ROWS_COUNT * 2)); + assertNotNull(res); + assertThat(res.items(), hasSize(BIG_TABLE_ROWS_COUNT)); + + verifyFinishedTxCount(1); + + cursors.forEach(AsyncSqlCursor::closeAsync); + } + + @Test + void readMoreRowsThanCanBePrefetchedReadWriteTx() { + String query = "START TRANSACTION;" + + "UPDATE big SET salary=1 WHERE id=1;" + + "SELECT id FROM big;" + + "COMMIT;"; + + AsyncSqlCursor<List<Object>> cursor = runScript(query); + + assertTrue(cursor.hasNextResult()); + + AsyncSqlCursor<List<Object>> updateCursor = await(cursor.nextResult()); + assertNotNull(updateCursor); + validateSingleResult(updateCursor, 1L); + assertTrue(updateCursor.hasNextResult()); + + AsyncSqlCursor<List<Object>> selectCursor = await(updateCursor.nextResult()); + assertNotNull(selectCursor); + + BatchedResult<List<Object>> res = await( + selectCursor.requestNextAsync(BIG_TABLE_ROWS_COUNT / 2)); + + assertNotNull(res); + assertThat(res.items(), hasSize(BIG_TABLE_ROWS_COUNT / 2)); + assertEquals(1, txManager().pending(), "Transaction must not finished until the cursor is closed."); + assertFalse(selectCursor.nextResult().isDone()); + + await(selectCursor.requestNextAsync(BIG_TABLE_ROWS_COUNT)); // Cursor must close implicitly. + verifyFinishedTxCount(1); + } + + @Test + void openedScriptTransactionRollsBackImplicitly() { + { + fetchAllCursors(runScript("START TRANSACTION;")); Review Comment: We have test `commitWithoutOpenTransactionDoesNothing` that checks commit without tx. We have test `emptyTransactionControlStatement` that checks `start; commit;` (validates cursor data, as you suggest here). We have this test that verifies that the transaction will be rolled back in case of user input error. What kind of test we need to add? And what results do we expect? > compare it with referral data bases I checked PG behavior - "COMMIT; COMMIT;" does not produce error and returns results set. I also checked ``` stmt = connection.createStatement(...) stmts.execute("BEGIN; INSERT..."); // open TX without COMMIT connection.close(); // rolls back the changes made ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
