/** * Unit test with SELECT clause having one value expression, which is a * simple variable also appearing as the sole value expression in the * GROUP_BY clause. * <pre> * SELECT ?org * GROUP BY ?org * </pre> */ public void test_aggregateExpr_01() { final VarNode org = new VarNode("org"); final ProjectionNode select = new ProjectionNode(); select.addProjectionVar(org); final GroupByNode groupBy = new GroupByNode(); groupBy.addGroupByVar(org); final HavingNode having = null; new VerifyAggregates(select, groupBy, having); }
final IValueExpressionNode exprNode = n.getValueExpressionNode(); if (expr == null && exprNode instanceof FunctionNode) { expr = convertAggregates((FunctionNode)exprNode); final BOp expr = convertAggregates(exprNode); exprs2[i] = new SPARQLConstraint<XSDBooleanIV<BigdataLiteral>>(new BOp[]{ expr}, null); final IBind<?> bindExpr = (IBind<?>) expr; final IValueExpression<?> e = bindExpr.getExpr(); if (isAggregate(e, false/* isSelectClause */, if (!isAggregate(e, true/* isSelectClause */, selectDependency, nestedAggregates, anyDistinct)) throw new IllegalArgumentException("Not an aggregate: " if (!isAggregate(c, false/* isSelectClause */,
return isAggregate(op, isSelectClause, isSelectDependency, isNestedAggregates, isAnyDistinct, false/* withinAggregateFunction */);
private IValueExpression convertAggregates(final BOp exprNode) { IValueExpression expr; final BOp[] args = new BOp[exprNode.args().size()]; if (exprNode!=null && exprNode.arity()>0) { for (int i=0; i<exprNode.args().size(); i++) { final BOp arg = exprNode.args().get(i); final IValueExpression newValue = convertAggregates(arg); if (newValue!=null) { args[i] = newValue; } else { args[i] = arg; } } } if (exprNode instanceof FunctionNode) { if ((exprNode instanceof FunctionNode) && FunctionRegistry.isAggregate(((FunctionNode) exprNode).getFunctionURI())) { expr = new AggregateBase(args, null) { @Override public void reset() {} @Override public IV done() { return null; } }; } else { expr = new UnknownFunctionBOp(args, null); } } else { expr = null; } return expr; }
VerifyAggregates.verifyAggregate(queryRoot);
return isAggregate(op, isSelectClause, isSelectDependency, isNestedAggregates, isAnyDistinct, false/* withinAggregateFunction */);
private IValueExpression convertAggregates(final BOp exprNode) { IValueExpression expr; final BOp[] args = new BOp[exprNode.args().size()]; if (exprNode!=null && exprNode.arity()>0) { for (int i=0; i<exprNode.args().size(); i++) { final BOp arg = exprNode.args().get(i); final IValueExpression newValue = convertAggregates(arg); if (newValue!=null) { args[i] = newValue; } else { args[i] = arg; } } } if (exprNode instanceof FunctionNode) { if ((exprNode instanceof FunctionNode) && FunctionRegistry.isAggregate(((FunctionNode) exprNode).getFunctionURI())) { expr = new AggregateBase(args, null) { @Override public void reset() {} @Override public IV done() { return null; } }; } else { expr = new UnknownFunctionBOp(args, null); } } else { expr = null; } return expr; }
VerifyAggregates.verifyAggregate(queryRoot);
new VerifyAggregates(null/* select */, groupBy, having); fail("Expecting: " + IllegalArgumentException.class); } catch (final IllegalArgumentException ex) { new VerifyAggregates(new ProjectionNode()/* select */, groupBy, having); fail("Expecting: " + IllegalArgumentException.class);
final IValueExpressionNode exprNode = n.getValueExpressionNode(); if (expr == null && exprNode instanceof FunctionNode) { expr = convertAggregates((FunctionNode)exprNode); final BOp expr = convertAggregates(exprNode); exprs2[i] = new SPARQLConstraint<XSDBooleanIV<BigdataLiteral>>(new BOp[]{ expr}, null); final IBind<?> bindExpr = (IBind<?>) expr; final IValueExpression<?> e = bindExpr.getExpr(); if (isAggregate(e, false/* isSelectClause */, if (!isAggregate(e, true/* isSelectClause */, selectDependency, nestedAggregates, anyDistinct)) throw new IllegalArgumentException("Not an aggregate: " if (!isAggregate(c, false/* isSelectClause */,
); isAggregate |= isAggregate(arg, isSelectClause, isSelectDependency,
/** * Unit test with simple aggregate function in SELECT clause and no GROUP BY * clause (the aggregation is taken across all solutions as if they were a * single group). * * <pre> * SELECT SUM(?lprice) AS ?totalPrice * </pre> */ @SuppressWarnings({ "rawtypes", "unchecked" }) public void test_simpleAggregate_noGroupBy() { final VarNode lprice = new VarNode("lprice"); final VarNode totalPrice = new VarNode("totalPrice"); final IValueExpressionNode totalPriceExprNode = new FunctionNode( FunctionRegistry.SUM, null, lprice); final ProjectionNode select = new ProjectionNode(); select.addProjectionExpression(new AssignmentNode(totalPrice, totalPriceExprNode)); final GroupByNode groupBy = null; final HavingNode having = null; new VerifyAggregates(select, groupBy, having); }
); isAggregate |= isAggregate(arg, isSelectClause, isSelectDependency,
/** * Unit test when projecting a constant * <pre> * SELECT 12 as ?x * </pre> */ public void test_with_constant() { @SuppressWarnings({ "unchecked", "rawtypes" }) final VarNode x = new VarNode("x"); @SuppressWarnings({ "rawtypes", "unchecked" }) final ConstantNode xExpr = new ConstantNode( new XSDNumericIV(12)); final ProjectionNode select = new ProjectionNode(); select.addExpr(new AssignmentNode(x, xExpr)); final GroupByNode groupBy = null; final HavingNode having = null; new VerifyAggregates(select, groupBy, having); }
/** * Unit test with SELECT clause having one value expression, which is a * simple variable also appearing as the sole value expression in the * GROUP_BY clause. However, in this case we rename the variable when it is * projected out of the SELECT expression. * * <pre> * SELECT ?org as ?newVar * GROUP BY ?org * </pre> */ public void test_aggregateExpr_02() { final VarNode org = new VarNode("org"); final VarNode newVar = new VarNode("newVar"); @SuppressWarnings({ "unchecked", "rawtypes" }) final ProjectionNode select = new ProjectionNode(); select.addProjectionExpression(new AssignmentNode(newVar, org)); final GroupByNode groupBy = new GroupByNode(); groupBy.addGroupByVar(org); final HavingNode having = null; new VerifyAggregates(select, groupBy, having); }
/** * Unit test for references to aggregate declared in GROUP_BY with AS. * <pre> * SELECT ?org2 * GROUP BY UCASE(?org) as ?org2 * </pre> */ @SuppressWarnings({ "rawtypes", "unchecked" }) public void test_aggregateExpr_03() { // Note: UcaseBOp is what we need here. final VarNode org = new VarNode("org"); final VarNode org2 = new VarNode("org2"); final IValueExpressionNode ucaseExpr = new FunctionNode(FunctionRegistry.UCASE, null, org); final ProjectionNode select = new ProjectionNode(); select.addProjectionVar(org2); final GroupByNode groupBy = new GroupByNode(); groupBy.addExpr(new AssignmentNode(org2, ucaseExpr)); final HavingNode having = null; new VerifyAggregates(select, groupBy, having); }
/** * Unit test for {@link IGroupByState#isAnyDistinct()) where the DISTINCT * keyword appears within an {@link IAggregate} in the SELECT clause. * <pre> * SELECT SUM(DISTINCT ?y) as ?x * GROUP BY ?z * HAVING ?x > 10 * </pre> */ @SuppressWarnings({ "rawtypes", "unchecked" }) public void test_isAnyDistinct_select() { final VarNode y = new VarNode("y"); final VarNode x = new VarNode("x"); final VarNode z = new VarNode("z"); final Map<String, Object> scalarValues = new HashMap<>(); scalarValues.put(Annotations.DISTINCT, Boolean.TRUE); final IValueExpressionNode xExpr = new FunctionNode(FunctionRegistry.SUM, scalarValues, y); final ProjectionNode select = new ProjectionNode(); select.addExpr(new AssignmentNode(x, xExpr)); final GroupByNode groupBy = new GroupByNode(); groupBy.addGroupByVar(z); final HavingNode having = new HavingNode(); having.addExpr(FunctionNode.GT( x, new ConstantNode(new XSDNumericIV(10)) )); new VerifyAggregates(select, groupBy, having); }
/** * Unit test for {@link IGroupByState#isAnyDistinct()) where the DISTINCT * keyword appears within an {@link IAggregate} in the SELECT clause. * <pre> * SELECT SUM(?y) as ?x * GROUP BY ?z * HAVING SUM(DISTINCT ?y) > 10 * </pre> */ @SuppressWarnings({ "rawtypes", "unchecked" }) public void test_isAnyDistinct_having() { final VarNode y = new VarNode("y"); final VarNode x = new VarNode("x"); final VarNode z = new VarNode("z"); final IValueExpressionNode xExpr = new FunctionNode(FunctionRegistry.SUM, null, y); final ProjectionNode select = new ProjectionNode(); select.addExpr(new AssignmentNode(x, xExpr)); final GroupByNode groupBy = new GroupByNode(); groupBy.addGroupByVar(z); final HavingNode having = new HavingNode(); final Map<String, Object> scalarValues = new HashMap<>(); scalarValues.put(Annotations.DISTINCT, Boolean.TRUE); having.addExpr(FunctionNode.GT( new FunctionNode(FunctionRegistry.SUM, scalarValues, y), new ConstantNode(new XSDNumericIV(10)) )); new VerifyAggregates(select, groupBy, having); }
/** * Unit test with SELECT clause having one value expression, which is a * simple variable also appearing as the sole value expression in the * GROUP_BY clause. * <pre> * SELECT ?org * GROUP BY ?org * </pre> */ public void test_aggregateExpr_01() { final VarNode org = new VarNode("org"); final ProjectionNode select = new ProjectionNode(); select.addProjectionVar(org); final GroupByNode groupBy = new GroupByNode(); groupBy.addGroupByVar(org); final HavingNode having = null; new VerifyAggregates(select, groupBy, having); }
new VerifyAggregates(select, groupBy, having); fail("Expecting: " + IllegalArgumentException.class); } catch (final IllegalArgumentException ex) {