|
1 | 1 | package com.scalar.db.storage.jdbc; |
2 | 2 |
|
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
3 | 5 | import com.scalar.db.api.DistributedStorageVirtualTablesIntegrationTestBase; |
| 6 | +import com.scalar.db.api.Put; |
| 7 | +import com.scalar.db.api.Result; |
| 8 | +import com.scalar.db.api.Scan; |
| 9 | +import com.scalar.db.api.Scanner; |
| 10 | +import com.scalar.db.api.TableMetadata; |
| 11 | +import com.scalar.db.api.VirtualTableJoinType; |
| 12 | +import com.scalar.db.config.DatabaseConfig; |
| 13 | +import com.scalar.db.io.DataType; |
| 14 | +import com.scalar.db.io.Key; |
| 15 | +import com.scalar.db.util.ScalarDbUtils; |
| 16 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
4 | 20 | import java.util.Properties; |
| 21 | +import org.assertj.core.api.Assertions; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.condition.DisabledIf; |
5 | 24 |
|
6 | 25 | public class JdbcDatabaseVirtualTablesIntegrationTest |
7 | 26 | extends DistributedStorageVirtualTablesIntegrationTestBase { |
8 | 27 |
|
| 28 | + private JdbcAdminImportTestUtils testUtils; |
| 29 | + private RdbEngineStrategy rdbEngine; |
| 30 | + |
9 | 31 | @Override |
10 | 32 | protected Properties getProperties(String testName) { |
11 | | - return JdbcEnv.getProperties(testName); |
| 33 | + Properties properties = JdbcEnv.getProperties(testName); |
| 34 | + JdbcConfig config = new JdbcConfig(new DatabaseConfig(properties)); |
| 35 | + rdbEngine = RdbEngineFactory.create(config); |
| 36 | + testUtils = new JdbcAdminImportTestUtils(properties); |
| 37 | + return properties; |
| 38 | + } |
| 39 | + |
| 40 | + @SuppressFBWarnings("SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE") |
| 41 | + @DisabledIf("com.scalar.db.storage.jdbc.JdbcEnv#isSqlite") |
| 42 | + @Test |
| 43 | + public void createVirtualTable_WithImportedTableHavingVariousPrimaryKeyTypes_ShouldWorkProperly() |
| 44 | + throws Exception { |
| 45 | + for (Map.Entry<String, DataType> entry : |
| 46 | + testUtils.getSupportedDataTypeMapForPrimaryKey().entrySet()) { |
| 47 | + String dataTypeName = entry.getKey(); |
| 48 | + DataType dataType = entry.getValue(); |
| 49 | + String tableBaseName = |
| 50 | + dataTypeName.replaceAll("[()]", "").replaceAll("[\\s,]", "_").toLowerCase(); |
| 51 | + String importedTableName = tableBaseName + "_imported"; |
| 52 | + String anotherTableName = tableBaseName + "_another"; |
| 53 | + String vtableInnerTableName = tableBaseName + "_vtable_inner"; |
| 54 | + String vtableLeftOuterTableName = tableBaseName + "_vtable_left_outer"; |
| 55 | + |
| 56 | + String createTableSql = |
| 57 | + "CREATE TABLE " |
| 58 | + + rdbEngine.encloseFullTableName(namespace, importedTableName) |
| 59 | + + " (" |
| 60 | + + rdbEngine.enclose("pk") |
| 61 | + + " " |
| 62 | + + dataTypeName |
| 63 | + + " PRIMARY KEY" |
| 64 | + + (JdbcEnv.isDb2() ? " NOT NULL" : "") |
| 65 | + + "," |
| 66 | + + rdbEngine.enclose("col1") |
| 67 | + + " VARCHAR(100))"; |
| 68 | + |
| 69 | + try { |
| 70 | + // Create a left source table to be imported |
| 71 | + testUtils.execute(createTableSql); |
| 72 | + |
| 73 | + // Import the left source table |
| 74 | + admin.importTable(namespace, importedTableName, Collections.emptyMap()); |
| 75 | + |
| 76 | + // Create a right source table |
| 77 | + admin.createTable( |
| 78 | + namespace, |
| 79 | + anotherTableName, |
| 80 | + TableMetadata.newBuilder() |
| 81 | + .addColumn("pk", dataType) |
| 82 | + .addColumn("col2", DataType.TEXT) |
| 83 | + .addPartitionKey("pk") |
| 84 | + .build()); |
| 85 | + |
| 86 | + // Create a virtual table that joins the above two source tables with different join types |
| 87 | + admin.createVirtualTable( |
| 88 | + namespace, |
| 89 | + vtableInnerTableName, |
| 90 | + namespace, |
| 91 | + importedTableName, |
| 92 | + namespace, |
| 93 | + anotherTableName, |
| 94 | + VirtualTableJoinType.INNER); |
| 95 | + admin.createVirtualTable( |
| 96 | + namespace, |
| 97 | + vtableLeftOuterTableName, |
| 98 | + namespace, |
| 99 | + importedTableName, |
| 100 | + namespace, |
| 101 | + anotherTableName, |
| 102 | + VirtualTableJoinType.LEFT_OUTER); |
| 103 | + |
| 104 | + // Verify that the virtual tables are created successfully |
| 105 | + TableMetadata expectedMetadata = |
| 106 | + TableMetadata.newBuilder() |
| 107 | + .addColumn("pk", dataType) |
| 108 | + .addColumn("col1", DataType.TEXT) |
| 109 | + .addColumn("col2", DataType.TEXT) |
| 110 | + .addPartitionKey("pk") |
| 111 | + .build(); |
| 112 | + assertThat(admin.getTableMetadata(namespace, vtableInnerTableName)) |
| 113 | + .isEqualTo(expectedMetadata); |
| 114 | + assertThat(admin.getTableMetadata(namespace, vtableLeftOuterTableName)) |
| 115 | + .isEqualTo(expectedMetadata); |
| 116 | + |
| 117 | + // Put data into the virtual table |
| 118 | + Key partitionKey1 = getPartitionKey(dataType, 1); |
| 119 | + Key partitionKey2 = getPartitionKey(dataType, 2); |
| 120 | + storage.put( |
| 121 | + Put.newBuilder() |
| 122 | + .namespace(namespace) |
| 123 | + .table(vtableInnerTableName) |
| 124 | + .partitionKey(partitionKey1) |
| 125 | + .textValue("col1", "value1") |
| 126 | + .textValue("col2", "value2") |
| 127 | + .build()); |
| 128 | + storage.put( |
| 129 | + Put.newBuilder() |
| 130 | + .namespace(namespace) |
| 131 | + .table(vtableInnerTableName) |
| 132 | + .partitionKey(partitionKey2) |
| 133 | + .textValue("col1", "value3") |
| 134 | + .textValue("col2", "value4") |
| 135 | + .build()); |
| 136 | + |
| 137 | + // Scan data from the virtual table and verify |
| 138 | + try (Scanner scanner = |
| 139 | + storage.scan( |
| 140 | + Scan.newBuilder().namespace(namespace).table(vtableInnerTableName).all().build())) { |
| 141 | + List<Result> results = scanner.all(); |
| 142 | + assertThat(results).hasSize(2); |
| 143 | + |
| 144 | + // Verify results in any order |
| 145 | + assertThat(results) |
| 146 | + .anySatisfy( |
| 147 | + result -> { |
| 148 | + Assertions.<Key>assertThat( |
| 149 | + ScalarDbUtils.getPartitionKey(result, expectedMetadata)) |
| 150 | + .isEqualTo(partitionKey1); |
| 151 | + assertThat(result.getText("col1")).isEqualTo("value1"); |
| 152 | + assertThat(result.getText("col2")).isEqualTo("value2"); |
| 153 | + }) |
| 154 | + .anySatisfy( |
| 155 | + result -> { |
| 156 | + Assertions.<Key>assertThat( |
| 157 | + ScalarDbUtils.getPartitionKey(result, expectedMetadata)) |
| 158 | + .isEqualTo(partitionKey2); |
| 159 | + assertThat(result.getText("col1")).isEqualTo("value3"); |
| 160 | + assertThat(result.getText("col2")).isEqualTo("value4"); |
| 161 | + }); |
| 162 | + } |
| 163 | + } finally { |
| 164 | + // Drop the created tables |
| 165 | + admin.dropTable(namespace, vtableInnerTableName, true); |
| 166 | + admin.dropTable(namespace, vtableLeftOuterTableName, true); |
| 167 | + admin.dropTable(namespace, anotherTableName, true); |
| 168 | + admin.dropTable(namespace, importedTableName, true); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private Key getPartitionKey(DataType dataType, int index) { |
| 174 | + switch (dataType) { |
| 175 | + case BOOLEAN: |
| 176 | + return Key.ofBoolean("pk", index == 1); |
| 177 | + case INT: |
| 178 | + return Key.ofInt("pk", index); |
| 179 | + case BIGINT: |
| 180 | + return Key.ofBigInt("pk", index); |
| 181 | + case FLOAT: |
| 182 | + return Key.ofFloat("pk", (float) index); |
| 183 | + case DOUBLE: |
| 184 | + return Key.ofDouble("pk", index); |
| 185 | + case TEXT: |
| 186 | + return Key.ofText("pk", String.valueOf(index * 100)); |
| 187 | + default: |
| 188 | + throw new AssertionError("Unsupported data type: " + dataType); |
| 189 | + } |
12 | 190 | } |
13 | 191 | } |
0 commit comments