User: sgrimstad Date: 10 Oct 18 16:42 Revision: dbd16209f58ebdb281044466566487743782ab0b Summary: IGNITE-9771 implementation TeamCity URL: https://ci.ignite.apache.org/viewModification.html?tab=vcsModificationFiles&modId=834635&personal=false Index: modules/core/src/main/java/org/apache/ignite/cache/query/SqlQuery.java =================================================================== --- modules/core/src/main/java/org/apache/ignite/cache/query/SqlQuery.java (revision fe58835b14d0fe129affd024f76b984c093d5cd9) +++ modules/core/src/main/java/org/apache/ignite/cache/query/SqlQuery.java (revision dbd16209f58ebdb281044466566487743782ab0b) @@ -36,6 +36,9 @@ private static final long serialVersionUID = 0L; /** */ + private String cacheName; + + /** */ private String type; /** Table alias */ @@ -67,7 +70,19 @@ * @param sql SQL Query. */ public SqlQuery(String type, String sql) { + if (type != null) { + int pp = type.indexOf("."); + + if ( pp >= 0) { + setCacheName(type.substring(0, type.indexOf("."))); + + if ( pp < type.length()-1) + setType(type.substring(type.indexOf(".") + 1)); + } + else - setType(type); + setType(type); + } + setSql(sql); } @@ -83,6 +98,18 @@ } /** + * Constructs query for the given type and SQL query. + * + * @param type Type. + * @param sql SQL Query. + */ + public SqlQuery(String cacheName, Class type, String sql) { + setCacheName(cacheName); + setType(type); + setSql(sql); + } + + /** * Gets SQL clause. * * @return SQL clause. @@ -127,6 +154,23 @@ } /** + * Gets cache name for query. + * + * @return Type. + */ + public String getCacheName() { + return cacheName; + } + + /** + * Sets cache name for query. + * @param cacheName + */ + public void setCacheName(String cacheName) { + this.cacheName = cacheName; + } + + /** * Gets type for query. * * @return Type. @@ -205,6 +249,8 @@ * @return {@code this} For chaining. */ public SqlQuery setType(Class type) { + A.notNull(type, "type"); + return setType(QueryUtils.typeName(type)); } @@ -280,4 +326,4 @@ @Override public String toString() { return S.toString(SqlQuery.class, this); } -} \ No newline at end of file +} Index: modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java =================================================================== --- modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java (revision fe58835b14d0fe129affd024f76b984c093d5cd9) +++ modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java (revision dbd16209f58ebdb281044466566487743782ab0b) @@ -2284,12 +2284,12 @@ } /** - * @param cctx Cache context. + * @param pcctx Cache context. * @param qry Query. * @param keepBinary Keep binary flag. * @return Cursor. */ - private QueryCursor> queryDistributedSql(final GridCacheContext cctx, + private QueryCursor> queryDistributedSql(final GridCacheContext pcctx, final SqlQuery qry, final boolean keepBinary) { checkxEnabled(); @@ -2297,8 +2297,18 @@ throw new IllegalStateException("Failed to execute query (grid is stopping)."); try { - final String schemaName = idx.schema(cctx.name()); + final GridCacheContext cctx; + if (F.isEmpty(qry.getCacheName())) { + qry.setCacheName(pcctx.name()); + + cctx = pcctx; + } + else + cctx = this.ctx.cache().context().cacheContext(CU.cacheId(qry.getCacheName())); + + final String schemaName = idx.schema(qry.getCacheName()); + return executeQuery(GridCacheQueryType.SQL, qry.getSql(), cctx, new IgniteOutClosureX>>() { @Override public QueryCursor> applyx() throws IgniteCheckedException { @@ -2315,18 +2325,28 @@ } /** - * @param cctx Cache context. + * @param pcctx Cache context parameter. * @param qry Query. * @param keepBinary Keep binary flag. * @return Cursor. */ - private QueryCursor> queryLocalSql(final GridCacheContext cctx, final SqlQuery qry, + private QueryCursor> queryLocalSql(final GridCacheContext pcctx, final SqlQuery qry, final boolean keepBinary) { if (!busyLock.enterBusy()) throw new IllegalStateException("Failed to execute query (grid is stopping)."); - final String schemaName = idx.schema(cctx.name()); + final GridCacheContext cctx; + if (F.isEmpty(qry.getCacheName())) { + qry.setCacheName(pcctx.name()); + + cctx = pcctx; + } + else + cctx = this.ctx.cache().context().cacheContext(CU.cacheId(qry.getCacheName())); + + final String schemaName = idx.schema(qry.getCacheName()); + try { return executeQuery(GridCacheQueryType.SQL, qry.getSql(), cctx, new IgniteOutClosureX>>() { Index: modules/core/src/test/java/org/apache/ignite/cache/query/SqlQuerySelfTest.java =================================================================== --- modules/core/src/test/java/org/apache/ignite/cache/query/SqlQuerySelfTest.java (revision dbd16209f58ebdb281044466566487743782ab0b) +++ modules/core/src/test/java/org/apache/ignite/cache/query/SqlQuerySelfTest.java (revision dbd16209f58ebdb281044466566487743782ab0b) @@ -0,0 +1,115 @@ +/* + * 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.cache.query; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * {@link SqlQuery} unit tests. + */ +public class SqlQuerySelfTest { + /** */ + private final String TYPE = "String"; + + /** */ + private final Class TYPE_CL = String.class; + + /** */ + private final String QRY = "query"; + + /** */ + private final String CACHE_NAME = "A1"; + + /** */ + private final Class NULL_CL = null; + + /** */ + @Test(expected = NullPointerException.class) + public void testFailedConstructionStringParams(){ + new SqlQuery<>(TYPE, null); + } + + /** */ + @Test(expected = NullPointerException.class) + public void testFailedConstructionClassParams(){ + new SqlQuery<>(TYPE_CL, null); + } + + @Test(expected = NullPointerException.class) + public void testFailedConstructionClassParams2() { + new SqlQuery<>(NULL_CL, QRY); + } + + /** */ + @Test(expected = NullPointerException.class) + public void testFailedConstructionTreeParams(){ + new SqlQuery<>(CACHE_NAME, TYPE_CL, null); + } + + /** */ + @Test(expected = NullPointerException.class) + public void testFailedConstructionTreeParams2(){ + new SqlQuery<>(CACHE_NAME, null, QRY); + } + + /** */ + @Test + public void testConstructionStringParams(){ + final String nullStr = null; + checkQuery( new SqlQuery<>("", QRY), null, "", QRY); + checkQuery( new SqlQuery<>(TYPE, ""), null, TYPE, ""); + checkQuery( new SqlQuery<>(nullStr, QRY), null, null, QRY); + checkQuery( new SqlQuery<>(TYPE, QRY), null, TYPE, QRY); + checkQuery( new SqlQuery<>("."+ TYPE, QRY), "", TYPE, QRY); + checkQuery( new SqlQuery<>(CACHE_NAME+"."+ TYPE, QRY), CACHE_NAME, TYPE, QRY); + checkQuery( new SqlQuery<>(CACHE_NAME+".", QRY), CACHE_NAME, null, QRY); + } + + /** */ + @Test + public void testConstructionClassParams() { + checkQuery( new SqlQuery<>(TYPE_CL, ""), null, TYPE, ""); + checkQuery( new SqlQuery<>(TYPE_CL, QRY), null, TYPE, QRY); + } + + /** */ + @Test + public void testConstructionTreeParams(){ + checkQuery( new SqlQuery<>(CACHE_NAME, TYPE_CL, ""), CACHE_NAME, TYPE, ""); + checkQuery( new SqlQuery<>(null, TYPE_CL, QRY), null, TYPE, QRY); + checkQuery( new SqlQuery<>("", TYPE_CL, QRY), "", TYPE, QRY); + checkQuery( new SqlQuery<>(CACHE_NAME, TYPE_CL, QRY), CACHE_NAME, TYPE, QRY); + } + + /** + * + * @param q Created query. + * @param cacheName Expected cache name. + * @param type Expected type. + * @param sql Expected sql. + */ + private void checkQuery(SqlQuery q, String cacheName, String type, String sql){ + assertEquals(cacheName, q.getCacheName()); + + assertEquals(type, q.getType()); + + assertEquals(sql, q.getSql()); + } +} Index: modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/metrics/CacheAbstractQueryDetailMetricsSelfTest.java =================================================================== --- modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/metrics/CacheAbstractQueryDetailMetricsSelfTest.java (revision fe58835b14d0fe129affd024f76b984c093d5cd9) +++ modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/metrics/CacheAbstractQueryDetailMetricsSelfTest.java (revision dbd16209f58ebdb281044466566487743782ab0b) @@ -336,12 +336,11 @@ IgniteCache cache = grid(0).context().cache().jcache("A"); IgniteCache cache2 = grid(0).context().cache().jcache("B"); - SqlQuery qry = new SqlQuery<>(Long.class, "Select * from \"B\".Long"); + SqlQuery qry = new SqlQuery<>("B", Long.class, "Select * from Long"); qry.setPageSize(10); - //todo: Enable after fix of IGNITE-9771 - //checkQueryNotFullyFetchedMetrics(cache, cache2, true, qry); + checkQueryNotFullyFetchedMetrics(cache, cache2, true, qry); } /** Index: modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/metrics/CachePartitionedQueryDetailMetricsDistributedSelfTest.java =================================================================== --- modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/metrics/CachePartitionedQueryDetailMetricsDistributedSelfTest.java (revision fe58835b14d0fe129affd024f76b984c093d5cd9) +++ modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/metrics/CachePartitionedQueryDetailMetricsDistributedSelfTest.java (revision dbd16209f58ebdb281044466566487743782ab0b) @@ -17,7 +17,10 @@ package org.apache.ignite.internal.processors.cache.metrics; +import org.apache.ignite.configuration.CacheConfiguration; + import static org.apache.ignite.cache.CacheMode.PARTITIONED; +import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC; /** * Tests for partitioned distributed cache query metrics. @@ -30,4 +33,21 @@ super.beforeTest(); } + + /** + * @param cacheName Cache name. + * @return Cache configuration. + */ + protected CacheConfiguration configureCache2(String cacheName) { + CacheConfiguration ccfg = defaultCacheConfiguration(); + + ccfg.setName(cacheName); + ccfg.setCacheMode(cacheMode); + ccfg.setWriteSynchronizationMode(FULL_SYNC); + ccfg.setIndexedTypes(Integer.class, Long.class); + ccfg.setStatisticsEnabled(true); + ccfg.setQueryDetailMetricsSize(QRY_DETAIL_METRICS_SIZE); + + return ccfg; -} + } +} Index: modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/metrics/CachePartitionedQueryDetailMetricsLocalSelfTest.java =================================================================== --- modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/metrics/CachePartitionedQueryDetailMetricsLocalSelfTest.java (revision fe58835b14d0fe129affd024f76b984c093d5cd9) +++ modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/metrics/CachePartitionedQueryDetailMetricsLocalSelfTest.java (revision dbd16209f58ebdb281044466566487743782ab0b) @@ -17,7 +17,10 @@ package org.apache.ignite.internal.processors.cache.metrics; +import org.apache.ignite.configuration.CacheConfiguration; + import static org.apache.ignite.cache.CacheMode.PARTITIONED; +import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC; /** * Tests for partitioned local cache query metrics. @@ -30,4 +33,21 @@ super.beforeTest(); } + + /** + * @param cacheName Cache name. + * @return Cache configuration. + */ + protected CacheConfiguration configureCache2(String cacheName) { + CacheConfiguration ccfg = defaultCacheConfiguration(); + + ccfg.setName(cacheName); + ccfg.setCacheMode(cacheMode); + ccfg.setWriteSynchronizationMode(FULL_SYNC); + ccfg.setIndexedTypes(Integer.class, Long.class); + ccfg.setStatisticsEnabled(true); + ccfg.setQueryDetailMetricsSize(QRY_DETAIL_METRICS_SIZE); + + return ccfg; -} + } +} Index: modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteSqlQueryTestSuite.java =================================================================== --- modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteSqlQueryTestSuite.java (revision dbd16209f58ebdb281044466566487743782ab0b) +++ modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteSqlQueryTestSuite.java (revision dbd16209f58ebdb281044466566487743782ab0b) @@ -0,0 +1,33 @@ +/* + * 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.testsuites; + +import org.apache.ignite.cache.query.SqlQuerySelfTest; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * Unit tests for SQL query. + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + SqlQuerySelfTest.class +}) +public class IgniteSqlQueryTestSuite { + //no op +}