Search in sources :

Example 31 with BinaryOperator

use of java.util.function.BinaryOperator in project radixdlt by radixdlt.

the class UInt256sTest method testPrimeLCM.

private static <T> void testPrimeLCM(IntFunction<T[]> arrayBuilder, LongFunction<T> builder, BinaryOperator<T> multiply, T one, T max, Function<T, BigInteger> toBI, BiFunction<T, T[], T> lcmFunction) {
    List<Integer> primeList = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149);
    IntFunction<LongStream> primeNumbers = size -> primeList.stream().mapToLong(i -> i).limit(size);
    BigInteger maxBI = toBI.apply(max);
    for (int size = 1; size < primeList.size(); size++) {
        T[] values = primeNumbers.apply(size).mapToObj(builder).toArray(arrayBuilder);
        BigInteger biExpected = primeNumbers.apply(size).mapToObj(BigInteger::valueOf).reduce(BigInteger.ONE, BigInteger::multiply);
        final T expected;
        if (biExpected.compareTo(maxBI) > 0) {
            expected = null;
        } else {
            expected = primeNumbers.apply(size).mapToObj(builder).reduce(one, multiply);
        }
        assertThat(lcmFunction.apply(max, values)).isEqualTo(expected);
    }
}
Also used : BigInteger(java.math.BigInteger) Arrays(java.util.Arrays) LongStream(java.util.stream.LongStream) LongFunction(java.util.function.LongFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BiFunction(java.util.function.BiFunction) Test(org.junit.Test) Function(java.util.function.Function) BinaryOperator(java.util.function.BinaryOperator) BigDecimal(java.math.BigDecimal) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Stream(java.util.stream.Stream) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BigInteger(java.math.BigInteger) Assert.assertEquals(org.junit.Assert.assertEquals) IntFunction(java.util.function.IntFunction) BigInteger(java.math.BigInteger) LongStream(java.util.stream.LongStream)

Example 32 with BinaryOperator

use of java.util.function.BinaryOperator in project random-cut-forest-by-aws by aws.

the class RandomCutForest method getAnomalyAttribution.

public DiVector getAnomalyAttribution(float[] point) {
    // getAnomalyScore
    if (!isOutputReady()) {
        return new DiVector(dimensions);
    }
    IVisitorFactory<DiVector> visitorFactory = new VisitorFactory<>((tree, y) -> new AnomalyAttributionVisitor(tree.projectToTree(y), tree.getMass()), (tree, x) -> x.lift(tree::liftFromTree));
    BinaryOperator<DiVector> accumulator = DiVector::addToLeft;
    Function<DiVector, DiVector> finisher = x -> x.scale(1.0 / numberOfTrees);
    return traverseForest(transformToShingledPoint(point), visitorFactory, accumulator, finisher);
}
Also used : CommonUtils.checkNotNull(com.amazon.randomcutforest.CommonUtils.checkNotNull) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) ParallelForestTraversalExecutor(com.amazon.randomcutforest.executor.ParallelForestTraversalExecutor) Random(java.util.Random) ParallelForestUpdateExecutor(com.amazon.randomcutforest.executor.ParallelForestUpdateExecutor) AbstractForestUpdateExecutor(com.amazon.randomcutforest.executor.AbstractForestUpdateExecutor) IStateCoordinator(com.amazon.randomcutforest.executor.IStateCoordinator) RandomCutTree(com.amazon.randomcutforest.tree.RandomCutTree) CommonUtils.toFloatArray(com.amazon.randomcutforest.CommonUtils.toFloatArray) Neighbor(com.amazon.randomcutforest.returntypes.Neighbor) ConditionalSampleSummarizer(com.amazon.randomcutforest.imputation.ConditionalSampleSummarizer) ImputeVisitor(com.amazon.randomcutforest.imputation.ImputeVisitor) NearNeighborVisitor(com.amazon.randomcutforest.inspect.NearNeighborVisitor) IBoundingBoxView(com.amazon.randomcutforest.tree.IBoundingBoxView) Collector(java.util.stream.Collector) PointStoreCoordinator(com.amazon.randomcutforest.executor.PointStoreCoordinator) AnomalyAttributionVisitor(com.amazon.randomcutforest.anomalydetection.AnomalyAttributionVisitor) OneSidedConvergingDoubleAccumulator(com.amazon.randomcutforest.returntypes.OneSidedConvergingDoubleAccumulator) AnomalyScoreVisitor(com.amazon.randomcutforest.anomalydetection.AnomalyScoreVisitor) AbstractForestTraversalExecutor(com.amazon.randomcutforest.executor.AbstractForestTraversalExecutor) BinaryOperator(java.util.function.BinaryOperator) SequentialForestTraversalExecutor(com.amazon.randomcutforest.executor.SequentialForestTraversalExecutor) List(java.util.List) Math.max(java.lang.Math.max) Optional(java.util.Optional) DensityOutput(com.amazon.randomcutforest.returntypes.DensityOutput) CommonUtils.toDoubleArray(com.amazon.randomcutforest.CommonUtils.toDoubleArray) Precision(com.amazon.randomcutforest.config.Precision) CompactSampler(com.amazon.randomcutforest.sampler.CompactSampler) SamplerPlusTree(com.amazon.randomcutforest.executor.SamplerPlusTree) ShingleBuilder(com.amazon.randomcutforest.util.ShingleBuilder) Function(java.util.function.Function) ArrayList(java.util.ArrayList) SimulatedTransductiveScalarScoreVisitor(com.amazon.randomcutforest.anomalydetection.SimulatedTransductiveScalarScoreVisitor) PointStore(com.amazon.randomcutforest.store.PointStore) DynamicAttributionVisitor(com.amazon.randomcutforest.anomalydetection.DynamicAttributionVisitor) ConvergingAccumulator(com.amazon.randomcutforest.returntypes.ConvergingAccumulator) Config(com.amazon.randomcutforest.config.Config) SimpleInterpolationVisitor(com.amazon.randomcutforest.interpolation.SimpleInterpolationVisitor) InterpolationMeasure(com.amazon.randomcutforest.returntypes.InterpolationMeasure) IPointStore(com.amazon.randomcutforest.store.IPointStore) SequentialForestUpdateExecutor(com.amazon.randomcutforest.executor.SequentialForestUpdateExecutor) ArrayUtils(com.amazon.randomcutforest.util.ArrayUtils) OneSidedConvergingDiVectorAccumulator(com.amazon.randomcutforest.returntypes.OneSidedConvergingDiVectorAccumulator) CommonUtils.checkArgument(com.amazon.randomcutforest.CommonUtils.checkArgument) DynamicScoreVisitor(com.amazon.randomcutforest.anomalydetection.DynamicScoreVisitor) DiVector(com.amazon.randomcutforest.returntypes.DiVector) ITree(com.amazon.randomcutforest.tree.ITree) ConditionalTreeSample(com.amazon.randomcutforest.returntypes.ConditionalTreeSample) ConditionalSampleSummary(com.amazon.randomcutforest.returntypes.ConditionalSampleSummary) Collections(java.util.Collections) IStreamSampler(com.amazon.randomcutforest.sampler.IStreamSampler) DiVector(com.amazon.randomcutforest.returntypes.DiVector) AnomalyAttributionVisitor(com.amazon.randomcutforest.anomalydetection.AnomalyAttributionVisitor)

Example 33 with BinaryOperator

use of java.util.function.BinaryOperator in project random-cut-forest-by-aws by aws.

the class RandomCutForest method getDynamicAttribution.

/**
 * Same as above, but for dynamic scoring. See the params of
 * getDynamicScoreParallel
 *
 * @param point                   point to be scored
 * @param ignoreLeafMassThreshold said threshold
 * @param seen                    score function for seen points
 * @param unseen                  score function for unseen points
 * @param newDamp                 dampening function for duplicates in the seen
 *                                function
 * @return dynamic scoring attribution DiVector
 */
public DiVector getDynamicAttribution(float[] point, int ignoreLeafMassThreshold, BiFunction<Double, Double, Double> seen, BiFunction<Double, Double, Double> unseen, BiFunction<Double, Double, Double> newDamp) {
    if (!isOutputReady()) {
        return new DiVector(dimensions);
    }
    VisitorFactory<DiVector> visitorFactory = new VisitorFactory<>((tree, y) -> new DynamicAttributionVisitor(tree.projectToTree(y), tree.getMass(), ignoreLeafMassThreshold, seen, unseen, newDamp), (tree, x) -> x.lift(tree::liftFromTree));
    BinaryOperator<DiVector> accumulator = DiVector::addToLeft;
    Function<DiVector, DiVector> finisher = x -> x.scale(1.0 / numberOfTrees);
    return traverseForest(transformToShingledPoint(point), visitorFactory, accumulator, finisher);
}
Also used : CommonUtils.checkNotNull(com.amazon.randomcutforest.CommonUtils.checkNotNull) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) ParallelForestTraversalExecutor(com.amazon.randomcutforest.executor.ParallelForestTraversalExecutor) Random(java.util.Random) ParallelForestUpdateExecutor(com.amazon.randomcutforest.executor.ParallelForestUpdateExecutor) AbstractForestUpdateExecutor(com.amazon.randomcutforest.executor.AbstractForestUpdateExecutor) IStateCoordinator(com.amazon.randomcutforest.executor.IStateCoordinator) RandomCutTree(com.amazon.randomcutforest.tree.RandomCutTree) CommonUtils.toFloatArray(com.amazon.randomcutforest.CommonUtils.toFloatArray) Neighbor(com.amazon.randomcutforest.returntypes.Neighbor) ConditionalSampleSummarizer(com.amazon.randomcutforest.imputation.ConditionalSampleSummarizer) ImputeVisitor(com.amazon.randomcutforest.imputation.ImputeVisitor) NearNeighborVisitor(com.amazon.randomcutforest.inspect.NearNeighborVisitor) IBoundingBoxView(com.amazon.randomcutforest.tree.IBoundingBoxView) Collector(java.util.stream.Collector) PointStoreCoordinator(com.amazon.randomcutforest.executor.PointStoreCoordinator) AnomalyAttributionVisitor(com.amazon.randomcutforest.anomalydetection.AnomalyAttributionVisitor) OneSidedConvergingDoubleAccumulator(com.amazon.randomcutforest.returntypes.OneSidedConvergingDoubleAccumulator) AnomalyScoreVisitor(com.amazon.randomcutforest.anomalydetection.AnomalyScoreVisitor) AbstractForestTraversalExecutor(com.amazon.randomcutforest.executor.AbstractForestTraversalExecutor) BinaryOperator(java.util.function.BinaryOperator) SequentialForestTraversalExecutor(com.amazon.randomcutforest.executor.SequentialForestTraversalExecutor) List(java.util.List) Math.max(java.lang.Math.max) Optional(java.util.Optional) DensityOutput(com.amazon.randomcutforest.returntypes.DensityOutput) CommonUtils.toDoubleArray(com.amazon.randomcutforest.CommonUtils.toDoubleArray) Precision(com.amazon.randomcutforest.config.Precision) CompactSampler(com.amazon.randomcutforest.sampler.CompactSampler) SamplerPlusTree(com.amazon.randomcutforest.executor.SamplerPlusTree) ShingleBuilder(com.amazon.randomcutforest.util.ShingleBuilder) Function(java.util.function.Function) ArrayList(java.util.ArrayList) SimulatedTransductiveScalarScoreVisitor(com.amazon.randomcutforest.anomalydetection.SimulatedTransductiveScalarScoreVisitor) PointStore(com.amazon.randomcutforest.store.PointStore) DynamicAttributionVisitor(com.amazon.randomcutforest.anomalydetection.DynamicAttributionVisitor) ConvergingAccumulator(com.amazon.randomcutforest.returntypes.ConvergingAccumulator) Config(com.amazon.randomcutforest.config.Config) SimpleInterpolationVisitor(com.amazon.randomcutforest.interpolation.SimpleInterpolationVisitor) InterpolationMeasure(com.amazon.randomcutforest.returntypes.InterpolationMeasure) IPointStore(com.amazon.randomcutforest.store.IPointStore) SequentialForestUpdateExecutor(com.amazon.randomcutforest.executor.SequentialForestUpdateExecutor) ArrayUtils(com.amazon.randomcutforest.util.ArrayUtils) OneSidedConvergingDiVectorAccumulator(com.amazon.randomcutforest.returntypes.OneSidedConvergingDiVectorAccumulator) CommonUtils.checkArgument(com.amazon.randomcutforest.CommonUtils.checkArgument) DynamicScoreVisitor(com.amazon.randomcutforest.anomalydetection.DynamicScoreVisitor) DiVector(com.amazon.randomcutforest.returntypes.DiVector) ITree(com.amazon.randomcutforest.tree.ITree) ConditionalTreeSample(com.amazon.randomcutforest.returntypes.ConditionalTreeSample) ConditionalSampleSummary(com.amazon.randomcutforest.returntypes.ConditionalSampleSummary) Collections(java.util.Collections) IStreamSampler(com.amazon.randomcutforest.sampler.IStreamSampler) DynamicAttributionVisitor(com.amazon.randomcutforest.anomalydetection.DynamicAttributionVisitor) DiVector(com.amazon.randomcutforest.returntypes.DiVector)

Example 34 with BinaryOperator

use of java.util.function.BinaryOperator in project Alchemist by AlchemistSimulator.

the class ConrecIsolinesFinder method findIsolines.

/**
 * {@inheritDoc}
 */
@Override
public Collection<Isoline> findIsolines(final BinaryOperator<Number> function, final Number x1, final Number y1, final Number x2, final Number y2, final Collection<Number> levels) {
    Objects.requireNonNull(function);
    Objects.requireNonNull(x1);
    Objects.requireNonNull(y1);
    Objects.requireNonNull(x2);
    Objects.requireNonNull(y2);
    Objects.requireNonNull(levels);
    // preparing parameters for the algorithm
    final double startX = x1.doubleValue();
    final double startY = y1.doubleValue();
    final double endX = x2.doubleValue();
    final double endY = y2.doubleValue();
    // the rectangular region defined by start and end is continuous,
    // this means we will sample a discrete amount of points in that region.
    // How many points will be sampled on each side is defined by SAMPLE,
    // step is the distance between two of these points
    final double stepX = (endX - startX) / SAMPLES;
    final double stepY = (endY - startY) / SAMPLES;
    // indexes for accessing x, y and d arrays
    final int ilb = 0;
    final int jlb = 0;
    final int iub = SAMPLES;
    final int jub = SAMPLES;
    // x and y contains respectively the x and y coordinates of the sampling points
    // +1 because iub, jub are included
    final double[] x = new double[iub + 1];
    final double[] y = new double[jub + 1];
    for (int i = ilb; i <= iub; i++) {
        if (i == ilb) {
            x[i] = startX;
        } else {
            x[i] = x[i - 1] + stepX;
        }
    }
    for (int j = jlb; j <= jub; j++) {
        if (j == jlb) {
            y[j] = startY;
        } else {
            y[j] = y[j - 1] + stepY;
        }
    }
    // d contains the data of the sampling points, d[i][j] = f(x[i], x[j])
    final double[][] d = new double[iub + 1][jub + 1];
    for (int i = ilb; i <= iub; i++) {
        for (int j = jlb; j <= jub; j++) {
            d[i][j] = function.apply(x[i], y[j]).doubleValue();
        }
    }
    // finding the isolines
    final Map<Double, List<Segment2D>> isolines = new HashMap<>();
    try {
        new Conrec((startX1, startY1, endX1, endY1, contourLevel) -> {
            // render
            final Segment2D segment = factory.makeSegment(startX1, startY1, endX1, endY1);
            final double flooredValue = Math.floor(contourLevel * 10) / 10;
            if (!isolines.containsKey(flooredValue)) {
                isolines.put(flooredValue, new ArrayList<>());
            }
            isolines.get(flooredValue).add(segment);
        }).contour(d, ilb, iub, jlb, jub, x, y, levels.size(), levels.stream().mapToDouble(Number::doubleValue).toArray());
    } catch (Exception e) {
        // NOPMD
        // this is horrible but necessary
        L.warn("couldn't find isolines, conrec threw an exception: " + e.getMessage());
    }
    return isolines.entrySet().stream().map(entry -> factory.makeIsoline(entry.getKey(), entry.getValue())).collect(Collectors.toList());
}
Also used : Objects(java.util.Objects) List(java.util.List) Logger(org.slf4j.Logger) Collection(java.util.Collection) Map(java.util.Map) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Conrec(conrec.Conrec) Collectors(java.util.stream.Collectors) DrawShape(it.unibo.alchemist.boundary.gui.effects.DrawShape) BinaryOperator(java.util.function.BinaryOperator) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Conrec(conrec.Conrec) List(java.util.List) ArrayList(java.util.ArrayList)

Example 35 with BinaryOperator

use of java.util.function.BinaryOperator in project Mycat2 by MyCATApache.

the class DrdsSqlWithParams method getHintDataNodeFilter.

public Optional<List<PartitionGroup>> getHintDataNodeFilter() {
    for (MycatHint hint : this.getHints()) {
        for (MycatHint.Function hintFunction : hint.getFunctions()) {
            if ("SCAN".equalsIgnoreCase(hintFunction.getName())) {
                List<MycatHint.Argument> arguments = hintFunction.getArguments();
                Map<String, List<String>> collect = arguments.stream().collect(Collectors.toMap(m -> SQLUtils.toSQLString(m.getName()), v -> Arrays.stream(SQLUtils.normalize(SQLUtils.toSQLString((v.getValue())).replace("'", "").replace("(", "").replace(")", "").trim()).split(",")).map(i -> SQLUtils.normalize(i)).collect(Collectors.toList())));
                NameMap<List<String>> nameMap = NameMap.immutableCopyOf(collect);
                List<String> condition = new LinkedList<>(Optional.ofNullable(nameMap.get("CONDITION", false)).orElse(Collections.emptyList()));
                List<String> logicalTables = new LinkedList<>(Optional.ofNullable(nameMap.get("TABLE", false)).orElse(Collections.emptyList()));
                List<List<String>> physicalTables = new LinkedList<>(Optional.ofNullable(nameMap.get("PARTITION", false)).orElse(Collections.emptyList()).stream().map(i -> Arrays.asList(i.split(","))).collect(Collectors.toList()));
                if (!physicalTables.isEmpty()) {
                    for (MycatHint.Argument argument : arguments) {
                        if ("PARTITION".equals(argument.getName().toString())) {
                            if (argument.getValue() instanceof SQLListExpr) {
                                physicalTables.clear();
                                SQLListExpr argumentValue = (SQLListExpr) argument.getValue();
                                for (Object o : argumentValue.getItems()) {
                                    ArrayList<String> objects = new ArrayList<>();
                                    physicalTables.add(objects);
                                    for (String s : Arrays.asList(SQLUtils.normalize(o.toString()).split(","))) {
                                        objects.add(SQLUtils.normalize(s));
                                    }
                                }
                            }
                        }
                    }
                }
                HashSet<String> targets = new HashSet<>(Optional.ofNullable(nameMap.get("TARGET", false)).orElse(Collections.emptyList()));
                Predicate<PartitionGroup> targetFilter = targets.isEmpty() ? (u) -> true : new Predicate<PartitionGroup>() {

                    @Override
                    public boolean test(PartitionGroup stringPartitionMap) {
                        String targetName = stringPartitionMap.getTargetName();
                        boolean contains = targets.contains(targetName);
                        return contains;
                    }
                };
                SQLStatement parameterizedStatement = this.getParameterizedStatement();
                Detector detector = new Detector();
                parameterizedStatement.accept(detector);
                if (logicalTables.isEmpty()) {
                    logicalTables.addAll(detector.tableHandlerMap.keySet());
                }
                if (condition.isEmpty()) {
                    condition.add("true");
                }
                if (!detector.isSharding) {
                    List<Partition> partitions = Collections.emptyList();
                    List<Partition> newPartitions = Collections.emptyList();
                    if (!logicalTables.isEmpty()) {
                        if (!condition.isEmpty()) {
                            String sql = "select * from " + logicalTables.get(0) + "where " + condition;
                            QueryPlanner queryPlanner = MetaClusterCurrent.wrapper(QueryPlanner.class);
                            DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sql, null);
                            CodeExecuterContext codeExecuterContext = queryPlanner.innerComputeMinCostCodeExecuterContext(drdsSqlWithParams);
                            List<PartitionGroup> collect2 = codeExecuterContext.getRelContext().values().stream().flatMap(i -> AsyncMycatDataContextImpl.getSqlMap(codeExecuterContext.getConstantMap(), i.getRelNode(), drdsSqlWithParams, Optional.empty()).stream()).filter(targetFilter).collect(Collectors.toList());
                            return Optional.of(collect2);
                        } else if (!physicalTables.isEmpty()) {
                            newPartitions = physicalTables.get(0).stream().map(dataNodeParser()).collect(Collectors.toList());
                        }
                        if (!targets.isEmpty()) {
                            newPartitions = partitions.stream().filter(dataNode -> {
                                return targets.contains(dataNode.getTargetName());
                            }).collect(Collectors.toList());
                        }
                        List<Partition> finalNewPartitions = newPartitions;
                        List<PartitionGroup> maps = finalNewPartitions.stream().map(i -> {
                            return new PartitionGroup(i.getTargetName(), ImmutableMap.of(logicalTables.get(0), i));
                        }).collect(Collectors.toList());
                        return Optional.of(maps);
                    }
                } else {
                    Map<String, List<Partition>> logicalPhysicalMap = new HashMap<>();
                    int physicalTableCount = 0;
                    if (!physicalTables.isEmpty()) {
                        int size = logicalTables.size();
                        for (int i = 0; i < size; i++) {
                            String alias = logicalTables.get(i);
                            List<Partition> phyTableList = physicalTables.get(i).stream().map(dataNodeParser()).collect(Collectors.toList());
                            physicalTableCount += phyTableList.size();
                            alias = detector.tableHandlerMap.get(alias).getUniqueName();
                            List<Partition> partitions = logicalPhysicalMap.computeIfAbsent(alias, s -> new ArrayList<>());
                            partitions.addAll(phyTableList);
                        }
                    }
                    if (!logicalTables.isEmpty() && !condition.isEmpty()) {
                        String where = condition.get(0);
                        SQLExpr oneSqlExpr = SQLUtils.toMySqlExpr(where);
                        List<SQLExpr> sqlExprs;
                        if (oneSqlExpr instanceof SQLBinaryOpExpr && ((SQLBinaryOpExpr) oneSqlExpr).getOperator() == SQLBinaryOperator.BooleanAnd) {
                            sqlExprs = SQLUtils.split((SQLBinaryOpExpr) oneSqlExpr);
                        } else {
                            sqlExprs = Collections.singletonList(oneSqlExpr);
                        }
                        Map<String, TableHandler> aliasTableMap = detector.tableHandlerMap;
                        int whereIndex = 0;
                        List<List<PartitionGroup>> partitionGroupMap = new ArrayList<>();
                        for (Map.Entry<String, TableHandler> e : aliasTableMap.entrySet()) {
                            TableHandler table = e.getValue();
                            String schemaName = table.getSchemaName();
                            String tableName = table.getTableName();
                            String sql = "select *  from `" + schemaName + "`.`" + tableName + "` as `" + e.getKey() + "` where " + ((whereIndex < sqlExprs.size()) ? sqlExprs.get(whereIndex).toString() : "true");
                            whereIndex++;
                            DrdsSqlWithParams hintSql = DrdsRunnerHelper.preParse(sql, null);
                            QueryPlanner planner = MetaClusterCurrent.wrapper(QueryPlanner.class);
                            ParamHolder paramHolder = ParamHolder.CURRENT_THREAD_LOCAL.get();
                            paramHolder.setData(hintSql.getParams(), hintSql.getTypeNames());
                            try {
                                CodeExecuterContext codeExecuterContext = planner.innerComputeMinCostCodeExecuterContext(hintSql);
                                partitionGroupMap.add(codeExecuterContext.getRelContext().values().stream().flatMap(i -> AsyncMycatDataContextImpl.getSqlMap(codeExecuterContext.getConstantMap(), i.getRelNode(), hintSql, Optional.empty()).stream()).filter(targetFilter).collect(Collectors.toList()));
                            } finally {
                                paramHolder.clear();
                            }
                        }
                        List<PartitionGroup> partitionGroups = partitionGroupMap.stream().reduce(new BinaryOperator<List<PartitionGroup>>() {

                            @Override
                            public List<PartitionGroup> apply(List<PartitionGroup> partitionGroups, List<PartitionGroup> partitionGroups2) {
                                for (PartitionGroup partitionGroup : partitionGroups) {
                                    Optional<PartitionGroup> first = partitionGroups2.stream().filter(i -> i.getTargetName().equals(partitionGroup.getTargetName())).findFirst();
                                    first.ifPresent(group -> partitionGroup.map.putAll(group.map));
                                }
                                return partitionGroups;
                            }
                        }).orElse(Collections.emptyList());
                        if (!logicalPhysicalMap.isEmpty()) {
                            int index = 0;
                            for (PartitionGroup group : new ArrayList<>(partitionGroups)) {
                                for (Map.Entry<String, Partition> entry : new ArrayList<>(group.getMap().entrySet())) {
                                    List<Partition> partitions = logicalPhysicalMap.get(entry.getKey());
                                    if (partitions == null)
                                        continue;
                                    if (index >= partitions.size()) {
                                        partitionGroups.remove(group);
                                        continue;
                                    }
                                    entry.setValue(partitions.get(index));
                                }
                                ++index;
                            }
                        }
                        return Optional.of(partitionGroups);
                    }
                }
            }
        }
    }
    return Optional.empty();
}
Also used : QueryPlanner(io.mycat.calcite.spm.QueryPlanner) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) Function(java.util.function.Function) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLBinaryOperator(com.alibaba.druid.sql.ast.expr.SQLBinaryOperator) ParamHolder(io.mycat.calcite.spm.ParamHolder) SQLListExpr(com.alibaba.druid.sql.ast.expr.SQLListExpr) MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) ImmutableMap(org.apache.curator.shaded.com.google.common.collect.ImmutableMap) ToString(lombok.ToString) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Predicate(java.util.function.Predicate) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) NameMap(io.mycat.util.NameMap) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) DrdsRunnerHelper(io.mycat.calcite.DrdsRunnerHelper) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MycatHint(io.mycat.calcite.MycatHint) NotNull(org.jetbrains.annotations.NotNull) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) ToString(lombok.ToString) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLBinaryOperator(com.alibaba.druid.sql.ast.expr.SQLBinaryOperator) BinaryOperator(java.util.function.BinaryOperator) MycatHint(io.mycat.calcite.MycatHint) QueryPlanner(io.mycat.calcite.spm.QueryPlanner) MycatHint(io.mycat.calcite.MycatHint) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) ParamHolder(io.mycat.calcite.spm.ParamHolder) SQLListExpr(com.alibaba.druid.sql.ast.expr.SQLListExpr) ImmutableMap(org.apache.curator.shaded.com.google.common.collect.ImmutableMap) NameMap(io.mycat.util.NameMap)

Aggregations

BinaryOperator (java.util.function.BinaryOperator)38 List (java.util.List)20 ArrayList (java.util.ArrayList)12 Arrays (java.util.Arrays)12 Function (java.util.function.Function)10 Test (org.junit.jupiter.api.Test)10 Collections (java.util.Collections)8 Optional (java.util.Optional)8 Collectors (java.util.stream.Collectors)8 Stream (java.util.stream.Stream)8 BiFunction (java.util.function.BiFunction)7 Map (java.util.Map)6 Collector (java.util.stream.Collector)6 CommonUtils.checkArgument (com.amazon.randomcutforest.CommonUtils.checkArgument)4 CommonUtils.checkNotNull (com.amazon.randomcutforest.CommonUtils.checkNotNull)4 CommonUtils.toDoubleArray (com.amazon.randomcutforest.CommonUtils.toDoubleArray)4 CommonUtils.toFloatArray (com.amazon.randomcutforest.CommonUtils.toFloatArray)4 AnomalyAttributionVisitor (com.amazon.randomcutforest.anomalydetection.AnomalyAttributionVisitor)4 AnomalyScoreVisitor (com.amazon.randomcutforest.anomalydetection.AnomalyScoreVisitor)4 DynamicAttributionVisitor (com.amazon.randomcutforest.anomalydetection.DynamicAttributionVisitor)4