Skip to content

Commit 6c3d2fb

Browse files
feat: move on if type cast is fail
1 parent 2696bc7 commit 6c3d2fb

File tree

7 files changed

+471
-176
lines changed

7 files changed

+471
-176
lines changed

src/main/java/com/influxdb/v3/client/PointValues.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,17 @@ public PointValues setField(@Nonnull final String field, @Nullable final Object
435435
return putField(field, value);
436436
}
437437

438+
/**
439+
* Add a null field.
440+
*
441+
* @param field the field name
442+
* @return this
443+
*/
444+
@Nonnull
445+
public PointValues setNullField(@Nonnull final String field) {
446+
return putField(field, null);
447+
}
448+
438449
/**
439450
* Adds or replaces fields for this point.
440451
*

src/main/java/com/influxdb/v3/client/internal/InfluxDBClientImpl.java

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323

2424
import java.io.ByteArrayOutputStream;
2525
import java.io.IOException;
26-
import java.math.BigInteger;
2726
import java.nio.charset.StandardCharsets;
28-
import java.util.ArrayList;
2927
import java.util.Collections;
3028
import java.util.HashMap;
3129
import java.util.List;
@@ -42,7 +40,6 @@
4240
import io.netty.handler.codec.http.HttpMethod;
4341
import org.apache.arrow.vector.FieldVector;
4442
import org.apache.arrow.vector.VectorSchemaRoot;
45-
import org.apache.arrow.vector.util.Text;
4643

4744
import com.influxdb.v3.client.InfluxDBApiException;
4845
import com.influxdb.v3.client.InfluxDBClient;
@@ -184,46 +181,7 @@ public Stream<Object[]> query(@Nonnull final String query,
184181
@Nonnull final QueryOptions options) {
185182
return queryData(query, parameters, options)
186183
.flatMap(vector -> IntStream.range(0, vector.getRowCount())
187-
.mapToObj(rowNumber -> {
188-
ArrayList<Object> row = new ArrayList<>();
189-
for (FieldVector fieldVector : vector.getFieldVectors()) {
190-
var field = fieldVector.getField();
191-
var metaType = field.getMetadata().get("iox::column::type");
192-
String valueType = metaType != null ? metaType.split("::")[2] : null;
193-
194-
Object value = fieldVector.getObject(rowNumber);
195-
if ("field".equals(valueType)) {
196-
switch (metaType) {
197-
case "iox::column_type::field::integer":
198-
case "iox::column_type::field::uinteger":
199-
var intValue = (Long) value;
200-
row.add(intValue);
201-
break;
202-
case "iox::column_type::field::float":
203-
var doubleValue = (Double) value;
204-
row.add(doubleValue);
205-
break;
206-
case "iox::column_type::field::string":
207-
var textValue = (Text) value;
208-
row.add(textValue.toString());
209-
break;
210-
case "iox::column_type::field::boolean":
211-
var boolValue = (Boolean) value;
212-
row.add(boolValue);
213-
break;
214-
default:
215-
}
216-
} else if ("timestamp".equals(valueType)
217-
|| Objects.equals(field.getName(), "time")) {
218-
BigInteger time = NanosecondConverter.getTimestampNano(value, field);
219-
row.add(time);
220-
} else {
221-
row.add(value);
222-
}
223-
}
224-
225-
return row.toArray();
226-
}));
184+
.mapToObj(rowNumber -> VectorSchemaRootConverter.INSTANCE.getArrayObjectFromVectorSchemaRoot(vector, rowNumber)));
227185
}
228186

229187
@Nonnull
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.influxdb.v3.client.internal;
2+
3+
import javax.annotation.Nonnull;
4+
5+
import org.apache.arrow.vector.util.Text;
6+
7+
/**
8+
* Functions for safe type casting
9+
*/
10+
public final class TypeCasting {
11+
12+
/**
13+
* Safe casting to long value
14+
*
15+
* @param value object to cast
16+
* @return long value
17+
*/
18+
public static long toLongValue(@Nonnull final Object value) {
19+
20+
if (long.class.isAssignableFrom(value.getClass())
21+
|| Long.class.isAssignableFrom(value.getClass())) {
22+
return (long) value;
23+
}
24+
25+
return ((Number) value).longValue();
26+
}
27+
28+
/**
29+
* Safe casting to double value
30+
*
31+
* @param value object to cast
32+
* @return double value
33+
*/
34+
public static double toDoubleValue(@Nonnull final Object value) {
35+
36+
if (double.class.isAssignableFrom(value.getClass())
37+
|| Double.class.isAssignableFrom(value.getClass())) {
38+
return (double) value;
39+
}
40+
41+
return ((Number) value).doubleValue();
42+
}
43+
44+
/**
45+
* Safe casting to string value
46+
*
47+
* @param value object to cast
48+
* @return string value
49+
*/
50+
public static String toStringValue(@Nonnull final Object value) {
51+
52+
if (Text.class.isAssignableFrom(value.getClass())) {
53+
return ((Text) value).toString();
54+
}
55+
56+
return (String) value;
57+
}
58+
}

src/main/java/com/influxdb/v3/client/internal/VectorSchemaRootConverter.java

Lines changed: 112 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
*/
2222
package com.influxdb.v3.client.internal;
2323

24+
import java.math.BigInteger;
2425
import java.time.LocalDateTime;
26+
import java.util.ArrayList;
2527
import java.util.List;
2628
import java.util.Objects;
29+
import java.util.logging.Logger;
2730
import javax.annotation.Nonnull;
2831
import javax.annotation.concurrent.ThreadSafe;
2932

@@ -42,9 +45,11 @@
4245
* This class is thread-safe.
4346
*/
4447
@ThreadSafe
45-
final class VectorSchemaRootConverter {
48+
public final class VectorSchemaRootConverter {
4649

47-
static final VectorSchemaRootConverter INSTANCE = new VectorSchemaRootConverter();
50+
private static final Logger LOG = Logger.getLogger(VectorSchemaRootConverter.class.getName());
51+
52+
public static final VectorSchemaRootConverter INSTANCE = new VectorSchemaRootConverter();
4853

4954
/**
5055
* Converts a given row of data from a VectorSchemaRoot object to PointValues.
@@ -103,8 +108,16 @@ PointValues toPointValues(final int rowNumber,
103108
return p;
104109
}
105110

106-
private void setFieldWithMetaType(final PointValues p,
107-
final String name,
111+
/**
112+
* Set field value for PointValues base on iox::column::type
113+
*
114+
* @param p The target PointValues
115+
* @param fieldName Field name in PointValues
116+
* @param value The value to be set
117+
* @param metaType The iox::column::type column meta type, eg: iox::column_type::field::integer, iox::column_type::field::float
118+
*/
119+
public void setFieldWithMetaType(final PointValues p,
120+
final String fieldName,
108121
final Object value,
109122
final String metaType) {
110123
if (value == null) {
@@ -114,20 +127,110 @@ private void setFieldWithMetaType(final PointValues p,
114127
switch (metaType) {
115128
case "iox::column_type::field::integer":
116129
case "iox::column_type::field::uinteger":
117-
p.setIntegerField(name, (Long) value);
130+
if (value instanceof Long) {
131+
p.setIntegerField(fieldName, TypeCasting.toLongValue(value));
132+
} else {
133+
p.setNullField(fieldName);
134+
LOG.warning(String.format("Value of %s is not an Integer", fieldName));
135+
}
118136
break;
119137
case "iox::column_type::field::float":
120-
p.setFloatField(name, (Double) value);
138+
if (value instanceof Double) {
139+
p.setFloatField(fieldName, TypeCasting.toDoubleValue(value));
140+
} else {
141+
p.setNullField(fieldName);
142+
LOG.warning(String.format("Value of %s is not a Double", fieldName));
143+
}
121144
break;
122145
case "iox::column_type::field::string":
123-
p.setStringField(name, (String) value);
146+
if (value instanceof String || value instanceof Text) {
147+
p.setStringField(fieldName, TypeCasting.toStringValue(value));
148+
} else {
149+
p.setNullField(fieldName);
150+
LOG.warning(String.format("Value of %s is not a String", fieldName));
151+
}
124152
break;
125153
case "iox::column_type::field::boolean":
126-
p.setBooleanField(name, (Boolean) value);
154+
if (value instanceof Boolean) {
155+
p.setBooleanField(fieldName, (Boolean) value);
156+
} else {
157+
p.setNullField(fieldName);
158+
LOG.warning(String.format("Value of %s is not a Boolean", fieldName));
159+
}
127160
break;
128161
default:
129-
p.setField(name, value);
162+
p.setField(fieldName, value);
130163
break;
131164
}
132165
}
166+
167+
/**
168+
* Get array of values from VectorSchemaRoot
169+
*
170+
* @param vector The data return from InfluxDB
171+
* @param rowNumber The row number of data
172+
* @return An array of Objects represent for a row of data
173+
*/
174+
public Object[] getArrayObjectFromVectorSchemaRoot(VectorSchemaRoot vector, int rowNumber) {
175+
List<Object> row = new ArrayList<>();
176+
for (FieldVector fieldVector : vector.getFieldVectors()) {
177+
var field = fieldVector.getField();
178+
var metaType = field.getMetadata().get("iox::column::type");
179+
String valueType = metaType != null ? metaType.split("::")[2] : null;
180+
String fieldName = field.getName();
181+
182+
Object value = fieldVector.getObject(rowNumber);
183+
if (value == null) {
184+
row.add(null);
185+
continue;
186+
}
187+
188+
if ("field".equals(valueType)) {
189+
switch (metaType) {
190+
case "iox::column_type::field::integer":
191+
case "iox::column_type::field::uinteger":
192+
if (value instanceof Long) {
193+
row.add(TypeCasting.toLongValue(value));
194+
} else {
195+
row.add(null);
196+
LOG.warning(String.format("Value of %s is not an Integer", fieldName));
197+
}
198+
break;
199+
case "iox::column_type::field::float":
200+
if (value instanceof Double) {
201+
row.add(TypeCasting.toDoubleValue(value));
202+
} else {
203+
row.add(null);
204+
LOG.warning(String.format("Value of %s is not a Double", fieldName));
205+
}
206+
break;
207+
case "iox::column_type::field::string":
208+
if (value instanceof Text || value instanceof String) {
209+
row.add(TypeCasting.toStringValue(value));
210+
} else {
211+
row.add(null);
212+
LOG.warning(String.format("Value of %s is not a String", fieldName));
213+
}
214+
break;
215+
case "iox::column_type::field::boolean":
216+
if (value instanceof Boolean) {
217+
row.add((Boolean) value);
218+
} else {
219+
row.add(null);
220+
LOG.warning(String.format("Value of %s is not a Boolean", fieldName));
221+
}
222+
break;
223+
default:
224+
}
225+
} else if ("timestamp".equals(valueType)
226+
|| Objects.equals(fieldName, "time")) {
227+
BigInteger time = NanosecondConverter.getTimestampNano(value, field);
228+
row.add(time);
229+
} else {
230+
row.add(value);
231+
}
232+
}
233+
234+
return row.toArray();
235+
}
133236
}

0 commit comments

Comments
 (0)