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;
}
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;
}
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;
}
Aggregations