|
25 | 25 | import java.nio.charset.StandardCharsets; |
26 | 26 | import java.util.Arrays; |
27 | 27 | import java.util.HashMap; |
| 28 | +import java.util.List; |
28 | 29 | import java.util.Map; |
29 | 30 | import javax.annotation.Nonnull; |
30 | 31 |
|
31 | 32 | import org.apache.arrow.memory.RootAllocator; |
32 | 33 | import org.apache.arrow.vector.BaseFixedWidthVector; |
33 | 34 | import org.apache.arrow.vector.BigIntVector; |
| 35 | +import org.apache.arrow.vector.BitVector; |
| 36 | +import org.apache.arrow.vector.Float8Vector; |
34 | 37 | import org.apache.arrow.vector.TimeMilliVector; |
35 | 38 | import org.apache.arrow.vector.TimeStampVector; |
36 | 39 | import org.apache.arrow.vector.VarCharVector; |
37 | 40 | import org.apache.arrow.vector.VectorSchemaRoot; |
| 41 | +import org.apache.arrow.vector.types.FloatingPointPrecision; |
38 | 42 | import org.apache.arrow.vector.types.TimeUnit; |
39 | 43 | import org.apache.arrow.vector.types.pojo.ArrowType; |
40 | 44 | import org.apache.arrow.vector.types.pojo.Field; |
@@ -125,6 +129,41 @@ void timestampAsArrowInt() { |
125 | 129 | } |
126 | 130 | } |
127 | 131 |
|
| 132 | + @Test |
| 133 | + public void testConverterWithMetaType() { |
| 134 | + try (VectorSchemaRoot root = generateVectorSchemaRoot()) { |
| 135 | + PointValues pointValues = VectorSchemaRootConverter.INSTANCE.toPointValues(0, root, root.getFieldVectors()); |
| 136 | + |
| 137 | + String measurement = pointValues.getMeasurement(); |
| 138 | + Assertions.assertThat(measurement).isEqualTo("host"); |
| 139 | + Assertions.assertThat(measurement.getClass()).isEqualTo(String.class); |
| 140 | + |
| 141 | + BigInteger expected = BigInteger.valueOf(123_456L * 1_000_000); |
| 142 | + Assertions.assertThat((BigInteger) pointValues.getTimestamp()).isEqualByComparingTo(expected); |
| 143 | + |
| 144 | + Long memTotal = (Long) pointValues.getField("mem_total"); |
| 145 | + Assertions.assertThat(memTotal).isEqualTo(2048); |
| 146 | + Assertions.assertThat(memTotal.getClass()).isEqualTo(Long.class); |
| 147 | + |
| 148 | + Long diskFree = (Long) pointValues.getField("disk_free"); |
| 149 | + Assertions.assertThat(diskFree).isEqualTo(1_000_000); |
| 150 | + Assertions.assertThat(diskFree.getClass()).isEqualTo(Long.class); |
| 151 | + |
| 152 | + Double temperature = (Double) pointValues.getField("temperature"); |
| 153 | + Assertions.assertThat(temperature).isEqualTo(100.8766f); |
| 154 | + Assertions.assertThat(temperature.getClass()).isEqualTo(Double.class); |
| 155 | + |
| 156 | + String name = (String) pointValues.getField("name"); |
| 157 | + Assertions.assertThat(name).isEqualTo("intel"); |
| 158 | + Assertions.assertThat(name.getClass()).isEqualTo(String.class); |
| 159 | + |
| 160 | + Boolean isActive = (Boolean) pointValues.getField("isActive"); |
| 161 | + Assertions.assertThat(isActive).isEqualTo(true); |
| 162 | + Assertions.assertThat(isActive.getClass()).isEqualTo(Boolean.class); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + |
128 | 167 | @Test |
129 | 168 | void timestampWithoutMetadataAndFieldWithoutMetadata() { |
130 | 169 | FieldType timeType = new FieldType(true, new ArrowType.Time(TimeUnit.MILLISECOND, 32), null); |
@@ -237,4 +276,87 @@ private VectorSchemaRoot initializeVectorSchemaRoot(@Nonnull final Field... fiel |
237 | 276 |
|
238 | 277 | return root; |
239 | 278 | } |
| 279 | + |
| 280 | + public VectorSchemaRoot generateVectorSchemaRoot() { |
| 281 | + Field measurementField = generateStringField("measurement"); |
| 282 | + Field timeField = generateTimeField(); |
| 283 | + Field memTotalField = generateIntField("mem_total"); |
| 284 | + Field diskFreeField = generateUnsignedIntField("disk_free"); |
| 285 | + Field temperatureField = generateFloatField("temperature"); |
| 286 | + Field nameField = generateStringField("name"); |
| 287 | + Field isActiveField = generateBoolField("isActive"); |
| 288 | + List<Field> fields = List.of(measurementField, timeField, memTotalField, diskFreeField, temperatureField, nameField, isActiveField); |
| 289 | + |
| 290 | + VectorSchemaRoot root = initializeVectorSchemaRoot(fields.toArray(new Field[0])); |
| 291 | + VarCharVector measurement = (VarCharVector) root.getVector("measurement"); |
| 292 | + measurement.allocateNew(); |
| 293 | + measurement.set(0, "host".getBytes()); |
| 294 | + |
| 295 | + TimeMilliVector timeVector = (TimeMilliVector) root.getVector("time"); |
| 296 | + timeVector.allocateNew(); |
| 297 | + timeVector.setSafe(0, 123_456); |
| 298 | + |
| 299 | + BigIntVector intVector = (BigIntVector) root.getVector("mem_total"); |
| 300 | + intVector.allocateNew(); |
| 301 | + intVector.set(0, 2048); |
| 302 | + |
| 303 | + BigIntVector unsignedIntVector = (BigIntVector) root.getVector("disk_free"); |
| 304 | + unsignedIntVector.allocateNew(); |
| 305 | + unsignedIntVector.set(0, 1_000_000); |
| 306 | + |
| 307 | + Float8Vector floatVector = (Float8Vector) root.getVector("temperature"); |
| 308 | + floatVector.allocateNew(); |
| 309 | + floatVector.set(0, 100.8766f); |
| 310 | + |
| 311 | + VarCharVector stringVector = (VarCharVector) root.getVector("name"); |
| 312 | + stringVector.allocateNew(); |
| 313 | + stringVector.setSafe(0, "intel".getBytes()); |
| 314 | + |
| 315 | + BitVector boolVector = (BitVector) root.getVector("isActive"); |
| 316 | + boolVector.allocateNew(); |
| 317 | + boolVector.setSafe(0, 1); |
| 318 | + |
| 319 | + return root; |
| 320 | + } |
| 321 | + |
| 322 | + private Field generateIntField(final String fieldName) { |
| 323 | + Map<String, String> metadata = new HashMap<>(); |
| 324 | + metadata.put("iox::column::type", "iox::column_type::field::integer"); |
| 325 | + FieldType intType = new FieldType(true, new ArrowType.Int(64, true), null, metadata); |
| 326 | + return new Field(fieldName, intType, null); |
| 327 | + } |
| 328 | + |
| 329 | + private Field generateUnsignedIntField(final String fieldName) { |
| 330 | + Map<String, String> metadata = new HashMap<>(); |
| 331 | + metadata.put("iox::column::type", "iox::column_type::field::uinteger"); |
| 332 | + FieldType intType = new FieldType(true, new ArrowType.Int(64, true), null, metadata); |
| 333 | + return new Field(fieldName, intType, null); |
| 334 | + } |
| 335 | + |
| 336 | + private Field generateFloatField(final String fieldName) { |
| 337 | + Map<String, String> metadata = new HashMap<>(); |
| 338 | + metadata.put("iox::column::type", "iox::column_type::field::float"); |
| 339 | + FieldType floatType = new FieldType(true, new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), null, metadata); |
| 340 | + return new Field(fieldName, floatType, null); |
| 341 | + } |
| 342 | + |
| 343 | + private Field generateStringField(final String fieldName) { |
| 344 | + Map<String, String> metadata = new HashMap<>(); |
| 345 | + metadata.put("iox::column::type", "iox::column_type::field::string"); |
| 346 | + FieldType stringType = new FieldType(true, new ArrowType.Utf8(), null, metadata); |
| 347 | + return new Field(fieldName, stringType, null); |
| 348 | + } |
| 349 | + |
| 350 | + private Field generateBoolField(final String fieldName) { |
| 351 | + Map<String, String> metadata = new HashMap<>(); |
| 352 | + metadata.put("iox::column::type", "iox::column_type::field::boolean"); |
| 353 | + FieldType boolType = new FieldType(true, new ArrowType.Bool(), null, metadata); |
| 354 | + return new Field(fieldName, boolType, null); |
| 355 | + } |
| 356 | + |
| 357 | + private Field generateTimeField() { |
| 358 | + FieldType timeType = new FieldType(true, new ArrowType.Time(TimeUnit.MILLISECOND, 32), null); |
| 359 | + return new Field("time", timeType, null); |
| 360 | + } |
| 361 | + |
240 | 362 | } |
0 commit comments