未验证 提交 6752a21e 编写于 作者: L Liang Zhang 提交者: GitHub

Add rewrite test cases for sharding (#3277)

* add in predicate for encrypt test case

* add in predicate for encrypt with plain test case

* remove useless test cases

* remove useless rule config

* remove useless test cases

* fix show_columns_from_table

* finish show.xml

* add drop_index_without_table

* add drop_index_for_mysql

* fix drop_index_for_postgresql

* add test cases for regular insert

* add FIXME for insert_values_without_columns_without_id_for_literals

* finish insert values test cases

* finish insert set test cases

* Merge branch 'dev' into issue1548

# Conflicts:
#	sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_for_query_with_cipher.xml

* add ON DUPLICATE KEY UPDATE

* add delete test cases

* add update test cases

* add basic select test cases

* add binding select test cases

* update binding select test cases

* add avg select test cases

* add order by and group by select test cases

* add pagination basic test cases

* add all pagination test cases

* add distinct test cases
上级 6da4128b
......@@ -21,6 +21,7 @@ import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.core.parse.sql.segment.ddl.index.IndexSegment;
import org.apache.shardingsphere.core.parse.sql.segment.generic.TableSegment;
import org.apache.shardingsphere.core.parse.sql.statement.generic.IndexSegmentsAvailable;
import org.apache.shardingsphere.core.parse.sql.statement.generic.TableSegmentAvailable;
import java.util.Collection;
......@@ -33,7 +34,7 @@ import java.util.LinkedList;
*/
@Getter
@Setter
public final class DropIndexStatement extends DDLStatement implements TableSegmentAvailable {
public final class DropIndexStatement extends DDLStatement implements TableSegmentAvailable, IndexSegmentsAvailable {
private final Collection<IndexSegment> indexes = new LinkedList<>();
......
......@@ -31,7 +31,7 @@ public final class ShardingKeyGeneratorFixture implements ShardingKeyGenerator {
@Override
public Comparable<?> generateKey() {
return 1L;
return 1;
}
@Override
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.core.rewrite.fixture;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator;
import java.util.Properties;
@Getter
@Setter
public final class TestShardingKeyGenerator implements ShardingKeyGenerator {
private Properties properties = new Properties();
@Override
public Comparable<?> generateKey() {
return 1L;
}
@Override
public String getType() {
return "TEST";
}
}
......@@ -17,18 +17,21 @@
package org.apache.shardingsphere.core.rewrite.parameterized.engine;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.database.DatabaseTypes;
import org.apache.shardingsphere.core.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.core.metadata.datasource.DataSourceMetas;
import org.apache.shardingsphere.core.metadata.table.TableMetaData;
import org.apache.shardingsphere.core.metadata.table.TableMetas;
import org.apache.shardingsphere.core.parse.SQLParseEngine;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.rewrite.context.SQLRewriteContext;
import org.apache.shardingsphere.core.rewrite.engine.SQLRewriteResult;
import org.apache.shardingsphere.core.rewrite.feature.encrypt.context.EncryptSQLRewriteContextDecorator;
import org.apache.shardingsphere.core.rewrite.feature.sharding.ShardingSQLRewriteEngineTest;
import org.apache.shardingsphere.core.rewrite.feature.sharding.context.ShardingSQLRewriteContextDecorator;
import org.apache.shardingsphere.core.rewrite.feature.sharding.engine.ShardingSQLRewriteEngine;
......@@ -54,6 +57,7 @@ import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
......@@ -66,7 +70,9 @@ import java.util.Objects;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(Parameterized.class)
@RequiredArgsConstructor
......@@ -113,7 +119,21 @@ public final class ShardingSQLRewriteEngineParameterizedTest {
result[1] = rootAssertions.getYamlRule();
result[2] = assertion.getId();
result[3] = assertion.getInput().getSql();
result[4] = null == assertion.getInput().getParameters() ? Collections.emptyList() : Splitter.on(",").trimResults().splitToList(assertion.getInput().getParameters());
if (null == assertion.getInput().getParameters()) {
result[4] = Collections.emptyList();
} else {
result[4] = Lists.transform(Splitter.on(",").trimResults().splitToList(assertion.getInput().getParameters()), new Function<String, Object>() {
@Override
public Object apply(final String input) {
try {
return Integer.parseInt(input);
} catch (final NumberFormatException ignore) {
return input;
}
}
});
}
List<RewriteOutputEntity> outputs = assertion.getOutputs();
List<String> outputSQLs = new ArrayList<>(outputs.size());
List<Object> outputGroupedParameters = new ArrayList<>(outputs.size());
......@@ -145,7 +165,10 @@ public final class ShardingSQLRewriteEngineParameterizedTest {
int count = 0;
for (SQLRewriteResult each : actual) {
assertThat(each.getSql(), is(outputSQLs.get(count)));
assertThat(each.getParameters(), is(outputGroupedParameters.get(count)));
assertThat(each.getParameters().size(), is(outputGroupedParameters.get(count).size()));
for (int i = 0; i < each.getParameters().size(); i++) {
assertThat(each.getParameters().get(i).toString(), is(outputGroupedParameters.get(count).get(i).toString()));
}
count++;
}
}
......@@ -154,13 +177,11 @@ public final class ShardingSQLRewriteEngineParameterizedTest {
YamlRootShardingConfiguration ruleConfiguration = createRuleConfiguration();
ShardingRule shardingRule = new ShardingRule(new ShardingRuleConfigurationYamlSwapper().swap(ruleConfiguration.getShardingRule()), ruleConfiguration.getDataSources().keySet());
SQLParseEngine parseEngine = new SQLParseEngine(DatabaseTypes.getActualDatabaseType(null == databaseType ? "SQL92" : databaseType));
ShardingRouter shardingRouter = new ShardingRouter(shardingRule, mock(ShardingSphereMetaData.class), parseEngine);
ShardingRouter shardingRouter = new ShardingRouter(shardingRule, createShardingSphereMetaData(), parseEngine);
SQLStatement sqlStatement = shardingRouter.parse(inputSQL, false);
SQLRouteResult sqlRouteResult = shardingRouter.route(inputSQL, inputParameters, sqlStatement);
SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(mock(TableMetas.class), sqlRouteResult.getSqlStatementContext(), inputSQL, inputParameters);
new ShardingSQLRewriteContextDecorator(shardingRule, sqlRouteResult).decorate(sqlRewriteContext);
boolean isQueryWithCipherColumn = (boolean) ruleConfiguration.getProps().get("query.with.cipher.column");
new EncryptSQLRewriteContextDecorator(shardingRule.getEncryptRule(), isQueryWithCipherColumn).decorate(sqlRewriteContext);
Collection<SQLRewriteResult> result = new LinkedList<>();
for (RoutingUnit each : sqlRouteResult.getRoutingResult().getRoutingUnits()) {
result.add(new ShardingSQLRewriteEngine(sqlRouteResult.getShardingConditions(),
......@@ -175,6 +196,17 @@ public final class ShardingSQLRewriteEngineParameterizedTest {
return YamlEngine.unmarshal(new File(url.getFile()), YamlRootShardingConfiguration.class);
}
private ShardingSphereMetaData createShardingSphereMetaData() {
TableMetas tableMetas = mock(TableMetas.class);
when(tableMetas.getAllTableNames()).thenReturn(Arrays.asList("t_order", "t_order_item"));
TableMetaData orderTableMetaData = mock(TableMetaData.class);
when(orderTableMetaData.containsIndex(anyString())).thenReturn(true);
when(tableMetas.get("t_order")).thenReturn(orderTableMetaData);
when(tableMetas.get("t_order_item")).thenReturn(mock(TableMetaData.class));
when(tableMetas.getAllColumnNames("t_order")).thenReturn(Arrays.asList("order_id", "user_id", "status"));
return new ShardingSphereMetaData(mock(DataSourceMetas.class), tableMetas);
}
private Map<String, String> getLogicAndActualTables(final ShardingRule shardingRule, final RoutingUnit routingUnit, final Collection<String> parsedTableNames) {
Map<String, String> result = new HashMap<>();
for (TableUnit each : routingUnit.getTableUnits()) {
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<rewrite-assertions yaml-rule="yaml/sharding/sharding-rule.yaml">
<rewrite-assertion id="delete_with_sharding_value_for_parameters">
<input sql="DELETE FROM t_order WHERE order_id = ?" parameters="100" />
<output sql="DELETE FROM t_order_0 WHERE order_id = ?" parameters="100" />
</rewrite-assertion>
<rewrite-assertion id="delete_with_sharding_value_for_literals">
<input sql="DELETE FROM t_order WHERE order_id = 100" />
<output sql="DELETE FROM t_order_0 WHERE order_id = 100" />
</rewrite-assertion>
<rewrite-assertion id="delete_without_sharding_value_for_parameters">
<input sql="DELETE FROM sharding_db.t_order WHERE user_id = ?" parameters="10" />
<output sql="DELETE FROM t_order_0 WHERE user_id = ?" parameters="10" />
<output sql="DELETE FROM t_order_1 WHERE user_id = ?" parameters="10" />
</rewrite-assertion>
<rewrite-assertion id="delete_without_sharding_value_for_literals">
<input sql="DELETE FROM sharding_db.t_order WHERE user_id = 10" />
<output sql="DELETE FROM t_order_0 WHERE user_id = 10" />
<output sql="DELETE FROM t_order_1 WHERE user_id = 10" />
</rewrite-assertion>
</rewrite-assertions>
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<rewrite-assertions yaml-rule="yaml/sharding/sharding-rule.yaml">
<rewrite-assertion id="create_index_for_mysql" db-type="MySQL">
<input sql="CREATE INDEX index_name ON t_order ('user_id')" />
<output sql="CREATE INDEX index_name ON t_order_0 ('user_id')" />
<output sql="CREATE INDEX index_name ON t_order_1 ('user_id')" />
</rewrite-assertion>
<rewrite-assertion id="create_index_for_postgresql" db-type="PostgreSQL">
<input sql="CREATE INDEX index_name ON t_order ('user_id')" />
<output sql="CREATE INDEX index_name_t_order_0 ON t_order_0 ('user_id')" />
<output sql="CREATE INDEX index_name_t_order_1 ON t_order_1 ('user_id')" />
</rewrite-assertion>
<rewrite-assertion id="drop_index_for_mysql" db-type="MySQL">
<input sql="DROP INDEX index_name ON t_order" />
<output sql="DROP INDEX index_name ON t_order_0" />
<output sql="DROP INDEX index_name ON t_order_1" />
</rewrite-assertion>
<rewrite-assertion id="drop_index_for_postgresql" db-type="PostgreSQL">
<input sql="DROP INDEX index_name" />
<output sql="DROP INDEX index_name_t_order_0" />
<output sql="DROP INDEX index_name_t_order_1" />
</rewrite-assertion>
</rewrite-assertions>
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<rewrite-assertions yaml-rule="yaml/sharding/sharding-rule.yaml">
<rewrite-assertion id="insert_values_with_columns_with_id_for_parameters">
<input sql="INSERT INTO t_order (order_id, user_id, status) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE user_id = VALUES(user_id)" parameters="100, 10, OK" />
<output sql="INSERT INTO t_order_0 (order_id, user_id, status) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE user_id = VALUES(user_id)" parameters="100, 10, OK" />
</rewrite-assertion>
<rewrite-assertion id="insert_values_with_columns_with_id_for_literals" db-type="MySQL">
<input sql="INSERT INTO t_order (order_id, user_id, status) VALUES (100, 10, 'OK') ON DUPLICATE KEY UPDATE user_id = VALUES(user_id)" />
<output sql="INSERT INTO t_order_0 (order_id, user_id, status) VALUES (100, 10, 'OK') ON DUPLICATE KEY UPDATE user_id = VALUES(user_id)" />
</rewrite-assertion>
<rewrite-assertion id="insert_values_with_columns_without_id_for_parameters">
<input sql="INSERT INTO t_order (user_id, status) VALUES (?, ?)" parameters="10, OK" />
<output sql="INSERT INTO t_order_1 (user_id, status, order_id) VALUES (?, ?, ?)" parameters="10, OK, 1" />
</rewrite-assertion>
<rewrite-assertion id="insert_values_with_columns_without_id_for_literals" db-type="MySQL">
<input sql="INSERT INTO t_order (user_id, status) VALUES (10, 'OK')" />
<output sql="INSERT INTO t_order_1 (user_id, status, order_id) VALUES (10, 'OK', 1)" />
</rewrite-assertion>
<rewrite-assertion id="insert_values_without_columns_with_id_for_parameters">
<input sql="INSERT INTO t_order VALUES (?, ?, ?)" parameters="100, 10, OK" />
<output sql="INSERT INTO t_order_0 VALUES (?, ?, ?)" parameters="100, 10, OK" />
</rewrite-assertion>
<rewrite-assertion id="insert_values_without_columns_with_id_for_literals" db-type="MySQL">
<input sql="INSERT INTO t_order VALUES (100, 10, 'OK')" />
<output sql="INSERT INTO t_order_0 VALUES (100, 10, 'OK')" />
</rewrite-assertion>
<rewrite-assertion id="insert_values_without_columns_without_id_for_parameters">
<input sql="INSERT INTO t_order VALUES (?, ?)" parameters="10, OK" />
<output sql="INSERT INTO t_order_1(user_id, status, order_id) VALUES (?, ?, ?)" parameters="10, OK, 1" />
</rewrite-assertion>
<!-- FIXME -->
<!--<rewrite-assertion id="insert_values_without_columns_without_id_for_literals" db-type="MySQL">-->
<!--<input sql="INSERT INTO t_order VALUES (10, 'OK')" parameters="10, OK" />-->
<!--<output sql="INSERT INTO t_order_1(user_id, status, order_id) VALUES (10, 'OK', 1)" parameters="10, OK, 1" />-->
<!--</rewrite-assertion>-->
<rewrite-assertion id="insert_multiple_values_with_columns_with_id_for_parameters">
<input sql="INSERT INTO t_order (order_id, user_id, status) VALUES (100, 10, 'OK'), (?, ?, ?), (102, 10, 'OK'), (?, ?, ?), (?, ?, ?), (105, 10, 'OK')" parameters="101, 10, OK, 103, 10, OK, 104, 10, OK" />
<output sql="INSERT INTO t_order_0 (order_id, user_id, status) VALUES (100, 10, 'OK'), (102, 10, 'OK'), (?, ?, ?)" parameters="104, 10, OK" />
<output sql="INSERT INTO t_order_1 (order_id, user_id, status) VALUES (?, ?, ?), (?, ?, ?), (105, 10, 'OK')" parameters="101, 10, OK, 103, 10, OK" />
</rewrite-assertion>
<rewrite-assertion id="insert_multiple_values_with_columns_with_id_for_literals">
<input sql="INSERT INTO t_order (order_id, user_id, status) VALUES (100, 10, 'OK'), (101, 10, 'OK'), (102, 10, 'OK'), (103, 10, 'OK'), (104, 10, 'OK'), (105, 10, 'OK')" />
<output sql="INSERT INTO t_order_0 (order_id, user_id, status) VALUES (100, 10, 'OK'), (102, 10, 'OK'), (104, 10, 'OK')" />
<output sql="INSERT INTO t_order_1 (order_id, user_id, status) VALUES (101, 10, 'OK'), (103, 10, 'OK'), (105, 10, 'OK')" />
</rewrite-assertion>
<!-- FIXME -->
<!--<rewrite-assertion id="insert_multiple_values_with_columns_without_id_for_parameters">-->
<!--<input sql="INSERT INTO t_order (user_id, status) VALUES (?, ?), (10, 'OK')" parameters="10, OK" />-->
<!--<output sql="INSERT INTO t_order_1 (user_id, status, order_id) VALUES (?, ?, ?), (10, 'OK', ?)" parameters="10, OK, 1, 1" />-->
<!--</rewrite-assertion>-->
<rewrite-assertion id="insert_multiple_values_with_columns_without_id_for_literals">
<input sql="INSERT INTO t_order (user_id, status) VALUES (10, 'OK'), (10, 'OK')" />
<output sql="INSERT INTO t_order_1 (user_id, status, order_id) VALUES (10, 'OK', 1), (10, 'OK', 1)" />
</rewrite-assertion>
<rewrite-assertion id="insert_multiple_values_without_columns_with_id_for_parameters" db-type="MySQL">
<input sql="INSERT INTO t_order VALUES (?, ?, ?), (101, 10, 'OK')" parameters="100, 10, OK" />
<output sql="INSERT INTO t_order_0 VALUES (?, ?, ?)" parameters="100, 10, OK" />
<output sql="INSERT INTO t_order_1 VALUES (101, 10, 'OK')" />
</rewrite-assertion>
<rewrite-assertion id="insert_multiple_values_without_columns_with_id_for_literals" db-type="MySQL">
<input sql="INSERT INTO t_order VALUES (100, 10, 'OK'), (101, 10, 'OK')" />
<output sql="INSERT INTO t_order_0 VALUES (100, 10, 'OK')" />
<output sql="INSERT INTO t_order_1 VALUES (101, 10, 'OK')" />
</rewrite-assertion>
<!-- FIXME -->
<!--<rewrite-assertion id="insert_multiple_values_without_columns_without_id_for_parameters" db-type="MySQL">-->
<!--<input sql="INSERT INTO t_order VALUES (?, ?), (10, 'OK')" parameters="10, OK" />-->
<!--<output sql="INSERT INTO t_order_1(user_id, status, order_id) VALUES (?, ?, ?), (10, 'OK', ?)" parameters="10, OK, 1, 1" />-->
<!--</rewrite-assertion>-->
<rewrite-assertion id="insert_multiple_values_without_columns_without_id_for_literals" db-type="MySQL">
<input sql="INSERT INTO t_order VALUES (10, 'OK'), (10, 'OK')" />
<output sql="INSERT INTO t_order_1(user_id, status, order_id) VALUES (10, 'OK', 1), (10, 'OK', 1)" />
</rewrite-assertion>
<rewrite-assertion id="insert_set_with_columns_with_id_for_parameters" db-type="MySQL">
<input sql="INSERT INTO t_order SET order_id = ?, user_id = ?, status = ?" parameters="100, 10, OK" />
<output sql="INSERT INTO t_order_0 SET order_id = ?, user_id = ?, status = ?" parameters="100, 10, OK" />
</rewrite-assertion>
<rewrite-assertion id="insert_set_with_columns_with_id_for_literals" db-type="MySQL">
<input sql="INSERT INTO t_order SET order_id = 100, user_id = 10, status = 'OK'" />
<output sql="INSERT INTO t_order_0 SET order_id = 100, user_id = 10, status = 'OK'" />
</rewrite-assertion>
<rewrite-assertion id="insert_set_with_columns_without_id_for_parameters" db-type="MySQL">
<input sql="INSERT INTO t_order SET user_id = ?, status = ?" parameters="10, OK" />
<output sql="INSERT INTO t_order_1 SET user_id = ?, status = ?, order_id = ?" parameters="10, OK, 1" />
</rewrite-assertion>
<!-- FIXME -->
<!--<rewrite-assertion id="insert_set_with_columns_without_id_for_literals" db-type="MySQL">-->
<!--<input sql="INSERT INTO t_order SET user_id = 10, status = 'OK'" parameters="10, OK" />-->
<!--<output sql="INSERT INTO t_order_1 SET user_id = 10, status = 'OK', order_id = 1" />-->
<!--</rewrite-assertion>-->
<rewrite-assertion id="insert_into_with_columns_without_id_for_parameters" db-type="MySQL">
<input sql="INSERT INTO t_order SET user_id = ?, status = ?" parameters="10, OK" />
<output sql="INSERT INTO t_order_1 SET user_id = ?, status = ?, order_id = ?" parameters="10, OK, 1" />
</rewrite-assertion>
</rewrite-assertions>
......@@ -16,10 +16,24 @@
~ limitations under the License.
-->
<rewrite-assertions yaml-rule="yaml/sharding-rewrite-rule.yaml">
<rewrite-assertion id="show_columns_from_table" db-type="MySQL">
<input sql="SHOW COLUMNS FROM table_x" />
<output sql="SHOW COLUMNS FROM table_x" />
<rewrite-assertions yaml-rule="yaml/sharding/sharding-rule.yaml">
<rewrite-assertion id="show_columns_from_table_without_schema" db-type="MySQL">
<input sql="SHOW COLUMNS FROM t_order" />
<output sql="SHOW COLUMNS FROM t_order_0" />
</rewrite-assertion>
<rewrite-assertion id="show_columns_from_table_with_schema" db-type="MySQL">
<input sql="SHOW COLUMNS FROM t_order FROM sharding_db" />
<output sql="SHOW COLUMNS FROM t_order_0 " />
</rewrite-assertion>
<rewrite-assertion id="show_columns_from_table_with_owner" db-type="MySQL">
<input sql="SHOW COLUMNS FROM sharding_db.t_order" />
<output sql="SHOW COLUMNS FROM t_order_0" />
</rewrite-assertion>
<rewrite-assertion id="show_columns_from_table_with_back_quote" db-type="MySQL">
<input sql="SHOW COLUMNS FROM `sharding_db`.`t_order` FROM `sharding_db`" />
<output sql="SHOW COLUMNS FROM `t_order_0` " />
</rewrite-assertion>
</rewrite-assertions>
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<rewrite-assertions yaml-rule="yaml/sharding/sharding-rule.yaml">
<rewrite-assertion id="update_with_sharding_value_for_parameters">
<input sql="UPDATE t_order SET status = ? WHERE order_id = ?" parameters="'OK', 100" />
<output sql="UPDATE t_order_0 SET status = ? WHERE order_id = ?" parameters="'OK', 100" />
</rewrite-assertion>
<rewrite-assertion id="update_with_sharding_value_for_literals">
<input sql="UPDATE t_order SET status = 'OK' WHERE order_id = 100" />
<output sql="UPDATE t_order_0 SET status = 'OK' WHERE order_id = 100" />
</rewrite-assertion>
<rewrite-assertion id="update_without_sharding_value_for_parameters">
<input sql="UPDATE t_order SET status = ? WHERE user_id = ?" parameters="'OK', 10" />
<output sql="UPDATE t_order_0 SET status = ? WHERE user_id = ?" parameters="'OK', 10" />
<output sql="UPDATE t_order_1 SET status = ? WHERE user_id = ?" parameters="'OK', 10" />
</rewrite-assertion>
<rewrite-assertion id="update_without_sharding_value_for_literals">
<input sql="UPDATE t_order SET status = 'OK' WHERE user_id = 10" parameters="'OK', 10" />
<output sql="UPDATE t_order_0 SET status = 'OK' WHERE user_id = 10" parameters="'OK', 10" />
<output sql="UPDATE t_order_1 SET status = 'OK' WHERE user_id = 10" parameters="'OK', 10" />
</rewrite-assertion>
</rewrite-assertions>
......@@ -38,10 +38,6 @@ shardingRule:
worker.id: 123
table_y:
actualDataNodes: db${0..1}.table_y
t_cipher:
actualDataNodes: db${0..1}.table_z
table_k:
actualDataNodes: db${0..1}.table_k
table_w:
actualDataNodes: db${0..1}.table_w
keyGenerator:
......@@ -59,22 +55,6 @@ shardingRule:
k_encryptor:
type: ASSISTED_QUERY_ENCRYPT
tables:
t_cipher:
columns:
password:
cipherColumn: cipher_password
plainColumn: plain_password
encryptor: normal
table_k:
columns:
id:
cipherColumn: cipher
assistedQueryColumn: query
encryptor: k_encryptor
id1:
cipherColumn: id1
assistedQueryColumn: query_id1
encryptor: k_encryptor
table_w:
columns:
name:
......
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
dataSources:
db: !!com.zaxxer.hikari.HikariDataSource
driverClassName: org.h2.Driver
jdbcUrl: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
shardingRule:
tables:
t_order:
actualDataNodes: db.t_order_${0..1}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
keyGenerator:
type: TEST
column: order_id
t_order_item:
actualDataNodes: db.t_order_item_${0..1}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
bindingTables:
- t_order, t_order_item
......@@ -61,21 +61,21 @@ public final class TableBroadcastRoutingEngine implements RoutingEngine {
}
private Collection<String> getLogicTableNames() {
return sqlStatementContext.getSqlStatement() instanceof DropIndexStatement
? getTableNames((DropIndexStatement) sqlStatementContext.getSqlStatement()) : sqlStatementContext.getTablesContext().getTableNames();
return sqlStatementContext.getSqlStatement() instanceof DropIndexStatement && !((DropIndexStatement) sqlStatementContext.getSqlStatement()).getIndexes().isEmpty()
? getTableNamesFromMetaData((DropIndexStatement) sqlStatementContext.getSqlStatement()) : sqlStatementContext.getTablesContext().getTableNames();
}
private Collection<String> getTableNames(final DropIndexStatement dropIndexStatement) {
private Collection<String> getTableNamesFromMetaData(final DropIndexStatement dropIndexStatement) {
Collection<String> result = new LinkedList<>();
for (IndexSegment each : dropIndexStatement.getIndexes()) {
Optional<String> tableName = findLogicTableName(each.getName());
Optional<String> tableName = findLogicTableNameFromMetaData(each.getName());
Preconditions.checkState(tableName.isPresent(), "Cannot find index name `%s`.", each.getName());
result.add(tableName.get());
}
return result;
}
private Optional<String> findLogicTableName(final String logicIndexName) {
private Optional<String> findLogicTableNameFromMetaData(final String logicIndexName) {
for (String each : tableMetas.getAllTableNames()) {
if (tableMetas.get(each).containsIndex(logicIndexName)) {
return Optional.of(each);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册