This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new ccca38d08be Support extracting sharding condition parameter marker
indexes (#21235)
ccca38d08be is described below
commit ccca38d08beb537fc2181b1c3d12808345b366c9
Author: 吴伟杰 <[email protected]>
AuthorDate: Wed Sep 28 16:51:41 2022 +0800
Support extracting sharding condition parameter marker indexes (#21235)
* Support extracting sharding condition parameter marker indexes
* Complete ConditionValueBetweenOperatorGenerator
* Format InsertClauseShardingConditionEngine
* Complete ConditionValueBetweenOperatorGeneratorTest
* Complete ConditionValueCompareOperatorGeneratorTest
* Complete ConditionValueInOperatorGeneratorTest
* Complete InsertClauseShardingConditionEngineTest
* Complete ConditionValueTest
* Format codes
* Revise InsertClauseShardingConditionEngineTest
---
.../impl/InsertClauseShardingConditionEngine.java | 6 ++++-
.../impl/WhereClauseShardingConditionEngine.java | 11 +++++---
.../engine/condition/generator/ConditionValue.java | 12 +++++++++
.../ConditionValueBetweenOperatorGenerator.java | 30 ++++++++++++++--------
.../ConditionValueCompareOperatorGenerator.java | 22 ++++++++--------
.../impl/ConditionValueInOperatorGenerator.java | 13 +++++++---
.../value/AlwaysFalseShardingConditionValue.java | 8 ++++++
.../value/ListShardingConditionValue.java | 8 ++++++
.../value/RangeShardingConditionValue.java | 9 +++++++
.../condition/value/ShardingConditionValue.java | 9 +++++++
.../InsertClauseShardingConditionEngineTest.java | 16 ++++++++++--
.../condition/generator/ConditionValueTest.java | 23 +++++++++++------
...ConditionValueBetweenOperatorGeneratorTest.java | 5 ++++
...ConditionValueCompareOperatorGeneratorTest.java | 7 +++++
.../ConditionValueInOperatorGeneratorTest.java | 4 ++-
15 files changed, 143 insertions(+), 40 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
index 3518bf76345..43a88648f88 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
@@ -103,7 +103,11 @@ public final class InsertClauseShardingConditionEngine
implements ShardingCondit
continue;
}
if (each instanceof SimpleExpressionSegment) {
- result.getValues().add(new
ListShardingConditionValue<>(shardingColumn.get(), tableName,
Collections.singletonList(getShardingValue((SimpleExpressionSegment) each,
parameters))));
+ List<Integer> parameterMarkerIndexes = each instanceof
ParameterMarkerExpressionSegment
+ ?
Collections.singletonList(((ParameterMarkerExpressionSegment)
each).getParameterMarkerIndex())
+ : Collections.emptyList();
+ result.getValues().add(new
ListShardingConditionValue<>(shardingColumn.get(), tableName,
Collections.singletonList(getShardingValue((SimpleExpressionSegment) each,
parameters)),
+ parameterMarkerIndexes));
} else if (each instanceof CommonExpressionSegment) {
generateShardingCondition((CommonExpressionSegment) each,
result, shardingColumn.get(), tableName);
} else if (ExpressionConditionUtils.isNowExpression(each)) {
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/WhereClauseShardingConditionEngine.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/WhereClauseShardingConditionEngine.java
index 7e6c33a03c7..bc8803f45ce 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/WhereClauseShardingConditionEngine.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/WhereClauseShardingConditionEngine.java
@@ -47,11 +47,13 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
+import java.util.Set;
/**
* Sharding condition engine for where clause.
@@ -137,7 +139,9 @@ public final class WhereClauseShardingConditionEngine
implements ShardingConditi
private ShardingConditionValue mergeShardingConditionValues(final Column
column, final Collection<ShardingConditionValue> shardingConditionValues) {
Collection<Comparable<?>> listValue = null;
Range<Comparable<?>> rangeValue = null;
+ Set<Integer> parameterMarkerIndexes = new HashSet<>();
for (ShardingConditionValue each : shardingConditionValues) {
+ parameterMarkerIndexes.addAll(each.getParameterMarkerIndexes());
if (each instanceof ListShardingConditionValue) {
listValue =
mergeListShardingValues(((ListShardingConditionValue) each).getValues(),
listValue);
if (listValue.isEmpty()) {
@@ -152,13 +156,14 @@ public final class WhereClauseShardingConditionEngine
implements ShardingConditi
}
}
if (null == listValue) {
- return new RangeShardingConditionValue<>(column.getName(),
column.getTableName(), rangeValue);
+ return new RangeShardingConditionValue<>(column.getName(),
column.getTableName(), rangeValue, new ArrayList<>(parameterMarkerIndexes));
}
if (null == rangeValue) {
- return new ListShardingConditionValue<>(column.getName(),
column.getTableName(), listValue);
+ return new ListShardingConditionValue<>(column.getName(),
column.getTableName(), listValue, new ArrayList<>(parameterMarkerIndexes));
}
listValue = mergeListAndRangeShardingValues(listValue, rangeValue);
- return listValue.isEmpty() ? new AlwaysFalseShardingConditionValue() :
new ListShardingConditionValue<>(column.getName(), column.getTableName(),
listValue);
+ return listValue.isEmpty() ? new AlwaysFalseShardingConditionValue()
+ : new ListShardingConditionValue<>(column.getName(),
column.getTableName(), listValue, new ArrayList<>(parameterMarkerIndexes));
}
private Collection<Comparable<?>> mergeListShardingValues(final
Collection<Comparable<?>> value1, final Collection<Comparable<?>> value2) {
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/ConditionValue.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/ConditionValue.java
index 52c9d5e09a1..b3a7ff5f3f6 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/ConditionValue.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/ConditionValue.java
@@ -32,8 +32,11 @@ public final class ConditionValue {
private final Comparable<?> value;
+ private final int parameterMarkerIndex;
+
public ConditionValue(final ExpressionSegment expressionSegment, final
List<Object> parameters) {
value = getValue(expressionSegment, parameters);
+ parameterMarkerIndex = expressionSegment instanceof
ParameterMarkerExpressionSegment ? ((ParameterMarkerExpressionSegment)
expressionSegment).getParameterMarkerIndex() : -1;
}
private Comparable<?> getValue(final ExpressionSegment expressionSegment,
final List<Object> parameters) {
@@ -70,4 +73,13 @@ public final class ConditionValue {
public Optional<Comparable<?>> getValue() {
return Optional.ofNullable(value);
}
+
+ /**
+ * Get parameter marker index.
+ *
+ * @return parameter marker index
+ */
+ public Optional<Integer> getParameterMarkerIndex() {
+ return parameterMarkerIndex > -1 ? Optional.of(parameterMarkerIndex) :
Optional.empty();
+ }
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGenerator.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGenerator.java
index 1769777737c..2a6ad693497 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGenerator.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGenerator.java
@@ -28,6 +28,7 @@ import
org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingC
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BetweenExpression;
import
org.apache.shardingsphere.sql.parser.sql.common.util.SafeNumberOperationUtil;
+import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
@@ -39,20 +40,27 @@ public final class ConditionValueBetweenOperatorGenerator
implements ConditionVa
@Override
public Optional<ShardingConditionValue> generate(final BetweenExpression
predicate, final Column column, final List<Object> parameters) {
- Optional<Comparable<?>> betweenConditionValue = new
ConditionValue(predicate.getBetweenExpr(), parameters).getValue();
- Optional<Comparable<?>> andConditionValue = new
ConditionValue(predicate.getAndExpr(), parameters).getValue();
- if (betweenConditionValue.isPresent() &&
andConditionValue.isPresent()) {
- return Optional.of(new
RangeShardingConditionValue<>(column.getName(), column.getTableName(),
SafeNumberOperationUtil.safeClosed(betweenConditionValue.get(),
andConditionValue.get())));
+ ConditionValue betweenConditionValue = new
ConditionValue(predicate.getBetweenExpr(), parameters);
+ ConditionValue andConditionValue = new
ConditionValue(predicate.getAndExpr(), parameters);
+ Optional<Comparable<?>> betweenValue =
betweenConditionValue.getValue();
+ Optional<Comparable<?>> andValue = andConditionValue.getValue();
+ List<Integer> parameterMarkerIndexes = new ArrayList<>(2);
+
betweenConditionValue.getParameterMarkerIndex().ifPresent(parameterMarkerIndexes::add);
+
andConditionValue.getParameterMarkerIndex().ifPresent(parameterMarkerIndexes::add);
+ if (betweenValue.isPresent() && andValue.isPresent()) {
+ return Optional.of(new
RangeShardingConditionValue<>(column.getName(), column.getTableName(),
SafeNumberOperationUtil.safeClosed(betweenValue.get(), andValue.get()),
+ parameterMarkerIndexes));
}
Date datetime = DatetimeServiceFactory.getInstance().getDatetime();
- if (!betweenConditionValue.isPresent() &&
ExpressionConditionUtils.isNowExpression(predicate.getBetweenExpr())) {
- betweenConditionValue = Optional.of(datetime);
+ if (!betweenValue.isPresent() &&
ExpressionConditionUtils.isNowExpression(predicate.getBetweenExpr())) {
+ betweenValue = Optional.of(datetime);
}
- if (!andConditionValue.isPresent() &&
ExpressionConditionUtils.isNowExpression(predicate.getAndExpr())) {
- andConditionValue = Optional.of(datetime);
+ if (!andValue.isPresent() &&
ExpressionConditionUtils.isNowExpression(predicate.getAndExpr())) {
+ andValue = Optional.of(datetime);
}
- return betweenConditionValue.isPresent() &&
andConditionValue.isPresent()
- ? Optional.of(new
RangeShardingConditionValue<>(column.getName(), column.getTableName(),
Range.closed(betweenConditionValue.get(), andConditionValue.get())))
- : Optional.empty();
+ if (!betweenValue.isPresent() || !andValue.isPresent()) {
+ return Optional.empty();
+ }
+ return Optional.of(new RangeShardingConditionValue<>(column.getName(),
column.getTableName(), Range.closed(betweenValue.get(), andValue.get()),
parameterMarkerIndexes));
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGenerator.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGenerator.java
index e392a165911..2182df4e2ed 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGenerator.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGenerator.java
@@ -62,30 +62,32 @@ public final class ConditionValueCompareOperatorGenerator
implements ConditionVa
return Optional.empty();
}
ExpressionSegment valueExpression = predicate.getLeft() instanceof
ColumnSegment ? predicate.getRight() : predicate.getLeft();
- Optional<Comparable<?>> conditionValue = new
ConditionValue(valueExpression, parameters).getValue();
- if (conditionValue.isPresent()) {
- return generate(conditionValue.get(), column, operator);
+ ConditionValue conditionValue = new ConditionValue(valueExpression,
parameters);
+ Optional<Comparable<?>> value = conditionValue.getValue();
+ if (value.isPresent()) {
+ return generate(value.get(), column, operator,
conditionValue.getParameterMarkerIndex().orElse(-1));
}
if (ExpressionConditionUtils.isNowExpression(valueExpression)) {
- return
generate(DatetimeServiceFactory.getInstance().getDatetime(), column, operator);
+ return
generate(DatetimeServiceFactory.getInstance().getDatetime(), column, operator,
-1);
}
return Optional.empty();
}
- private Optional<ShardingConditionValue> generate(final Comparable<?>
comparable, final Column column, final String operator) {
+ private Optional<ShardingConditionValue> generate(final Comparable<?>
comparable, final Column column, final String operator, final int
parameterMarkerIndex) {
String columnName = column.getName();
String tableName = column.getTableName();
+ List<Integer> parameterMarkerIndexes = parameterMarkerIndex > -1 ?
Collections.singletonList(parameterMarkerIndex) : Collections.emptyList();
switch (operator) {
case EQUAL:
- return Optional.of(new
ListShardingConditionValue<>(columnName, tableName, new
ArrayList<>(Collections.singleton(comparable))));
+ return Optional.of(new
ListShardingConditionValue<>(columnName, tableName, new
ArrayList<>(Collections.singleton(comparable)), parameterMarkerIndexes));
case GREATER_THAN:
- return Optional.of(new
RangeShardingConditionValue<>(columnName, tableName,
Range.greaterThan(comparable)));
+ return Optional.of(new
RangeShardingConditionValue<>(columnName, tableName,
Range.greaterThan(comparable), parameterMarkerIndexes));
case LESS_THAN:
- return Optional.of(new
RangeShardingConditionValue<>(columnName, tableName,
Range.lessThan(comparable)));
+ return Optional.of(new
RangeShardingConditionValue<>(columnName, tableName,
Range.lessThan(comparable), parameterMarkerIndexes));
case AT_MOST:
- return Optional.of(new
RangeShardingConditionValue<>(columnName, tableName, Range.atMost(comparable)));
+ return Optional.of(new
RangeShardingConditionValue<>(columnName, tableName, Range.atMost(comparable),
parameterMarkerIndexes));
case AT_LEAST:
- return Optional.of(new
RangeShardingConditionValue<>(columnName, tableName,
Range.atLeast(comparable)));
+ return Optional.of(new
RangeShardingConditionValue<>(columnName, tableName, Range.atLeast(comparable),
parameterMarkerIndexes));
default:
return Optional.empty();
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueInOperatorGenerator.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueInOperatorGenerator.java
index fd6842fb6b3..ed4cbc06616 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueInOperatorGenerator.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueInOperatorGenerator.java
@@ -28,6 +28,7 @@ import
org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingC
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.InExpression;
+import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
@@ -40,17 +41,21 @@ public final class ConditionValueInOperatorGenerator
implements ConditionValueGe
@Override
public Optional<ShardingConditionValue> generate(final InExpression
predicate, final Column column, final List<Object> parameters) {
List<Comparable<?>> shardingConditionValues = new LinkedList<>();
+ List<Integer> parameterMarkerIndexes = new
ArrayList<>(predicate.getExpressionList().size());
DatetimeService datetimeService = DatetimeServiceFactory.getInstance();
for (ExpressionSegment each : predicate.getExpressionList()) {
- Optional<Comparable<?>> shardingConditionValue = new
ConditionValue(each, parameters).getValue();
- if (shardingConditionValue.isPresent()) {
- shardingConditionValues.add(shardingConditionValue.get());
+ ConditionValue conditionValue = new ConditionValue(each,
parameters);
+ Optional<Comparable<?>> value = conditionValue.getValue();
+ if (value.isPresent()) {
+ shardingConditionValues.add(value.get());
+
conditionValue.getParameterMarkerIndex().ifPresent(parameterMarkerIndexes::add);
continue;
}
if (ExpressionConditionUtils.isNowExpression(each)) {
shardingConditionValues.add(datetimeService.getDatetime());
}
}
- return shardingConditionValues.isEmpty() ? Optional.empty() :
Optional.of(new ListShardingConditionValue<>(column.getName(),
column.getTableName(), shardingConditionValues));
+ return shardingConditionValues.isEmpty() ? Optional.empty()
+ : Optional.of(new
ListShardingConditionValue<>(column.getName(), column.getTableName(),
shardingConditionValues, parameterMarkerIndexes));
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/AlwaysFalseShardingConditionValue.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/AlwaysFalseShardingConditionValue.java
index 323df1c7252..c13a6378407 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/AlwaysFalseShardingConditionValue.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/AlwaysFalseShardingConditionValue.java
@@ -17,6 +17,9 @@
package org.apache.shardingsphere.sharding.route.engine.condition.value;
+import java.util.Collections;
+import java.util.List;
+
/**
* Always false sharding condition value.
*/
@@ -31,4 +34,9 @@ public final class AlwaysFalseShardingConditionValue
implements ShardingConditio
public String getTableName() {
return "";
}
+
+ @Override
+ public List<Integer> getParameterMarkerIndexes() {
+ return Collections.emptyList();
+ }
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ListShardingConditionValue.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ListShardingConditionValue.java
index ec4c6695c86..583d1f0c699 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ListShardingConditionValue.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ListShardingConditionValue.java
@@ -22,6 +22,8 @@ import lombok.RequiredArgsConstructor;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
import java.util.stream.Collectors;
/**
@@ -37,6 +39,12 @@ public final class ListShardingConditionValue<T extends
Comparable<?>> implement
private final Collection<T> values;
+ private final List<Integer> parameterMarkerIndexes;
+
+ public ListShardingConditionValue(final String columnName, final String
tableName, final Collection<T> values) {
+ this(columnName, tableName, values, Collections.emptyList());
+ }
+
@Override
public String toString() {
String condition = 1 == values.size() ? " = " + new
ArrayList<>(values).get(0) : " in (" +
values.stream().map(Object::toString).collect(Collectors.joining(",")) + ")";
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/RangeShardingConditionValue.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/RangeShardingConditionValue.java
index 6f6f7c242cb..65c4b2615ab 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/RangeShardingConditionValue.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/RangeShardingConditionValue.java
@@ -22,6 +22,9 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
+import java.util.Collections;
+import java.util.List;
+
/**
* Sharding condition value for range.
*/
@@ -35,4 +38,10 @@ public final class RangeShardingConditionValue<T extends
Comparable<?>> implemen
private final String tableName;
private final Range<T> valueRange;
+
+ private final List<Integer> parameterMarkerIndexes;
+
+ public RangeShardingConditionValue(final String columnName, final String
tableName, final Range<T> valueRange) {
+ this(columnName, tableName, valueRange, Collections.emptyList());
+ }
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ShardingConditionValue.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ShardingConditionValue.java
index 09165fdc567..15cbe9a77f8 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ShardingConditionValue.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/value/ShardingConditionValue.java
@@ -17,6 +17,8 @@
package org.apache.shardingsphere.sharding.route.engine.condition.value;
+import java.util.List;
+
/**
* Sharding condition value.
*/
@@ -35,4 +37,11 @@ public interface ShardingConditionValue {
* @return table name
*/
String getTableName();
+
+ /**
+ * Get parameter marker indexes.
+ *
+ * @return parameter marker indexes
+ */
+ List<Integer> getParameterMarkerIndexes();
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/InsertClauseShardingConditionEngineTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/InsertClauseShardingConditionEngineTest.java
index 593c4f2f32c..cd6f2993414 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/InsertClauseShardingConditionEngineTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/InsertClauseShardingConditionEngineTest.java
@@ -17,11 +17,11 @@
package org.apache.shardingsphere.sharding.route.engine.condition.engine;
+import
org.apache.shardingsphere.dialect.exception.data.InsertColumnsAndValuesMismatchedException;
import
org.apache.shardingsphere.infra.binder.segment.insert.keygen.GeneratedKeyContext;
import
org.apache.shardingsphere.infra.binder.segment.insert.values.InsertSelectContext;
import
org.apache.shardingsphere.infra.binder.segment.insert.values.InsertValueContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-import
org.apache.shardingsphere.dialect.exception.data.InsertColumnsAndValuesMismatchedException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition;
import
org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.InsertClauseShardingConditionEngine;
@@ -29,6 +29,7 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.TableRule;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.complex.CommonExpressionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
@@ -45,8 +46,8 @@ import java.util.List;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
@@ -149,6 +150,17 @@ public final class InsertClauseShardingConditionEngineTest
{
assertFalse(shardingConditions.get(0).getValues().isEmpty());
}
+ @Test
+ public void assertCreateShardingConditionsWithParameterMarkers() {
+ InsertValueContext insertValueContext = new
InsertValueContext(Collections.singletonList(new
ParameterMarkerExpressionSegment(0, 0, 0)), Collections.singletonList(1), 0);
+
when(insertStatementContext.getInsertValueContexts()).thenReturn(Collections.singletonList(insertValueContext));
+ when(shardingRule.findShardingColumn(any(),
any())).thenReturn(Optional.of("foo_sharding_col"));
+ List<ShardingCondition> shardingConditions =
shardingConditionEngine.createShardingConditions(insertStatementContext,
Collections.singletonList(1));
+ assertThat(shardingConditions.size(), is(1));
+ assertThat(shardingConditions.get(0).getValues().size(), is(1));
+
assertThat(shardingConditions.get(0).getValues().get(0).getParameterMarkerIndexes(),
is(Collections.singletonList(0)));
+ }
+
@Test
public void assertCreateShardingConditionsSelectStatement() {
when(insertStatementContext.getGeneratedKeyContext()).thenReturn(Optional.empty());
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/ConditionValueTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/ConditionValueTest.java
index afe5e4bc2f0..7399f3dd910 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/ConditionValueTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/ConditionValueTest.java
@@ -19,28 +19,35 @@ package
org.apache.shardingsphere.sharding.route.engine.condition.generator;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
-import org.junit.Before;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.junit.Test;
+import java.util.Collections;
import java.util.LinkedList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public final class ConditionValueTest {
- private ConditionValue conditionValue;
-
- @Before
- public void setUp() {
+ @Test
+ public void assertGetValueFromLiteralExpressionSegment() {
ExpressionSegment expressionSegment = new LiteralExpressionSegment(0,
0, "shardingsphere");
- conditionValue = new ConditionValue(expressionSegment, new
LinkedList<>());
+ ConditionValue conditionValue = new ConditionValue(expressionSegment,
new LinkedList<>());
+ assertTrue(conditionValue.getValue().isPresent());
+ assertThat(conditionValue.getValue().get(), is("shardingsphere"));
+ assertFalse(conditionValue.getParameterMarkerIndex().isPresent());
}
@Test
- public void assertGetValue() {
+ public void assertGetValueFromParameterMarkerSegment() {
+ ExpressionSegment expressionSegment = new
ParameterMarkerExpressionSegment(0, 0, 0);
+ ConditionValue conditionValue = new ConditionValue(expressionSegment,
Collections.singletonList(1));
assertTrue(conditionValue.getValue().isPresent());
- assertThat(conditionValue.getValue().get(), is("shardingsphere"));
+ assertThat(conditionValue.getValue().get(), is(1));
+ assertTrue(conditionValue.getParameterMarkerIndex().isPresent());
+ assertThat(conditionValue.getParameterMarkerIndex().get(), is(0));
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGeneratorTest.java
index 750e0fd87af..64bb57601a1 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGeneratorTest.java
@@ -64,6 +64,7 @@ public final class ConditionValueBetweenOperatorGeneratorTest
{
assertThat(rangeShardingConditionValue.getTableName(),
is(column.getTableName()));
assertTrue(rangeShardingConditionValue.getValueRange().contains(between));
assertTrue(rangeShardingConditionValue.getValueRange().contains(and));
+
assertTrue(rangeShardingConditionValue.getParameterMarkerIndexes().isEmpty());
}
@SuppressWarnings("unchecked")
@@ -81,6 +82,7 @@ public final class ConditionValueBetweenOperatorGeneratorTest
{
assertThat(rangeShardingConditionValue.getTableName(),
is(column.getTableName()));
assertTrue(SafeNumberOperationUtil.safeContains(rangeShardingConditionValue.getValueRange(),
between));
assertTrue(SafeNumberOperationUtil.safeContains(rangeShardingConditionValue.getValueRange(),
and));
+
assertTrue(rangeShardingConditionValue.getParameterMarkerIndexes().isEmpty());
}
@Test(expected = ClassCastException.class)
@@ -105,6 +107,7 @@ public final class
ConditionValueBetweenOperatorGeneratorTest {
assertThat(rangeShardingConditionValue.getColumnName(),
is(column.getName()));
assertThat(rangeShardingConditionValue.getTableName(),
is(column.getTableName()));
assertThat(rangeShardingConditionValue.getValueRange().lowerEndpoint(),
is(date));
+
assertTrue(rangeShardingConditionValue.getParameterMarkerIndexes().isEmpty());
}
@SuppressWarnings("unchecked")
@@ -122,6 +125,7 @@ public final class
ConditionValueBetweenOperatorGeneratorTest {
assertThat(rangeShardingConditionValue.getColumnName(),
is(column.getName()));
assertThat(rangeShardingConditionValue.getTableName(),
is(column.getTableName()));
assertTrue(rangeShardingConditionValue.getValueRange().upperEndpoint().before(after));
+
assertTrue(rangeShardingConditionValue.getParameterMarkerIndexes().isEmpty());
}
@SuppressWarnings("unchecked")
@@ -138,6 +142,7 @@ public final class
ConditionValueBetweenOperatorGeneratorTest {
assertThat(conditionValue.getTableName(), is("tbl"));
assertThat(conditionValue.getColumnName(), is("id"));
assertThat(conditionValue.getValueRange(), is(Range.closed(1, 2)));
+ assertThat(conditionValue.getParameterMarkerIndexes(),
is(Arrays.asList(0, 1)));
}
@Test
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGeneratorTest.java
index 930a94901df..e3ffc3c67cd 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGeneratorTest.java
@@ -55,6 +55,7 @@ public final class ConditionValueCompareOperatorGeneratorTest
{
Optional<ShardingConditionValue> shardingConditionValue =
generator.generate(rightValue, column, new LinkedList<>());
assertTrue(shardingConditionValue.isPresent());
assertTrue(((ListShardingConditionValue<Integer>)
shardingConditionValue.get()).getValues().contains(value));
+
assertTrue(shardingConditionValue.get().getParameterMarkerIndexes().isEmpty());
}
@SuppressWarnings("unchecked")
@@ -64,6 +65,7 @@ public final class ConditionValueCompareOperatorGeneratorTest
{
Optional<ShardingConditionValue> shardingConditionValue =
generator.generate(rightValue, column, new LinkedList<>());
assertTrue(shardingConditionValue.isPresent());
assertTrue(Range.lessThan(1).encloses(((RangeShardingConditionValue<Integer>)
shardingConditionValue.get()).getValueRange()));
+
assertTrue(shardingConditionValue.get().getParameterMarkerIndexes().isEmpty());
}
@SuppressWarnings("unchecked")
@@ -73,6 +75,7 @@ public final class ConditionValueCompareOperatorGeneratorTest
{
Optional<ShardingConditionValue> shardingConditionValue =
generator.generate(rightValue, column, new LinkedList<>());
assertTrue(shardingConditionValue.isPresent());
assertTrue(Range.greaterThan(1).encloses(((RangeShardingConditionValue<Integer>)
shardingConditionValue.get()).getValueRange()));
+
assertTrue(shardingConditionValue.get().getParameterMarkerIndexes().isEmpty());
}
@SuppressWarnings("unchecked")
@@ -82,6 +85,7 @@ public final class ConditionValueCompareOperatorGeneratorTest
{
Optional<ShardingConditionValue> shardingConditionValue =
generator.generate(rightValue, column, new LinkedList<>());
assertTrue(shardingConditionValue.isPresent());
assertTrue(Range.atMost(1).encloses(((RangeShardingConditionValue<Integer>)
shardingConditionValue.get()).getValueRange()));
+
assertTrue(shardingConditionValue.get().getParameterMarkerIndexes().isEmpty());
}
@SuppressWarnings("unchecked")
@@ -91,6 +95,7 @@ public final class ConditionValueCompareOperatorGeneratorTest
{
Optional<ShardingConditionValue> shardingConditionValue =
generator.generate(rightValue, column, new LinkedList<>());
assertTrue(shardingConditionValue.isPresent());
assertTrue(Range.atLeast(1).encloses(((RangeShardingConditionValue<Integer>)
shardingConditionValue.get()).getValueRange()));
+
assertTrue(shardingConditionValue.get().getParameterMarkerIndexes().isEmpty());
}
@Test
@@ -112,6 +117,7 @@ public final class
ConditionValueCompareOperatorGeneratorTest {
Optional<ShardingConditionValue> shardingConditionValue =
generator.generate(rightValue, column, new LinkedList<>());
assertTrue(shardingConditionValue.isPresent());
assertFalse(((ListShardingConditionValue<Integer>)
shardingConditionValue.get()).getValues().isEmpty());
+
assertTrue(shardingConditionValue.get().getParameterMarkerIndexes().isEmpty());
}
@SuppressWarnings("unchecked")
@@ -127,6 +133,7 @@ public final class
ConditionValueCompareOperatorGeneratorTest {
assertThat(conditionValue.getTableName(), is("tbl"));
assertThat(conditionValue.getColumnName(), is("id"));
assertThat(conditionValue.getValues(),
is(Collections.singletonList(1)));
+ assertThat(conditionValue.getParameterMarkerIndexes(),
is(Collections.singletonList(0)));
}
@Test
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueInOperatorGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueInOperatorGeneratorTest.java
index 4e4e2466b7b..b2c14bdf413 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueInOperatorGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueInOperatorGeneratorTest.java
@@ -35,8 +35,8 @@ import java.util.Optional;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public final class ConditionValueInOperatorGeneratorTest {
@@ -53,6 +53,7 @@ public final class ConditionValueInOperatorGeneratorTest {
Optional<ShardingConditionValue> shardingConditionValue =
generator.generate(inExpression, column, new LinkedList<>());
assertTrue(shardingConditionValue.isPresent());
assertThat(((ListShardingConditionValue<?>)
shardingConditionValue.get()).getValues().iterator().next(),
instanceOf(Date.class));
+
assertTrue(shardingConditionValue.get().getParameterMarkerIndexes().isEmpty());
}
@SuppressWarnings("unchecked")
@@ -69,6 +70,7 @@ public final class ConditionValueInOperatorGeneratorTest {
assertThat(conditionValue.getTableName(), is("tbl"));
assertThat(conditionValue.getColumnName(), is("id"));
assertThat(conditionValue.getValues(),
is(Collections.singletonList(1)));
+ assertThat(conditionValue.getParameterMarkerIndexes(),
is(Collections.singletonList(0)));
}
@Test