Skip to content

Commit

Permalink
feat: respect iox::column_type::field metadata when mapping query (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
NguyenHoangSon96 authored Dec 6, 2024
1 parent 86adf7e commit 7fd6c42
Show file tree
Hide file tree
Showing 11 changed files with 836 additions and 96 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
## 0.10.0 [unreleased]
## 1.0.0 [unreleased]

### Features

1. [#200](https://github.com/InfluxCommunity/influxdb3-java/pull/200): Respect iox::column_type::field metadata when
mapping query results into values.
- iox::column_type::field::integer: => Long
- iox::column_type::field::uinteger: => Long
- iox::column_type::field::float: => Double
- iox::column_type::field::string: => String
- iox::column_type::field::boolean: => Boolean

### Dependencies

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<groupId>com.influxdb</groupId>
<artifactId>influxdb3-java</artifactId>
<packaging>jar</packaging>
<version>0.10.0-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>

<name>InfluxDB 3 Java Client</name>
<description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -181,19 +180,13 @@ public Stream<Object[]> query(@Nonnull final String query,
@Nonnull final Map<String, Object> parameters,
@Nonnull final QueryOptions options) {
return queryData(query, parameters, options)
.flatMap(vector -> {
List<FieldVector> fieldVectors = vector.getFieldVectors();
return IntStream
.range(0, vector.getRowCount())
.mapToObj(rowNumber -> {

ArrayList<Object> row = new ArrayList<>();
for (FieldVector fieldVector : fieldVectors) {
row.add(fieldVector.getObject(rowNumber));
}
return row.toArray();
});
});
.flatMap(vector -> IntStream.range(0, vector.getRowCount())
.mapToObj(rowNumber ->
VectorSchemaRootConverter.INSTANCE
.getArrayObjectFromVectorSchemaRoot(
vector,
rowNumber
)));
}

@Nonnull
Expand Down Expand Up @@ -225,7 +218,7 @@ public Stream<PointValues> queryPoints(@Nonnull final String query,
return IntStream
.range(0, vector.getRowCount())
.mapToObj(row ->
VectorSchemaRootConverter.INSTANCE.toPointValues(row, vector, fieldVectors));
VectorSchemaRootConverter.INSTANCE.toPointValues(row, fieldVectors));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.arrow.vector.types.pojo.Field;

import com.influxdb.v3.client.write.WritePrecision;

import static java.util.function.Function.identity;
Expand Down Expand Up @@ -111,4 +118,55 @@ public static BigInteger convert(final Instant instant, final WritePrecision pre

return FROM_NANOS.get(precision).apply(nanos);
}

/**
* Convert Long or LocalDateTime to timestamp nanosecond.
*
* @param value the time in Long or LocalDateTime
* @param field the arrow field metadata
* @return the time in nanosecond
*/
@Nullable
public static BigInteger getTimestampNano(@Nonnull final Object value, @Nonnull final Field field) {
BigInteger result = null;

if (value instanceof Long) {
if (field.getFieldType().getType() instanceof ArrowType.Timestamp) {
ArrowType.Timestamp type = (ArrowType.Timestamp) field.getFieldType().getType();
TimeUnit timeUnit;
switch (type.getUnit()) {
case SECOND:
timeUnit = TimeUnit.SECONDS;
break;
case MILLISECOND:
timeUnit = TimeUnit.MILLISECONDS;
break;
case MICROSECOND:
timeUnit = TimeUnit.MICROSECONDS;
break;
case NANOSECOND:
default:
timeUnit = TimeUnit.NANOSECONDS;
break;
}
long nanoseconds = TimeUnit.NANOSECONDS.convert((Long) value, timeUnit);
Instant instant = Instant.ofEpochSecond(0, nanoseconds);
result = convertInstantToNano(instant);
} else {
Instant instant = Instant.ofEpochMilli((Long) value);
result = convertInstantToNano(instant);
}
} else if (value instanceof LocalDateTime) {
Instant instant = ((LocalDateTime) value).toInstant(ZoneOffset.UTC);
result = convertInstantToNano(instant);
}
return result;
}

@Nullable
private static BigInteger convertInstantToNano(@Nonnull final Instant instant) {
var writePrecision = WritePrecision.NS;
BigInteger convertedTime = NanosecondConverter.convert(instant, writePrecision);
return NanosecondConverter.convertToNanos(convertedTime, writePrecision);
}
}
81 changes: 81 additions & 0 deletions src/main/java/com/influxdb/v3/client/internal/TypeCasting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.influxdb.v3.client.internal;

import javax.annotation.Nonnull;

import org.apache.arrow.vector.util.Text;

/**
* Functions for safe type casting.
*/
public final class TypeCasting {

private TypeCasting() { }

/**
* Safe casting to long value.
*
* @param value object to cast
* @return long value
*/
public static long toLongValue(@Nonnull final Object value) {

if (long.class.isAssignableFrom(value.getClass())
|| Long.class.isAssignableFrom(value.getClass())) {
return (long) value;
}

return ((Number) value).longValue();
}

/**
* Safe casting to double value.
*
* @param value object to cast
* @return double value
*/
public static double toDoubleValue(@Nonnull final Object value) {

if (double.class.isAssignableFrom(value.getClass())
|| Double.class.isAssignableFrom(value.getClass())) {
return (double) value;
}

return ((Number) value).doubleValue();
}

/**
* Safe casting to string value.
*
* @param value object to cast
* @return string value
*/
public static String toStringValue(@Nonnull final Object value) {

if (Text.class.isAssignableFrom(value.getClass())) {
return value.toString();
}

return (String) value;
}
}
Loading

0 comments on commit 7fd6c42

Please sign in to comment.