use of io.mycat.RangeVariableType 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;
}
Aggregations