vldpyatkov commented on code in PR #13366:
URL: https://github.com/apache/ignite/pull/13366#discussion_r3747513617


##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SelectForUpdateIntegrationTest.java:
##########
@@ -0,0 +1,682 @@
+/*
+ * 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.processors.query.calcite.integration;
+
+import java.util.UUID;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.TransactionConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.TestRecordingCommunicationSpi;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.processors.query.QueryEngine;
+import org.apache.ignite.internal.processors.query.calcite.QueryChecker;
+import 
org.apache.ignite.internal.processors.query.calcite.message.QueryBatchMessage;
+import org.apache.ignite.internal.processors.query.calcite.util.Commons;
+import org.apache.ignite.internal.processors.query.calcite.util.IgniteResource;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.transactions.Transaction;
+import org.junit.Test;
+
+import static 
org.apache.ignite.internal.processors.query.calcite.integration.AbstractBasicIntegrationTransactionalTest.SqlTransactionMode.ALL;
+import static org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC;
+import static 
org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC;
+import static 
org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED;
+import static org.apache.ignite.transactions.TransactionState.ACTIVE;
+
+/**
+ * Integration tests for {@code SELECT ... FOR UPDATE} syntax.
+ */
+public class SelectForUpdateIntegrationTest extends 
AbstractBasicIntegrationTest {
+    /** */
+    private static IgniteEx ignite0;
+
+    /** */
+    private static IgniteEx ignite1;
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String 
igniteInstanceName) throws Exception {
+        return super.getConfiguration(igniteInstanceName)
+            .setTransactionConfiguration(new TransactionConfiguration()
+                .setTxAwareQueriesEnabled(true))
+            .setCommunicationSpi(new TestRecordingCommunicationSpi());
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        super.beforeTestsStarted();
+
+        ignite0 = grid(0);
+        ignite1 = grid(1);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTestsStopped() throws Exception {
+        ignite0 = null;
+        ignite1 = null;
+
+        super.afterTestsStopped();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected QueryChecker assertQuery(Ignite ignite, String qry) {
+        Transaction tx = ignite.transactions().tx();
+        QueryChecker checker;
+
+        if (tx == null)
+            checker = super.assertQuery(ignite, qry);
+        else {
+            checker = new QueryChecker(qry, tx, ALL) {
+                @Override public void check() {
+                    tx.suspend();
+
+                    try {
+                        super.check();
+                    }
+                    finally {
+                        tx.resume();
+                    }
+                }
+
+                @Override protected QueryEngine getEngine() {
+                    return 
Commons.lookupComponent(((IgniteEx)ignite).context(), QueryEngine.class);
+                }
+            };
+        }
+
+        return checker;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        super.beforeTest();
+
+        sql("CREATE TABLE Person (id INT PRIMARY KEY, name VARCHAR, age INT, 
deptId INT, managerId INT) " +
+            "WITH atomicity=TRANSACTIONAL");
+        sql("INSERT INTO Person (id, name, age) VALUES " +
+            "(1, 'Alice', 20), (2, 'Bob', 21), (3, 'Ann', 22), (4, 'Bill', 
23)" +
+            ", (5, 'Alex', 24), (6, 'Ben', 25), (7, 'Cathy', 26), (8, 'Carl', 
27), (9, 'Diana', 28)" +
+            ", (10, 'David', 29), (11, 'Eva', 30), (12, 'Evan', 31), (13, 
'Fiona', 32), (14, 'Frank', 33)" +
+            ", (15, 'Grace', 34), (16, 'George', 35), (17, 'Hannah', 36), (18, 
'Harry', 37), (19, 'Ivy', 38)" +
+            ", (20, 'Ian', 39), (21, 'Jack', 40), (22, 'Jill', 41), (23, 
'Karen', 42), (24, 'Kyle', 43)" +
+            ", (25, 'Laura', 44), (26, 'Leo', 45), (27, 'Mia', 46), (28, 
'Mike', 47), (29, 'Nina', 48)" +
+            ", (30, 'Nick', 49)");
+        sql("UPDATE Person SET deptId = 1, managerId = 2 WHERE id = 1");
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        sql("DROP TABLE IF EXISTS Dept");
+        sql("DROP TABLE IF EXISTS Person");
+
+        super.afterTest();
+    }
+
+    /** SELECT FOR UPDATE without OF locks rows of every table participating 
in a JOIN. */
+    @Test
+    public void testSelectForUpdateJoinLocksAllTables() throws Exception {
+        createDeptTable();
+
+        CountDownLatch locked = new CountDownLatch(1);
+        CountDownLatch release = new CountDownLatch(1);
+
+        IgniteInternalFuture<?> lockFut = GridTestUtils.runAsync(() -> {
+            try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, 
READ_COMMITTED)) {
+                assertQuery(ignite0,
+                    "SELECT p.id FROM Person p JOIN Dept d ON p.deptId = d.id 
WHERE p.id = 1 FOR UPDATE")
+                    .returns(1)
+                    .check();
+
+                locked.countDown();
+
+                assertTrue("Timed out waiting to release JOIN locks", 
release.await(30, TimeUnit.SECONDS));
+
+                tx.commit();
+            }
+        });
+
+        try {
+            assertTrue("JOIN transaction did not acquire locks in time", 
locked.await(10, TimeUnit.SECONDS));
+
+            assertTableRowLocked(ignite1, "Person", 1);
+            assertTableRowLocked(ignite1, "Dept", 1);
+        }
+        finally {
+            release.countDown();
+        }
+
+        lockFut.get(10_000);
+    }
+
+    /** FOR UPDATE OF locks only the table owning the specified JOIN column. */
+    @Test
+    public void testSelectForUpdateJoinOfLocksSelectedTable() throws Exception 
{
+        createDeptTable();
+
+        CountDownLatch locked = new CountDownLatch(1);
+        CountDownLatch release = new CountDownLatch(1);
+
+        IgniteInternalFuture<?> lockFut = GridTestUtils.runAsync(() -> {
+            try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, 
READ_COMMITTED)) {
+                assertQuery(ignite0,
+                    "SELECT p.id FROM Person p JOIN Dept d ON p.deptId = d.id 
WHERE p.id = 1 FOR UPDATE OF p.id")
+                    .returns(1)
+                    .check();
+
+                locked.countDown();
+
+                assertTrue("Timed out waiting to release JOIN locks", 
release.await(30, TimeUnit.SECONDS));
+
+                tx.commit();
+            }
+        });
+
+        try {
+            assertTrue("JOIN transaction did not acquire locks in time", 
locked.await(10, TimeUnit.SECONDS));
+
+            assertTableRowLocked(ignite1, "Person", 1);
+
+            try (Transaction tx = ignite1.transactions().txStart(PESSIMISTIC, 
READ_COMMITTED)) {
+                assertQuery(ignite1, "SELECT * FROM Dept WHERE id = 1 FOR 
UPDATE NOWAIT")
+                    .returns(1, "Engineering")
+                    .check();
+
+                tx.commit();
+            }
+        }
+        finally {
+            release.countDown();
+        }
+
+        lockFut.get(10_000);
+    }
+
+    /** A qualified OF column selects a particular table occurrence in a 
self-join. */
+    @Test
+    public void testSelectForUpdateSelfJoinOfUsesAlias() throws Exception {
+        assertSelfJoinOfLocks("employee", 1, 2);
+        assertSelfJoinOfLocks("manager", 2, 1);
+    }
+
+    /** Creates a second transactional table used by JOIN tests. */
+    private void createDeptTable() {
+        sql("CREATE TABLE Dept (id INT PRIMARY KEY, name VARCHAR) WITH 
atomicity=TRANSACTIONAL");
+        sql("INSERT INTO Dept VALUES (1, 'Engineering'), (2, 'Sales'), (3, 
'HR')");
+    }
+
+    /** Verifies that OF resolves an alias to the correct side of a self-join. 
*/
+    private void assertSelfJoinOfLocks(String alias, int lockedId, int 
unlockedId) throws Exception {
+        CountDownLatch locked = new CountDownLatch(1);
+        CountDownLatch release = new CountDownLatch(1);
+
+        IgniteInternalFuture<?> lockFut = GridTestUtils.runAsync(() -> {
+            try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, 
READ_COMMITTED)) {
+                assertQuery(ignite0, "SELECT employee.id, manager.id FROM 
Person employee " +
+                    "JOIN Person manager ON employee.managerId = manager.id " +
+                    "WHERE employee.id = 1 FOR UPDATE OF " + alias + ".id")
+                    .returns(1, 2)
+                    .check();
+
+                locked.countDown();
+
+                assertTrue("Timed out waiting to release self-join lock", 
release.await(30, TimeUnit.SECONDS));
+
+                tx.commit();
+            }
+        });
+
+        try {
+            assertTrue("Self-join transaction did not acquire lock in time", 
locked.await(10, TimeUnit.SECONDS));
+
+            assertTableRowLocked(ignite1, "Person", lockedId);
+            assertTableRowUnlocked(ignite1, "Person", unlockedId);
+        }
+        finally {
+            release.countDown();
+        }
+
+        lockFut.get(10_000);
+    }
+
+    /** FOR UPDATE without an active transaction produces an exception. */
+    @Test
+    public void testSelectForUpdateOutsideTransaction() {
+        assertThrows("SELECT id FROM Person FOR UPDATE", 
IgniteSQLException.class,
+            
IgniteResource.INSTANCE.selectForUpdateRequiresPessimisticTx().str());
+    }
+
+    /** SELECT FOR UPDATE inside an OPTIMISTIC transaction throws an 
appropriate error. */
+    @Test
+    public void testSelectForUpdateInOptimisticTransaction() {
+        try (Transaction tx = ignite0.transactions().txStart(OPTIMISTIC, 
READ_COMMITTED)) {
+            GridTestUtils.assertThrowsAnyCause(log,
+                () -> sql(ignite0, "SELECT * FROM Person WHERE id = 1 FOR 
UPDATE"),
+                IgniteSQLException.class, "PESSIMISTIC");
+        }
+    }
+
+    /** SELECT FOR UPDATE does not support DISTINCT because a result row may 
represent several cache entries. */
+    @Test
+    public void testSelectForUpdateRejectsDistinct() {
+        assertSelectForUpdateUnsupported(
+            "SELECT DISTINCT deptId FROM Person FOR UPDATE",
+            "DISTINCT");
+    }
+
+    /** SELECT FOR UPDATE does not support GROUP BY because grouped rows do 
not identify individual cache entries. */
+    @Test
+    public void testSelectForUpdateRejectsGroupBy() {
+        assertSelectForUpdateUnsupported(
+            "SELECT deptId, COUNT(*) FROM Person GROUP BY deptId FOR UPDATE",
+            "GROUP BY");
+    }
+
+    /** SELECT FOR UPDATE does not support HAVING because it operates on 
grouped result rows. */
+    @Test
+    public void testSelectForUpdateRejectsHaving() {
+        assertSelectForUpdateUnsupported(
+            "SELECT COUNT(*) FROM Person HAVING COUNT(*) > 1 FOR UPDATE",
+            "HAVING");
+    }
+
+    /** Aggregate queries without GROUP BY also collapse cache entries into a 
derived result row. */
+    @Test
+    public void testSelectForUpdateRejectsAggregateWithoutGroupBy() {
+        assertSelectForUpdateUnsupported(
+            "SELECT COUNT(*) FROM Person FOR UPDATE",
+            "aggregate functions");
+    }
+
+    /** An aggregate used only by ORDER BY still collapses all source rows 
into one result row. */
+    @Test
+    public void testSelectForUpdateRejectsAggregateInOrderBy() {
+        assertSelectForUpdateUnsupported(
+            "SELECT 1 FROM Person ORDER BY COUNT(*) FOR UPDATE",
+            "aggregate functions");
+    }
+
+    /** SELECT FOR UPDATE cannot lock rows exposed by a system view. */
+    @Test
+    public void testSelectForUpdateRejectsSystemView() {
+        try (Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, 
READ_COMMITTED)) {
+            assertThrows(ignite0, "SELECT node_id FROM SYS.NODES FOR UPDATE", 
IgniteSQLException.class,
+                "Column '_KEY' not found in table 'NODES'");

Review Comment:
   Fixed.



-- 
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]

Reply via email to