From e6ea7b1c7c9c4036c6c422a907d28af3f89a460d Mon Sep 17 00:00:00 2001 From: Mitsunori Komatsu Date: Wed, 3 Jul 2024 21:58:14 +0900 Subject: [PATCH] Change some JDBC data types to proper ones (#2000) --- .../db/storage/jdbc/RdbEngineMysql.java | 3 ++- .../db/storage/jdbc/RdbEngineOracle.java | 2 +- .../db/storage/jdbc/RdbEnginePostgresql.java | 2 +- .../scalar/db/storage/jdbc/JdbcAdminTest.java | 23 ++++++++++++------- docs/schema-loader.md | 4 ++-- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineMysql.java b/core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineMysql.java index 753050de46..a81dd2b365 100644 --- a/core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineMysql.java +++ b/core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineMysql.java @@ -185,8 +185,9 @@ public String getDataTypeForEngine(DataType scalarDbDataType) { case BOOLEAN: return "BOOLEAN"; case DOUBLE: - case FLOAT: return "DOUBLE"; + case FLOAT: + return "REAL"; case INT: return "INT"; case TEXT: diff --git a/core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineOracle.java b/core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineOracle.java index 35297c1717..f9e7d85cd6 100644 --- a/core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineOracle.java +++ b/core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineOracle.java @@ -196,7 +196,7 @@ public String getDataTypeForEngine(DataType scalarDbDataType) { case FLOAT: return "BINARY_FLOAT"; case INT: - return "INT"; + return "NUMBER(10)"; case TEXT: return "VARCHAR2(4000)"; default: diff --git a/core/src/main/java/com/scalar/db/storage/jdbc/RdbEnginePostgresql.java b/core/src/main/java/com/scalar/db/storage/jdbc/RdbEnginePostgresql.java index 9bc1221cf5..fb07202f3c 100644 --- a/core/src/main/java/com/scalar/db/storage/jdbc/RdbEnginePostgresql.java +++ b/core/src/main/java/com/scalar/db/storage/jdbc/RdbEnginePostgresql.java @@ -192,7 +192,7 @@ public String getDataTypeForEngine(DataType scalarDbDataType) { case DOUBLE: return "DOUBLE PRECISION"; case FLOAT: - return "FLOAT"; + return "REAL"; case INT: return "INT"; case TEXT: diff --git a/core/src/test/java/com/scalar/db/storage/jdbc/JdbcAdminTest.java b/core/src/test/java/com/scalar/db/storage/jdbc/JdbcAdminTest.java index 45838a5cea..796564b1ea 100644 --- a/core/src/test/java/com/scalar/db/storage/jdbc/JdbcAdminTest.java +++ b/core/src/test/java/com/scalar/db/storage/jdbc/JdbcAdminTest.java @@ -451,7 +451,7 @@ public void createTableInternal_ForMysql_ShouldCreateTableAndIndexes() throws ExecutionException, SQLException { createTableInternal_ForX_CreateTableAndIndexes( RdbEngine.MYSQL, - "CREATE TABLE `my_ns`.`foo_table`(`c3` BOOLEAN,`c1` VARCHAR(64),`c4` VARBINARY(64),`c2` BIGINT,`c5` INT,`c6` DOUBLE,`c7` DOUBLE, PRIMARY KEY (`c3` ASC,`c1` DESC,`c4` ASC))", + "CREATE TABLE `my_ns`.`foo_table`(`c3` BOOLEAN,`c1` VARCHAR(64),`c4` VARBINARY(64),`c2` BIGINT,`c5` INT,`c6` DOUBLE,`c7` REAL, PRIMARY KEY (`c3` ASC,`c1` DESC,`c4` ASC))", "CREATE INDEX `index_my_ns_foo_table_c4` ON `my_ns`.`foo_table` (`c4`)", "CREATE INDEX `index_my_ns_foo_table_c1` ON `my_ns`.`foo_table` (`c1`)"); } @@ -461,7 +461,7 @@ public void createTableInternal_ForPostgresql_ShouldCreateTableAndIndexes() throws ExecutionException, SQLException { createTableInternal_ForX_CreateTableAndIndexes( RdbEngine.POSTGRESQL, - "CREATE TABLE \"my_ns\".\"foo_table\"(\"c3\" BOOLEAN,\"c1\" VARCHAR(10485760),\"c4\" BYTEA,\"c2\" BIGINT,\"c5\" INT,\"c6\" DOUBLE PRECISION,\"c7\" FLOAT, PRIMARY KEY (\"c3\",\"c1\",\"c4\"))", + "CREATE TABLE \"my_ns\".\"foo_table\"(\"c3\" BOOLEAN,\"c1\" VARCHAR(10485760),\"c4\" BYTEA,\"c2\" BIGINT,\"c5\" INT,\"c6\" DOUBLE PRECISION,\"c7\" REAL, PRIMARY KEY (\"c3\",\"c1\",\"c4\"))", "CREATE UNIQUE INDEX \"my_ns.foo_table_clustering_order_idx\" ON \"my_ns\".\"foo_table\" (\"c3\" ASC,\"c1\" DESC,\"c4\" ASC)", "CREATE INDEX \"index_my_ns_foo_table_c4\" ON \"my_ns\".\"foo_table\" (\"c4\")", "CREATE INDEX \"index_my_ns_foo_table_c1\" ON \"my_ns\".\"foo_table\" (\"c1\")"); @@ -483,7 +483,7 @@ public void createTableInternal_ForOracle_ShouldCreateTableAndIndexes() throws ExecutionException, SQLException { createTableInternal_ForX_CreateTableAndIndexes( RdbEngine.ORACLE, - "CREATE TABLE \"my_ns\".\"foo_table\"(\"c3\" NUMBER(1),\"c1\" VARCHAR2(64),\"c4\" RAW(64),\"c2\" NUMBER(19),\"c5\" INT,\"c6\" BINARY_DOUBLE,\"c7\" BINARY_FLOAT, PRIMARY KEY (\"c3\",\"c1\",\"c4\")) ROWDEPENDENCIES", + "CREATE TABLE \"my_ns\".\"foo_table\"(\"c3\" NUMBER(1),\"c1\" VARCHAR2(64),\"c4\" RAW(64),\"c2\" NUMBER(19),\"c5\" NUMBER(10),\"c6\" BINARY_DOUBLE,\"c7\" BINARY_FLOAT, PRIMARY KEY (\"c3\",\"c1\",\"c4\")) ROWDEPENDENCIES", "ALTER TABLE \"my_ns\".\"foo_table\" INITRANS 3 MAXTRANS 255", "CREATE UNIQUE INDEX \"my_ns.foo_table_clustering_order_idx\" ON \"my_ns\".\"foo_table\" (\"c3\" ASC,\"c1\" DESC,\"c4\" ASC)", "CREATE INDEX \"index_my_ns_foo_table_c4\" ON \"my_ns\".\"foo_table\" (\"c4\")", @@ -548,7 +548,7 @@ public void createTableInternal_IfNotExistsForMysql_ShouldCreateTableAndIndexesI throws ExecutionException, SQLException { createTableInternal_IfNotExistsForX_createTableAndIndexesIfNotExists( RdbEngine.MYSQL, - "CREATE TABLE IF NOT EXISTS `my_ns`.`foo_table`(`c3` BOOLEAN,`c1` VARCHAR(64),`c4` VARBINARY(64),`c2` BIGINT,`c5` INT,`c6` DOUBLE,`c7` DOUBLE, PRIMARY KEY (`c3` ASC,`c1` DESC,`c4` ASC))", + "CREATE TABLE IF NOT EXISTS `my_ns`.`foo_table`(`c3` BOOLEAN,`c1` VARCHAR(64),`c4` VARBINARY(64),`c2` BIGINT,`c5` INT,`c6` DOUBLE,`c7` REAL, PRIMARY KEY (`c3` ASC,`c1` DESC,`c4` ASC))", "CREATE INDEX `index_my_ns_foo_table_c4` ON `my_ns`.`foo_table` (`c4`)", "CREATE INDEX `index_my_ns_foo_table_c1` ON `my_ns`.`foo_table` (`c1`)"); } @@ -558,7 +558,7 @@ public void createTableInternal_IfNotExistsForPostgresql_ShouldCreateTableAndInd throws ExecutionException, SQLException { createTableInternal_IfNotExistsForX_createTableAndIndexesIfNotExists( RdbEngine.POSTGRESQL, - "CREATE TABLE IF NOT EXISTS \"my_ns\".\"foo_table\"(\"c3\" BOOLEAN,\"c1\" VARCHAR(10485760),\"c4\" BYTEA,\"c2\" BIGINT,\"c5\" INT,\"c6\" DOUBLE PRECISION,\"c7\" FLOAT, PRIMARY KEY (\"c3\",\"c1\",\"c4\"))", + "CREATE TABLE IF NOT EXISTS \"my_ns\".\"foo_table\"(\"c3\" BOOLEAN,\"c1\" VARCHAR(10485760),\"c4\" BYTEA,\"c2\" BIGINT,\"c5\" INT,\"c6\" DOUBLE PRECISION,\"c7\" REAL, PRIMARY KEY (\"c3\",\"c1\",\"c4\"))", "CREATE UNIQUE INDEX IF NOT EXISTS \"my_ns.foo_table_clustering_order_idx\" ON \"my_ns\".\"foo_table\" (\"c3\" ASC,\"c1\" DESC,\"c4\" ASC)", "CREATE INDEX IF NOT EXISTS \"index_my_ns_foo_table_c4\" ON \"my_ns\".\"foo_table\" (\"c4\")", "CREATE INDEX IF NOT EXISTS \"index_my_ns_foo_table_c1\" ON \"my_ns\".\"foo_table\" (\"c1\")"); @@ -580,7 +580,7 @@ public void createTableInternal_IfNotExistsForOracle_ShouldCreateTableAndIndexes throws ExecutionException, SQLException { createTableInternal_IfNotExistsForX_createTableAndIndexesIfNotExists( RdbEngine.ORACLE, - "CREATE TABLE \"my_ns\".\"foo_table\"(\"c3\" NUMBER(1),\"c1\" VARCHAR2(64),\"c4\" RAW(64),\"c2\" NUMBER(19),\"c5\" INT,\"c6\" BINARY_DOUBLE,\"c7\" BINARY_FLOAT, PRIMARY KEY (\"c3\",\"c1\",\"c4\")) ROWDEPENDENCIES", + "CREATE TABLE \"my_ns\".\"foo_table\"(\"c3\" NUMBER(1),\"c1\" VARCHAR2(64),\"c4\" RAW(64),\"c2\" NUMBER(19),\"c5\" NUMBER(10),\"c6\" BINARY_DOUBLE,\"c7\" BINARY_FLOAT, PRIMARY KEY (\"c3\",\"c1\",\"c4\")) ROWDEPENDENCIES", "ALTER TABLE \"my_ns\".\"foo_table\" INITRANS 3 MAXTRANS 255", "CREATE UNIQUE INDEX \"my_ns.foo_table_clustering_order_idx\" ON \"my_ns\".\"foo_table\" (\"c3\" ASC,\"c1\" DESC,\"c4\" ASC)", "CREATE INDEX \"index_my_ns_foo_table_c4\" ON \"my_ns\".\"foo_table\" (\"c4\")", @@ -2374,7 +2374,7 @@ public void addNewColumnToTable_ForOracle_ShouldWorkProperly() "SELECT \"column_name\",\"data_type\",\"key_type\",\"clustering_order\",\"indexed\" FROM \"" + METADATA_SCHEMA + "\".\"metadata\" WHERE \"full_table_name\"=? ORDER BY \"ordinal_position\" ASC", - "ALTER TABLE \"ns\".\"table\" ADD \"c2\" INT", + "ALTER TABLE \"ns\".\"table\" ADD \"c2\" NUMBER(10)", "DELETE FROM \"" + METADATA_SCHEMA + "\".\"metadata\" WHERE \"full_table_name\" = 'ns.table'", @@ -2887,11 +2887,18 @@ private String prepareSqlForTableCheck(RdbEngine rdbEngine, String namespace, St private String prepareSqlForAlterTableAddColumn(RdbEngine rdbEngine, String column) { RdbEngineStrategy rdbEngineStrategy = getRdbEngineStrategy(rdbEngine); + String intType; + if (rdbEngineStrategy instanceof RdbEngineOracle) { + intType = "NUMBER(10)"; + } else { + intType = "INT"; + } return "ALTER TABLE " + rdbEngineStrategy.encloseFullTableName(NAMESPACE, TABLE) + " ADD " + rdbEngineStrategy.enclose(column) - + " INT"; + + " " + + intType; } private RdbEngineStrategy getRdbEngineStrategy(RdbEngine rdbEngine) { diff --git a/docs/schema-loader.md b/docs/schema-loader.md index 7d01c30569..48a4ed1879 100644 --- a/docs/schema-loader.md +++ b/docs/schema-loader.md @@ -625,9 +625,9 @@ The following table shows the supported data types in ScalarDB and their mapping | ScalarDB | Cassandra | Cosmos DB for NoSQL | DynamoDB | MySQL | PostgreSQL/YugabyteDB | Oracle | SQL Server | SQLite | |-----------|-----------|---------------------|----------|----------|-----------------------|----------------|-----------------|---------| | BOOLEAN | boolean | boolean (JSON) | BOOL | boolean | boolean | number(1) | bit | boolean | -| INT | int | number (JSON) | N | int | int | int | int | int | +| INT | int | number (JSON) | N | int | int | number(10) | int | int | | BIGINT | bigint | number (JSON) | N | bigint | bigint | number(19) | bigint | bigint | -| FLOAT | float | number (JSON) | N | double | float | binary_float | float(24) | float | +| FLOAT | float | number (JSON) | N | real | real | binary_float | float(24) | float | | DOUBLE | double | number (JSON) | N | double | double precision | binary_double | float | double | | TEXT | text | string (JSON) | S | longtext | text | varchar2(4000) | varchar(8000) | text | | BLOB | blob | string (JSON) | B | longblob | bytea | RAW(2000) | varbinary(8000) | blob |