diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java index 1d8610dc8e7bf7..fb7389a8db66fb 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java @@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory; +import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource; import org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; @@ -31,6 +32,7 @@ import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; @@ -47,10 +49,10 @@ public final class DatabaseTypeEngine { /** * Get protocol type. - * - * @param databaseName database name + * + * @param databaseName database name * @param databaseConfig database configuration - * @param props configuration properties + * @param props configuration properties * @return protocol type */ public static DatabaseType getProtocolType(final String databaseName, final DatabaseConfiguration databaseConfig, final ConfigurationProperties props) { @@ -66,7 +68,7 @@ public static DatabaseType getProtocolType(final String databaseName, final Data * Get protocol type. * * @param databaseConfigs database configurations - * @param props configuration properties + * @param props configuration properties * @return protocol type */ public static DatabaseType getProtocolType(final Map databaseConfigs, final ConfigurationProperties props) { @@ -94,7 +96,7 @@ private static Map getEnabledDataSources(final Map getStorageTypes(final String databaseNam /** * Get storage type. + * Similar to Apache Hive 4.0.0's `org.apache.hive.jdbc.HiveDatabaseMetaData`, it does not implement {@link java.sql.DatabaseMetaData#getURL()}. + * So use {@link CatalogSwitchableDataSource#getUrl()} to try fuzzy matching. * * @param dataSource data source * @return storage type @@ -117,6 +121,11 @@ public static Map getStorageTypes(final String databaseNam public static DatabaseType getStorageType(final DataSource dataSource) { try (Connection connection = dataSource.getConnection()) { return DatabaseTypeFactory.get(connection.getMetaData().getURL()); + } catch (final SQLFeatureNotSupportedException sqlFeatureNotSupportedException) { + if (dataSource instanceof CatalogSwitchableDataSource) { + return DatabaseTypeFactory.get(((CatalogSwitchableDataSource) dataSource).getUrl()); + } + throw new SQLWrapperException(sqlFeatureNotSupportedException); } catch (final SQLException ex) { throw new SQLWrapperException(ex); } diff --git a/infra/database/type/hive/src/main/java/org/apache/shardingsphere/infra/database/hive/connector/HiveConnectionPropertiesParser.java b/infra/database/type/hive/src/main/java/org/apache/shardingsphere/infra/database/hive/connector/HiveConnectionPropertiesParser.java new file mode 100644 index 00000000000000..895adcfcc518b1 --- /dev/null +++ b/infra/database/type/hive/src/main/java/org/apache/shardingsphere/infra/database/hive/connector/HiveConnectionPropertiesParser.java @@ -0,0 +1,45 @@ +/* + * 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.infra.database.hive.connector; + +import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties; +import org.apache.shardingsphere.infra.database.core.connector.ConnectionPropertiesParser; +import org.apache.shardingsphere.infra.database.core.connector.StandardConnectionProperties; +import org.apache.shardingsphere.infra.database.core.connector.url.JdbcUrl; +import org.apache.shardingsphere.infra.database.core.connector.url.StandardJdbcUrlParser; + +import java.util.Properties; + +/** + * Connection properties parser of Hive. + */ +public final class HiveConnectionPropertiesParser implements ConnectionPropertiesParser { + + private static final int DEFAULT_PORT = 10000; + + @Override + public ConnectionProperties parse(final String url, final String username, final String catalog) { + JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url); + return new StandardConnectionProperties(jdbcUrl.getHostname(), jdbcUrl.getPort(DEFAULT_PORT), jdbcUrl.getDatabase(), null, jdbcUrl.getQueryProperties(), new Properties()); + } + + @Override + public String getDatabaseType() { + return "Hive"; + } +} diff --git a/infra/database/type/hive/src/main/java/org/apache/shardingsphere/infra/database/hive/metadata/database/HiveDatabaseMetaData.java b/infra/database/type/hive/src/main/java/org/apache/shardingsphere/infra/database/hive/metadata/database/HiveDatabaseMetaData.java new file mode 100644 index 00000000000000..af729fd5ff374c --- /dev/null +++ b/infra/database/type/hive/src/main/java/org/apache/shardingsphere/infra/database/hive/metadata/database/HiveDatabaseMetaData.java @@ -0,0 +1,43 @@ +/* + * 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.infra.database.hive.metadata.database; + +import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData; +import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType; +import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter; + +/** + * Database metadata of Hive. + */ +public final class HiveDatabaseMetaData implements DialectDatabaseMetaData { + + @Override + public QuoteCharacter getQuoteCharacter() { + return QuoteCharacter.QUOTE; + } + + @Override + public NullsOrderType getDefaultNullsOrderType() { + return NullsOrderType.FIRST; + } + + @Override + public String getDatabaseType() { + return "Hive"; + } +} diff --git a/infra/database/type/hive/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.connector.ConnectionPropertiesParser b/infra/database/type/hive/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.connector.ConnectionPropertiesParser new file mode 100644 index 00000000000000..65c988d7ebffa8 --- /dev/null +++ b/infra/database/type/hive/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.connector.ConnectionPropertiesParser @@ -0,0 +1,18 @@ +# +# 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. +# + +org.apache.shardingsphere.infra.database.hive.connector.HiveConnectionPropertiesParser diff --git a/infra/database/type/hive/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData b/infra/database/type/hive/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData new file mode 100644 index 00000000000000..c1ac35b449f77a --- /dev/null +++ b/infra/database/type/hive/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData @@ -0,0 +1,18 @@ +# +# 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. +# + +org.apache.shardingsphere.infra.database.hive.metadata.database.HiveDatabaseMetaData diff --git a/pom.xml b/pom.xml index d539451795a734..8f8462b9f688ae 100644 --- a/pom.xml +++ b/pom.xml @@ -126,6 +126,8 @@ 3.1.0-og 2.4.2 0.6.0-patch5 + 4.0.0 + 3.3.6 4.0.3 @@ -479,6 +481,24 @@ http test + + org.apache.hive + hive-jdbc + ${hive.version} + test + + + org.apache.hive + hive-service + ${hive.version} + test + + + org.apache.hadoop + hadoop-client-runtime + ${hadoop-client-runtime.version} + test + com.zaxxer @@ -970,7 +990,8 @@ maven-surefire-plugin false - --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED + + --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED diff --git a/test/native/pom.xml b/test/native/pom.xml index 1266875230cdda..cd180ca9460f8a 100644 --- a/test/native/pom.xml +++ b/test/native/pom.xml @@ -123,6 +123,12 @@ using Seata Client under GraalVM Native Image requires using the version release ${project.version} test + + org.apache.shardingsphere + shardingsphere-parser-sql-hive + ${project.version} + test + org.awaitility @@ -155,6 +161,31 @@ using Seata Client under GraalVM Native Image requires using the version release http test + + org.apache.hive + hive-jdbc + test + + + org.apache.hive + hive-service + test + + + org.apache.logging.log4j + log4j-slf4j-impl + + + org.slf4j + slf4j-reload4j + + + + + org.apache.hadoop + hadoop-client-runtime + test + org.testcontainers postgresql diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/TestShardingService.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/TestShardingService.java index 8e577b480b3f18..c1d37b03e64b13 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/TestShardingService.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/TestShardingService.java @@ -27,6 +27,7 @@ import javax.sql.DataSource; import java.sql.SQLException; +import java.sql.Statement; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -60,7 +61,7 @@ public TestShardingService(final DataSource dataSource) { * @throws SQLException An exception that provides information on a database access error or other errors. */ public void processSuccess() throws SQLException { - final Collection orderIds = insertData(); + final Collection orderIds = insertData(Statement.RETURN_GENERATED_KEYS); Collection orders = orderRepository.selectAll(); assertThat(orders.stream().map(Order::getOrderType).collect(Collectors.toList()), equalTo(Arrays.asList(1, 1, 1, 1, 1, 0, 0, 0, 0, 0))); @@ -94,7 +95,7 @@ public void processSuccess() throws SQLException { * @throws SQLException An exception that provides information on a database access error or other errors. */ public void processSuccessInClickHouse() throws SQLException { - Collection orderIds = insertDataInClickHouse(); + Collection orderIds = insertData(Statement.NO_GENERATED_KEYS); assertThat(orderIds, notNullValue()); Collection orders = orderRepository.selectAll(); assertThat(orders.stream().map(Order::getOrderType).collect(Collectors.toList()), @@ -121,40 +122,29 @@ public void processSuccessInClickHouse() throws SQLException { } /** - * Insert data. + * Process success in Hive. * - * @return orderId of the insert statement. * @throws SQLException An exception that provides information on a database access error or other errors. */ - public Collection insertData() throws SQLException { - Collection result = new ArrayList<>(10); - for (int i = 1; i <= 10; i++) { - Order order = new Order(); - order.setUserId(i); - order.setOrderType(i % 2); - order.setAddressId(i); - order.setStatus("INSERT_TEST"); - orderRepository.insert(order); - OrderItem orderItem = new OrderItem(); - orderItem.setOrderId(order.getOrderId()); - orderItem.setUserId(i); - orderItem.setPhone("13800000001"); - orderItem.setStatus("INSERT_TEST"); - orderItemRepository.insert(orderItem); - Address address = new Address((long) i, "address_test_" + i); - addressRepository.insert(address); - result.add(order.getOrderId()); - } - return result; + public void processSuccessInHive() throws SQLException { + insertDataInHive(); + assertThat(addressRepository.selectAll(), + equalTo(LongStream.range(1L, 11L).mapToObj(each -> new Address(each, "address_test_" + each)).collect(Collectors.toList()))); + // deleteDataInHive(); + // assertThat(addressRepository.selectAll(), equalTo(Collections.emptyList())); } /** - * Insert data in ClickHouse. + * Insert data. * + * @param autoGeneratedKeys a flag indicating whether auto-generated keys + * should be returned; one of + * {@code Statement.RETURN_GENERATED_KEYS} or + * {@code Statement.NO_GENERATED_KEYS} * @return orderId of the insert statement. * @throws SQLException An exception that provides information on a database access error or other errors. */ - public Collection insertDataInClickHouse() throws SQLException { + public Collection insertData(final int autoGeneratedKeys) throws SQLException { Collection result = new ArrayList<>(10); for (int i = 1; i <= 10; i++) { Order order = new Order(); @@ -162,13 +152,13 @@ public Collection insertDataInClickHouse() throws SQLException { order.setOrderType(i % 2); order.setAddressId(i); order.setStatus("INSERT_TEST"); - orderRepository.insertWithoutAutoGeneratedKey(order); + orderRepository.insert(order, autoGeneratedKeys); OrderItem orderItem = new OrderItem(); orderItem.setOrderId(order.getOrderId()); orderItem.setUserId(i); orderItem.setPhone("13800000001"); orderItem.setStatus("INSERT_TEST"); - orderItemRepository.insertWithoutAutoGeneratedKey(orderItem); + orderItemRepository.insert(orderItem, autoGeneratedKeys); Address address = new Address((long) i, "address_test_" + i); addressRepository.insert(address); result.add(order.getOrderId()); @@ -176,6 +166,20 @@ public Collection insertDataInClickHouse() throws SQLException { return result; } + /** + * Insert data in Hive. + */ + public void insertDataInHive() { + LongStream.range(1L, 11L).forEach(action -> { + Address address = new Address(action, "address_test_" + action); + try { + addressRepository.insert(address); + } catch (final SQLException ex) { + throw new RuntimeException(ex); + } + }); + } + /** * Delete data. * @@ -206,6 +210,18 @@ public void deleteDataInClickHouse(final Collection orderIds) throws SQLEx } } + /** + * Delete data in Hive. + * + * @throws SQLException An exception that provides information on a database access error or other errors. + */ + public void deleteDataInHive() throws SQLException { + long count = 1L; + for (int i = 1; i <= 10; i++) { + addressRepository.deleteInHive(count++); + } + } + /** * Clean environment. * diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/AddressRepository.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/AddressRepository.java index 1e519f617a7582..2dede51278ab7c 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/AddressRepository.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/AddressRepository.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.test.natived.jdbc.commons.repository; import org.apache.shardingsphere.test.natived.jdbc.commons.entity.Address; +import org.apache.shardingsphere.test.natived.jdbc.commons.utils.HiveUtil; import javax.sql.DataSource; import java.sql.Connection; @@ -38,11 +39,11 @@ public AddressRepository(final DataSource dataSource) { } /** - * create table t_address if not exists. + * create table t_address if not exists in MySQL. * * @throws SQLException SQL exception */ - public void createTableIfNotExists() throws SQLException { + public void createTableIfNotExistsInMySQL() throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS t_address (address_id BIGINT NOT NULL, address_name VARCHAR(100) NOT NULL, PRIMARY KEY (address_id))"; try ( Connection connection = dataSource.getConnection(); @@ -62,7 +63,7 @@ public void createTableInSQLServer() throws SQLException { + " address_id bigint NOT NULL,\n" + " address_name varchar(100) NOT NULL,\n" + " PRIMARY KEY (address_id)\n" - + ");"; + + ")"; try ( Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { @@ -70,6 +71,26 @@ public void createTableInSQLServer() throws SQLException { } } + /** + * create table t_address if not exists in Hive. + * + * @throws SQLException SQL exception + */ + public void createTableIfNotExistsInHive() throws SQLException { + String sql = "CREATE TABLE IF NOT EXISTS t_address\n" + + "(\n" + + " address_id BIGINT NOT NULL,\n" + + " address_name VARCHAR(100) NOT NULL,\n" + + " PRIMARY KEY (address_id) disable novalidate\n" + + ") STORED AS ORC TBLPROPERTIES ('transactional'='true')"; + try ( + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement()) { + HiveUtil.turnOnTransactionsInClientConnection(statement); + statement.executeUpdate(sql); + } + } + /** * drop table t_address. * @@ -133,6 +154,24 @@ public void delete(final Long id) throws SQLException { } } + /** + * delete by id. + * + * @param id id + * @throws SQLException SQL exception + */ + public void deleteInHive(final Long id) throws SQLException { + String sql = "DELETE FROM t_address WHERE address_id=?"; + try ( + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement(); + PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + HiveUtil.turnOnTransactionsInClientConnection(statement); + preparedStatement.setLong(1, id); + preparedStatement.executeUpdate(); + } + } + /** * delete by id in ClickHouse. * diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderItemRepository.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderItemRepository.java index 8979e284eff261..5acc36713fd540 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderItemRepository.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderItemRepository.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.test.natived.jdbc.commons.repository; import org.apache.shardingsphere.test.natived.jdbc.commons.entity.OrderItem; +import org.apache.shardingsphere.test.natived.jdbc.commons.utils.HiveUtil; import javax.sql.DataSource; import java.sql.Connection; @@ -52,7 +53,7 @@ public void createTableIfNotExistsInMySQL() throws SQLException { + "user_id INT NOT NULL,\n" + "phone VARCHAR(50),\n" + "status VARCHAR(50),\n" - + "PRIMARY KEY (order_item_id));"; + + "PRIMARY KEY (order_item_id))"; try ( Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { @@ -72,7 +73,7 @@ public void createTableIfNotExistsInPostgres() throws SQLException { + " user_id INTEGER NOT NULL,\n" + " phone VARCHAR(50),\n" + " status VARCHAR(50)\n" - + ");"; + + ")"; try ( Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { @@ -94,7 +95,7 @@ public void createTableInSQLServer() throws SQLException { + " phone varchar(50),\n" + " status varchar(50),\n" + " PRIMARY KEY (order_item_id)\n" - + ");"; + + ")"; try ( Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { @@ -104,6 +105,7 @@ public void createTableInSQLServer() throws SQLException { /** * create table if not exists in ClickHouse. + * ClickHouse does not support `AUTO_INCREMENT`, refer to ClickHouse/ClickHouse#56228 . * * @throws SQLException SQL exception */ @@ -124,6 +126,29 @@ public void createTableIfNotExistsInClickHouse() throws SQLException { } } + /** + * create table if not exists in Hive. + * Hive does not support `AUTO_INCREMENT`, refer to HIVE-6905 . + * + * @throws SQLException SQL exception + */ + public void createTableIfNotExistsInHive() throws SQLException { + String sql = "CREATE TABLE IF NOT EXISTS t_order_item\n" + + "(order_item_id BIGINT,\n" + + " order_id BIGINT NOT NULL,\n" + + " user_id INT NOT NULL,\n" + + " phone VARCHAR(50),\n" + + " status VARCHAR(50),\n" + + " PRIMARY KEY (order_item_id) disable novalidate\n" + + ") STORED AS ORC TBLPROPERTIES ('transactional'='true')"; + try ( + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement()) { + HiveUtil.turnOnTransactionsInClientConnection(statement); + statement.executeUpdate(sql); + } + } + /** * drop table. * @@ -208,7 +233,8 @@ public Long insert(final OrderItem orderItem) throws SQLException { } /** - * insert OrderItem to table. + * insert OrderItem to table. Databases like ClickHouse do not support returning auto generated keys after executing SQL, + * see ClickHouse/ClickHouse#56228 . * * @param orderItem orderItem * @param autoGeneratedKeys a flag indicating whether auto-generated keys @@ -237,17 +263,6 @@ public Long insert(final OrderItem orderItem, final int autoGeneratedKeys) throw return orderItem.getOrderItemId(); } - /** - * insert OrderItem to table without auto generated key. Databases like ClickHouse do not support returning auto generated keys after executing SQL, - * see ClickHouse/ClickHouse#56228 . - * - * @param orderItem orderItem - * @throws SQLException SQL Exception - */ - public void insertWithoutAutoGeneratedKey(final OrderItem orderItem) throws SQLException { - insert(orderItem, Statement.NO_GENERATED_KEYS); - } - /** * delete by orderItemId. * diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderRepository.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderRepository.java index 0919940a716618..8bd1e3311836c4 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderRepository.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderRepository.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.test.natived.jdbc.commons.repository; import org.apache.shardingsphere.test.natived.jdbc.commons.entity.Order; +import org.apache.shardingsphere.test.natived.jdbc.commons.utils.HiveUtil; import javax.sql.DataSource; import java.sql.Connection; @@ -49,7 +50,7 @@ public void createTableIfNotExistsInMySQL() throws SQLException { + "user_id INT NOT NULL,\n" + "address_id BIGINT NOT NULL,\n" + "status VARCHAR(50),\n" - + "PRIMARY KEY (order_id));"; + + "PRIMARY KEY (order_id))"; try ( Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { @@ -69,7 +70,7 @@ public void createTableIfNotExistsInPostgres() throws SQLException { + " user_id INTEGER NOT NULL,\n" + " address_id BIGINT NOT NULL,\n" + " status VARCHAR(50)\n" - + ");"; + + ")"; try ( Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { @@ -91,7 +92,7 @@ public void createTableInSQLServer() throws SQLException { + " address_id bigint NOT NULL,\n" + " status varchar(50),\n" + " PRIMARY KEY (order_id)\n" - + ");"; + + ")"; try ( Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { @@ -101,6 +102,7 @@ public void createTableInSQLServer() throws SQLException { /** * create table in ClickHouse. + * ClickHouse does not support `AUTO_INCREMENT`, refer to ClickHouse/ClickHouse#56228 . * * @throws SQLException SQL exception */ @@ -113,7 +115,7 @@ public void createTableIfNotExistsInClickHouse() throws SQLException { + "status String\n" + ") engine = MergeTree \n" + "primary key (order_id)\n" - + "order by(order_id); "; + + "order by(order_id)"; try ( Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { @@ -121,6 +123,30 @@ public void createTableIfNotExistsInClickHouse() throws SQLException { } } + /** + * create table in Hive. + * Hive does not support `AUTO_INCREMENT`, refer to HIVE-6905 . + * + * @throws SQLException SQL exception + */ + public void createTableIfNotExistsInHive() throws SQLException { + String sql = "CREATE TABLE IF NOT EXISTS t_order\n" + + "(\n" + + " order_id BIGINT,\n" + + " order_type INT,\n" + + " user_id INT NOT NULL,\n" + + " address_id BIGINT NOT NULL,\n" + + " status VARCHAR(50),\n" + + " PRIMARY KEY (order_id) disable novalidate\n" + + ") STORED AS ORC TBLPROPERTIES ('transactional'='true')"; + try ( + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement()) { + HiveUtil.turnOnTransactionsInClientConnection(statement); + statement.executeUpdate(sql); + } + } + /** * drop table. * TODO There is a bug in this function in shadow's unit test and requires additional fixes. @@ -234,7 +260,8 @@ public Long insert(final Order order) throws SQLException { } /** - * insert Order to table. + * insert Order to table. Databases like ClickHouse do not support returning auto generated keys after executing SQL, + * see ClickHouse/ClickHouse#56228 . * * @param order order * @param autoGeneratedKeys a flag indicating whether auto-generated keys @@ -263,17 +290,6 @@ public Long insert(final Order order, final int autoGeneratedKeys) throws SQLExc return order.getOrderId(); } - /** - * insert Order to table without auto generated key. Databases like ClickHouse do not support returning auto generated keys after executing SQL, - * see ClickHouse/ClickHouse#56228 . - * - * @param order order - * @throws SQLException SQL Exception - */ - public void insertWithoutAutoGeneratedKey(final Order order) throws SQLException { - insert(order, Statement.NO_GENERATED_KEYS); - } - /** * delete by orderId. * diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/utils/HiveUtil.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/utils/HiveUtil.java new file mode 100644 index 00000000000000..01da8f9646ed50 --- /dev/null +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/utils/HiveUtil.java @@ -0,0 +1,37 @@ +/* + * 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.test.natived.jdbc.commons.utils; + +import java.sql.SQLException; +import java.sql.Statement; + +@SuppressWarnings("SqlDialectInspection") +public class HiveUtil { + + /** + * Turn on ACID transactions In Hive client connection. + * + * @param statement Statement From HiveServer2 JDBC Driver + * @throws SQLException An exception that provides information on a database access error or other errors. + */ + public static void turnOnTransactionsInClientConnection(final Statement statement) throws SQLException { + statement.execute("set hive.support.concurrency=true"); + statement.execute("set hive.exec.dynamic.partition.mode=nonstrict"); + statement.execute("set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager"); + } +} diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/ClickHouseTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/ClickHouseTest.java index 0c526f833d94df..f8340d1956d362 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/ClickHouseTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/ClickHouseTest.java @@ -32,7 +32,7 @@ class ClickHouseTest { /** * TODO Need to fix `shardingsphere-parser-sql-clickhouse` module to use `testShardingService.cleanEnvironment()` - * after `testShardingService.processSuccessInClickHouse()`. + * after {@link TestShardingService#processSuccessInClickHouse()}. * * @throws SQLException An exception that provides information on a database access error or other errors. */ @@ -49,7 +49,7 @@ void assertShardingInLocalTransactions() throws SQLException { /** * TODO Need to fix `shardingsphere-parser-sql-clickhouse` module to use `initEnvironment()` - * before `testShardingService.processSuccess()`. + * before {@link TestShardingService#processSuccessInClickHouse()}. * * @throws SQLException An exception that provides information on a database access error or other errors. */ @@ -57,7 +57,7 @@ void assertShardingInLocalTransactions() throws SQLException { private void initEnvironment() throws SQLException { testShardingService.getOrderRepository().createTableIfNotExistsInClickHouse(); testShardingService.getOrderItemRepository().createTableIfNotExistsInClickHouse(); - testShardingService.getAddressRepository().createTableIfNotExists(); + testShardingService.getAddressRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderRepository().truncateTable(); testShardingService.getOrderItemRepository().truncateTable(); testShardingService.getAddressRepository().truncateTable(); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/HiveTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/HiveTest.java new file mode 100644 index 00000000000000..c367c27faae4f4 --- /dev/null +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/HiveTest.java @@ -0,0 +1,124 @@ +/* + * 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.test.natived.jdbc.databases; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import org.apache.shardingsphere.test.natived.jdbc.commons.TestShardingService; +import org.awaitility.Awaitility; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledInNativeImage; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.utility.DockerImageName; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.time.Duration; +import java.util.Objects; +import java.util.Properties; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.nullValue; + +@SuppressWarnings("SqlDialectInspection") +class HiveTest { + + private static final String SYSTEM_PROP_KEY_PREFIX = "fixture.test-native.yaml.database.hive."; + + private String jdbcUrlPrefix; + + private TestShardingService testShardingService; + + @SuppressWarnings("resource") + @Test + @DisabledInNativeImage + void assertShardingInLocalTransactions() throws SQLException { + try ( + GenericContainer container = new GenericContainer<>(DockerImageName.parse("apache/hive:4.0.0")) + .withEnv("SERVICE_NAME", "hiveserver2") + .withExposedPorts(10000, 10002)) { + container.start(); + jdbcUrlPrefix = "jdbc:hive2://localhost:" + container.getMappedPort(10000) + "/"; + DataSource dataSource = createDataSource(); + testShardingService = new TestShardingService(dataSource); + testShardingService.processSuccessInHive(); + testShardingService.cleanEnvironment(); + } + } + + /** + * TODO Need to fix `shardingsphere-parser-sql-hive` module to use `initEnvironment()` + * before {@link TestShardingService#processSuccessInHive()}. + * + * @throws SQLException An exception that provides information on a database access error or other errors. + */ + @SuppressWarnings("unused") + private void initEnvironment() throws SQLException { + testShardingService.getOrderRepository().createTableIfNotExistsInHive(); + testShardingService.getOrderItemRepository().createTableIfNotExistsInHive(); + testShardingService.getAddressRepository().createTableIfNotExistsInHive(); + testShardingService.getOrderRepository().truncateTable(); + testShardingService.getOrderItemRepository().truncateTable(); + testShardingService.getAddressRepository().truncateTable(); + } + + private Connection openConnection() throws SQLException { + Properties props = new Properties(); + return DriverManager.getConnection(jdbcUrlPrefix, props); + } + + private DataSource createDataSource() { + Awaitility.await().atMost(Duration.ofMinutes(1L)).ignoreExceptions().until(() -> { + openConnection().close(); + return true; + }); + try ( + Connection connection = openConnection(); + Statement statement = connection.createStatement()) { + statement.execute("set hive.compactor.initiator.on=true"); + statement.execute("set hive.compactor.worker.threads=1"); + statement.executeUpdate("CREATE DATABASE demo_ds_0"); + statement.executeUpdate("CREATE DATABASE demo_ds_1"); + statement.executeUpdate("CREATE DATABASE demo_ds_2"); + } catch (final SQLException ex) { + throw new RuntimeException(ex); + } + HikariConfig config = new HikariConfig(); + config.setDriverClassName("org.apache.shardingsphere.driver.ShardingSphereDriver"); + config.setJdbcUrl("jdbc:shardingsphere:classpath:test-native/yaml/databases/hive.yaml?placeholder-type=system_props"); + try { + assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url"), Matchers.is(nullValue())); + assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url"), Matchers.is(nullValue())); + assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url"), Matchers.is(nullValue())); + // TODO https://issues.apache.org/jira/browse/HIVE-5867 + String absolutePath = Objects.requireNonNull(HiveTest.class.getClassLoader().getResource("test-native/sql/test-native-databases-hive.sql")).getFile(); + System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url", jdbcUrlPrefix + "demo_ds_0" + ";initFile=" + absolutePath); + System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url", jdbcUrlPrefix + "demo_ds_1" + ";initFile=" + absolutePath); + System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url", jdbcUrlPrefix + "demo_ds_2" + ";initFile=" + absolutePath); + return new HikariDataSource(config); + } finally { + System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url"); + System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url"); + System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url"); + } + } +} diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java index 7e1a53706d69d7..6737510ceb8fee 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java @@ -31,6 +31,7 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import java.sql.Statement; import java.time.Duration; import java.util.Properties; @@ -78,7 +79,7 @@ void assertShardingInLocalTransactions() throws SQLException { private void initEnvironment() throws SQLException { testShardingService.getOrderRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderItemRepository().createTableIfNotExistsInMySQL(); - testShardingService.getAddressRepository().createTableIfNotExists(); + testShardingService.getAddressRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderRepository().truncateTable(); testShardingService.getOrderItemRepository().truncateTable(); testShardingService.getAddressRepository().truncateTable(); @@ -98,10 +99,12 @@ private DataSource createDataSource() { openConnection().close(); return true; }); - try (Connection connection = openConnection()) { - connection.createStatement().executeUpdate("CREATE DATABASE demo_ds_0;"); - connection.createStatement().executeUpdate("CREATE DATABASE demo_ds_1;"); - connection.createStatement().executeUpdate("CREATE DATABASE demo_ds_2;"); + try ( + Connection connection = openConnection(); + Statement statement = connection.createStatement()) { + statement.executeUpdate("CREATE DATABASE demo_ds_0"); + statement.executeUpdate("CREATE DATABASE demo_ds_1"); + statement.executeUpdate("CREATE DATABASE demo_ds_2"); } catch (final SQLException ex) { throw new RuntimeException(ex); } diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/OpenGaussTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/OpenGaussTest.java index edff542ce3e589..1f68af1efde6e2 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/OpenGaussTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/OpenGaussTest.java @@ -30,6 +30,7 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import java.sql.Statement; import java.time.Duration; import java.util.Properties; @@ -72,7 +73,7 @@ void assertShardingInLocalTransactions() throws SQLException { private void initEnvironment() throws SQLException { testShardingService.getOrderRepository().createTableIfNotExistsInPostgres(); testShardingService.getOrderItemRepository().createTableIfNotExistsInPostgres(); - testShardingService.getAddressRepository().createTableIfNotExists(); + testShardingService.getAddressRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderRepository().truncateTable(); testShardingService.getOrderItemRepository().truncateTable(); testShardingService.getAddressRepository().truncateTable(); @@ -91,10 +92,12 @@ private DataSource createDataSource() { openConnection().close(); return true; }); - try (Connection connection = openConnection()) { - connection.createStatement().executeUpdate("CREATE DATABASE demo_ds_0;"); - connection.createStatement().executeUpdate("CREATE DATABASE demo_ds_1;"); - connection.createStatement().executeUpdate("CREATE DATABASE demo_ds_2;"); + try ( + Connection connection = openConnection(); + Statement statement = connection.createStatement()) { + statement.executeUpdate("CREATE DATABASE demo_ds_0"); + statement.executeUpdate("CREATE DATABASE demo_ds_1"); + statement.executeUpdate("CREATE DATABASE demo_ds_2"); } catch (final SQLException ex) { throw new RuntimeException(ex); } diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/PostgresTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/PostgresTest.java index a9852c3ca31dac..f2fd80218fbd08 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/PostgresTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/PostgresTest.java @@ -46,7 +46,7 @@ void assertShardingInLocalTransactions() throws SQLException { private void initEnvironment() throws SQLException { testShardingService.getOrderRepository().createTableIfNotExistsInPostgres(); testShardingService.getOrderItemRepository().createTableIfNotExistsInPostgres(); - testShardingService.getAddressRepository().createTableIfNotExists(); + testShardingService.getAddressRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderRepository().truncateTable(); testShardingService.getOrderItemRepository().truncateTable(); testShardingService.getAddressRepository().truncateTable(); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/EncryptTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/EncryptTest.java index bd6a2874ce42f1..31565acf931981 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/EncryptTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/EncryptTest.java @@ -63,7 +63,7 @@ void assertEncryptInLocalTransactions() throws SQLException { private void initEnvironment() throws SQLException { orderRepository.createTableIfNotExistsInMySQL(); orderItemRepository.createTableIfNotExistsInMySQL(); - addressRepository.createTableIfNotExists(); + addressRepository.createTableIfNotExistsInMySQL(); orderRepository.truncateTable(); orderItemRepository.truncateTable(); addressRepository.truncateTable(); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/MaskTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/MaskTest.java index f6fbd6320c48cf..660719488731ca 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/MaskTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/MaskTest.java @@ -64,7 +64,7 @@ void assertMaskInLocalTransactions() throws SQLException { private void initEnvironment() throws SQLException { orderRepository.createTableIfNotExistsInMySQL(); orderItemRepository.createTableIfNotExistsInMySQL(); - addressRepository.createTableIfNotExists(); + addressRepository.createTableIfNotExistsInMySQL(); orderRepository.truncateTable(); orderItemRepository.truncateTable(); addressRepository.truncateTable(); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/ReadWriteSplittingTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/ReadWriteSplittingTest.java index d2c52b66a6556a..2bb48c20d21a93 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/ReadWriteSplittingTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/ReadWriteSplittingTest.java @@ -60,7 +60,7 @@ void assertReadWriteSplittingInLocalTransactions() throws SQLException { private void initEnvironment() throws SQLException { orderRepository.createTableIfNotExistsInMySQL(); orderItemRepository.createTableIfNotExistsInMySQL(); - addressRepository.createTableIfNotExists(); + addressRepository.createTableIfNotExistsInMySQL(); orderRepository.truncateTable(); orderItemRepository.truncateTable(); addressRepository.truncateTable(); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/ShadowTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/ShadowTest.java index f195e623f0f00e..c7e29830dccf9e 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/ShadowTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/ShadowTest.java @@ -64,7 +64,7 @@ void assertShadowInLocalTransactions() throws SQLException { private void initEnvironment() throws SQLException { orderRepository.createTableIfNotExistsInMySQL(); orderItemRepository.createTableIfNotExistsInMySQL(); - addressRepository.createTableIfNotExists(); + addressRepository.createTableIfNotExistsInMySQL(); orderRepository.truncateTable(); orderItemRepository.truncateTable(); addressRepository.truncateTable(); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/ShardingTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/ShardingTest.java index 99d18af28438bf..32efaef02140c7 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/ShardingTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/features/ShardingTest.java @@ -44,7 +44,7 @@ void assertShardingInLocalTransactions() throws SQLException { private void initEnvironment() throws SQLException { testShardingService.getOrderRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderItemRepository().createTableIfNotExistsInMySQL(); - testShardingService.getAddressRepository().createTableIfNotExists(); + testShardingService.getAddressRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderRepository().truncateTable(); testShardingService.getOrderItemRepository().truncateTable(); testShardingService.getAddressRepository().truncateTable(); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/modes/cluster/EtcdTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/modes/cluster/EtcdTest.java index 325047d48ff760..a95f7dc4fea9ab 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/modes/cluster/EtcdTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/modes/cluster/EtcdTest.java @@ -80,7 +80,7 @@ void assertShardingInLocalTransactions() throws SQLException { private void initEnvironment() throws SQLException { testShardingService.getOrderRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderItemRepository().createTableIfNotExistsInMySQL(); - testShardingService.getAddressRepository().createTableIfNotExists(); + testShardingService.getAddressRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderRepository().truncateTable(); testShardingService.getOrderItemRepository().truncateTable(); testShardingService.getAddressRepository().truncateTable(); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/modes/cluster/ZookeeperTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/modes/cluster/ZookeeperTest.java index 9c9d1d5617cefe..88836303e1a26c 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/modes/cluster/ZookeeperTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/modes/cluster/ZookeeperTest.java @@ -68,7 +68,7 @@ void assertShardingInLocalTransactions() throws Exception { private void initEnvironment() throws SQLException { testShardingService.getOrderRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderItemRepository().createTableIfNotExistsInMySQL(); - testShardingService.getAddressRepository().createTableIfNotExists(); + testShardingService.getAddressRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderRepository().truncateTable(); testShardingService.getOrderItemRepository().truncateTable(); testShardingService.getAddressRepository().truncateTable(); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/base/SeataTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/base/SeataTest.java index b3f0b73301adec..827d856a1c13cb 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/base/SeataTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/base/SeataTest.java @@ -60,7 +60,7 @@ void assertShardingInSeataTransactions() throws SQLException { private void initEnvironment() throws SQLException { testShardingService.getOrderRepository().createTableIfNotExistsInPostgres(); testShardingService.getOrderItemRepository().createTableIfNotExistsInPostgres(); - testShardingService.getAddressRepository().createTableIfNotExists(); + testShardingService.getAddressRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderRepository().truncateTable(); testShardingService.getOrderItemRepository().truncateTable(); testShardingService.getAddressRepository().truncateTable(); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/xa/AtomikosTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/xa/AtomikosTest.java index 9df766bec81e61..141c4f87d6dfe1 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/xa/AtomikosTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/xa/AtomikosTest.java @@ -44,7 +44,7 @@ void assertShardingInAtomikosTransactions() throws SQLException { private void initEnvironment() throws SQLException { testShardingService.getOrderRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderItemRepository().createTableIfNotExistsInMySQL(); - testShardingService.getAddressRepository().createTableIfNotExists(); + testShardingService.getAddressRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderRepository().truncateTable(); testShardingService.getOrderItemRepository().truncateTable(); testShardingService.getAddressRepository().truncateTable(); diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/xa/NarayanaTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/xa/NarayanaTest.java index 87993a52150993..ba7103fd8851a2 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/xa/NarayanaTest.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/transactions/xa/NarayanaTest.java @@ -47,7 +47,7 @@ void assertShardingInNarayanaTransactions() throws SQLException, CoreEnvironment private void initEnvironment() throws SQLException { testShardingService.getOrderRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderItemRepository().createTableIfNotExistsInMySQL(); - testShardingService.getAddressRepository().createTableIfNotExists(); + testShardingService.getAddressRepository().createTableIfNotExistsInMySQL(); testShardingService.getOrderRepository().truncateTable(); testShardingService.getOrderItemRepository().truncateTable(); testShardingService.getAddressRepository().truncateTable(); diff --git a/test/native/src/test/resources/test-native/sql/test-native-databases-hive.sql b/test/native/src/test/resources/test-native/sql/test-native-databases-hive.sql new file mode 100644 index 00000000000000..4ac7da28f6ee1a --- /dev/null +++ b/test/native/src/test/resources/test-native/sql/test-native-databases-hive.sql @@ -0,0 +1,47 @@ +-- +-- 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. +-- + +set hive.support.concurrency=true; +set hive.exec.dynamic.partition.mode=nonstrict; +set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager; + +CREATE TABLE IF NOT EXISTS t_order +( + order_id BIGINT, + order_type INT, + user_id INT NOT NULL, + address_id BIGINT NOT NULL, + status VARCHAR(50), + PRIMARY KEY (order_id) disable novalidate +) STORED AS ORC TBLPROPERTIES ('transactional' = 'true'); + +CREATE TABLE IF NOT EXISTS t_order +( + order_id BIGINT, + order_type INT, + user_id INT NOT NULL, + address_id BIGINT NOT NULL, + status VARCHAR(50), + PRIMARY KEY (order_id) disable novalidate +) STORED AS ORC TBLPROPERTIES ('transactional' = 'true'); + +CREATE TABLE IF NOT EXISTS t_address +( + address_id BIGINT NOT NULL, + address_name VARCHAR(100) NOT NULL, + PRIMARY KEY (address_id) disable novalidate +) STORED AS ORC TBLPROPERTIES ('transactional' = 'true'); diff --git a/test/native/src/test/resources/test-native/yaml/databases/hive.yaml b/test/native/src/test/resources/test-native/yaml/databases/hive.yaml new file mode 100644 index 00000000000000..c44cd303bb3bc5 --- /dev/null +++ b/test/native/src/test/resources/test-native/yaml/databases/hive.yaml @@ -0,0 +1,72 @@ +# +# 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. +# + +mode: + type: Standalone + repository: + type: JDBC + +dataSources: + ds_0: + dataSourceClassName: com.zaxxer.hikari.HikariDataSource + driverClassName: org.apache.hive.jdbc.HiveDriver + jdbcUrl: $${fixture.test-native.yaml.database.hive.ds0.jdbc-url::} + ds_1: + dataSourceClassName: com.zaxxer.hikari.HikariDataSource + driverClassName: org.apache.hive.jdbc.HiveDriver + jdbcUrl: $${fixture.test-native.yaml.database.hive.ds1.jdbc-url::} + ds_2: + dataSourceClassName: com.zaxxer.hikari.HikariDataSource + driverClassName: org.apache.hive.jdbc.HiveDriver + jdbcUrl: $${fixture.test-native.yaml.database.hive.ds2.jdbc-url::} + +rules: +- !SHARDING + tables: + t_order: + actualDataNodes: + keyGenerateStrategy: + column: order_id + keyGeneratorName: snowflake + t_order_item: + actualDataNodes: + keyGenerateStrategy: + column: order_item_id + keyGeneratorName: snowflake + defaultDatabaseStrategy: + standard: + shardingColumn: user_id + shardingAlgorithmName: inline + shardingAlgorithms: + inline: + type: CLASS_BASED + props: + strategy: STANDARD + algorithmClassName: org.apache.shardingsphere.test.natived.jdbc.commons.algorithm.ClassBasedInlineShardingAlgorithmFixture + keyGenerators: + snowflake: + type: SNOWFLAKE + auditors: + sharding_key_required_auditor: + type: DML_SHARDING_CONDITIONS + +- !BROADCAST + tables: + - t_address + +props: + sql-show: false