diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlOperationType.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlOperationType.java index 8d3165148219f..9ffabad1b613f 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlOperationType.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlOperationType.java @@ -31,7 +31,7 @@ public enum GridSqlOperationType { MULTIPLY(2, new BiExpressionSqlGenerator("*")), DIVIDE(2, new BiExpressionSqlGenerator("/")), MODULUS(2, new BiExpressionSqlGenerator("%")), - NEGATE(1, new PrefixSqlGenerator("-")), + NEGATE(1, new PrefixSqlGenerator("-", true)), // from org.h2.expression.Comparison EQUAL(2, new BiExpressionSqlGenerator("=")), @@ -47,7 +47,7 @@ public enum GridSqlOperationType { IS_NULL(1, new SuffixSqlGenerator("IS NULL")), IS_NOT_NULL(1, new SuffixSqlGenerator("IS NOT NULL")), - NOT(1, new PrefixSqlGenerator("NOT")), + NOT(1, new PrefixSqlGenerator("NOT", true)), // from org.h2.expression.ConditionAndOr AND(2, new BiExpressionSqlGenerator("AND")), @@ -58,6 +58,7 @@ public enum GridSqlOperationType { LIKE(2, new BiExpressionSqlGenerator("LIKE")), IN(-1, new ConditionInSqlGenerator()), + EXISTS(1, new PrefixSqlGenerator("EXISTS", false)), ; /** */ @@ -145,18 +146,32 @@ private static class PrefixSqlGenerator implements SqlGenerator { /** */ private final String text; + /** */ + private final boolean addSpace; + /** * @param text Text. + * @param addSpace Add space char after the prefix. */ - private PrefixSqlGenerator(String text) { + private PrefixSqlGenerator(String text, boolean addSpace) { this.text = text; + this.addSpace = addSpace; } /** {@inheritDoc} */ @Override public String getSql(GridSqlOperation operation) { assert operation.operationType().childrenCnt == 1; - return '(' + text + ' ' + operation.child().getSQL() + ')'; + StringBuilder b = new StringBuilder(); + + b.append('(').append(text); + + if (addSpace) + b.append(' '); + + b.append(operation.child(0).getSQL()).append(')'); + + return b.toString(); } } diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQueryParser.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQueryParser.java index d9c546c7fb104..a3e80ec6424da 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQueryParser.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQueryParser.java @@ -46,6 +46,7 @@ import org.h2.expression.CompareLike; import org.h2.expression.Comparison; import org.h2.expression.ConditionAndOr; +import org.h2.expression.ConditionExists; import org.h2.expression.ConditionIn; import org.h2.expression.ConditionInConstantSet; import org.h2.expression.ConditionInSelect; @@ -80,6 +81,7 @@ import static org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.DIVIDE; import static org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.EQUAL; import static org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.EQUAL_NULL_SAFE; +import static org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.EXISTS; import static org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN; import static org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IS_NOT_NULL; import static org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IS_NULL; @@ -186,7 +188,10 @@ public class GridSqlQueryParser { "compareType"); /** */ - private static final Getter QUERY = getter(ConditionInSelect.class, "query"); + private static final Getter QUERY_IN = getter(ConditionInSelect.class, "query"); + + /** */ + private static final Getter QUERY_EXISTS = getter(ConditionExists.class, "query"); /** */ private static final Getter LEFT = getter(CompareLike.class, "left"); @@ -854,7 +859,7 @@ private GridSqlElement parseExpression0(Expression expression, boolean calcTypes res.addChild(parseExpression(LEFT_CIS.get((ConditionInSelect)expression), calcTypes)); - Query qry = QUERY.get((ConditionInSelect)expression); + Query qry = QUERY_IN.get((ConditionInSelect)expression); assert0(qry instanceof Select, qry); @@ -959,6 +964,16 @@ private GridSqlElement parseExpression0(Expression expression, boolean calcTypes return res; } + if (expression instanceof ConditionExists) { + Query qry = QUERY_EXISTS.get((ConditionExists)expression); + + GridSqlOperation res = new GridSqlOperation(EXISTS); + + res.addChild(new GridSqlSubquery(parse(qry, null))); + + return res; + } + throw new IgniteException("Unsupported expression: " + expression + " [type=" + expression.getClass().getSimpleName() + ']'); } diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSplitterSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSplitterSelfTest.java index e72c9cb5b6d62..c3f1bd277a2de 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSplitterSelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSplitterSelfTest.java @@ -21,9 +21,11 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Random; +import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.atomic.AtomicLong; import javax.cache.CacheException; @@ -33,10 +35,8 @@ import org.apache.ignite.cache.CacheKeyConfiguration; import org.apache.ignite.cache.CacheMode; import org.apache.ignite.cache.CachePeekMode; -import org.apache.ignite.cache.affinity.AffinityKeyMapped; -import org.apache.ignite.cache.affinity.Affinity; -import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.affinity.Affinity; +import org.apache.ignite.cache.affinity.AffinityKeyMapped; import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.SqlFieldsQuery; import org.apache.ignite.cache.query.annotations.QuerySqlField; @@ -63,8 +63,8 @@ public class IgniteSqlSplitterSelfTest extends GridCommonAbstractTest { private static final TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - IgniteConfiguration cfg = super.getConfiguration(gridName); + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); CacheKeyConfiguration keyCfg = new CacheKeyConfiguration(TestKey.class.getName(), "affKey"); @@ -81,6 +81,11 @@ public class IgniteSqlSplitterSelfTest extends GridCommonAbstractTest { return cfg; } + @Override + protected long getTestTimeout() { + return 100_000_000; + } + /** {@inheritDoc} */ @Override protected void beforeTestsStarted() throws Exception { startGridsMultiThreaded(3, false); @@ -148,6 +153,47 @@ public void testOffsetLimit() throws Exception { } } + @SuppressWarnings("SuspiciousMethodCalls") + public void testExists() { + IgniteCache x = ignite(0).getOrCreateCache(cacheConfig("x", true, + Integer.class, Person2.class)); + IgniteCache y = ignite(0).getOrCreateCache(cacheConfig("y", true, + Integer.class, Person2.class)); + + try { + GridRandom rnd = new GridRandom(); + + Set intersects = new HashSet<>(); + + for (int i = 0; i < 3000; i++) { + int r = rnd.nextInt(3); + + if (r != 0) + x.put(i, new Person2(i, "pers_x_" + i)); + + if (r != 1) + y.put(i, new Person2(i, "pers_y_" + i)); + + if (r == 2) + intersects.add(i); + } + + assertFalse(intersects.isEmpty()); + + List> res = x.query(new SqlFieldsQuery("select _key from \"x\".Person2 px " + + "where exists(select 1 from \"y\".Person2 py where px._key = py._key)")).getAll(); + + assertEquals(intersects.size(), res.size()); + + for (List row : res) + assertTrue(intersects.contains(row.get(0))); + } + finally { + x.destroy(); + y.destroy(); + } + } + /** * @throws Exception If failed. */ @@ -550,15 +596,15 @@ public void testDistributedJoinsPlan() throws Exception { "\"orgRepl\".Organization o", "where p.affKey = o._key", true); - checkNoBatchedJoin(persPart, "select p._key k1, o._key k2 ", - "(select * from \"persPart\".Person2) p", - "\"orgPart\".Organization o", - "where p._key = o._key", false); - - checkNoBatchedJoin(persPart, "select p._key k1, o._key k2 ", - "\"persPart\".Person2 p", - "(select * from \"orgPart\".Organization) o", - "where p._key = o._key", false); + // TODO Now we can not analyze subqueries to decide if we are collocated or not. +// checkNoBatchedJoin(persPart, "select p._key k1, o._key k2 ", +// "(select * from \"persPart\".Person2) p", +// "\"orgPart\".Organization o", +// "where p._key = o._key", false); +// checkNoBatchedJoin(persPart, "select p._key k1, o._key k2 ", +// "\"persPart\".Person2 p", +// "(select * from \"orgPart\".Organization) o", +// "where p._key = o._key", false); // Join multiple. @@ -703,6 +749,7 @@ public void testDistributedJoinsPlan() throws Exception { ignite(0).destroyCache(cache.getName()); } } + /** * @throws Exception If failed. */ @@ -791,26 +838,26 @@ private void checkNoBatchedJoin(IgniteCache cache, false, 0, select + - "from " + cache1 + "," + cache2 + " "+ where); + "from " + cache1 + "," + cache2 + " " + where); checkQueryPlan(cache, false, 0, select + - "from " + cache2 + "," + cache1 + " "+ where); + "from " + cache2 + "," + cache1 + " " + where); if (testEnforceJoinOrder) { checkQueryPlan(cache, true, 0, select + - "from " + cache1 + "," + cache2 + " "+ where); + "from " + cache1 + "," + cache2 + " " + where); checkQueryPlan(cache, true, 0, select + - "from " + cache2 + "," + cache1 + " "+ where); + "from " + cache2 + "," + cache1 + " " + where); } } @@ -825,7 +872,8 @@ private void checkQueryPlan(IgniteCache cache, boolean enforceJoinOrder, int expBatchedJoins, String sql, - String...expText) { + String...expText + ) { checkQueryPlan(cache, enforceJoinOrder, expBatchedJoins, @@ -850,13 +898,13 @@ private void checkQueryPlan(IgniteCache cache, boolean enforceJoinOrder, int expBatchedJoins, SqlFieldsQuery qry, - String...expText) { + String... expText) { qry.setEnforceJoinOrder(enforceJoinOrder); qry.setDistributedJoins(true); String plan = queryPlan(cache, qry); - log.info("Plan: " + plan); + log.info("\n Plan:\n" + plan); assertEquals("Unexpected number of batched joins in plan [plan=" + plan + ", qry=" + qry + ']', expBatchedJoins, @@ -986,7 +1034,7 @@ private void doTestDistributedJoins(IgniteCache c, int orgs, in * @param args Arguments. * @return Column as list. */ - private static List columnQuery(IgniteCache c, String qry, Object... args) { + private static List columnQuery(IgniteCache c, String qry, Object... args) { return column(0, c.query(new SqlFieldsQuery(qry).setArgs(args)).getAll()); } @@ -1584,4 +1632,4 @@ private static class OrderGood implements Serializable { @QuerySqlField private int goodId; } -} \ No newline at end of file +} diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/GridQueryParsingTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/GridQueryParsingTest.java index 537ccdfd57953..ecdb593758f8a 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/GridQueryParsingTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/GridQueryParsingTest.java @@ -39,12 +39,8 @@ import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder; import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; import org.h2.command.Prepared; -import org.h2.command.dml.Query; -import org.h2.command.dml.Update; import org.h2.engine.Session; -import org.h2.expression.Expression; import org.h2.jdbc.JdbcConnection; -import org.h2.util.StringUtils; import static org.apache.ignite.cache.CacheRebalanceMode.SYNC; import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC; @@ -56,13 +52,19 @@ public class GridQueryParsingTest extends GridCommonAbstractTest { /** */ private static final TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); + /** */ + private static final String TEST_SCHEMA = "SCH"; + + /** */ + private static final String TEST_CACHE = "my-cache"; + /** */ private static Ignite ignite; /** {@inheritDoc} */ @SuppressWarnings("unchecked") - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - IgniteConfiguration c = super.getConfiguration(gridName); + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration c = super.getConfiguration(igniteInstanceName); TcpDiscoverySpi disco = new TcpDiscoverySpi(); @@ -73,12 +75,14 @@ public class GridQueryParsingTest extends GridCommonAbstractTest { // Cache. CacheConfiguration cc = defaultCacheConfiguration(); + cc.setName(TEST_CACHE); cc.setCacheMode(CacheMode.PARTITIONED); cc.setAtomicityMode(CacheAtomicityMode.ATOMIC); cc.setNearConfiguration(null); cc.setWriteSynchronizationMode(FULL_SYNC); cc.setRebalanceMode(SYNC); cc.setSwapEnabled(false); + cc.setSqlSchema(TEST_SCHEMA); cc.setSqlFunctionClasses(GridQueryParsingTest.class); cc.setIndexedTypes( String.class, Address.class, @@ -110,6 +114,12 @@ public class GridQueryParsingTest extends GridCommonAbstractTest { * @throws Exception If failed. */ public void testParseSelectAndUnion() throws Exception { + checkQuery("select 1 from Person p where addrIds in ((1,2,3), (3,4,5))"); + checkQuery("select 1 from Person p where addrId in ((1,))"); + checkQuery("select 1 from Person p " + + "where p.addrId in (select a.id from Address a)"); + checkQuery("select 1 from Person p " + + "where exists(select 1 from Address a where p.addrId = a.id)"); checkQuery("select 42"); checkQuery("select ()"); checkQuery("select (1)"); @@ -244,16 +254,16 @@ public void testParseSelectAndUnion() throws Exception { checkQuery("select street from Person p, (select a.street from Address a where a.street is not null) "); checkQuery("select addr.street from Person p, (select a.street from Address a where a.street is not null) addr"); - checkQuery("select p.name n from \"\".Person p order by p.old + 10"); + checkQuery("select p.name n from sch.Person p order by p.old + 10"); - checkQuery("select case when p.name is null then 'Vasya' end x from \"\".Person p"); - checkQuery("select case when p.name like 'V%' then 'Vasya' else 'Other' end x from \"\".Person p"); - checkQuery("select case when upper(p.name) = 'VASYA' then 'Vasya' when p.name is not null then p.name else 'Other' end x from \"\".Person p"); + checkQuery("select case when p.name is null then 'Vasya' end x from sch.Person p"); + checkQuery("select case when p.name like 'V%' then 'Vasya' else 'Other' end x from sch.Person p"); + checkQuery("select case when upper(p.name) = 'VASYA' then 'Vasya' when p.name is not null then p.name else 'Other' end x from sch.Person p"); - checkQuery("select case p.name when 'Vasya' then 1 end z from \"\".Person p"); - checkQuery("select case p.name when 'Vasya' then 1 when 'Petya' then 2 end z from \"\".Person p"); - checkQuery("select case p.name when 'Vasya' then 1 when 'Petya' then 2 else 3 end z from \"\".Person p"); - checkQuery("select case p.name when 'Vasya' then 1 else 3 end z from \"\".Person p"); + checkQuery("select case p.name when 'Vasya' then 1 end z from sch.Person p"); + checkQuery("select case p.name when 'Vasya' then 1 when 'Petya' then 2 end z from sch.Person p"); + checkQuery("select case p.name when 'Vasya' then 1 when 'Petya' then 2 else 3 end z from sch.Person p"); + checkQuery("select case p.name when 'Vasya' then 1 else 3 end z from sch.Person p"); checkQuery("select count(*) as a from Person union select count(*) as a from Address"); checkQuery("select old, count(*) as a from Person group by old union select 1, count(*) as a from Address"); @@ -268,6 +278,54 @@ public void testParseSelectAndUnion() throws Exception { checkQuery("(select 2 a) union all (select 1) order by a desc nulls first limit ? offset ?"); } + /** + * Query AST transformation heavily depends on this behavior. + * + * @throws Exception If failed. + */ + public void testParseTableFilter() throws Exception { + Prepared prepared = parse("select Person.old, p1.old, p1.addrId from Person, Person p1 " + + "where exists(select 1 from Address a where a.id = p1.addrId)"); + + GridSqlSelect select = (GridSqlSelect)new GridSqlQueryParser().parse(prepared); + + GridSqlJoin join = (GridSqlJoin)select.from(); + + GridSqlTable tbl1 = (GridSqlTable)join.leftTable(); + GridSqlAlias tbl2Alias = (GridSqlAlias)join.rightTable(); + GridSqlTable tbl2 = tbl2Alias.child(); + + // Must be distinct objects, even if it is the same table. + //assertNotSame(tbl1, tbl2); + + assertNotNull(tbl1.dataTable()); + assertNotNull(tbl2.dataTable()); + assertSame(tbl1.dataTable(), tbl2.dataTable()); + + GridSqlColumn col1 = (GridSqlColumn)select.column(0); + GridSqlColumn col2 = (GridSqlColumn)select.column(1); + + assertSame(tbl1, col1.expressionInFrom()); + + // Alias in FROM must be included in column. + assertSame(tbl2Alias, col2.expressionInFrom()); + + // In EXISTS we must correctly reference the column from the outer query. + GridSqlElement exists = select.where(); + GridSqlSubquery subqry = exists.child(); + GridSqlSelect subSelect = (GridSqlSelect)subqry.select(); + + GridSqlColumn p1AddrIdCol = (GridSqlColumn)select.column(2); + + assertEquals("ADDRID", p1AddrIdCol.column().getName()); + assertSame(tbl2Alias, p1AddrIdCol.expressionInFrom()); + + GridSqlColumn p1AddrIdColExists = subSelect.where().child(1); + assertEquals("ADDRID", p1AddrIdCol.column().getName()); + + assertSame(tbl2Alias, p1AddrIdColExists.expressionInFrom()); + } + /** */ public void testParseMerge() throws Exception { /* Plain rows w/functions, operators, defaults, and placeholders. */ @@ -378,7 +436,7 @@ private JdbcConnection connection() throws Exception { IgniteH2Indexing idx = U.field(qryProcessor, "idx"); - return (JdbcConnection)idx.connectionForSpace(null); + return (JdbcConnection)idx.connectionForSpace(TEST_CACHE); } /** @@ -455,6 +513,9 @@ public static class Person implements Serializable { @QuerySqlField(index = true) public int addrId; + @QuerySqlField + public Integer[] addrIds; + @QuerySqlField(index = true) public int old; }