protected void modifyGroupByToOrderBy(boolean setDistinct, ExecutionContext context) throws TypeMismatchException, NameResolutionException { if (setDistinct) { this.distinct = setDistinct; } this.orderByAttrs = new ArrayList<CompiledSortCriterion>(this.groupBy.size()); int colIndex = 0; for (CompiledValue cv : this.groupBy) { CompiledSortCriterion csc = new CompiledSortCriterion(false, cv); csc.mapExpressionToProjectionField(projAttrs, context); this.orderByAttrs.add(csc); } this.groupBy = null; }
private Object[] calculateSortCriteria(ExecutionContext context, Object row) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException { if (orderByAttrs != null) { Object[] evaluatedResult = new Object[this.orderByAttrs.size()]; int index = 0; for (CompiledSortCriterion csc : orderByAttrs) { Object[] arr = new Object[2]; if (csc.getColumnIndex() == -1) { arr[0] = csc.evaluate(context); } else { arr[0] = csc.evaluate(row, context); } arr[1] = csc.getCriterion(); evaluatedResult[index++] = arr; } return evaluatedResult; } return null; } }
private void evalCanonicalizedExpressionForCSC(CompiledSortCriterion csc, ExecutionContext context, StringBuilder buffer) throws TypeMismatchException, NameResolutionException { csc.getExpr().generateCanonicalizedExpression(buffer, context); }
/** * This method evaluates sort criteria and returns an ArrayList of Object[] arrays of the * evaluated criteria. * * @param value the criteria to be evaluated. * @return an Object array of Object arrays of the evaluated criteria. */ protected Object[] evaluateSortCriteria(Object value) { Object[] array = null; if (orderByAttrs != null) { array = new Object[orderByAttrs.size()]; int i = 0; for (CompiledSortCriterion csc : orderByAttrs) { Object[] arr = {csc.evaluate(value, context), csc.getCriterion()}; array[i++] = arr; } } return array; }
if (applyOrderBy) { CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttribs.get(0); asc = !csc.getCriterion(); multiColOrderBy = orderByAttribs.size() > 1;
/** * Yogesh: compiles sort criteria present in order by clause and push into the stack * */ public void compileSortCriteria(String sortCriterion) { CompiledValue obj = (CompiledValue) this.stack.pop(); boolean criterion = false; if (sortCriterion.equals("desc")) criterion = true; CompiledSortCriterion csc = new CompiledSortCriterion(criterion, obj); push(csc); }
if (prj[0] != null && prj[0].equals(((CompiledID) expr).getId())) { this.substituteExpressionWithProjectionField(i); this.originalCorrectedExpression = (CompiledValue) prj[1]; mappedColumn = true; if (projAttribStr.equals(orderByExprStr)) { this.substituteExpressionWithProjectionField(i); mappedColumn = true; break; } else if (orderByExprStr.startsWith(projAttribStr)) { CompiledValue newExpr = getReconstructedExpression(projAttribStr, context); this.substituteExpression(newExpr, i); mappedColumn = true; break; String orderbyStr = temp.toString(); if (projAttribStr.equals(orderbyStr)) { this.substituteExpressionWithProjectionField(i); mappedColumn = true; break; } else { CompiledValue newExpr = getReconstructedExpression(projAttribStr, context); this.substituteExpression(newExpr, i); mappedColumn = true; break;
@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); }
private void mapOrderByColumns(ExecutionContext context) throws TypeMismatchException, NameResolutionException { if (this.orderByAttrs != null) { Iterator<CompiledSortCriterion> iter = this.orderByAttrs.iterator(); while (iter.hasNext()) { CompiledSortCriterion csc = iter.next(); // Ideally for replicated regions, the requirement that // projected columns should // contain order by fields ( directly or derivable on it), // is not needed. But for PR , the query gathers only projected // columns, so applying order by on the query node // will need order by values ( which we dont send). So this // restriction is needed. // Also if this restriction is assumed to be correct, then the order // by comparator can be optimized as // it does not need to keep the mapping of evaluated order by clause, // for comparison if (!csc.mapExpressionToProjectionField(this.projAttrs, context)) { this.hasUnmappedOrderByCols = true; } } } }
private Object getOrderByEvaluatedTuple(ExecutionContext context, boolean isOrderByTupleSingle, Object[] holder, Object data, boolean[] objectChangedMarker) { if (isOrderByTupleSingle) { return PDXUtils.convertPDX(this.orderByAttrs.get(0).evaluate(data, context), false, true, true, true, objectChangedMarker, false); } else { int i = 0; for (CompiledSortCriterion csc : this.orderByAttrs) { holder[i++] = PDXUtils.convertPDX(csc.evaluate(data, context), false, true, true, true, objectChangedMarker, false); } return holder; } }
if (orderBys != null) { for (CompiledSortCriterion orderBy : orderBys) { orderBy.getRegionsInQuery(regions, parameters); if (!regions.isEmpty()) { throw new UnsupportedOperationException(
new OrderByComparator(this.originalOrderByClause, elementType, context); newResults = new SortedStructBag(comparator, (StructType) elementType, !this.originalOrderByClause.get(0).getCriterion()); } else { newResults = new OrderByComparator(this.originalOrderByClause, elementType, context); newResults = new SortedResultsBag(comparator, elementType, !this.originalOrderByClause.get(0).getCriterion()); } else { newResults = QueryUtils.createResultCollection(this.isDistinct, elementType, context);
private NWayMergeResults<Struct> createStructFieldMergedResult( Collection<? extends Collection<Struct>> sortedResults, boolean isDistinct, int limit, StructTypeImpl structType) throws Exception { CompiledSortCriterion csc1 = new CompiledSortCriterion(false, CompiledSortCriterion.ProjectionField.getProjectionField()); CompiledSortCriterion csc2 = new CompiledSortCriterion(true, CompiledSortCriterion.ProjectionField.getProjectionField()); Method method = CompiledSortCriterion.class .getDeclaredMethod("substituteExpressionWithProjectionField", Integer.TYPE); method.setAccessible(true); method.invoke(csc1, 0); method.invoke(csc2, 1); List<CompiledSortCriterion> orderByAttribs = new ArrayList<CompiledSortCriterion>(); orderByAttribs.add(csc1); orderByAttribs.add(csc2); ExecutionContext context = new ExecutionContext(null, null); return new NWayMergeResults<Struct>(sortedResults, false, -1, orderByAttribs, context, structType); }
/** * This method evaluates sort criteria and returns the resulting integer value of comparing the * two objects passed into it based on these criteria. * * @param value1 the first object to be compared. * @param value2 the second object to be compared. * @return a negative integer, zero, or a positive integer if the first argument is less than, * equal to, or greater than the second, based on the evaluated sort criteria. */ protected int evaluateSortCriteria(Object value1, Object value2) { int result = -1; if (orderByAttrs != null) { for (CompiledSortCriterion csc : orderByAttrs) { Object sortCriteriaForValue1 = csc.evaluate(value1, context); Object sortCriteriaForValue2 = csc.evaluate(value2, context); result = compareHelperMethod(sortCriteriaForValue1, sortCriteriaForValue2); if (result != 0) { if (csc.getCriterion()) { result *= -1; } break; } } } return result; }
@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); }
private void mapOriginalOrderByColumns(ExecutionContext context) throws AmbiguousNameException, TypeMismatchException, NameResolutionException { this.revertAggregateFunctionInProjection(); Iterator<CompiledSortCriterion> iter = this.originalOrderByClause.iterator(); while (iter.hasNext()) { CompiledSortCriterion csc = iter.next(); if (!csc.mapExpressionToProjectionField(this.projAttrs, context)) { throw new QueryInvalidException( "Query contains atleast one order by field which is not present in projected fields."); } } this.replaceAggregateFunctionInProjection(); }
List orderByAttrs = (List) context.cacheGet(CompiledValue.ORDERBY_ATTRIB); useLinkedDataStructure = orderByAttrs.size() == 1; nullValuesAtStart = !((CompiledSortCriterion) orderByAttrs.get(0)).getCriterion();
private <E> NWayMergeResults<E> createSingleFieldMergedResult( Collection<? extends Collection<E>> sortedResults, boolean isDistinct, int limit) throws Exception { CompiledSortCriterion csc = new CompiledSortCriterion(false, CompiledSortCriterion.ProjectionField.getProjectionField()); Method method = CompiledSortCriterion.class .getDeclaredMethod("substituteExpressionWithProjectionField", Integer.TYPE); method.setAccessible(true); method.invoke(csc, 0); List<CompiledSortCriterion> orderByAttribs = new ArrayList<CompiledSortCriterion>(); orderByAttribs.add(csc); ExecutionContext context = new ExecutionContext(null, null); ObjectType elementType = new ObjectTypeImpl(Object.class); return new NWayMergeResults<E>(sortedResults, isDistinct, limit, orderByAttribs, context, elementType); }
List orderByAttrs = (List) context.cacheGet(CompiledValue.ORDERBY_ATTRIB); useLinkedDataStructure = orderByAttrs.size() == 1; nullValuesAtStart = !((CompiledSortCriterion) orderByAttrs.get(0)).getCriterion();
new CompiledNegation(compiledValue1), new CompiledRegion("test"), new CompiledSortCriterion(true, compiledValue1), new CompiledSubtraction(compiledValue1, compiledValue2, 13), new CompiledUnaryMinus(compiledValue1),