Search in sources :

Example 1 with ComparisonOperator

use of io.mycat.querycondition.ComparisonOperator in project Mycat2 by MyCATApache.

the class ValueIndexCondition method getPartitions.

@NotNull
public static List<Partition> getPartitions(CustomRuleFunction customRuleFunction, Map<QueryType, List<ValueIndexCondition>> conditions, List<Object> params) {
    if (conditions == null || conditions.isEmpty()) {
        return customRuleFunction.calculate(Collections.emptyMap());
    }
    Objects.requireNonNull(conditions);
    List<Partition> partitions = customRuleFunction.calculate(Collections.emptyMap());
    for (Map.Entry<QueryType, List<ValueIndexCondition>> entry : conditions.entrySet()) {
        for (ValueIndexCondition condition : entry.getValue()) {
            List<Object> pointQueryKey = resolveParam(params, condition.getPointQueryKey());
            ComparisonOperator rangeQueryLowerOp = condition.getRangeQueryLowerOp();
            List<Object> rangeQueryLowerKey = resolveParam(params, condition.getRangeQueryLowerKey());
            ComparisonOperator rangeQueryUpperOp = condition.getRangeQueryUpperOp();
            List<Object> rangeQueryUpperKey = resolveParam(params, condition.getRangeQueryUpperKey());
            switch(condition.getQueryType()) {
                case PK_POINT_QUERY:
                    {
                        // queryByPrimaryKey
                        Map<String, RangeVariable> map = new HashMap<>();
                        if (pointQueryKey.size() > 1) {
                            List<Partition> curPartitions = new LinkedList<>();
                            for (Object o1 : pointQueryKey) {
                                String indexColumnName = condition.getIndexColumnNames().get(0);
                                RangeVariable rangeVariable = new RangeVariable(indexColumnName, RangeVariableType.EQUAL, o1);
                                map.put(indexColumnName, rangeVariable);
                                List<Partition> calculate = customRuleFunction.calculate(map);
                                curPartitions.addAll(calculate);
                            }
                            partitions = calculatePartitions(curPartitions, partitions);
                        } else {
                            Object o = pointQueryKey.get(0);
                            String indexColumnName = condition.getIndexColumnNames().get(0);
                            RangeVariable rangeVariable = new RangeVariable(indexColumnName, RangeVariableType.EQUAL, o);
                            map.put(indexColumnName, rangeVariable);
                            partitions = calculatePartitions(customRuleFunction, map, partitions);
                        }
                        break;
                    }
                case PK_RANGE_QUERY:
                    {
                        ArrayList<Object> leftValues = new ArrayList<>(2);
                        ArrayList<Object> rightValues = new ArrayList<>(2);
                        if (rangeQueryUpperOp == ComparisonOperator.LT || rangeQueryUpperOp == ComparisonOperator.LTE) {
                            for (Object o1 : rangeQueryLowerKey) {
                                if (o1 instanceof RexNode) {
                                    o1 = MycatRexUtil.resolveParam((RexNode) o1, params);
                                }
                                leftValues.add(o1);
                            }
                            Collections.sort((List) rangeQueryLowerKey);
                        }
                        if (rangeQueryLowerOp == ComparisonOperator.GT || rangeQueryLowerOp == ComparisonOperator.GTE) {
                            for (Object o1 : rangeQueryUpperKey) {
                                if (o1 instanceof RexNode) {
                                    o1 = MycatRexUtil.resolveParam((RexNode) o1, params);
                                }
                                rightValues.add(o1);
                            }
                            Collections.sort((List) rangeQueryUpperKey);
                        }
                        Object smallOne = null;
                        Object bigOne = null;
                        if (!rangeQueryLowerKey.isEmpty()) {
                            smallOne = rangeQueryLowerKey.get(0);
                        }
                        if (!rangeQueryUpperKey.isEmpty()) {
                            bigOne = rangeQueryUpperKey.get(rangeQueryUpperKey.size() - 1);
                        }
                        Map<String, RangeVariable> map = new HashMap<>();
                        if (smallOne != null && bigOne != null && rangeQueryUpperOp == ComparisonOperator.LTE && rangeQueryLowerOp == ComparisonOperator.GTE) {
                            for (String indexColumnName : condition.getIndexColumnNames()) {
                                RangeVariable rangeVariable = new RangeVariable(indexColumnName, RangeVariableType.RANGE, smallOne, bigOne);
                                map.put(indexColumnName, rangeVariable);
                            }
                        } else if (smallOne != null) {
                            RangeVariableType type = null;
                            if (rangeQueryUpperOp == ComparisonOperator.LT) {
                                type = RangeVariableType.LT;
                            } else if (rangeQueryUpperOp == ComparisonOperator.LTE) {
                                type = RangeVariableType.LTE;
                            }
                            if (type != null) {
                                for (String indexColumnName : condition.getIndexColumnNames()) {
                                    RangeVariable rangeVariable = new RangeVariable(indexColumnName, type, smallOne);
                                    map.put(indexColumnName, rangeVariable);
                                }
                            }
                        } else if (bigOne != null) {
                            RangeVariableType type = null;
                            if (rangeQueryUpperOp == ComparisonOperator.GT) {
                                type = RangeVariableType.GT;
                            } else if (rangeQueryUpperOp == ComparisonOperator.GTE) {
                                type = RangeVariableType.GTE;
                            }
                            if (type != null) {
                                for (String indexColumnName : condition.getIndexColumnNames()) {
                                    RangeVariable rangeVariable = new RangeVariable(indexColumnName, type, bigOne);
                                    map.put(indexColumnName, rangeVariable);
                                }
                            }
                        }
                        partitions = calculatePartitions(customRuleFunction, map, partitions);
                        break;
                    }
                case PK_FULL_SCAN:
                    break;
                default:
            }
        }
    }
    return partitions;
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) ComparisonOperator(io.mycat.querycondition.ComparisonOperator) RangeVariableType(io.mycat.RangeVariableType) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) ByteString(org.apache.calcite.avatica.util.ByteString) TimestampString(org.apache.calcite.util.TimestampString) TimeString(org.apache.calcite.util.TimeString) ToString(lombok.ToString) QueryType(io.mycat.querycondition.QueryType) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ComparisonOperator

use of io.mycat.querycondition.ComparisonOperator in project Mycat2 by MyCATApache.

the class PredicateAnalyzer method handleRangeQuery.

private static IndexCondition handleRangeQuery(IndexCondition condition, Collection<InternalRexNode> leftMostKeyNodes, List<RexNode> pushDownRexNodeList, List<RexNode> remainderRexNodeList, String... opList) {
    Optional<InternalRexNode> node = findFirstOp(leftMostKeyNodes, opList);
    if (node.isPresent()) {
        pushDownRexNodeList.add(node.get().node);
        remainderRexNodeList.remove(node.get().node);
        ComparisonOperator op = ComparisonOperator.parse(node.get().op);
        if (ComparisonOperator.isLowerBoundOp(opList)) {
            return condition.withQueryType(QueryType.PK_RANGE_QUERY);
        } else if (ComparisonOperator.isUpperBoundOp(opList)) {
            return condition.withQueryType(QueryType.PK_RANGE_QUERY);
        } else {
            throw new AssertionError("comparison operation is invalid " + op);
        }
    }
    return condition;
}
Also used : ComparisonOperator(io.mycat.querycondition.ComparisonOperator)

Example 3 with ComparisonOperator

use of io.mycat.querycondition.ComparisonOperator in project Mycat2 by MyCATApache.

the class ValuePredicateAnalyzer method handleRangeQuery.

private static ValueIndexCondition handleRangeQuery(ValueIndexCondition condition, Collection<InternalRexNode> leftMostKeyNodes, List<RexNode> pushDownRexNodeList, List<RexNode> remainderRexNodeList, String... opList) {
    Optional<InternalRexNode> node = findFirstOp(leftMostKeyNodes, opList);
    if (node.isPresent()) {
        pushDownRexNodeList.add(node.get().node);
        remainderRexNodeList.remove(node.get().node);
        List<Object> key = createKey(Lists.newArrayList(node.get()));
        ComparisonOperator op = ComparisonOperator.parse(node.get().op);
        if (ComparisonOperator.isLowerBoundOp(opList)) {
            return condition.withQueryType(QueryType.PK_RANGE_QUERY).withRangeQueryLowerOp(op).withRangeQueryLowerKey(key);
        } else if (ComparisonOperator.isUpperBoundOp(opList)) {
            return condition.withQueryType(QueryType.PK_RANGE_QUERY).withRangeQueryUpperOp(op).withRangeQueryUpperKey(key);
        } else {
            throw new AssertionError("comparison operation is invalid " + op);
        }
    }
    return condition;
}
Also used : ComparisonOperator(io.mycat.querycondition.ComparisonOperator)

Aggregations

ComparisonOperator (io.mycat.querycondition.ComparisonOperator)3 Partition (io.mycat.Partition)1 RangeVariable (io.mycat.RangeVariable)1 RangeVariableType (io.mycat.RangeVariableType)1 QueryType (io.mycat.querycondition.QueryType)1 ToString (lombok.ToString)1 ByteString (org.apache.calcite.avatica.util.ByteString)1 DateString (org.apache.calcite.util.DateString)1 NlsString (org.apache.calcite.util.NlsString)1 TimeString (org.apache.calcite.util.TimeString)1 TimestampString (org.apache.calcite.util.TimestampString)1 NotNull (org.jetbrains.annotations.NotNull)1