/** * set keepSerialized flag for remote queries of type 'select *' having independent operators */ void keepResultsSerialized(CompiledSelect cs, ExecutionContext context) { if (isRemoteQuery()) { // for dependent iterators, deserialization is required if (cs.getIterators().size() == context.getAllIndependentIteratorsOfCurrentScope().size() && cs.getWhereClause() == null && cs.getProjectionAttributes() == null && !cs.isDistinct() && cs.getOrderByAttrs() == null) { setKeepSerialized(); } } }
private Collection coalesceOrderedResults(Collection<Collection> results, ExecutionContext context, CompiledSelect cs, int limit) { List<Collection> sortedResults = new ArrayList<Collection>(results.size()); // TODO :Asif : Deal with UNDEFINED for (Object o : results) { if (o instanceof Collection) { sortedResults.add((Collection) o); } } return new NWayMergeResults(sortedResults, cs.isDistinct(), limit, cs.getOrderByAttrs(), context, cs.getElementTypeForOrderByQueries()); }
/** * @return int identifying the limit. A value of -1 indicates that no limit is imposed or the * query is not a select query */ public int getLimit(Object[] bindArguments) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException { return this.compiledQuery instanceof CompiledSelect ? ((CompiledSelect) this.compiledQuery).getLimitValue(bindArguments) : -1; }
CompiledSelect cs = q.getSimpleSelect(); List<CompiledSortCriterion> orderByAttribs = null; if (cs.getType() == CompiledValue.GROUP_BY_SELECT) { Field originalOrderByMethod = CompiledGroupBySelect.class.getDeclaredField("originalOrderByClause"); orderByAttribs = (List<CompiledSortCriterion>) originalOrderByMethod.get(cs); } else { orderByAttribs = cs.getOrderByAttrs(); ObjectType resultType = cs.getElementTypeForOrderByQueries(); ExecutionContext context = new ExecutionContext(null, CacheUtils.getCache()); final OrderByComparator obc = new OrderByComparator(orderByAttribs, resultType, context); if (cs.getType() == CompiledValue.GROUP_BY_SELECT) { Field limitCVField = CompiledGroupBySelect.class.getDeclaredField("limit"); limitCVField.setAccessible(true); limit = ((Integer) evaluateLimitMethod.invoke(null, context, limitCV)).intValue(); } else { limit = cs.getLimitValue(null);
cs = this.query.getSimpleSelect(); limit = this.query.getLimit(parameters); isDistinct = (cs != null) ? cs.isDistinct() : true; isCount = (cs != null) ? cs.isCount() : false; boolean isGroupByResults = cs.getType() == CompiledValue.GROUP_BY_SELECT; if (isGroupByResults) { SelectResults baseResults = null; } else { if (this.cumulativeResults.getCollectionType().isOrdered() && cs.getOrderByAttrs() != null) {
@Override @Test public void testConvertibleGroupByQuery_1() throws Exception { Region region = this.createRegion("portfolio", PortfolioPdx.class); for (int i = 1; i < 200; ++i) { PortfolioPdx pf = new PortfolioPdx(i); pf.shortID = (short) ((short) i / 5); region.put("key-" + i, pf); } String queryStr = "select p.status as status, p.ID from /portfolio p where p.ID > 0 group by status, p.ID "; QueryService qs = CacheUtils.getQueryService(); Query query = qs.newQuery(queryStr); CompiledSelect cs = ((DefaultQuery) query).getSelect(); assertTrue(cs.isDistinct()); assertTrue(cs.isOrderBy()); assertFalse(cs.isGroupBy()); }
SelectResults results = selectExpr.getEmptyResultSet(parameters, getCache(), query); if (selectExpr.isDistinct()) { if (selectExpr.getOrderByAttrs() != null) { results = new ResultsCollectionWrapper(elementType, results.asSet()); if (selectExpr.isCount() && (results.isEmpty() || selectExpr.isDistinct())) {
applyProjectionAndAddToResultSet(context, results, this.orderByAttrs == null); if (this.whereClause == null && iterators.size() == 1 && isCount() && !isDistinct() && sr instanceof QRegion) { QRegion qr = (QRegion) sr; QueryObserver observer = QueryObserverHolder.getInstance(); observer.beforeIterationEvaluation(rIter, currObj); numElementsInResult = doNestedIterations(level + 1, results, context, evaluateWhereClause, numElementsInResult); Integer limitValue = evaluateLimitValue(context, this.limit); if (this.orderByAttrs == null && limitValue > -1 && numElementsInResult == limitValue) { break;
if (sender.getVersionObject().compareTo(Version.GFE_90) < 0) { CompiledSelect cs = this.query.getSimpleSelect(); if (cs != null && cs.isOrderBy()) { sortNeeded = true; orderByAttribs = cs.getOrderByAttrs();
@Before public void setup() throws Exception { localNode = new InternalDistributedMember("localhost", 8888); remoteNodeA = new InternalDistributedMember("localhost", 8889); remoteNodeB = new InternalDistributedMember("localhost", 8890); cache = Fakes.cache(); system = (InternalDistributedSystem) cache.getDistributedSystem(); allNodes.add(localNode); allNodes.add(remoteNodeA); allNodes.add(remoteNodeB); pr = mock(PartitionedRegion.class); dataStore = new ExtendedPartitionedRegionDataStore(); CompiledSelect select = mock(CompiledSelect.class); when(select.getType()).thenReturn(CompiledValue.COMPARISON); when(select.getElementTypeForOrderByQueries()).thenReturn(new ObjectTypeImpl(String.class)); query = mock(DefaultQuery.class); when(query.getSimpleSelect()).thenReturn(select); when(query.getLimit(any())).thenReturn(-1); when(pr.getCachePerfStats()).thenReturn(mock(CachePerfStats.class)); when(pr.getMyId()).thenReturn(localNode); when(pr.getDataStore()).thenReturn(dataStore); when(pr.getCache()).thenReturn(cache); }
@Test public void testOrderByWithColumnAlias_Bug52041_1() throws Exception { Region region = createRegion("portfolio", Portfolio.class); for (int i = 1; i < 200; ++i) { Portfolio pf = new Portfolio(i); pf.shortID = (short) ((short) i / 5); pf.status = "active"; region.put("" + i, pf); } String queryStr = "select distinct p.status, p.shortID as short_id from /portfolio p where p.ID >= 0 " + "order by short_id asc"; QueryService qs = CacheUtils.getQueryService(); Query query = qs.newQuery(queryStr); SelectResults<Struct> results = (SelectResults<Struct>) query.execute(); Iterator<Struct> iter = results.asList().iterator(); int counter = 0; while (iter.hasNext()) { Struct str = iter.next(); assertEquals(counter, ((Short) str.get("short_id")).intValue()); ++counter; } assertEquals(39, counter - 1); CompiledSelect cs = ((DefaultQuery) query).getSimpleSelect(); List<CompiledSortCriterion> orderbyAtts = cs.getOrderByAttrs(); assertEquals(orderbyAtts.get(0).getColumnIndex(), 1); }
/** * Should be constructed from DefaultQueryService * * @see QueryService#newQuery */ public DefaultQuery(String queryString, InternalCache cache, boolean isForRemote) { this.queryString = queryString; QCompiler compiler = new QCompiler(); this.compiledQuery = compiler.compileQuery(queryString); CompiledSelect cs = getSimpleSelect(); if (cs != null && !isForRemote && (cs.isGroupBy() || cs.isOrderBy())) { QueryExecutionContext ctx = new QueryExecutionContext(null, cache); try { cs.computeDependencies(ctx); } catch (QueryException qe) { throw new QueryInvalidException("", qe); } } this.traceOn = compiler.isTraceRequested() || QUERY_VERBOSE; this.cache = cache; this.stats = new DefaultQueryStatistics(); this.cancelationTask = Optional.empty(); }
? ((DefaultQuery) context.getQuery()).getSelect().isDistinct() : false;
private void executeSequentially(Collection<Collection> resultCollector, List buckets) throws QueryException, InterruptedException, ForceReattemptException { ExecutionContext context = new QueryExecutionContext(this.parameters, this.pr.getCache(), this.query); CompiledSelect cs = this.query.getSimpleSelect(); int limit = this.query.getLimit(parameters); if (cs != null && cs.isOrderBy()) { for (Integer bucketID : this._bucketsToQuery) { List<Integer> singleBucket = Collections.singletonList(bucketID); context.setBucketList(singleBucket); executeQueryOnBuckets(resultCollector, context); } Collection mergedResults = coalesceOrderedResults(resultCollector, context, cs, limit); resultCollector.clear(); resultCollector.add(mergedResults); } else { context.setBucketList(buckets); executeQueryOnBuckets(resultCollector, context); } }
/** * Returns parameterized query used by the server. This method replaces Region name with $1 and if * type is not specified in the query, looks for type from cqattributes and appends into the * query. * * @return String modified query. */ private Query constructServerSideQuery() throws QueryException { InternalCache cache = cqService.getInternalCache(); DefaultQuery locQuery = (DefaultQuery) cache.getLocalQueryService().newQuery(this.queryString); CompiledSelect select = locQuery.getSimpleSelect(); CompiledIteratorDef from = (CompiledIteratorDef) select.getIterators().get(0); // WARNING: ASSUMES QUERY WAS ALREADY VALIDATED FOR PROPER "FORM" ON CLIENT; // THIS VALIDATION WILL NEED TO BE DONE ON THE SERVER FOR NATIVE CLIENTS, // BUT IS NOT DONE HERE FOR JAVA CLIENTS. // The query was already checked on the client that the sole iterator is a // CompiledRegion this.regionName = ((CompiledRegion) from.getCollectionExpr()).getRegionPath(); from.setCollectionExpr(new CompiledBindArgument(1)); return locQuery; }
@Override @Test public void testConvertibleGroupByQuery_1() throws Exception { Region region = this.createRegion("portfolio", Portfolio.class); for (int i = 1; i < 200; ++i) { Portfolio pf = new Portfolio(i); pf.shortID = (short) ((short) i / 5); region.put("" + i, pf); } String queryStr = "select p.status as status, p.ID from /portfolio p where p.ID > 0 group by status, p.ID "; QueryService qs = CacheUtils.getQueryService(); Query query = qs.newQuery(queryStr); CompiledSelect cs = ((DefaultQuery) query).getSelect(); assertTrue(cs.isDistinct()); assertTrue(cs.isOrderBy()); assertFalse(cs.isGroupBy()); }
@Test public void testOrderByWithColumnAlias_Bug52041_2() throws Exception { Region region = createRegion("portfolio", Portfolio.class); for (int i = 0; i < 200; ++i) { Portfolio pf = new Portfolio(i); pf.shortID = (short) ((short) i / 5); pf.status = "active"; region.put("" + i, pf); } String queryStr = "select distinct p.ID as _id, p.shortID as short_id from /portfolio p where p.ID >= 0 " + "order by short_id asc, p.ID desc"; QueryService qs = CacheUtils.getQueryService(); Query query = qs.newQuery(queryStr); SelectResults<Struct> results = (SelectResults<Struct>) query.execute(); Iterator<Struct> iter = results.asList().iterator(); int counter = 0; int k = 0; while (iter.hasNext()) { k = ((counter) / 5 + 1) * 5 - 1; Struct str = iter.next(); assertEquals(counter / 5, ((Short) str.get("short_id")).intValue()); assertEquals(k - (counter) % 5, ((Integer) str.get("_id")).intValue()); ++counter; } CompiledSelect cs = ((DefaultQuery) query).getSimpleSelect(); List<CompiledSortCriterion> orderbyAtts = cs.getOrderByAttrs(); assertEquals(orderbyAtts.get(0).getColumnIndex(), 1); assertEquals(orderbyAtts.get(1).getColumnIndex(), 0); }
&& ((DefaultQuery) context.getQuery()).getSelect().isDistinct()) { set = intermediateResults; intermediateResults = null;
if (select.isDistinct()) { throw new UnsupportedOperationException( "select DISTINCT queries not supported in CQ"); CompiledValue whereClause = select.getWhereClause(); if (whereClause != null) { whereClause.getRegionsInQuery(regions, parameters); List fromClause = select.getIterators(); List projs = select.getProjectionAttributes(); if (projs != null) { throw new UnsupportedOperationException( List orderBys = select.getOrderByAttrs(); if (orderBys != null) { throw new UnsupportedOperationException(