Skip to content

Commit

Permalink
Add value traversal methods to Type
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Nov 18, 2022
1 parent 4311642 commit be73377
Show file tree
Hide file tree
Showing 20 changed files with 688 additions and 181 deletions.
34 changes: 34 additions & 0 deletions core/trino-main/src/test/java/io/trino/type/AbstractTestType.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package io.trino.type;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import io.airlift.slice.DynamicSliceOutput;
import io.airlift.slice.Slice;
import io.airlift.slice.SliceOutput;
Expand Down Expand Up @@ -197,6 +198,39 @@ public void testRange()
.isEmpty();
}

@Test
public void testPreviousValue()
{
Object sampleValue = getSampleValue();
if (!type.isOrderable()) {
assertThatThrownBy(() -> type.getPreviousValue(sampleValue))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Type is not orderable: " + type);
return;
}
assertThat(type.getPreviousValue(sampleValue))
.isEmpty();
}

@Test
public void testNextValue()
{
Object sampleValue = getSampleValue();
if (!type.isOrderable()) {
assertThatThrownBy(() -> type.getNextValue(sampleValue))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Type is not orderable: " + type);
return;
}
assertThat(type.getNextValue(sampleValue))
.isEmpty();
}

protected Object getSampleValue()
{
return requireNonNull(Iterables.get(expectedStackValues.values(), 0), "sample value is null");
}

protected void assertPositionEquals(Block block, int position, Object expectedStackValue, Object expectedObjectValue)
{
long hash = 0;
Expand Down
43 changes: 43 additions & 0 deletions core/trino-main/src/test/java/io/trino/type/TestBigintType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import io.trino.spi.block.BlockBuilder;
import io.trino.spi.type.Type.Range;

import java.util.Optional;

import static io.trino.spi.type.BigintType.BIGINT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertEquals;

public class TestBigintType
Expand Down Expand Up @@ -58,4 +61,44 @@ public void testRange()
assertEquals(range.getMin(), Long.MIN_VALUE);
assertEquals(range.getMax(), Long.MAX_VALUE);
}

@Override
public void testPreviousValue()
{
long minValue = Long.MIN_VALUE;
long maxValue = Long.MAX_VALUE;

assertThat(type.getPreviousValue(minValue))
.isEqualTo(Optional.empty());
assertThat(type.getPreviousValue(minValue + 1))
.isEqualTo(Optional.of(minValue));

assertThat(type.getPreviousValue(getSampleValue()))
.isEqualTo(Optional.of(1110L));

assertThat(type.getPreviousValue(maxValue - 1))
.isEqualTo(Optional.of(maxValue - 2));
assertThat(type.getPreviousValue(maxValue))
.isEqualTo(Optional.of(maxValue - 1));
}

@Override
public void testNextValue()
{
long minValue = Long.MIN_VALUE;
long maxValue = Long.MAX_VALUE;

assertThat(type.getNextValue(minValue))
.isEqualTo(Optional.of(minValue + 1));
assertThat(type.getNextValue(minValue + 1))
.isEqualTo(Optional.of(minValue + 2));

assertThat(type.getNextValue(getSampleValue()))
.isEqualTo(Optional.of(1112L));

assertThat(type.getNextValue(maxValue - 1))
.isEqualTo(Optional.of(maxValue));
assertThat(type.getNextValue(maxValue))
.isEqualTo(Optional.empty());
}
}
43 changes: 43 additions & 0 deletions core/trino-main/src/test/java/io/trino/type/TestDateType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import io.trino.spi.type.SqlDate;
import io.trino.spi.type.Type.Range;

import java.util.Optional;

import static io.trino.spi.type.DateType.DATE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertEquals;

public class TestDateType
Expand Down Expand Up @@ -59,4 +62,44 @@ public void testRange()
assertEquals(range.getMin(), (long) Integer.MIN_VALUE);
assertEquals(range.getMax(), (long) Integer.MAX_VALUE);
}

@Override
public void testPreviousValue()
{
long minValue = Integer.MIN_VALUE;
long maxValue = Integer.MAX_VALUE;

assertThat(type.getPreviousValue(minValue))
.isEqualTo(Optional.empty());
assertThat(type.getPreviousValue(minValue + 1))
.isEqualTo(Optional.of(minValue));

assertThat(type.getPreviousValue(getSampleValue()))
.isEqualTo(Optional.of(1110L));

assertThat(type.getPreviousValue(maxValue - 1))
.isEqualTo(Optional.of(maxValue - 2));
assertThat(type.getPreviousValue(maxValue))
.isEqualTo(Optional.of(maxValue - 1));
}

@Override
public void testNextValue()
{
long minValue = Integer.MIN_VALUE;
long maxValue = Integer.MAX_VALUE;

assertThat(type.getNextValue(minValue))
.isEqualTo(Optional.of(minValue + 1));
assertThat(type.getNextValue(minValue + 1))
.isEqualTo(Optional.of(minValue + 2));

assertThat(type.getNextValue(getSampleValue()))
.isEqualTo(Optional.of(1112L));

assertThat(type.getNextValue(maxValue - 1))
.isEqualTo(Optional.of(maxValue));
assertThat(type.getNextValue(maxValue))
.isEqualTo(Optional.empty());
}
}
43 changes: 43 additions & 0 deletions core/trino-main/src/test/java/io/trino/type/TestIntegerType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import io.trino.spi.block.BlockBuilder;
import io.trino.spi.type.Type.Range;

import java.util.Optional;

import static io.trino.spi.type.IntegerType.INTEGER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertEquals;

public class TestIntegerType
Expand Down Expand Up @@ -58,4 +61,44 @@ public void testRange()
assertEquals(range.getMin(), (long) Integer.MIN_VALUE);
assertEquals(range.getMax(), (long) Integer.MAX_VALUE);
}

@Override
public void testPreviousValue()
{
long minValue = Integer.MIN_VALUE;
long maxValue = Integer.MAX_VALUE;

assertThat(type.getPreviousValue(minValue))
.isEqualTo(Optional.empty());
assertThat(type.getPreviousValue(minValue + 1))
.isEqualTo(Optional.of(minValue));

assertThat(type.getPreviousValue(getSampleValue()))
.isEqualTo(Optional.of(1110L));

assertThat(type.getPreviousValue(maxValue - 1))
.isEqualTo(Optional.of(maxValue - 2));
assertThat(type.getPreviousValue(maxValue))
.isEqualTo(Optional.of(maxValue - 1));
}

@Override
public void testNextValue()
{
long minValue = Integer.MIN_VALUE;
long maxValue = Integer.MAX_VALUE;

assertThat(type.getNextValue(minValue))
.isEqualTo(Optional.of(minValue + 1));
assertThat(type.getNextValue(minValue + 1))
.isEqualTo(Optional.of(minValue + 2));

assertThat(type.getNextValue(getSampleValue()))
.isEqualTo(Optional.of(1112L));

assertThat(type.getNextValue(maxValue - 1))
.isEqualTo(Optional.of(maxValue));
assertThat(type.getNextValue(maxValue))
.isEqualTo(Optional.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@
import io.trino.spi.block.BlockBuilder;
import io.trino.spi.type.LongTimestampWithTimeZone;
import io.trino.spi.type.SqlTimestampWithTimeZone;
import io.trino.spi.type.Type;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Optional;

import static io.trino.spi.type.TimeZoneKey.UTC_KEY;
import static io.trino.spi.type.TimeZoneKey.getTimeZoneKeyForOffset;
import static io.trino.spi.type.TimestampType.createTimestampType;
import static io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MICROS;
import static org.assertj.core.api.Assertions.assertThat;

public class TestLongTimestampWithTimeZoneType
extends AbstractTestType
Expand Down Expand Up @@ -52,4 +60,104 @@ protected Object getGreaterValue(Object value)
// time zone doesn't matter for ordering
return LongTimestampWithTimeZone.fromEpochMillisAndFraction(((LongTimestampWithTimeZone) value).getEpochMillis() + 1, 0, getTimeZoneKeyForOffset(33));
}

@Override
public void testPreviousValue()
{
LongTimestampWithTimeZone minValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY);
LongTimestampWithTimeZone nextToMinValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MIN_VALUE, 1_000_000, UTC_KEY);
LongTimestampWithTimeZone previousToMaxValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MAX_VALUE, 998_000_000, UTC_KEY);
LongTimestampWithTimeZone maxValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MAX_VALUE, 999_000_000, UTC_KEY);

assertThat(type.getPreviousValue(minValue))
.isEqualTo(Optional.empty());
assertThat(type.getPreviousValue(nextToMinValue))
.isEqualTo(Optional.of(minValue));

assertThat(type.getPreviousValue(getSampleValue()))
.isEqualTo(Optional.of(LongTimestampWithTimeZone.fromEpochMillisAndFraction(1110, 999_000_000, getTimeZoneKeyForOffset(0))));

assertThat(type.getPreviousValue(previousToMaxValue))
.isEqualTo(Optional.of(LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MAX_VALUE, 997_000_000, UTC_KEY)));
assertThat(type.getPreviousValue(maxValue))
.isEqualTo(Optional.of(previousToMaxValue));
}

@Override
public void testNextValue()
{
LongTimestampWithTimeZone minValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY);
LongTimestampWithTimeZone nextToMinValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MIN_VALUE, 1_000_000, UTC_KEY);
LongTimestampWithTimeZone previousToMaxValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MAX_VALUE, 998_000_000, UTC_KEY);
LongTimestampWithTimeZone maxValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MAX_VALUE, 999_000_000, UTC_KEY);

assertThat(type.getNextValue(minValue))
.isEqualTo(Optional.of(nextToMinValue));
assertThat(type.getNextValue(nextToMinValue))
.isEqualTo(Optional.of(LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MIN_VALUE, 2_000_000, UTC_KEY)));

assertThat(type.getNextValue(getSampleValue()))
.isEqualTo(Optional.of(LongTimestampWithTimeZone.fromEpochMillisAndFraction(1111, 1_000_000, getTimeZoneKeyForOffset(0))));

assertThat(type.getNextValue(previousToMaxValue))
.isEqualTo(Optional.of(maxValue));
assertThat(type.getNextValue(maxValue))
.isEqualTo(Optional.empty());
}

@Test(dataProvider = "testPreviousNextValueEveryPrecisionDatProvider")
public void testPreviousValueEveryPrecision(int precision, long minValue, long maxValue, long step)
{
Type type = createTimestampType(precision);

assertThat(type.getPreviousValue(minValue))
.isEqualTo(Optional.empty());
assertThat(type.getPreviousValue(minValue + step))
.isEqualTo(Optional.of(minValue));

assertThat(type.getPreviousValue(0L))
.isEqualTo(Optional.of(-step));
assertThat(type.getPreviousValue(123_456_789_000_000L))
.isEqualTo(Optional.of(123_456_789_000_000L - step));

assertThat(type.getPreviousValue(maxValue - step))
.isEqualTo(Optional.of(maxValue - 2 * step));
assertThat(type.getPreviousValue(maxValue))
.isEqualTo(Optional.of(maxValue - step));
}

@Test(dataProvider = "testPreviousNextValueEveryPrecisionDatProvider")
public void testNextValueEveryPrecision(int precision, long minValue, long maxValue, long step)
{
Type type = createTimestampType(precision);

assertThat(type.getNextValue(minValue))
.isEqualTo(Optional.of(minValue + step));
assertThat(type.getNextValue(minValue + step))
.isEqualTo(Optional.of(minValue + 2 * step));

assertThat(type.getNextValue(0L))
.isEqualTo(Optional.of(step));
assertThat(type.getNextValue(123_456_789_000_000L))
.isEqualTo(Optional.of(123_456_789_000_000L + step));

assertThat(type.getNextValue(maxValue - step))
.isEqualTo(Optional.of(maxValue));
assertThat(type.getNextValue(maxValue))
.isEqualTo(Optional.empty());
}

@DataProvider
public Object[][] testPreviousNextValueEveryPrecisionDatProvider()
{
return new Object[][] {
{0, Long.MIN_VALUE + 775808, Long.MAX_VALUE - 775807, 1_000_000L},
{1, Long.MIN_VALUE + 75808, Long.MAX_VALUE - 75807, 100_000L},
{2, Long.MIN_VALUE + 5808, Long.MAX_VALUE - 5807, 10_000L},
{3, Long.MIN_VALUE + 808, Long.MAX_VALUE - 807, 1_000L},
{4, Long.MIN_VALUE + 8, Long.MAX_VALUE - 7, 100L},
{5, Long.MIN_VALUE + 8, Long.MAX_VALUE - 7, 10L},
{6, Long.MIN_VALUE, Long.MAX_VALUE, 1L},
};
}
}
Loading

0 comments on commit be73377

Please sign in to comment.