forked from deephaven/deephaven-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ZonedDateTime and Array Shenanigans
- Loading branch information
1 parent
89eee34
commit 480bedf
Showing
3 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...e/table/src/main/java/io/deephaven/engine/table/impl/select/ZonedDateTimeRangeFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending | ||
// | ||
package io.deephaven.engine.table.impl.select; | ||
|
||
import io.deephaven.base.verify.Assert; | ||
import io.deephaven.engine.table.ColumnDefinition; | ||
import io.deephaven.engine.table.TableDefinition; | ||
import io.deephaven.engine.table.impl.chunkfilter.ChunkFilter; | ||
import io.deephaven.engine.rowset.chunkattributes.OrderedRowKeys; | ||
import io.deephaven.engine.table.ColumnSource; | ||
import io.deephaven.engine.table.impl.sources.ReinterpretUtils; | ||
import io.deephaven.chunk.*; | ||
import io.deephaven.chunk.attributes.Values; | ||
import io.deephaven.engine.rowset.WritableRowSet; | ||
import io.deephaven.engine.rowset.RowSet; | ||
import io.deephaven.time.DateTimeUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
public class ZonedDateTimeRangeFilter extends LongRangeFilter { | ||
|
||
public ZonedDateTimeRangeFilter(String columnName, ZonedDateTime val1, ZonedDateTime val2) { | ||
super(columnName, DateTimeUtils.epochNanos(val1), DateTimeUtils.epochNanos(val2), true, true); | ||
} | ||
|
||
public ZonedDateTimeRangeFilter( | ||
String columnName, ZonedDateTime val1, ZonedDateTime val2, boolean lowerInclusive, boolean upperInclusive) { | ||
super(columnName, DateTimeUtils.epochNanos(val1), DateTimeUtils.epochNanos(val2), | ||
lowerInclusive, upperInclusive); | ||
} | ||
|
||
public ZonedDateTimeRangeFilter( | ||
String columnName, long val1, long val2, boolean lowerInclusive, boolean upperInclusive) { | ||
super(columnName, val1, val2, lowerInclusive, upperInclusive); | ||
} | ||
|
||
@Override | ||
public void init(@NotNull final TableDefinition tableDefinition) { | ||
if (chunkFilter != null) { | ||
return; | ||
} | ||
|
||
final ColumnDefinition<?> def = tableDefinition.getColumn(columnName); | ||
if (def == null) { | ||
throw new RuntimeException("Column \"" + columnName + "\" doesn't exist in this table, available columns: " | ||
+ tableDefinition.getColumnNames()); | ||
} | ||
|
||
final Class<?> colClass = def.getDataType(); | ||
Assert.eq(colClass, "colClass", ZonedDateTime.class); | ||
|
||
longFilter = super.initChunkFilter(); | ||
chunkFilter = new ZonedDateTimeLongChunkFilterAdapter(); | ||
} | ||
|
||
@Override | ||
public ZonedDateTimeRangeFilter copy() { | ||
final ZonedDateTimeRangeFilter copy = | ||
new ZonedDateTimeRangeFilter(columnName, lower, upper, lowerInclusive, upperInclusive); | ||
copy.chunkFilter = chunkFilter; | ||
copy.longFilter = longFilter; | ||
return copy; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ZonedDateTimeRangeFilter(" + columnName + " in " | ||
+ (lowerInclusive ? "[" : "(") | ||
+ DateTimeUtils.epochNanosToInstant(lower) + "," + DateTimeUtils.epochNanosToInstant(upper) | ||
+ (upperInclusive ? "]" : ")") + ")"; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
WritableRowSet binarySearch( | ||
@NotNull final RowSet selection, | ||
@NotNull final ColumnSource<?> columnSource, | ||
final boolean usePrev, | ||
final boolean reverse) { | ||
if (selection.isEmpty()) { | ||
return selection.copy(); | ||
} | ||
|
||
// noinspection unchecked | ||
final ColumnSource<Long> instantColumnSource = | ||
ReinterpretUtils.zonedDateTimeToLongSource((ColumnSource<ZonedDateTime>) columnSource); | ||
return super.binarySearch(selection, instantColumnSource, usePrev, reverse); | ||
} | ||
|
||
private class ZonedDateTimeLongChunkFilterAdapter implements ChunkFilter { | ||
@Override | ||
public void filter(Chunk<? extends Values> values, LongChunk<OrderedRowKeys> keys, | ||
WritableLongChunk<OrderedRowKeys> results) { | ||
try (final WritableLongChunk<Values> writableLongChunk = | ||
WritableLongChunk.makeWritableChunk(values.size())) { | ||
|
||
final ObjectChunk<ZonedDateTime, ? extends Values> objectValues = values.asObjectChunk(); | ||
for (int ii = 0; ii < values.size(); ++ii) { | ||
final ZonedDateTime instant = objectValues.get(ii); | ||
writableLongChunk.set(ii, DateTimeUtils.epochNanos(instant)); | ||
} | ||
writableLongChunk.setSize(values.size()); | ||
longFilter.filter(writableLongChunk, keys, results); | ||
} | ||
} | ||
} | ||
} |