com.hazelcast.org.apache.calcite.adapter.enumerable.LazyAggregateLambdaFactory Maven / Gradle / Ivy
/*
* 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 com.hazelcast.com.liance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.com.hazelcast.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 com.hazelcast.org.apache.calcite.adapter.enumerable;
import com.hazelcast.org.apache.calcite.linq4j.function.Function0;
import com.hazelcast.org.apache.calcite.linq4j.function.Function1;
import com.hazelcast.org.apache.calcite.linq4j.function.Function2;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Generate aggregate lambdas that preserve the input source before calling each
* aggregate adder, this implementation is generally used when we need to sort the input
* before performing aggregation.
*
* @param Type of the enumerable input source
* @param Type of the group-by key
* @param Type of the original accumulator
* @param Type of the enumerable output result
*/
public class LazyAggregateLambdaFactory
implements AggregateLambdaFactory, TResult, TKey> {
private final Function0 accumulatorInitializer;
private final List> accumulators;
public LazyAggregateLambdaFactory(
Function0 accumulatorInitializer,
List> accumulators) {
this.accumulatorInitializer = accumulatorInitializer;
this.accumulators = accumulators;
}
public Function0> accumulatorInitializer() {
return LazySource::new;
}
public Function2,
TSource, LazySource> accumulatorAdder() {
return (lazySource, source) -> {
lazySource.add(source);
return lazySource;
};
}
public Function1, TResult> singleGroupResultSelector(
Function1 resultSelector) {
return lazySource -> {
final TOrigAccumulate accumulator = accumulatorInitializer.apply();
for (LazyAccumulator acc : accumulators) {
acc.accumulate(lazySource, accumulator);
}
return resultSelector.apply(accumulator);
};
}
public Function2, TResult> resultSelector(
Function2 resultSelector) {
return (groupByKey, lazySource) -> {
final TOrigAccumulate accumulator = accumulatorInitializer.apply();
for (LazyAccumulator acc : accumulators) {
acc.accumulate(lazySource, accumulator);
}
return resultSelector.apply(groupByKey, accumulator);
};
}
/**
* Cache the input sources. (Will be aggregated in result selector.)
*
* @param Type of the enumerable input source.
*/
public static class LazySource implements Iterable {
private final List list = new ArrayList<>();
private void add(TSource source) {
list.add(source);
}
@Override public Iterator iterator() {
return list.iterator();
}
}
/**
* Accumulate on the cached input sources.
*
* @param Type of the original accumulator
* @param Type of the enumerable input source.
*/
public interface LazyAccumulator {
void accumulate(Iterable sourceIterable, TOrigAccumulate accumulator);
}
}