1414import java .util .HashMap ;
1515import java .util .List ;
1616import java .util .Map ;
17-
18- import io .grpc .netty .shaded .io .netty .util .collection .IntObjectHashMap ;
19- import io .grpc .netty .shaded .io .netty .util .collection .IntObjectMap ;
17+ import java .util .UUID ;
2018
2119import tech .ydb .jdbc .YdbConst ;
2220import tech .ydb .table .values .DecimalType ;
2927public class YdbTypes {
3028 private static final YdbTypes INSTANCE = new YdbTypes ();
3129
32- private final IntObjectMap < Type > typeBySqlType ;
30+ private final Map < Integer , Type > typeBySqlType ;
3331 private final Map <Class <?>, Type > typeByClass ;
3432
35- private final Map <Type , Integer > sqlTypeByPrimitiveNumId ;
36-
3733 private YdbTypes () {
38- typeBySqlType = new IntObjectHashMap <>(18 + PrimitiveType . values (). length );
34+ typeBySqlType = new HashMap <>();
3935
4036 // Store custom type ids to use it for PrepaparedStatement.setObject
41- for (PrimitiveType type : PrimitiveType .values ()) {
42- typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + type .ordinal (), type );
43- }
37+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 0 , PrimitiveType .Bool );
38+
39+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 1 , PrimitiveType .Int8 );
40+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 2 , PrimitiveType .Uint8 );
41+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 3 , PrimitiveType .Int16 );
42+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 4 , PrimitiveType .Uint16 );
43+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 5 , PrimitiveType .Int32 );
44+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 6 , PrimitiveType .Uint32 );
45+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 7 , PrimitiveType .Int64 );
46+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 8 , PrimitiveType .Uint64 );
47+
48+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 9 , PrimitiveType .Float );
49+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 10 , PrimitiveType .Double );
50+
51+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 11 , PrimitiveType .Bytes );
52+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 12 , PrimitiveType .Text );
53+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 13 , PrimitiveType .Yson );
54+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 14 , PrimitiveType .Json );
55+
56+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 15 , PrimitiveType .Uuid );
57+
58+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 16 , PrimitiveType .Date );
59+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 17 , PrimitiveType .Datetime );
60+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 18 , PrimitiveType .Timestamp );
61+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 19 , PrimitiveType .Interval );
62+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 20 , PrimitiveType .TzDate );
63+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 21 , PrimitiveType .TzDatetime );
64+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 22 , PrimitiveType .TzTimestamp );
65+
66+ typeBySqlType .put (YdbConst .SQL_KIND_PRIMITIVE + 23 , PrimitiveType .JsonDocument );
4467
4568 typeBySqlType .put (Types .VARCHAR , PrimitiveType .Text );
4669 typeBySqlType .put (Types .BIGINT , PrimitiveType .Int64 );
@@ -90,6 +113,7 @@ private YdbTypes() {
90113 typeByClass .put (String .class , PrimitiveType .Text );
91114 typeByClass .put (byte [].class , PrimitiveType .Bytes );
92115
116+ typeByClass .put (UUID .class , PrimitiveType .Uuid );
93117 typeByClass .put (java .sql .Date .class , PrimitiveType .Date );
94118 typeByClass .put (LocalDate .class , PrimitiveType .Date );
95119 typeByClass .put (LocalDateTime .class , PrimitiveType .Datetime );
@@ -104,71 +128,21 @@ private YdbTypes() {
104128 typeByClass .put (DecimalValue .class , DecimalType .getDefault ());
105129 typeByClass .put (BigDecimal .class , DecimalType .getDefault ());
106130 typeByClass .put (Duration .class , PrimitiveType .Interval );
107-
108- sqlTypeByPrimitiveNumId = new HashMap <>(PrimitiveType .values ().length );
109- for (PrimitiveType id : PrimitiveType .values ()) {
110- final int sqlType ;
111- switch (id ) {
112- case Text :
113- case Json :
114- case JsonDocument :
115- case Uuid :
116- sqlType = Types .VARCHAR ;
117- break ;
118- case Bytes :
119- case Yson :
120- sqlType = Types .BINARY ;
121- break ;
122- case Bool :
123- sqlType = Types .BOOLEAN ;
124- break ;
125- case Int8 :
126- case Int16 :
127- sqlType = Types .SMALLINT ;
128- break ;
129- case Uint8 :
130- case Int32 :
131- case Uint16 :
132- sqlType = Types .INTEGER ;
133- break ;
134- case Uint32 :
135- case Int64 :
136- case Uint64 :
137- case Interval :
138- sqlType = Types .BIGINT ;
139- break ;
140- case Float :
141- sqlType = Types .FLOAT ;
142- break ;
143- case Double :
144- sqlType = Types .DOUBLE ;
145- break ;
146- case Date :
147- sqlType = Types .DATE ;
148- break ;
149- case Datetime :
150- sqlType = Types .TIMESTAMP ;
151- break ;
152- case Timestamp :
153- sqlType = Types .TIMESTAMP ;
154- break ;
155- case TzDate :
156- case TzDatetime :
157- case TzTimestamp :
158- sqlType = Types .TIMESTAMP_WITH_TIMEZONE ;
159- break ;
160- default :
161- sqlType = Types .JAVA_OBJECT ;
162- }
163- sqlTypeByPrimitiveNumId .put (id , sqlType );
164- }
165131 }
166132
167133 public static Type findType (Object obj , int sqlType ) {
168134 return INSTANCE .findTypeImpl (obj , sqlType );
169135 }
170136
171137 private Type findTypeImpl (Object obj , int sqlType ) {
138+ if ((sqlType & YdbConst .SQL_KIND_DECIMAL ) != 0 ) {
139+ int precision = ((sqlType - YdbConst .SQL_KIND_DECIMAL ) >> 6 );
140+ int scale = ((sqlType - YdbConst .SQL_KIND_DECIMAL ) & 0b111111);
141+ if (precision > 0 && precision < 36 && scale >= 0 && scale <= precision ) {
142+ return DecimalType .of (precision , scale );
143+ }
144+ }
145+
172146 if (typeBySqlType .containsKey (sqlType )) {
173147 return typeBySqlType .get (sqlType );
174148 }
@@ -196,10 +170,46 @@ public static int toSqlType(Type type) {
196170 private int toSqlTypeImpl (Type type ) {
197171 switch (type .getKind ()) {
198172 case PRIMITIVE :
199- if (!sqlTypeByPrimitiveNumId .containsKey (type )) {
200- throw new RuntimeException ("Internal error. Unsupported YDB type: " + type );
173+ switch ((PrimitiveType ) type ) {
174+ case Text :
175+ case Json :
176+ case JsonDocument :
177+ case Uuid :
178+ return Types .VARCHAR ;
179+ case Bytes :
180+ case Yson :
181+ return Types .BINARY ;
182+ case Bool :
183+ return Types .BOOLEAN ;
184+ case Int8 :
185+ case Int16 :
186+ return Types .SMALLINT ;
187+ case Uint8 :
188+ case Int32 :
189+ case Uint16 :
190+ return Types .INTEGER ;
191+ case Uint32 :
192+ case Int64 :
193+ case Uint64 :
194+ case Interval :
195+ return Types .BIGINT ;
196+ case Float :
197+ return Types .FLOAT ;
198+ case Double :
199+ return Types .DOUBLE ;
200+ case Date :
201+ return Types .DATE ;
202+ case Datetime :
203+ return Types .TIMESTAMP ;
204+ case Timestamp :
205+ return Types .TIMESTAMP ;
206+ case TzDate :
207+ case TzDatetime :
208+ case TzTimestamp :
209+ return Types .TIMESTAMP_WITH_TIMEZONE ;
210+ default :
211+ return Types .JAVA_OBJECT ;
201212 }
202- return sqlTypeByPrimitiveNumId .get (type );
203213 case OPTIONAL :
204214 return toSqlTypeImpl (type .unwrapOptional ());
205215 case DECIMAL :
@@ -245,7 +255,7 @@ private int getSqlPrecisionImpl(Type type) {
245255 case OPTIONAL :
246256 return getSqlPrecisionImpl (type .unwrapOptional ());
247257 case DECIMAL :
248- return 8 + 8 ;
258+ return (( DecimalType ) type ). getPrecision () ;
249259 case PRIMITIVE :
250260 return getSqlPrecisionImpl ((PrimitiveType ) type );
251261 default :
@@ -287,8 +297,6 @@ private List<Type> getAllDatabaseTypesImpl() {
287297 DecimalType .getDefault ());
288298 }
289299
290- //
291-
292300 private int getSqlPrecisionImpl (PrimitiveType type ) {
293301 switch (type ) {
294302 case Bool :
0 commit comments