Java Code Examples for org.apache.calcite.tools.RelBuilder#AggCall

The following examples show how to use org.apache.calcite.tools.RelBuilder#AggCall . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtendedAggregateExtractProjectRule.java    From flink with Apache License 2.0 6 votes vote down vote up
private List<RelBuilder.AggCall> getNewAggCallList(
	Aggregate oldAggregate,
	RelBuilder relBuilder,
	Mapping mapping) {

	final List<RelBuilder.AggCall> newAggCallList = new ArrayList<>();

	for (AggregateCall aggCall : oldAggregate.getAggCallList()) {
		final RexNode filterArg = aggCall.filterArg < 0 ? null
			: relBuilder.field(Mappings.apply(mapping, aggCall.filterArg));
		newAggCallList.add(
			relBuilder
				.aggregateCall(
					aggCall.getAggregation(),
					relBuilder.fields(Mappings.apply2(mapping, aggCall.getArgList())))
				.distinct(aggCall.isDistinct())
				.filter(filterArg)
				.approximate(aggCall.isApproximate())
				.sort(relBuilder.fields(aggCall.collation))
				.as(aggCall.name));
	}
	return newAggCallList;
}
 
Example 2
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelBuilder.AggCall visit(CallExpression call) {
	FunctionDefinition def = call.getFunctionDefinition();
	if (BuiltInFunctionDefinitions.DISTINCT == def) {
		Expression innerAgg = call.getChildren().get(0);
		return innerAgg.accept(new AggCallVisitor(relBuilder, rexNodeConverter, name, true));
	} else {
		SqlAggFunction sqlAggFunction = call.accept(sqlAggFunctionVisitor);
		return relBuilder.aggregateCall(
			sqlAggFunction,
			isDistinct,
			false,
			null,
			name,
			call.getChildren().stream().map(expr -> expr.accept(rexNodeConverter))
				.collect(Collectors.toList()));
	}
}
 
Example 3
Source File: ExtendedAggregateExtractProjectRule.java    From flink with Apache License 2.0 6 votes vote down vote up
private List<RelBuilder.AggCall> getNewAggCallList(
	Aggregate oldAggregate,
	RelBuilder relBuilder,
	Mapping mapping) {

	final List<RelBuilder.AggCall> newAggCallList = new ArrayList<>();

	for (AggregateCall aggCall : oldAggregate.getAggCallList()) {
		final RexNode filterArg = aggCall.filterArg < 0 ? null
			: relBuilder.field(Mappings.apply(mapping, aggCall.filterArg));
		newAggCallList.add(
			relBuilder
				.aggregateCall(
					aggCall.getAggregation(),
					relBuilder.fields(Mappings.apply2(mapping, aggCall.getArgList())))
				.distinct(aggCall.isDistinct())
				.filter(filterArg)
				.approximate(aggCall.isApproximate())
				.sort(relBuilder.fields(aggCall.collation))
				.as(aggCall.name));
	}
	return newAggCallList;
}
 
Example 4
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelBuilder.AggCall visit(CallExpression call) {
	FunctionDefinition def = call.getFunctionDefinition();
	if (BuiltInFunctionDefinitions.DISTINCT == def) {
		Expression innerAgg = call.getChildren().get(0);
		return innerAgg.accept(new AggCallVisitor(relBuilder, expressionConverter, name, true));
	} else {
		SqlAggFunction sqlAggFunction = call.accept(sqlAggFunctionVisitor);
		return relBuilder.aggregateCall(
			sqlAggFunction,
			isDistinct,
			false,
			null,
			name,
			call.getChildren().stream().map(expr -> expr.accept(expressionConverter))
				.collect(Collectors.toList()));
	}
}
 
Example 5
Source File: ExtendedAggregateExtractProjectRule.java    From flink with Apache License 2.0 5 votes vote down vote up
private RelNode getNewAggregate(Aggregate oldAggregate, RelBuilder relBuilder, Mapping mapping) {

		final ImmutableBitSet newGroupSet =
			Mappings.apply(mapping, oldAggregate.getGroupSet());

		final Iterable<ImmutableBitSet> newGroupSets =
			oldAggregate.getGroupSets().stream()
				.map(bitSet -> Mappings.apply(mapping, bitSet))
				.collect(Collectors.toList());

		final List<RelBuilder.AggCall> newAggCallList =
			getNewAggCallList(oldAggregate, relBuilder, mapping);

		final RelBuilder.GroupKey groupKey =
			relBuilder.groupKey(newGroupSet, newGroupSets);

		if (oldAggregate instanceof LogicalWindowAggregate) {
			if (newGroupSet.size() == 0 && newAggCallList.size() == 0) {
				// Return the old LogicalWindowAggregate directly, as we can't get an empty Aggregate
				// from the relBuilder.
				return oldAggregate;
			} else {
				relBuilder.aggregate(groupKey, newAggCallList);
				Aggregate newAggregate = (Aggregate) relBuilder.build();
				LogicalWindowAggregate oldLogicalWindowAggregate = (LogicalWindowAggregate) oldAggregate;

				return LogicalWindowAggregate.create(
					oldLogicalWindowAggregate.getWindow(),
					oldLogicalWindowAggregate.getNamedProperties(),
					newAggregate);
			}
		} else {
			relBuilder.aggregate(groupKey, newAggCallList);
			return relBuilder.build();
		}
	}
 
Example 6
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RelBuilder.AggCall visit(CallExpression call) {
	SqlAggFunction sqlAggFunction = call.accept(sqlAggFunctionVisitor);
	return relBuilder.aggregateCall(
		sqlAggFunction,
		false,
		false,
		null,
		sqlAggFunction.toString(),
		call.getChildren().stream().map(expr -> expr.accept(rexNodeConverter)).collect(toList()));
}
 
Example 7
Source File: HBTQueryConvertor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private RelBuilder.AggCall toAggregateCall(AggregateCall expr) {
    return relBuilder.aggregateCall(toSqlAggFunction(expr.getFunction()),
            toRex(expr.getOperands() == null ? Collections.emptyList() : expr.getOperands()))
            .sort(expr.getOrderKeys() == null ? Collections.emptyList() : toSortRex(expr.getOrderKeys()))
            .distinct(Boolean.TRUE.equals(expr.getDistinct()))
            .approximate(Boolean.TRUE.equals(expr.getApproximate()))
            .ignoreNulls(Boolean.TRUE.equals(expr.getIgnoreNulls()))
            .filter(expr.getFilter() == null ? null : toRex(expr.getFilter()));
}
 
Example 8
Source File: ExtendedAggregateExtractProjectRule.java    From flink with Apache License 2.0 5 votes vote down vote up
private RelNode getNewAggregate(Aggregate oldAggregate, RelBuilder relBuilder, Mapping mapping) {

		final ImmutableBitSet newGroupSet =
			Mappings.apply(mapping, oldAggregate.getGroupSet());

		final Iterable<ImmutableBitSet> newGroupSets =
			oldAggregate.getGroupSets().stream()
				.map(bitSet -> Mappings.apply(mapping, bitSet))
				.collect(Collectors.toList());

		final List<RelBuilder.AggCall> newAggCallList =
			getNewAggCallList(oldAggregate, relBuilder, mapping);

		final RelBuilder.GroupKey groupKey =
			relBuilder.groupKey(newGroupSet, newGroupSets);

		if (oldAggregate instanceof LogicalWindowAggregate) {
			if (newGroupSet.size() == 0 && newAggCallList.size() == 0) {
				// Return the old LogicalWindowAggregate directly, as we can't get an empty Aggregate
				// from the relBuilder.
				return oldAggregate;
			} else {
				relBuilder.aggregate(groupKey, newAggCallList);
				Aggregate newAggregate = (Aggregate) relBuilder.build();
				LogicalWindowAggregate oldLogicalWindowAggregate = (LogicalWindowAggregate) oldAggregate;

				return LogicalWindowAggregate.create(
					oldLogicalWindowAggregate.getWindow(),
					oldLogicalWindowAggregate.getNamedProperties(),
					newAggregate);
			}
		} else {
			relBuilder.aggregate(groupKey, newAggCallList);
			return relBuilder.build();
		}
	}
 
Example 9
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RelBuilder.AggCall visit(CallExpression call) {
	SqlAggFunction sqlAggFunction = call.accept(sqlAggFunctionVisitor);
	return relBuilder.aggregateCall(
		sqlAggFunction,
		false,
		false,
		null,
		sqlAggFunction.toString(),
		call.getChildren().stream().map(expr -> expr.accept(expressionConverter)).collect(toList()));
}
 
Example 10
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected RelBuilder.AggCall defaultMethod(Expression expression) {
	throw new TableException("Unexpected expression: " + expression);
}
 
Example 11
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected RelBuilder.AggCall defaultMethod(Expression expression) {
	throw new TableException("Expected table aggregate. Got: " + expression);
}
 
Example 12
Source File: MycatCalcitePlanner.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 测试单库分表与不同分片两个情况
 *
 * @param relBuilder
 * @param cache
 * @param margeList
 * @param bestExp2
 * @return
 */
private RelNode simplyAggreate(MycatRelBuilder relBuilder, IdentityHashMap<RelNode, Boolean> cache, IdentityHashMap<RelNode, List<String>> margeList, RelNode bestExp2) {
    RelNode parent = bestExp2;
    RelNode child = bestExp2 instanceof Aggregate ? bestExp2.getInput(0) : null;
    RelNode bestExp3 = parent;
    if (parent instanceof Aggregate && child instanceof Union) {
        Aggregate aggregate = (Aggregate) parent;
        if (aggregate.getAggCallList() != null && !aggregate.getAggCallList().isEmpty()) {//distinct会没有参数
            List<AggregateCall> aggCallList = aggregate.getAggCallList();
            boolean allMatch = aggregate.getRowType().getFieldCount() == 1 && aggCallList.stream().allMatch(new Predicate<AggregateCall>() {
                @Override
                public boolean test(AggregateCall aggregateCall) {
                    return SUPPORTED_AGGREGATES.getOrDefault(aggregateCall.getAggregation().getKind(), false)
                            &&
                            aggregate.getRowType().getFieldList().stream().allMatch(i -> i.getType().getSqlTypeName().getFamily() == SqlTypeFamily.NUMERIC);
                }
            });
            if (allMatch) {
                List<RelNode> inputs = child.getInputs();
                List<RelNode> resList = new ArrayList<>(inputs.size());
                boolean allCanPush = true;//是否聚合节点涉及不同分片
                String target = null;
                for (RelNode input : inputs) {
                    RelNode res;
                    if (cache.get(input)) {
                        res = LogicalAggregate.create(input, aggregate.getGroupSet(), aggregate.getGroupSets(), aggregate.getAggCallList());
                        cache.put(res, Boolean.TRUE);
                        List<String> strings = margeList.getOrDefault(input, Collections.emptyList());
                        Objects.requireNonNull(strings);
                        if (target == null && strings.size() > 0) {
                            target = strings.get(0);
                        } else if (target != null && strings.size() > 0) {
                            if (!target.equals(strings.get(0))) {
                                allCanPush = false;
                            }
                        }
                        margeList.put(res, strings);
                    } else {
                        res = input;
                        allCanPush = false;
                    }
                    resList.add(res);
                }

                LogicalUnion logicalUnion = LogicalUnion.create(resList, ((Union) child).all);

                //构造sum
                relBuilder.clear();
                relBuilder.push(logicalUnion);
                List<RexNode> fields = relBuilder.fields();
                if (fields == null) {
                    fields = Collections.emptyList();
                }

                RelBuilder.GroupKey groupKey = relBuilder.groupKey();
                List<RelBuilder.AggCall> aggCalls = fields.stream().map(i -> relBuilder.sum(i)).collect(Collectors.toList());
                relBuilder.aggregate(groupKey, aggCalls);
                bestExp3 = relBuilder.build();

                cache.put(logicalUnion, allCanPush);
                cache.put(bestExp3, allCanPush);
                if (target != null) {//是否聚合节点涉及不同分片
                    List<String> targetSingelList = Collections.singletonList(target);
                    margeList.put(logicalUnion, targetSingelList);
                    margeList.put(bestExp3, targetSingelList);
                }
            }
        }
    }
    return bestExp3;
}
 
Example 13
Source File: HBTQueryConvertor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
private List<RelBuilder.AggCall> toAggregateCall(List<AggregateCall> exprs) {
    return exprs.stream().map(this::toAggregateCall).collect(Collectors.toList());
}
 
Example 14
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected RelBuilder.AggCall defaultMethod(Expression expression) {
	throw new TableException("Unexpected expression: " + expression);
}
 
Example 15
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected RelBuilder.AggCall defaultMethod(Expression expression) {
	throw new TableException("Expected table aggregate. Got: " + expression);
}