Skip to content

Commit 34ad7e1

Browse files
committed
Added support of null and void types
1 parent 16720c5 commit 34ad7e1

File tree

5 files changed

+106
-18
lines changed

5 files changed

+106
-18
lines changed

jdbc/src/main/java/tech/ydb/jdbc/common/MappingGetters.java

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.Objects;
1717
import java.util.concurrent.TimeUnit;
1818

19+
import tech.ydb.jdbc.YdbConst;
1920
import tech.ydb.jdbc.impl.YdbTypesImpl;
2021
import tech.ydb.table.result.PrimitiveReader;
2122
import tech.ydb.table.result.ValueReader;
@@ -24,8 +25,6 @@
2425
import tech.ydb.table.values.Type;
2526
import tech.ydb.table.values.Value;
2627

27-
import static tech.ydb.jdbc.YdbConst.UNABLE_TO_CAST;
28-
import static tech.ydb.jdbc.YdbConst.UNABLE_TO_CONVERT;
2928
import static tech.ydb.table.values.PrimitiveType.Bool;
3029
import static tech.ydb.table.values.PrimitiveType.Bytes;
3130
import static tech.ydb.table.values.PrimitiveType.Date;
@@ -96,6 +95,25 @@ static Getters buildGetters(Type type) {
9695
value -> value.getDecimal().toBigDecimal(),
9796
castToReaderNotSupported(clazz)
9897
);
98+
case VOID:
99+
case NULL:
100+
return new Getters(
101+
value -> null,
102+
value -> false,
103+
value -> 0,
104+
value -> 0,
105+
value -> 0,
106+
value -> 0,
107+
value -> 0,
108+
value -> 0,
109+
value -> null,
110+
value -> null,
111+
value -> 0,
112+
value -> null,
113+
value -> null,
114+
value -> null,
115+
value -> null
116+
);
99117
default:
100118
return new Getters(
101119
value -> String.valueOf(value.getValue()),
@@ -675,88 +693,88 @@ private static ValueToObject valueToObject(PrimitiveType id) {
675693
}
676694

677695
private static SQLException cannotConvert(PrimitiveType type, Class<?> javaType, Object value) {
678-
return new SQLException(String.format(UNABLE_TO_CONVERT, type, value, javaType));
696+
return new SQLException(String.format(YdbConst.UNABLE_TO_CONVERT, type, value, javaType));
679697
}
680698

681699
private static SQLException dataTypeNotSupported(PrimitiveType type, Class<?> javaType) {
682-
return new SQLException(String.format(UNABLE_TO_CAST, type, javaType));
700+
return new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, javaType));
683701
}
684702

685703
private static ValueToBoolean castToBooleanNotSupported(String type) {
686704
return value -> {
687-
throw new SQLException(String.format(UNABLE_TO_CAST, type, boolean.class));
705+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, boolean.class));
688706
};
689707
}
690708

691709
private static ValueToByte castToByteNotSupported(String type) {
692710
return value -> {
693-
throw new SQLException(String.format(UNABLE_TO_CAST, type, byte.class));
711+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, byte.class));
694712
};
695713
}
696714

697715
private static ValueToShort castToShortNotSupported(String type) {
698716
return value -> {
699-
throw new SQLException(String.format(UNABLE_TO_CAST, type, short.class));
717+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, short.class));
700718
};
701719
}
702720

703721
private static ValueToInt castToIntNotSupported(String type) {
704722
return value -> {
705-
throw new SQLException(String.format(UNABLE_TO_CAST, type, int.class));
723+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, int.class));
706724
};
707725
}
708726

709727
private static ValueToLong castToLongNotSupported(String type) {
710728
return value -> {
711-
throw new SQLException(String.format(UNABLE_TO_CAST, type, long.class));
729+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, long.class));
712730
};
713731
}
714732

715733
private static ValueToFloat castToFloatNotSupported(String type) {
716734
return value -> {
717-
throw new SQLException(String.format(UNABLE_TO_CAST, type, float.class));
735+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, float.class));
718736
};
719737
}
720738

721739
private static ValueToDouble castToDoubleNotSupported(String type) {
722740
return value -> {
723-
throw new SQLException(String.format(UNABLE_TO_CAST, type, double.class));
741+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, double.class));
724742
};
725743
}
726744

727745
private static ValueToBytes castToBytesNotSupported(String type) {
728746
return value -> {
729-
throw new SQLException(String.format(UNABLE_TO_CAST, type, byte[].class));
747+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, byte[].class));
730748
};
731749
}
732750

733751
private static ValueToDateMillis castToDateMillisNotSupported(String type) {
734752
return value -> {
735-
throw new SQLException(String.format(UNABLE_TO_CAST, type, long.class));
753+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, long.class));
736754
};
737755
}
738756

739757
private static ValueToNString castToNStringNotSupported(String type) {
740758
return value -> {
741-
throw new SQLException(String.format(UNABLE_TO_CAST, type, String.class));
759+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, String.class));
742760
};
743761
}
744762

745763
private static ValueToURL castToUrlNotSupported(String type) {
746764
return value -> {
747-
throw new SQLException(String.format(UNABLE_TO_CAST, type, URL.class));
765+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, URL.class));
748766
};
749767
}
750768

751769
private static ValueToBigDecimal castToBigDecimalNotSupported(String type) {
752770
return value -> {
753-
throw new SQLException(String.format(UNABLE_TO_CAST, type, BigDecimal.class));
771+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, BigDecimal.class));
754772
};
755773
}
756774

757775
private static ValueToReader castToReaderNotSupported(String type) {
758776
return value -> {
759-
throw new SQLException(String.format(UNABLE_TO_CAST, type, Reader.class));
777+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST, type, Reader.class));
760778
};
761779
}
762780

jdbc/src/main/java/tech/ydb/jdbc/common/TypeDescription.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ private TypeDescription(Type type,
4343
this.sqlType = Objects.requireNonNull(sqlType);
4444
}
4545

46+
public boolean isNullType() {
47+
return type.getKind() == Type.Kind.NULL || type.getKind() == Type.Kind.VOID;
48+
}
49+
4650
public boolean isOptional() {
4751
return optional;
4852
}

jdbc/src/main/java/tech/ydb/jdbc/impl/YdbResultSetImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ private <T> T getDateImpl(int columnIndex, LongFunction<T> fromMillis) throws SQ
557557

558558

559559
private boolean isNullValue(TypeDescription description, ValueReader value) {
560-
return description.isOptional() && !value.isOptionalItemPresent();
560+
return description.isNullType() || (description.isOptional() && !value.isOptionalItemPresent());
561561
}
562562

563563
private void initValueReader(int columnIndex) throws SQLException {

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbConnectionImplTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,39 @@ public void testWarningsInQuery() throws SQLException {
688688
}
689689
}
690690

691+
@Test
692+
public void testLiteralQuery() throws SQLException {
693+
try (Statement statement = jdbc.connection().createStatement()) {
694+
String query = "SELECT '1' AS p1, 123 AS p2, NULL AS p3";
695+
try (ResultSet rs = statement.executeQuery(query)) {
696+
Assertions.assertTrue(rs.next());
697+
698+
Assertions.assertEquals("1", rs.getString("p1"));
699+
Assertions.assertArrayEquals(new byte[] { '1' }, rs.getBytes("p1"));
700+
Assertions.assertFalse(rs.wasNull());
701+
702+
Assertions.assertEquals(123, rs.getByte("p2"));
703+
Assertions.assertEquals(123, rs.getShort("p2"));
704+
Assertions.assertEquals(123, rs.getInt("p2"));
705+
Assertions.assertEquals(123, rs.getLong("p2"));
706+
Assertions.assertFalse(rs.wasNull());
707+
708+
Assertions.assertNull(rs.getObject("p3"));
709+
Assertions.assertNull(rs.getString("p3"));
710+
Assertions.assertNull(rs.getBytes("p3"));
711+
712+
Assertions.assertEquals(0, rs.getByte("p3"));
713+
Assertions.assertEquals(0, rs.getShort("p3"));
714+
Assertions.assertEquals(0, rs.getInt("p3"));
715+
Assertions.assertEquals(0, rs.getLong("p3"));
716+
717+
Assertions.assertTrue(rs.wasNull());
718+
719+
Assertions.assertFalse(rs.next());
720+
}
721+
}
722+
}
723+
691724
@Test
692725
public void testAnsiLexer() throws SQLException {
693726
try (Statement statement = jdbc.connection().createStatement()) {

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbQueryConnectionImplTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,39 @@ public void testWarningsInQuery() throws SQLException {
689689
}
690690
}
691691

692+
@Test
693+
public void testLiteralQuery() throws SQLException {
694+
try (Statement statement = jdbc.connection().createStatement()) {
695+
String query = "SELECT '1' AS p1, 123 AS p2, NULL AS p3";
696+
try (ResultSet rs = statement.executeQuery(query)) {
697+
Assertions.assertTrue(rs.next());
698+
699+
Assertions.assertEquals("1", rs.getString("p1"));
700+
Assertions.assertArrayEquals(new byte[] { '1' }, rs.getBytes("p1"));
701+
Assertions.assertFalse(rs.wasNull());
702+
703+
Assertions.assertEquals(123, rs.getByte("p2"));
704+
Assertions.assertEquals(123, rs.getShort("p2"));
705+
Assertions.assertEquals(123, rs.getInt("p2"));
706+
Assertions.assertEquals(123, rs.getLong("p2"));
707+
Assertions.assertFalse(rs.wasNull());
708+
709+
Assertions.assertNull(rs.getObject("p3"));
710+
Assertions.assertNull(rs.getString("p3"));
711+
Assertions.assertNull(rs.getBytes("p3"));
712+
713+
Assertions.assertEquals(0, rs.getByte("p3"));
714+
Assertions.assertEquals(0, rs.getShort("p3"));
715+
Assertions.assertEquals(0, rs.getInt("p3"));
716+
Assertions.assertEquals(0, rs.getLong("p3"));
717+
718+
Assertions.assertTrue(rs.wasNull());
719+
720+
Assertions.assertFalse(rs.next());
721+
}
722+
}
723+
}
724+
692725
@Test
693726
public void testAnsiLexer() throws SQLException {
694727
try (Statement statement = jdbc.connection().createStatement()) {

0 commit comments

Comments
 (0)