Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 39 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 All @@ -24,6 +25,7 @@
import io.trino.spi.block.TestingBlockEncodingSerde;
import io.trino.spi.type.ArrayType;
import io.trino.spi.type.LongTimestamp;
import io.trino.spi.type.LongTimestampWithTimeZone;
import io.trino.spi.type.MapType;
import io.trino.spi.type.RowType;
import io.trino.spi.type.Type;
Expand Down Expand Up @@ -58,6 +60,7 @@
import static io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL;
import static io.trino.spi.function.InvocationConvention.InvocationReturnConvention.NULLABLE_RETURN;
import static io.trino.spi.function.InvocationConvention.simpleConvention;
import static io.trino.spi.type.TimeZoneKey.UTC_KEY;
import static io.trino.spi.type.TypeUtils.readNativeValue;
import static io.trino.sql.ExpressionUtils.isEffectivelyLiteral;
import static io.trino.testing.TestingConnectorSession.SESSION;
Expand Down Expand Up @@ -195,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 Expand Up @@ -485,6 +521,9 @@ private static Object getNonNullValueForType(Type type)
if (type.getJavaType() == LongTimestamp.class) {
return new LongTimestamp(1, 0);
}
if (type.getJavaType() == LongTimestampWithTimeZone.class) {
return LongTimestampWithTimeZone.fromEpochSecondsAndFraction(1, 0, UTC_KEY);
}
if (type instanceof ArrayType) {
ArrayType arrayType = (ArrayType) type;
Type elementType = arrayType.getElementType();
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());
}
}
Loading