Skip to content

Commit

Permalink
Support new types introduced in Tarantool 2.2
Browse files Browse the repository at this point in the history
There are a few new types such as BOOLEAN(BOOL), UNSIGNED, and VARBINARY
in Tarantool v2.2. Moreover, Number aliases REAL, FLOAT, DOUBLE were
replaced by one NUMBER type since v2.2.1.

Support tests for old number types on version before 2.2.

Closes: #209, #210, #214
Affects: #92
  • Loading branch information
nicktorwald committed Aug 23, 2019
1 parent 45cfe17 commit a865c7b
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 80 deletions.
22 changes: 17 additions & 5 deletions src/main/java/org/tarantool/jdbc/type/TarantoolSqlType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,45 @@ public enum TarantoolSqlType {

UNKNOWN(TarantoolType.UNKNOWN, JdbcType.UNKNOWN, "unknown"),

// float, double, real used to be number aliases before 2.2
FLOAT(TarantoolType.NUMBER, JdbcType.FLOAT, "float"),
DOUBLE(TarantoolType.NUMBER, JdbcType.DOUBLE, "double"),
REAL(TarantoolType.NUMBER, JdbcType.REAL, "real"),
// was introduced in Tarantool 2.2.1
NUMBER(TarantoolType.NUMBER, JdbcType.DOUBLE, "number"),

INT(TarantoolType.INTEGER, JdbcType.INTEGER, "int"),
INTEGER(TarantoolType.INTEGER, JdbcType.INTEGER, "integer"),
INT(TarantoolType.INTEGER, JdbcType.BIGINT, "int"),
INTEGER(TarantoolType.INTEGER, JdbcType.BIGINT, "integer"),
// was introduced in 2.2
UNSIGNED(TarantoolType.UNSIGNED, JdbcType.BIGINT, "integer"),

// were introduced in 2.2
BOOL(TarantoolType.BOOLEAN, JdbcType.BOOLEAN, "bool"),
BOOLEAN(TarantoolType.BOOLEAN, JdbcType.BOOLEAN, "boolean"),

STRING(TarantoolType.STRING, JdbcType.VARCHAR, "string"),
TEXT(TarantoolType.STRING, JdbcType.VARCHAR, "text"),
VARCHAR(TarantoolType.STRING, JdbcType.VARCHAR, "varchar") {
@Override
public String getDisplayType() {
return getTypeName() + "(128)";
}
},
TEXT(TarantoolType.STRING, JdbcType.VARCHAR, "text"),

// was introduced in 2.2
VARBINARY(TarantoolType.VARBINARY, JdbcType.VARBINARY, "varbinary"),

SCALAR(TarantoolType.SCALAR, JdbcType.BINARY, "scalar");

private static final Map<TarantoolType, TarantoolSqlType> defaultSqlTypeMapping;
static {
defaultSqlTypeMapping = new HashMap<>();
defaultSqlTypeMapping.put(TarantoolType.BOOLEAN, TarantoolSqlType.BOOLEAN);
defaultSqlTypeMapping.put(TarantoolType.STRING, TarantoolSqlType.VARCHAR);
defaultSqlTypeMapping.put(TarantoolType.STRING, TarantoolSqlType.STRING);
defaultSqlTypeMapping.put(TarantoolType.INTEGER, TarantoolSqlType.INTEGER);
defaultSqlTypeMapping.put(TarantoolType.NUMBER, TarantoolSqlType.DOUBLE);
defaultSqlTypeMapping.put(TarantoolType.UNSIGNED, TarantoolSqlType.UNSIGNED);
defaultSqlTypeMapping.put(TarantoolType.NUMBER, TarantoolSqlType.NUMBER);
defaultSqlTypeMapping.put(TarantoolType.VARBINARY, TarantoolSqlType.VARBINARY);
defaultSqlTypeMapping.put(TarantoolType.SCALAR, TarantoolSqlType.SCALAR);
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/tarantool/jdbc/type/TarantoolType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ public enum TarantoolType {
STRING("string", false, true, Integer.MAX_VALUE, 0, Integer.MAX_VALUE),
// precision is 20 due to Tarantool integer type has range [-2^63-1..2^64-1]
INTEGER("integer", true, false, 20, 0, 20),
// precision is 20 due to Tarantool unsigned integer type has range [0..2^64-1]
UNSIGNED("unsigned", false, false, 20, 0, 20),
// precision is 20 due to Tarantool allows both integer and floating-point values under number type
NUMBER("number", true, false, 20, 16, 24),
SCALAR("scalar", false, true, Integer.MAX_VALUE, 0, Integer.MAX_VALUE);
SCALAR("scalar", false, true, Integer.MAX_VALUE, 0, Integer.MAX_VALUE),
VARBINARY("varbinary", false, true, Integer.MAX_VALUE, 0, Integer.MAX_VALUE);

private final String typeName;
private final boolean signed;
Expand Down
11 changes: 8 additions & 3 deletions src/test/java/org/tarantool/ServerVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public enum ServerVersion {
V_1_9("1", "9", "0"),
V_1_10("1", "10", "0"),
V_2_1("2", "1", "0"),
V_2_2("2", "2", "0");
V_2_2("2", "2", "0"),
V_2_2_1("2", "2", "1");

private final String majorVersion;
private final String minorVersion;
Expand All @@ -32,14 +33,18 @@ public String getPatchVersion() {
return patchVersion;
}

public boolean haveMinimalVersion(String versionString) {
public boolean isLessOrEqualThan(String versionString) {
return compareVersions(versionString, (server, minimal) -> server >= minimal);
}

public boolean haveMaximalVersion(String versionString) {
public boolean isGreaterOrEqualThan(String versionString) {
return compareVersions(versionString, (server, maximal) -> server <= maximal);
}

public boolean isGreaterThan(String versionString) {
return compareVersions(versionString, (server, maximal) -> server < maximal);
}

private boolean compareVersions(String versionString, BiFunction<Integer, Integer, Boolean> comparator) {
int parsedVersion = toNumber(splitVersionParts(versionString));
int thisVersion = toNumber(new String[] { majorVersion, minorVersion, patchVersion });
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/org/tarantool/TestAssumptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
public class TestAssumptions {

public static void assumeMinimalServerVersion(String rawVersion, ServerVersion version) {
Assumptions.assumeTrue(version.haveMinimalVersion(rawVersion));
Assumptions.assumeTrue(version.isLessOrEqualThan(rawVersion));
}

public static void assumeMaximalServerVersion(String rawVersion, ServerVersion version) {
Assumptions.assumeTrue(version.haveMaximalVersion(rawVersion));
Assumptions.assumeTrue(version.isGreaterOrEqualThan(rawVersion));
}

public static void assumeServerVersionLessThan(String rawVersion, ServerVersion version) {
Assumptions.assumeTrue(version.isGreaterThan(rawVersion));
}

}
99 changes: 75 additions & 24 deletions src/test/java/org/tarantool/jdbc/JdbcResultSetMetaDataIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.tarantool.TestAssumptions.assumeMinimalServerVersion;
import static org.tarantool.TestAssumptions.assumeServerVersionLessThan;

import org.tarantool.ServerVersion;
import org.tarantool.TarantoolTestHelper;
import org.tarantool.jdbc.type.JdbcType;
import org.tarantool.jdbc.type.TarantoolSqlType;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -201,9 +200,11 @@ public void testCaseSensitiveColumns() throws SQLException {
@Test
@DisplayName("returned case insensitive columns")
public void testCaseInsensitiveColumns() throws SQLException {
testHelper.executeSql(
"CREATE TABLE test(id INT PRIMARY KEY, num_val DOUBLE)"
);
if (ServerVersion.V_2_2.isGreaterThan(testHelper.getInstanceVersion())) {
testHelper.executeSql("CREATE TABLE test(id INT PRIMARY KEY, num_val DOUBLE)");
} else {
testHelper.executeSql("CREATE TABLE test(id INT PRIMARY KEY, num_val NUMBER)");
}
try (
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM test")
Expand All @@ -218,9 +219,15 @@ public void testCaseInsensitiveColumns() throws SQLException {
@Test
@DisplayName("returned searchable columns")
public void testSearchableColumns() throws SQLException {
testHelper.executeSql(
"CREATE TABLE test(id INT PRIMARY KEY, num_val DOUBLE, text_val TEXT, bin_val SCALAR)"
);
if (ServerVersion.V_2_2.isGreaterThan(testHelper.getInstanceVersion())) {
testHelper.executeSql(
"CREATE TABLE test(id INT PRIMARY KEY, num_val DOUBLE, text_val TEXT, bin_val SCALAR)"
);
} else {
testHelper.executeSql(
"CREATE TABLE test(id INT PRIMARY KEY, num_val NUMBER, text_val TEXT, bin_val SCALAR)"
);
}
try (
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM test")
Expand All @@ -237,9 +244,15 @@ public void testSearchableColumns() throws SQLException {
@Test
@DisplayName("returned no monetary columns")
public void testCurrencyColumns() throws SQLException {
testHelper.executeSql(
"CREATE TABLE test(id INT PRIMARY KEY, num_val DOUBLE, text_val TEXT, bin_val SCALAR)"
);
if (ServerVersion.V_2_2.isGreaterThan(testHelper.getInstanceVersion())) {
testHelper.executeSql(
"CREATE TABLE test(id INT PRIMARY KEY, num_val DOUBLE, text_val TEXT, bin_val SCALAR)"
);
} else {
testHelper.executeSql(
"CREATE TABLE test(id INT PRIMARY KEY, num_val NUMBER, text_val TEXT, bin_val SCALAR)"
);
}
try (
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM test")
Expand All @@ -254,8 +267,9 @@ public void testCurrencyColumns() throws SQLException {
}

@Test
@DisplayName("returned signed columns")
public void testSignedColumns() throws SQLException {
@DisplayName("returned signed decimal columns")
public void testSignedDecimalColumns() throws SQLException {
assumeServerVersionLessThan(testHelper.getInstanceVersion(), ServerVersion.V_2_2);
testHelper.executeSql(
"CREATE TABLE test(id INT PRIMARY KEY, double_val DOUBLE, real_val REAL, float_val FLOAT)"
);
Expand All @@ -272,6 +286,23 @@ public void testSignedColumns() throws SQLException {
}
}

@Test
@DisplayName("returned signed number column")
public void testSignedNumberColumn() throws SQLException {
assumeMinimalServerVersion(testHelper.getInstanceVersion(), ServerVersion.V_2_2_1);
testHelper.executeSql(
"CREATE TABLE test(id INT PRIMARY KEY, num_val NUMBER)"
);
try (
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM test")
) {
ResultSetMetaData rsMeta = resultSet.getMetaData();
assertTrue(rsMeta.isSigned(1));
assertTrue(rsMeta.isSigned(2));
}
}

@Test
@DisplayName("returned not signed columns")
public void testNotSignedColumns() throws SQLException {
Expand All @@ -292,8 +323,9 @@ public void testNotSignedColumns() throws SQLException {
}

@Test
@DisplayName("returned numeric column types")
public void testColumnsNumericTypes() throws SQLException {
@DisplayName("returned number type aliases")
public void testColumnsNumberTypeAliases() throws SQLException {
assumeServerVersionLessThan(testHelper.getInstanceVersion(), ServerVersion.V_2_2);
testHelper.executeSql(
"CREATE TABLE test(id INT PRIMARY KEY, f_val FLOAT, d_val DOUBLE, r_val REAL)"
);
Expand All @@ -303,26 +335,45 @@ public void testColumnsNumericTypes() throws SQLException {
) {
ResultSetMetaData rsMeta = resultSet.getMetaData();

assertEquals(Types.INTEGER, rsMeta.getColumnType(1));
assertEquals(Types.BIGINT, rsMeta.getColumnType(1));
assertEquals("integer", rsMeta.getColumnTypeName(1));
assertEquals("java.lang.Integer", rsMeta.getColumnClassName(1));
assertEquals("java.lang.Long", rsMeta.getColumnClassName(1));

// we cannot distinguish numeric types because Tarantool
// receives double noSQL type for all the numeric SQL types
assertEquals(Types.DOUBLE, rsMeta.getColumnType(2));
assertEquals("double", rsMeta.getColumnTypeName(2));
assertEquals("number", rsMeta.getColumnTypeName(2));
assertEquals("java.lang.Double", rsMeta.getColumnClassName(2));

assertEquals(Types.DOUBLE, rsMeta.getColumnType(3));
assertEquals("double", rsMeta.getColumnTypeName(3));
assertEquals("number", rsMeta.getColumnTypeName(3));
assertEquals("java.lang.Double", rsMeta.getColumnClassName(3));

assertEquals(Types.DOUBLE, rsMeta.getColumnType(4));
assertEquals("double", rsMeta.getColumnTypeName(4));
assertEquals("number", rsMeta.getColumnTypeName(4));
assertEquals("java.lang.Double", rsMeta.getColumnClassName(4));
}
}

@Test
@DisplayName("returned number column type")
public void testColumnsNumericTypes() throws SQLException {
assumeMinimalServerVersion(testHelper.getInstanceVersion(), ServerVersion.V_2_2_1);
testHelper.executeSql(
"CREATE TABLE test(id INT PRIMARY KEY, num_val NUMBER)"
);
try (
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM test")
) {
ResultSetMetaData rsMeta = resultSet.getMetaData();

assertEquals(Types.DOUBLE, rsMeta.getColumnType(2));
assertEquals("number", rsMeta.getColumnTypeName(2));
assertEquals("java.lang.Double", rsMeta.getColumnClassName(2));
}
}

@Test
@DisplayName("returned textual column types")
public void testColumnsTextualTypes() throws SQLException {
Expand All @@ -335,17 +386,17 @@ public void testColumnsTextualTypes() throws SQLException {
) {
ResultSetMetaData rsMeta = resultSet.getMetaData();

assertEquals(Types.INTEGER, rsMeta.getColumnType(1));
assertEquals(Types.BIGINT, rsMeta.getColumnType(1));
assertEquals("integer", rsMeta.getColumnTypeName(1));
assertEquals("java.lang.Integer", rsMeta.getColumnClassName(1));
assertEquals("java.lang.Long", rsMeta.getColumnClassName(1));

assertEquals(Types.VARCHAR, rsMeta.getColumnType(2));
assertEquals("varchar", rsMeta.getColumnTypeName(2));
assertEquals("string", rsMeta.getColumnTypeName(2));
assertEquals("java.lang.String", rsMeta.getColumnClassName(2));

// TEXT and VARCHAR are not distinguishable
assertEquals(Types.VARCHAR, rsMeta.getColumnType(3));
assertEquals("varchar", rsMeta.getColumnTypeName(3));
assertEquals("string", rsMeta.getColumnTypeName(3));
assertEquals("java.lang.String", rsMeta.getColumnClassName(3));

assertEquals(Types.BINARY, rsMeta.getColumnType(4));
Expand Down
Loading

0 comments on commit a865c7b

Please sign in to comment.