|
28 | 28 | import org.apache.shardingsphere.sharding.rule.ShardingRule; |
29 | 29 | import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; |
30 | 30 | import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.BetweenExpression; |
| 31 | +import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.BinaryOperationExpression; |
31 | 32 | import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment; |
32 | 33 | import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.InExpression; |
33 | 34 | import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ListExpression; |
| 35 | +import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.TypeCastExpression; |
34 | 36 | import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.LiteralExpressionSegment; |
| 37 | +import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.ParameterMarkerExpressionSegment; |
35 | 38 | import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment; |
36 | 39 | import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; |
37 | 40 | import org.apache.shardingsphere.timeservice.core.rule.TimestampServiceRule; |
|
41 | 44 | import org.mockito.Mock; |
42 | 45 | import org.mockito.junit.jupiter.MockitoExtension; |
43 | 46 |
|
| 47 | +import java.math.BigDecimal; |
| 48 | +import java.util.Arrays; |
44 | 49 | import java.util.Collections; |
45 | 50 | import java.util.List; |
46 | 51 | import java.util.Optional; |
47 | 52 |
|
48 | 53 | import static org.hamcrest.Matchers.is; |
49 | 54 | import static org.hamcrest.MatcherAssert.assertThat; |
50 | 55 | import static org.hamcrest.Matchers.isA; |
| 56 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
51 | 57 | import static org.mockito.Mockito.mock; |
52 | 58 | import static org.mockito.Mockito.when; |
53 | 59 |
|
@@ -113,4 +119,54 @@ void assertCreateShardingConditionsForSelectInStatement() { |
113 | 119 | assertThat(actual.get(0).getStartIndex(), is(0)); |
114 | 120 | assertThat(actual.get(0).getValues().get(0), isA(ListShardingConditionValue.class)); |
115 | 121 | } |
| 122 | + |
| 123 | + @Test |
| 124 | + void assertCreateShardingConditionsForSelectCompareWithCastedParameter() { |
| 125 | + ColumnSegment left = new ColumnSegment(0, 0, new IdentifierValue("foo_sharding_col")); |
| 126 | + TypeCastExpression cast = new TypeCastExpression(0, 0, "?::int4", new ParameterMarkerExpressionSegment(0, 0, 0), "int4"); |
| 127 | + BinaryOperationExpression predicate = new BinaryOperationExpression(0, 0, left, cast, "=", "foo_sharding_col = ?::int4"); |
| 128 | + when(whereSegment.getExpr()).thenReturn(predicate); |
| 129 | + when(rule.findShardingColumn("foo_sharding_col", "")).thenReturn(Optional.of("foo_sharding_col")); |
| 130 | + List<ShardingCondition> actual = shardingConditionEngine.createShardingConditions(sqlStatementContext, Collections.singletonList("42")); |
| 131 | + ListShardingConditionValue<?> value = (ListShardingConditionValue<?>) actual.get(0).getValues().get(0); |
| 132 | + assertThat(value.getValues(), is(Collections.singletonList(42))); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void assertCreateShardingConditionsForSelectInWithCastedParameter() { |
| 137 | + ColumnSegment left = new ColumnSegment(0, 0, new IdentifierValue("foo_sharding_col")); |
| 138 | + ListExpression right = new ListExpression(0, 0); |
| 139 | + right.getItems().add(new TypeCastExpression(0, 0, "?::int4", new ParameterMarkerExpressionSegment(0, 0, 0), "int4")); |
| 140 | + InExpression inExpression = new InExpression(0, 0, left, right, false); |
| 141 | + when(whereSegment.getExpr()).thenReturn(inExpression); |
| 142 | + when(rule.findShardingColumn("foo_sharding_col", "")).thenReturn(Optional.of("foo_sharding_col")); |
| 143 | + List<ShardingCondition> actual = shardingConditionEngine.createShardingConditions(sqlStatementContext, Collections.singletonList("42")); |
| 144 | + ListShardingConditionValue<?> value = (ListShardingConditionValue<?>) actual.get(0).getValues().get(0); |
| 145 | + assertThat(value.getValues(), is(Collections.singletonList(42))); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void assertCreateShardingConditionsForSelectBetweenWithCastedParameters() { |
| 150 | + ColumnSegment left = new ColumnSegment(0, 0, new IdentifierValue("foo_sharding_col")); |
| 151 | + ExpressionSegment betweenCast = new TypeCastExpression(0, 0, "?::int4", new ParameterMarkerExpressionSegment(0, 0, 0), "int4"); |
| 152 | + ExpressionSegment andCast = new TypeCastExpression(0, 0, "?::int4", new ParameterMarkerExpressionSegment(0, 0, 1), "int4"); |
| 153 | + BetweenExpression betweenExpression = new BetweenExpression(0, 0, left, betweenCast, andCast, false); |
| 154 | + when(whereSegment.getExpr()).thenReturn(betweenExpression); |
| 155 | + when(rule.findShardingColumn("foo_sharding_col", "")).thenReturn(Optional.of("foo_sharding_col")); |
| 156 | + List<ShardingCondition> actual = shardingConditionEngine.createShardingConditions(sqlStatementContext, Arrays.asList("1", "100")); |
| 157 | + RangeShardingConditionValue<?> value = (RangeShardingConditionValue<?>) actual.get(0).getValues().get(0); |
| 158 | + assertThat(value.getValueRange().lowerEndpoint(), is(1)); |
| 159 | + assertThat(value.getValueRange().upperEndpoint(), is(100)); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void assertCreateShardingConditionsForSelectCompareWithUnsupportedTypmodCastDoesNotRoute() { |
| 164 | + ColumnSegment left = new ColumnSegment(0, 0, new IdentifierValue("foo_sharding_col")); |
| 165 | + TypeCastExpression cast = new TypeCastExpression(0, 0, "?::numeric(3,1)", new ParameterMarkerExpressionSegment(0, 0, 0), "numeric(3,1)"); |
| 166 | + BinaryOperationExpression predicate = new BinaryOperationExpression(0, 0, left, cast, "=", "foo_sharding_col = ?::numeric(3,1)"); |
| 167 | + when(whereSegment.getExpr()).thenReturn(predicate); |
| 168 | + when(rule.findShardingColumn("foo_sharding_col", "")).thenReturn(Optional.of("foo_sharding_col")); |
| 169 | + List<ShardingCondition> actual = shardingConditionEngine.createShardingConditions(sqlStatementContext, Collections.singletonList(new BigDecimal("1.55"))); |
| 170 | + assertTrue(actual.isEmpty()); |
| 171 | + } |
116 | 172 | } |
0 commit comments