-
Notifications
You must be signed in to change notification settings - Fork 41
Add integration test for virtual tables with imported tables having various primary key types #3240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
brfrn169
merged 6 commits into
master
from
add-virtual-table-integration-test-with-imported-table
Dec 4, 2025
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e68982
Add integration test for virtual tables with imported tables having v…
brfrn169 85a79b2
Fix
brfrn169 c7065ea
Fix
brfrn169 fcadccc
Add javadoc
brfrn169 4c3696b
Fix
brfrn169 117e07a
Merge branch 'master' into add-virtual-table-integration-test-with-im…
brfrn169 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 182 additions & 1 deletion
183
...ration-test/java/com/scalar/db/storage/jdbc/JdbcDatabaseVirtualTablesIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,194 @@ | ||
| package com.scalar.db.storage.jdbc; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import com.scalar.db.api.DistributedStorageVirtualTablesIntegrationTestBase; | ||
| import com.scalar.db.api.Put; | ||
| import com.scalar.db.api.Result; | ||
| import com.scalar.db.api.Scan; | ||
| import com.scalar.db.api.Scanner; | ||
| import com.scalar.db.api.TableMetadata; | ||
| import com.scalar.db.api.VirtualTableJoinType; | ||
| import com.scalar.db.config.DatabaseConfig; | ||
| import com.scalar.db.io.DataType; | ||
| import com.scalar.db.io.Key; | ||
| import com.scalar.db.util.ScalarDbUtils; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Properties; | ||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.condition.DisabledIf; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class JdbcDatabaseVirtualTablesIntegrationTest | ||
| extends DistributedStorageVirtualTablesIntegrationTestBase { | ||
|
|
||
| private static final Logger logger = | ||
| LoggerFactory.getLogger(JdbcDatabaseVirtualTablesIntegrationTest.class); | ||
brfrn169 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private JdbcAdminImportTestUtils testUtils; | ||
| private RdbEngineStrategy rdbEngine; | ||
brfrn169 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| protected Properties getProperties(String testName) { | ||
| return JdbcEnv.getProperties(testName); | ||
| Properties properties = JdbcEnv.getProperties(testName); | ||
| JdbcConfig config = new JdbcConfig(new DatabaseConfig(properties)); | ||
| rdbEngine = RdbEngineFactory.create(config); | ||
| testUtils = new JdbcAdminImportTestUtils(properties); | ||
| return properties; | ||
| } | ||
|
|
||
| @DisabledIf("com.scalar.db.storage.jdbc.JdbcEnv#isSqlite") | ||
| @Test | ||
brfrn169 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public void createVirtualTable_WithImportedTableHavingVariousPrimaryKeyTypes_ShouldWorkProperly() | ||
| throws Exception { | ||
| for (Map.Entry<String, DataType> entry : | ||
| testUtils.getSupportedDataTypeMapForPrimaryKey().entrySet()) { | ||
| String dataTypeName = entry.getKey(); | ||
| DataType dataType = entry.getValue(); | ||
| String tableBaseName = | ||
| dataTypeName.replaceAll("[()]", "").replaceAll("[\\s,]", "_").toLowerCase(); | ||
| String importedTableName = tableBaseName + "_imported"; | ||
| String anotherTableName = tableBaseName + "_another"; | ||
| String vtableInnerTableName = tableBaseName + "_vtable_inner"; | ||
| String vtableLeftOuterTableName = tableBaseName + "_vtable_left_outer"; | ||
|
|
||
| String createTableSql = | ||
| "CREATE TABLE " | ||
| + rdbEngine.encloseFullTableName(namespace, importedTableName) | ||
| + " (" | ||
| + rdbEngine.enclose("pk") | ||
| + " " | ||
| + dataTypeName | ||
| + " PRIMARY KEY" | ||
| + (JdbcEnv.isDb2() ? " NOT NULL" : "") | ||
| + "," | ||
| + rdbEngine.enclose("col1") | ||
| + " VARCHAR(100))"; | ||
|
|
||
| try { | ||
| // Create a left source table to be imported | ||
| testUtils.execute(createTableSql); | ||
|
|
||
| // Import the left source table | ||
| admin.importTable(namespace, importedTableName, Collections.emptyMap()); | ||
|
|
||
| // Create a right source table | ||
| admin.createTable( | ||
| namespace, | ||
| anotherTableName, | ||
| TableMetadata.newBuilder() | ||
| .addColumn("pk", dataType) | ||
| .addColumn("col2", DataType.TEXT) | ||
| .addPartitionKey("pk") | ||
| .build()); | ||
|
|
||
| // Create a virtual table that joins the above two source tables with different join types | ||
| admin.createVirtualTable( | ||
| namespace, | ||
| vtableInnerTableName, | ||
| namespace, | ||
| importedTableName, | ||
| namespace, | ||
| anotherTableName, | ||
| VirtualTableJoinType.INNER); | ||
| admin.createVirtualTable( | ||
| namespace, | ||
| vtableLeftOuterTableName, | ||
| namespace, | ||
| importedTableName, | ||
| namespace, | ||
| anotherTableName, | ||
| VirtualTableJoinType.LEFT_OUTER); | ||
|
|
||
| // Verify that the virtual tables are created successfully | ||
| TableMetadata expectedMetadata = | ||
| TableMetadata.newBuilder() | ||
| .addColumn("pk", dataType) | ||
| .addColumn("col1", DataType.TEXT) | ||
| .addColumn("col2", DataType.TEXT) | ||
| .addPartitionKey("pk") | ||
| .build(); | ||
| assertThat(admin.getTableMetadata(namespace, vtableInnerTableName)) | ||
| .isEqualTo(expectedMetadata); | ||
| assertThat(admin.getTableMetadata(namespace, vtableLeftOuterTableName)) | ||
| .isEqualTo(expectedMetadata); | ||
|
|
||
| // Put data into the virtual table | ||
| Key partitionKey1 = getPartitionKey(dataType, 1); | ||
| Key partitionKey2 = getPartitionKey(dataType, 2); | ||
| storage.put( | ||
| Put.newBuilder() | ||
| .namespace(namespace) | ||
| .table(vtableInnerTableName) | ||
| .partitionKey(partitionKey1) | ||
| .textValue("col1", "value1") | ||
| .textValue("col2", "value2") | ||
| .build()); | ||
| storage.put( | ||
| Put.newBuilder() | ||
| .namespace(namespace) | ||
| .table(vtableInnerTableName) | ||
| .partitionKey(partitionKey2) | ||
| .textValue("col1", "value3") | ||
| .textValue("col2", "value4") | ||
| .build()); | ||
|
|
||
| // Scan data from the virtual table and verify | ||
| try (Scanner scanner = | ||
| storage.scan( | ||
| Scan.newBuilder().namespace(namespace).table(vtableInnerTableName).all().build())) { | ||
| List<Result> results = scanner.all(); | ||
| assertThat(results).hasSize(2); | ||
|
|
||
| // Verify results in any order | ||
| assertThat(results) | ||
| .anySatisfy( | ||
| result -> { | ||
| Assertions.<Key>assertThat( | ||
| ScalarDbUtils.getPartitionKey(result, expectedMetadata)) | ||
| .isEqualTo(partitionKey1); | ||
| assertThat(result.getText("col1")).isEqualTo("value1"); | ||
| assertThat(result.getText("col2")).isEqualTo("value2"); | ||
| }) | ||
| .anySatisfy( | ||
| result -> { | ||
| Assertions.<Key>assertThat( | ||
| ScalarDbUtils.getPartitionKey(result, expectedMetadata)) | ||
| .isEqualTo(partitionKey2); | ||
| assertThat(result.getText("col1")).isEqualTo("value3"); | ||
| assertThat(result.getText("col2")).isEqualTo("value4"); | ||
| }); | ||
| } | ||
| } finally { | ||
| // Drop the created tables | ||
| admin.dropTable(namespace, vtableInnerTableName, true); | ||
| admin.dropTable(namespace, vtableLeftOuterTableName, true); | ||
| admin.dropTable(namespace, anotherTableName, true); | ||
| admin.dropTable(namespace, importedTableName, true); | ||
| } | ||
| } | ||
| } | ||
brfrn169 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
brfrn169 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private Key getPartitionKey(DataType dataType, int index) { | ||
| switch (dataType) { | ||
| case BOOLEAN: | ||
| return Key.ofBoolean("pk", index == 1); | ||
| case INT: | ||
| return Key.ofInt("pk", index); | ||
| case BIGINT: | ||
| return Key.ofBigInt("pk", index); | ||
| case FLOAT: | ||
| return Key.ofFloat("pk", (float) index); | ||
| case DOUBLE: | ||
| return Key.ofDouble("pk", index); | ||
| case TEXT: | ||
| return Key.ofText("pk", String.valueOf(index * 100)); | ||
| default: | ||
| throw new AssertionError("Unsupported data type: " + dataType); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.