/* * 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.cache.mvcc; import java.util.List; import org.apache.ignite.Ignite; import org.apache.ignite.IgniteCache; import org.apache.ignite.cache.CacheMode; import org.apache.ignite.cache.query.SqlFieldsQuery; import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.transactions.Transaction; import org.junit.Test; import static org.apache.ignite.cache.CacheMode.PARTITIONED; import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC; import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ; /** * */ public class SubQueryTest extends CacheMvccAbstractTest { /** {@inheritDoc} */ @Override protected CacheMode cacheMode() { return PARTITIONED; } /** * @throws Exception If failed */ @SuppressWarnings("unchecked") @Test public void tesSubQuery() throws Exception { Ignite node = startGrids(3); IgniteCache dummy = node.createCache(new CacheConfiguration<>("dummy")); dummy.query(new SqlFieldsQuery("create table person (id int primary key, name varchar) " + "with \"atomicity=transactional_snapshot,cache_name=Person\"")).getAll(); IgniteCache mvccCache = node.cache("Person"); final int size = 100; try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) { for (int i = 1; i <= size; i++) mvccCache.query(new SqlFieldsQuery("INSERT INTO Person (id, name) VALUES (" + i + ", 'a" + i + "')")).getAll(); tx.commit(); } List> res = mvccCache.query(new SqlFieldsQuery("SELECT COUNT(*) FROM person")).getAll(); long cnt = res.get(0).get(0); assertEquals(size, cnt); log.info("SELECT COUNT(*) FROM person"); log.info("Result=" + cnt); List resWithoutSubQry = mvccCache.query(new SqlFieldsQuery("SELECT id, name FROM person WHERE id=" + cnt)).getAll(); log.info("SELECT id, name FROM person WHERE id=" + cnt); log.info("Result=" + resWithoutSubQry); List resWithSubQry = mvccCache.query(new SqlFieldsQuery("SELECT id, name FROM person WHERE id = (SELECT COUNT(*) FROM person)")).getAll(); log.info("SELECT id, name FROM person WHERE id = (SELECT COUNT(*) FROM person)"); log.info("Result=" + resWithSubQry); List explain = mvccCache.query(new SqlFieldsQuery("EXPLAIN SELECT id, name FROM person WHERE id = (SELECT COUNT(*) FROM person)")).getAll(); log.info("explain result=" + explain); assertEquals(resWithoutSubQry, resWithSubQry); dummy.close(); mvccCache.close(); } }