Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: respect iox::column_type::field metadata when mapping query #200

Merged
merged 23 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6f214d9
feat: respect iox::column_type::field metadata when mapping query
NguyenHoangSon96 Nov 21, 2024
0c91353
chore: update CHANGELOG.mdlog.md
NguyenHoangSon96 Nov 21, 2024
def624c
chore: remove unused file
NguyenHoangSon96 Nov 21, 2024
a3408c0
chore: lint CHANGELOG.md
NguyenHoangSon96 Nov 21, 2024
cf375aa
refactor: remove setTimestamp function
NguyenHoangSon96 Nov 22, 2024
541e234
chore: update CHANGELOG.md
NguyenHoangSon96 Nov 22, 2024
7d35ff0
chore: update CHANGELOG.md and pom.xml release version
NguyenHoangSon96 Nov 25, 2024
95a29cb
chore: add javadoc for getTimestampNano function
NguyenHoangSon96 Nov 25, 2024
088b36d
refactor: change to enhance for loop
NguyenHoangSon96 Nov 25, 2024
2696bc7
fix: build
NguyenHoangSon96 Nov 25, 2024
6c3d2fb
feat: move on if type cast is fail
NguyenHoangSon96 Nov 26, 2024
24bfd5e
fix: linter
NguyenHoangSon96 Nov 26, 2024
1699443
fix: linter
NguyenHoangSon96 Nov 26, 2024
0fdf8e0
fix: linter
NguyenHoangSon96 Nov 26, 2024
c4bb8ab
feat: add getMappedValue function
NguyenHoangSon96 Nov 28, 2024
a796a13
refactor: getMappedValue function
NguyenHoangSon96 Nov 29, 2024
daab545
refactor: getMappedValue function
NguyenHoangSon96 Nov 29, 2024
06e29ed
refactor: getMappedValue function
NguyenHoangSon96 Nov 29, 2024
16ea54f
fix: linter
NguyenHoangSon96 Nov 29, 2024
547c87e
fix: linter
NguyenHoangSon96 Nov 29, 2024
dbaaf99
refactor: change warning message
NguyenHoangSon96 Dec 6, 2024
79159a9
fix: build
NguyenHoangSon96 Dec 6, 2024
fcca2df
refactor: getArrayObjectFromVectorSchemaRoot function
NguyenHoangSon96 Dec 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

## 0.9.0 [2024-08-12]

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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
11 changes: 11 additions & 0 deletions src/main/java/com/influxdb/v3/client/PointValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,17 @@ public PointValues setField(@Nonnull final String field, @Nullable final Object
return putField(field, value);
}

/**
* Add a null field.
*
* @param field the field name
* @return this
*/
@Nonnull
public PointValues setNullField(@Nonnull final String field) {
return putField(field, null);
}

NguyenHoangSon96 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Adds or replaces fields for this point.
*
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
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,56 @@ 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
*/
public static BigInteger getTimestampNano(@Nonnull final Object value, @Nonnull final Field field) {
NguyenHoangSon96 marked this conversation as resolved.
Show resolved Hide resolved
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, WritePrecision.NS);
} else {
Instant instant = Instant.ofEpochMilli((Long) value);
result = convertInstantToNano(instant, WritePrecision.NS);
}
} else if (value instanceof LocalDateTime) {
Instant instant = ((LocalDateTime) value).toInstant(ZoneOffset.UTC);
result = convertInstantToNano(instant, WritePrecision.NS);
}
return result;
}

private static BigInteger convertInstantToNano(final Instant instant, final WritePrecision precision) {
NguyenHoangSon96 marked this conversation as resolved.
Show resolved Hide resolved
var writePrecision = WritePrecision.NS;
if (precision != null) {
writePrecision = precision;
}
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 {
NguyenHoangSon96 marked this conversation as resolved.
Show resolved Hide resolved

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();

Check warning on line 48 in src/main/java/com/influxdb/v3/client/internal/TypeCasting.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/influxdb/v3/client/internal/TypeCasting.java#L48

Added line #L48 was not covered by tests
}

/**
* 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();

Check warning on line 64 in src/main/java/com/influxdb/v3/client/internal/TypeCasting.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/influxdb/v3/client/internal/TypeCasting.java#L64

Added line #L64 was not covered by tests
}

/**
* 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 ((Text) value).toString();
NguyenHoangSon96 marked this conversation as resolved.
Show resolved Hide resolved
}

return (String) value;
}
}
Loading
Loading