Repository: ignite Updated Branches: refs/heads/master 2e93148fa -> 1a9c942b4
IGNITE-5397: Fixed execution of queries without cache and without tables. This closes #2075. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/1a9c942b Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/1a9c942b Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/1a9c942b Branch: refs/heads/master Commit: 1a9c942b47a05e8dfaf4f3fd979a6ee94fda4e80 Parents: 2e93148 Author: devozerov <[email protected]> Authored: Sat Jun 3 17:52:31 2017 +0300 Committer: devozerov <[email protected]> Committed: Sat Jun 3 17:52:31 2017 +0300 ---------------------------------------------------------------------- .../processors/query/GridQueryProcessor.java | 2 +- .../processors/query/h2/IgniteH2Indexing.java | 19 +- .../query/h2/twostep/GridMapQueryExecutor.java | 75 +++--- .../h2/twostep/GridReduceQueryExecutor.java | 28 +-- .../h2/twostep/msg/GridH2QueryRequest.java | 37 ++- .../cache/IgniteCacheAbstractQuerySelfTest.java | 4 + .../query/SqlPublicSchemaSelfTest.java | 198 --------------- .../processors/query/SqlSchemaSelfTest.java | 238 +++++++++++++++++++ .../IgniteCacheQuerySelfTestSuite.java | 5 +- 9 files changed, 349 insertions(+), 257 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/1a9c942b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java index 62d826d..e729af5 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java @@ -1810,7 +1810,7 @@ public class GridQueryProcessor extends GridProcessorAdapter { throw new IgniteException("Local query is not supported without specific cache."); if (qry.getSchema() == null) - throw new IgniteException("Query schema is not set."); + qry.setSchema(QueryUtils.DFLT_SCHEMA); if (!busyLock.enterBusy()) throw new IllegalStateException("Failed to execute query (grid is stopping)."); http://git-wip-us.apache.org/repos/asf/ignite/blob/1a9c942b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java index 12addbd..53201a7 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java @@ -1356,15 +1356,16 @@ public class IgniteH2Indexing implements GridQueryIndexing { } if (caches0.isEmpty()) - throw new IgniteSQLException("Failed to find at least one cache for SQL statement: " + sqlQry); - - //Prohibit usage indices with different numbers of segments in same query. - List<Integer> cacheIds = new ArrayList<>(caches0); - - checkCacheIndexSegmentation(cacheIds); - - twoStepQry.cacheIds(cacheIds); - twoStepQry.local(qry.isLocal()); + twoStepQry.local(true); + else { + //Prohibit usage indices with different numbers of segments in same query. + List<Integer> cacheIds = new ArrayList<>(caches0); + + checkCacheIndexSegmentation(cacheIds); + + twoStepQry.cacheIds(cacheIds); + twoStepQry.local(qry.isLocal()); + } meta = H2Utils.meta(stmt.getMetaData()); } http://git-wip-us.apache.org/repos/asf/ignite/blob/1a9c942b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridMapQueryExecutor.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridMapQueryExecutor.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridMapQueryExecutor.java index 23504ad..aa97197 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridMapQueryExecutor.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridMapQueryExecutor.java @@ -81,6 +81,7 @@ import org.apache.ignite.plugin.extensions.communication.Message; import org.h2.jdbc.JdbcResultSet; import org.h2.result.ResultInterface; import org.h2.value.Value; +import org.jetbrains.annotations.Nullable; import org.jsr166.ConcurrentHashMap8; import static org.apache.ignite.events.EventType.EVT_CACHE_QUERY_EXECUTED; @@ -277,13 +278,16 @@ public class GridMapQueryExecutor { * @throws IgniteCheckedException If failed. */ private boolean reservePartitions( - List<Integer> cacheIds, + @Nullable List<Integer> cacheIds, AffinityTopologyVersion topVer, final int[] explicitParts, List<GridReservable> reserved ) throws IgniteCheckedException { assert topVer != null; + if (F.isEmpty(cacheIds)) + return true; + Collection<Integer> partIds = wrap(explicitParts); for (int i = 0; i < cacheIds.size(); i++) { @@ -433,8 +437,6 @@ public class GridMapQueryExecutor { final int[] parts = qryParts == null ? partsMap == null ? null : partsMap.get(ctx.localNodeId()) : qryParts; - assert !F.isEmpty(req.caches()); - final DistributedJoinMode joinMode = distributedJoinMode( req.isFlagSet(GridH2QueryRequest.FLAG_IS_LOCAL), req.isFlagSet(GridH2QueryRequest.FLAG_DISTRIBUTED_JOINS)); @@ -443,12 +445,16 @@ public class GridMapQueryExecutor { final boolean explain = req.isFlagSet(GridH2QueryRequest.FLAG_EXPLAIN); final boolean replicated = req.isFlagSet(GridH2QueryRequest.FLAG_REPLICATED); - int segments = explain || replicated ? 1 : - findFirstPartitioned(req.caches()).config().getQueryParallelism(); + final List<Integer> cacheIds = req.caches(); + + int segments = explain || replicated || F.isEmpty(cacheIds) ? 1 : + findFirstPartitioned(cacheIds).config().getQueryParallelism(); final Object[] params = req.parameters(); for (int i = 1; i < segments; i++) { + assert !F.isEmpty(cacheIds); + final int segment = i; ctx.closure().callLocal( @@ -457,8 +463,9 @@ public class GridMapQueryExecutor { onQueryRequest0(node, req.requestId(), segment, + req.schemaName(), req.queries(), - req.caches(), + cacheIds, req.topologyVersion(), partsMap, parts, @@ -466,7 +473,7 @@ public class GridMapQueryExecutor { req.pageSize(), joinMode, enforceJoinOrder, - replicated, + false, req.timeout(), params); @@ -479,8 +486,9 @@ public class GridMapQueryExecutor { onQueryRequest0(node, req.requestId(), 0, + req.schemaName(), req.queries(), - req.caches(), + cacheIds, req.topologyVersion(), partsMap, parts, @@ -497,6 +505,7 @@ public class GridMapQueryExecutor { * @param node Node authored request. * @param reqId Request ID. * @param segmentId index segment ID. + * @param schemaName Schema name. * @param qrys Queries to execute. * @param cacheIds Caches which will be affected by these queries. * @param topVer Topology version. @@ -510,6 +519,7 @@ public class GridMapQueryExecutor { ClusterNode node, long reqId, int segmentId, + String schemaName, Collection<GridCacheSqlQuery> qrys, List<Integer> cacheIds, AffinityTopologyVersion topVer, @@ -524,10 +534,8 @@ public class GridMapQueryExecutor { Object[] params ) { // Prepare to run queries. - GridCacheContext<?, ?> mainCctx = ctx.cache().context().cacheContext(cacheIds.get(0)); - - if (mainCctx == null) - throw new CacheException("Failed to find cache."); + GridCacheContext<?, ?> mainCctx = + !F.isEmpty(cacheIds) ? ctx.cache().context().cacheContext(cacheIds.get(0)) : null; NodeResults nodeRess = resultsForNode(node.id()); @@ -545,7 +553,7 @@ public class GridMapQueryExecutor { } } - qr = new QueryResults(reqId, qrys.size(), mainCctx); + qr = new QueryResults(reqId, qrys.size(), mainCctx != null ? mainCctx.name() : null); if (nodeRess.put(reqId, segmentId, qr) != null) throw new IllegalStateException(); @@ -579,8 +587,6 @@ public class GridMapQueryExecutor { } } - String schemaName = h2.schema(mainCctx.name()); - Connection conn = h2.connectionForSchema(schemaName); H2Utils.setupConnection(conn, distributedJoinMode != OFF, enforceJoinOrder); @@ -602,7 +608,7 @@ public class GridMapQueryExecutor { // Run queries. int qryIdx = 0; - boolean evt = ctx.event().isRecordable(EVT_CACHE_QUERY_EXECUTED); + boolean evt = mainCctx != null && ctx.event().isRecordable(EVT_CACHE_QUERY_EXECUTED); for (GridCacheSqlQuery qry : qrys) { ResultSet rs = null; @@ -616,6 +622,8 @@ public class GridMapQueryExecutor { qr.cancels[qryIdx]); if (evt) { + assert mainCctx != null; + ctx.event().record(new CacheQueryExecutedEvent<>( node, "SQL query executed.", @@ -961,7 +969,7 @@ public class GridMapQueryExecutor { private final GridQueryCancel[] cancels; /** */ - private final GridCacheContext<?,?> cctx; + private final String cacheName; /** */ private volatile boolean canceled; @@ -969,12 +977,12 @@ public class GridMapQueryExecutor { /** * @param qryReqId Query request ID. * @param qrys Number of queries. - * @param cctx Cache context. + * @param cacheName Cache name. */ @SuppressWarnings("unchecked") - private QueryResults(long qryReqId, int qrys, GridCacheContext<?, ?> cctx) { + private QueryResults(long qryReqId, int qrys, @Nullable String cacheName) { this.qryReqId = qryReqId; - this.cctx = cctx; + this.cacheName = cacheName; results = new AtomicReferenceArray<>(qrys); cancels = new GridQueryCancel[qrys]; @@ -998,7 +1006,7 @@ public class GridMapQueryExecutor { * @param rs Result set. */ void addResult(int qry, GridCacheSqlQuery q, UUID qrySrcNodeId, ResultSet rs, Object[] params) { - if (!results.compareAndSet(qry, null, new QueryResult(rs, cctx, qrySrcNodeId, q, params))) + if (!results.compareAndSet(qry, null, new QueryResult(rs, ctx, cacheName, qrySrcNodeId, q, params))) throw new IllegalStateException(); } @@ -1054,8 +1062,11 @@ public class GridMapQueryExecutor { /** */ private final ResultSet rs; + /** Kernal context. */ + private final GridKernalContext ctx; + /** */ - private final GridCacheContext<?,?> cctx; + private final String cacheName; /** */ private final GridCacheSqlQuery qry; @@ -1083,18 +1094,20 @@ public class GridMapQueryExecutor { /** * @param rs Result set. - * @param cctx Cache context. + * @param ctx Kernal context. + * @param cacheName Cache name. * @param qrySrcNodeId Query source node. * @param qry Query. * @param params Query params. */ - private QueryResult(ResultSet rs, GridCacheContext<?, ?> cctx, UUID qrySrcNodeId, GridCacheSqlQuery qry, - Object[] params) { - this.cctx = cctx; + private QueryResult(ResultSet rs, GridKernalContext ctx, @Nullable String cacheName, + UUID qrySrcNodeId, GridCacheSqlQuery qry, Object[] params) { + this.ctx = ctx; + this.cacheName = cacheName; this.qry = qry; this.params = params; this.qrySrcNodeId = qrySrcNodeId; - this.cpNeeded = cctx.isLocalNode(qrySrcNodeId); + this.cpNeeded = F.eq(ctx.localNodeId(), qrySrcNodeId); if (rs != null) { this.rs = rs; @@ -1127,7 +1140,7 @@ public class GridMapQueryExecutor { if (closed) return true; - boolean readEvt = cctx.gridEvents().isRecordable(EVT_CACHE_QUERY_OBJECT_READ); + boolean readEvt = cacheName != null && ctx.event().isRecordable(EVT_CACHE_QUERY_OBJECT_READ); page++; @@ -1163,12 +1176,12 @@ public class GridMapQueryExecutor { assert row != null; if (readEvt) { - cctx.gridEvents().record(new CacheQueryReadEvent<>( - cctx.localNode(), + ctx.event().record(new CacheQueryReadEvent<>( + ctx.discovery().localNode(), "SQL fields query result set row read.", EVT_CACHE_QUERY_OBJECT_READ, CacheQueryType.SQL.name(), - cctx.name(), + cacheName, null, qry.query(), null, http://git-wip-us.apache.org/repos/asf/ignite/blob/1a9c942b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java index 2d12635..f05fa5b 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java @@ -711,20 +711,20 @@ public class GridReduceQueryExecutor { if (isReplicatedOnly) flags |= GridH2QueryRequest.FLAG_REPLICATED; - if (send(nodes, - new GridH2QueryRequest() - .requestId(qryReqId) - .topologyVersion(topVer) - .pageSize(r.pageSize()) - .caches(qry.cacheIds()) - .tables(distributedJoins ? qry.tables() : null) - .partitions(convert(partsMap)) - .queries(mapQrys) - .parameters(params) - .flags(flags) - .timeout(timeoutMillis), - parts == null ? null : new ExplicitPartitionsSpecializer(qryMap), - false)) { + GridH2QueryRequest req = new GridH2QueryRequest() + .requestId(qryReqId) + .topologyVersion(topVer) + .pageSize(r.pageSize()) + .caches(qry.cacheIds()) + .tables(distributedJoins ? qry.tables() : null) + .partitions(convert(partsMap)) + .queries(mapQrys) + .parameters(params) + .flags(flags) + .timeout(timeoutMillis) + .schemaName(schemaName); + + if (send(nodes, req, parts == null ? null : new ExplicitPartitionsSpecializer(qryMap), false)) { awaitAllReplies(r, nodes, cancel); Object state = r.state(); http://git-wip-us.apache.org/repos/asf/ignite/blob/1a9c942b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/msg/GridH2QueryRequest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/msg/GridH2QueryRequest.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/msg/GridH2QueryRequest.java index beb1ae2..93a383c 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/msg/GridH2QueryRequest.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/msg/GridH2QueryRequest.java @@ -125,6 +125,9 @@ public class GridH2QueryRequest implements Message, GridCacheQueryMarshallable { /** */ private byte[] paramsBytes; + /** Schema name. */ + private String schemaName; + /** * Required by {@link Externalizable} */ @@ -148,6 +151,7 @@ public class GridH2QueryRequest implements Message, GridCacheQueryMarshallable { timeout = req.timeout; params = req.params; paramsBytes = req.paramsBytes; + schemaName = req.schemaName; } /** @@ -343,6 +347,23 @@ public class GridH2QueryRequest implements Message, GridCacheQueryMarshallable { return this; } + /** + * @return Schema name. + */ + public String schemaName() { + return schemaName; + } + + /** + * @param schemaName Schema name. + * @return {@code this}. + */ + public GridH2QueryRequest schemaName(String schemaName) { + this.schemaName = schemaName; + + return this; + } + /** {@inheritDoc} */ @Override public void marshall(Marshaller m) { if (paramsBytes != null) @@ -458,6 +479,12 @@ public class GridH2QueryRequest implements Message, GridCacheQueryMarshallable { return false; writer.incrementState(); + + case 11: + if (!writer.writeString("schemaName", schemaName)) + return false; + + writer.incrementState(); } return true; @@ -559,6 +586,14 @@ public class GridH2QueryRequest implements Message, GridCacheQueryMarshallable { return false; reader.incrementState(); + + case 11: + schemaName = reader.readString("schemaName"); + + if (!reader.isLastRead()) + return false; + + reader.incrementState(); } return reader.afterMessageRead(GridH2QueryRequest.class); @@ -571,7 +606,7 @@ public class GridH2QueryRequest implements Message, GridCacheQueryMarshallable { /** {@inheritDoc} */ @Override public byte fieldsCount() { - return 11; + return 12; } /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/ignite/blob/1a9c942b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractQuerySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractQuerySelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractQuerySelfTest.java index 9c06900..d35030a 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractQuerySelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractQuerySelfTest.java @@ -69,10 +69,14 @@ import org.apache.ignite.configuration.NearCacheConfiguration; import org.apache.ignite.events.CacheQueryExecutedEvent; import org.apache.ignite.events.CacheQueryReadEvent; import org.apache.ignite.events.Event; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.IgniteKernal; +import org.apache.ignite.internal.IgnitionEx; import org.apache.ignite.internal.binary.BinaryMarshaller; import org.apache.ignite.internal.processors.cache.distributed.replicated.IgniteCacheReplicatedQuerySelfTest; import org.apache.ignite.internal.processors.cache.query.QueryCursorEx; import org.apache.ignite.internal.processors.query.GridQueryFieldMetadata; +import org.apache.ignite.internal.processors.query.GridQueryProcessor; import org.apache.ignite.internal.util.lang.GridPlainCallable; import org.apache.ignite.internal.util.tostring.GridToStringExclude; import org.apache.ignite.internal.util.typedef.F; http://git-wip-us.apache.org/repos/asf/ignite/blob/1a9c942b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlPublicSchemaSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlPublicSchemaSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlPublicSchemaSelfTest.java deleted file mode 100644 index cbd8972..0000000 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlPublicSchemaSelfTest.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * 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; - -import org.apache.ignite.IgniteCache; -import org.apache.ignite.cache.query.SqlFieldsQuery; -import org.apache.ignite.cache.query.annotations.QuerySqlField; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.internal.IgniteEx; -import org.apache.ignite.internal.util.typedef.F; -import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; - -/** - * Tests for public schema. - */ -public class SqlPublicSchemaSelfTest extends GridCommonAbstractTest { - /** Person cache name. */ - private static final String CACHE_PERSON = "PersonCache"; - - /** Person cache 2 name. */ - private static final String CACHE_PERSON_2 = "PersonCache2"; - - /** Node. */ - private IgniteEx node; - - /** {@inheritDoc} */ - @Override protected void beforeTest() throws Exception { - node = (IgniteEx)startGrid(); - } - - /** {@inheritDoc} */ - @Override protected void afterTest() throws Exception { - stopAllGrids(); - } - - /** - * Test simple query. - * - * @throws Exception If failed. - */ - public void testSchemaChange() throws Exception { - IgniteCache<PersonKey, Person> cache = node.createCache(new CacheConfiguration<PersonKey, Person>() - .setName(CACHE_PERSON) - .setIndexedTypes(PersonKey.class, Person.class)); - - node.createCache(new CacheConfiguration<PersonKey, Person>() - .setName(CACHE_PERSON_2) - .setIndexedTypes(PersonKey.class, Person.class)); - - cache.put(new PersonKey(1), new Person("Vasya", 2)); - - // Normal calls. - assertEquals(1, cache.query( - new SqlFieldsQuery("SELECT id, name, orgId FROM Person") - ).getAll().size()); - - assertEquals(1, cache.query( - new SqlFieldsQuery("SELECT id, name, orgId FROM Person").setSchema(CACHE_PERSON) - ).getAll().size()); - - assertEquals(1, cache.query( - new SqlFieldsQuery("SELECT id, name, orgId FROM \"PersonCache\".Person") - ).getAll().size()); - - // Call from default schema. - assertEquals(1, cache.query( - new SqlFieldsQuery("SELECT id, name, orgId FROM \"PersonCache\".Person").setSchema(QueryUtils.DFLT_SCHEMA) - ).getAll().size()); - - // Call from another schema. - assertEquals(1, cache.query( - new SqlFieldsQuery("SELECT id, name, orgId FROM \"PersonCache\".Person").setSchema(CACHE_PERSON_2) - ).getAll().size()); - } - - /** - * Test simple query. - * - * @throws Exception If failed. - */ - public void testSchemaChangeOnCacheWithPublicSchema() throws Exception { - IgniteCache<PersonKey, Person> cache = node.createCache(new CacheConfiguration<PersonKey, Person>() - .setName(CACHE_PERSON) - .setIndexedTypes(PersonKey.class, Person.class) - .setSqlSchema(QueryUtils.DFLT_SCHEMA)); - - node.createCache(new CacheConfiguration<PersonKey, Person>() - .setName(CACHE_PERSON_2) - .setIndexedTypes(PersonKey.class, Person.class)); - - cache.put(new PersonKey(1), new Person("Vasya", 2)); - - // Normal calls. - assertEquals(1, cache.query( - new SqlFieldsQuery("SELECT id, name, orgId FROM Person") - ).getAll().size()); - - assertEquals(1, cache.query( - new SqlFieldsQuery("SELECT id, name, orgId FROM Person").setSchema(QueryUtils.DFLT_SCHEMA) - ).getAll().size()); - - // Call from another schema. - assertEquals(1, cache.query( - new SqlFieldsQuery("SELECT id, name, orgId FROM public.Person").setSchema(CACHE_PERSON_2) - ).getAll().size()); - - assertEquals(1, cache.query( - new SqlFieldsQuery("SELECT id, name, orgId FROM \"PUBLIC\".Person").setSchema(CACHE_PERSON_2) - ).getAll().size()); - } - - /** - * Test type conflict in public schema. - * - * @throws Exception If failed. - */ - public void _testTypeConflictInPublicSchema() throws Exception { - // TODO: IGNITE-5380: uncomment work after fix. - fail("Hang for now, need to fix"); - - node.createCache(new CacheConfiguration<PersonKey, Person>() - .setName(CACHE_PERSON) - .setIndexedTypes(PersonKey.class, Person.class) - .setSqlSchema(QueryUtils.DFLT_SCHEMA)); - - node.createCache(new CacheConfiguration<PersonKey, Person>() - .setName(CACHE_PERSON_2) - .setIndexedTypes(PersonKey.class, Person.class) - .setSqlSchema(QueryUtils.DFLT_SCHEMA)); - } - - /** - * Person key. - */ - public static class PersonKey { - @QuerySqlField - public long id; - - /** - * Constructor. - * - * @param id ID. - */ - PersonKey(long id) { - this.id = id; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - return (int)id; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object obj) { - return obj != null && obj instanceof PersonKey && (F.eq(id, ((PersonKey)obj).id)); - } - } - - /** - * Person. - */ - public static class Person { - /** Name. */ - @QuerySqlField - public String name; - - /** Organization ID. */ - @QuerySqlField(index = true) - public long orgId; - - /** - * Constructor. - * - * @param name Name. - * @param orgId Orgainzation ID. - */ - public Person(String name, long orgId) { - this.name = name; - this.orgId = orgId; - } - } - -} http://git-wip-us.apache.org/repos/asf/ignite/blob/1a9c942b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSchemaSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSchemaSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSchemaSelfTest.java new file mode 100644 index 0000000..32636fa --- /dev/null +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlSchemaSelfTest.java @@ -0,0 +1,238 @@ +/* + * 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; + +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.query.SqlFieldsQuery; +import org.apache.ignite.cache.query.annotations.QuerySqlField; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; + +import java.util.List; + +/** + * Tests for schemas. + */ +public class SqlSchemaSelfTest extends GridCommonAbstractTest { + /** Person cache name. */ + private static final String CACHE_PERSON = "PersonCache"; + + /** Person cache 2 name. */ + private static final String CACHE_PERSON_2 = "PersonCache2"; + + /** Node. */ + private IgniteEx node; + + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + node = (IgniteEx)startGrid(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + } + + /** + * Test query without caches. + * + * @throws Exception If failed. + */ + public void testQueryWithoutCacheOnPublicSchema() throws Exception { + GridQueryProcessor qryProc = node.context().query(); + + SqlFieldsQuery qry = new SqlFieldsQuery("SELECT 1").setSchema("PUBLIC"); + + List<List<?>> res = qryProc.querySqlFieldsNoCache(qry, true).getAll(); + + assertEquals(1, res.size()); + assertEquals(1, res.get(0).size()); + assertEquals(1, res.get(0).get(0)); + } + + /** + * Test query without caches. + * + * @throws Exception If failed. + */ + public void testQueryWithoutCacheOnCacheSchema() throws Exception { + node.createCache(new CacheConfiguration<PersonKey, Person>() + .setName(CACHE_PERSON) + .setIndexedTypes(PersonKey.class, Person.class)); + + GridQueryProcessor qryProc = node.context().query(); + + SqlFieldsQuery qry = new SqlFieldsQuery("SELECT 1").setSchema(CACHE_PERSON); + + List<List<?>> res = qryProc.querySqlFieldsNoCache(qry, true).getAll(); + + assertEquals(1, res.size()); + assertEquals(1, res.get(0).size()); + assertEquals(1, res.get(0).get(0)); + } + + /** + * Test simple query. + * + * @throws Exception If failed. + */ + public void testSchemaChange() throws Exception { + IgniteCache<PersonKey, Person> cache = node.createCache(new CacheConfiguration<PersonKey, Person>() + .setName(CACHE_PERSON) + .setIndexedTypes(PersonKey.class, Person.class)); + + node.createCache(new CacheConfiguration<PersonKey, Person>() + .setName(CACHE_PERSON_2) + .setIndexedTypes(PersonKey.class, Person.class)); + + cache.put(new PersonKey(1), new Person("Vasya", 2)); + + // Normal calls. + assertEquals(1, cache.query( + new SqlFieldsQuery("SELECT id, name, orgId FROM Person") + ).getAll().size()); + + assertEquals(1, cache.query( + new SqlFieldsQuery("SELECT id, name, orgId FROM Person").setSchema(CACHE_PERSON) + ).getAll().size()); + + assertEquals(1, cache.query( + new SqlFieldsQuery("SELECT id, name, orgId FROM \"PersonCache\".Person") + ).getAll().size()); + + // Call from default schema. + assertEquals(1, cache.query( + new SqlFieldsQuery("SELECT id, name, orgId FROM \"PersonCache\".Person").setSchema(QueryUtils.DFLT_SCHEMA) + ).getAll().size()); + + // Call from another schema. + assertEquals(1, cache.query( + new SqlFieldsQuery("SELECT id, name, orgId FROM \"PersonCache\".Person").setSchema(CACHE_PERSON_2) + ).getAll().size()); + } + + /** + * Test simple query. + * + * @throws Exception If failed. + */ + public void testSchemaChangeOnCacheWithPublicSchema() throws Exception { + IgniteCache<PersonKey, Person> cache = node.createCache(new CacheConfiguration<PersonKey, Person>() + .setName(CACHE_PERSON) + .setIndexedTypes(PersonKey.class, Person.class) + .setSqlSchema(QueryUtils.DFLT_SCHEMA)); + + node.createCache(new CacheConfiguration<PersonKey, Person>() + .setName(CACHE_PERSON_2) + .setIndexedTypes(PersonKey.class, Person.class)); + + cache.put(new PersonKey(1), new Person("Vasya", 2)); + + // Normal calls. + assertEquals(1, cache.query( + new SqlFieldsQuery("SELECT id, name, orgId FROM Person") + ).getAll().size()); + + assertEquals(1, cache.query( + new SqlFieldsQuery("SELECT id, name, orgId FROM Person").setSchema(QueryUtils.DFLT_SCHEMA) + ).getAll().size()); + + // Call from another schema. + assertEquals(1, cache.query( + new SqlFieldsQuery("SELECT id, name, orgId FROM public.Person").setSchema(CACHE_PERSON_2) + ).getAll().size()); + + assertEquals(1, cache.query( + new SqlFieldsQuery("SELECT id, name, orgId FROM \"PUBLIC\".Person").setSchema(CACHE_PERSON_2) + ).getAll().size()); + } + + /** + * Test type conflict in public schema. + * + * @throws Exception If failed. + */ + public void _testTypeConflictInPublicSchema() throws Exception { + // TODO: IGNITE-5380: uncomment work after fix. + fail("Hang for now, need to fix"); + + node.createCache(new CacheConfiguration<PersonKey, Person>() + .setName(CACHE_PERSON) + .setIndexedTypes(PersonKey.class, Person.class) + .setSqlSchema(QueryUtils.DFLT_SCHEMA)); + + node.createCache(new CacheConfiguration<PersonKey, Person>() + .setName(CACHE_PERSON_2) + .setIndexedTypes(PersonKey.class, Person.class) + .setSqlSchema(QueryUtils.DFLT_SCHEMA)); + } + + /** + * Person key. + */ + public static class PersonKey { + @QuerySqlField + public long id; + + /** + * Constructor. + * + * @param id ID. + */ + PersonKey(long id) { + this.id = id; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return (int)id; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object obj) { + return obj != null && obj instanceof PersonKey && (F.eq(id, ((PersonKey)obj).id)); + } + } + + /** + * Person. + */ + public static class Person { + /** Name. */ + @QuerySqlField + public String name; + + /** Organization ID. */ + @QuerySqlField(index = true) + public long orgId; + + /** + * Constructor. + * + * @param name Name. + * @param orgId Orgainzation ID. + */ + public Person(String name, long orgId) { + this.name = name; + this.orgId = orgId; + } + } + +} http://git-wip-us.apache.org/repos/asf/ignite/blob/1a9c942b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite.java b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite.java index 848ab49..fb6a86b 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite.java +++ b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite.java @@ -97,7 +97,6 @@ import org.apache.ignite.internal.processors.cache.index.DynamicIndexServerNodeF import org.apache.ignite.internal.processors.cache.index.DynamicIndexServerNodeFilterCoordinatorBasicSelfTest; import org.apache.ignite.internal.processors.cache.index.DynamicIndexServerBasicSelfTest; import org.apache.ignite.internal.processors.cache.index.H2DynamicTableSelfTest; -import org.apache.ignite.internal.processors.cache.index.QueryEntityValidationSelfTest; import org.apache.ignite.internal.processors.cache.index.SchemaExchangeSelfTest; import org.apache.ignite.internal.processors.cache.local.IgniteCacheLocalAtomicQuerySelfTest; import org.apache.ignite.internal.processors.cache.local.IgniteCacheLocalFieldsQuerySelfTest; @@ -115,7 +114,7 @@ import org.apache.ignite.internal.processors.query.IgniteSqlSchemaIndexingTest; import org.apache.ignite.internal.processors.query.IgniteSqlSegmentedIndexMultiNodeSelfTest; import org.apache.ignite.internal.processors.query.IgniteSqlSegmentedIndexSelfTest; import org.apache.ignite.internal.processors.query.IgniteSqlSplitterSelfTest; -import org.apache.ignite.internal.processors.query.SqlPublicSchemaSelfTest; +import org.apache.ignite.internal.processors.query.SqlSchemaSelfTest; import org.apache.ignite.internal.processors.query.h2.GridH2IndexingInMemSelfTest; import org.apache.ignite.internal.processors.query.h2.GridH2IndexingOffheapSelfTest; import org.apache.ignite.internal.processors.query.h2.IgniteSqlQueryMinMaxTest; @@ -137,7 +136,7 @@ public class IgniteCacheQuerySelfTestSuite extends TestSuite { public static TestSuite suite() throws Exception { IgniteTestSuite suite = new IgniteTestSuite("Ignite Cache Queries Test Suite"); - suite.addTestSuite(SqlPublicSchemaSelfTest.class); + suite.addTestSuite(SqlSchemaSelfTest.class); // Misc tests. // TODO: Enable when IGNITE-1094 is fixed.
