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
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected void testUnsupportedExtract(String extractField)
{
types().forEach(type -> {
String expression = format("EXTRACT(%s FROM CAST(NULL AS %s))", extractField, type);
assertThatThrownBy(() -> assertions.expression(expression), expression)
assertThatThrownBy(() -> assertions.expression(expression).evaluate(), expression)
.as(expression)
.isInstanceOf(TrinoException.class)
.hasMessageMatching(format("line 1:\\d+:\\Q Cannot extract %s from %s", extractField, type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,19 @@ public final void destroyTestFunctions()
functionAssertions = null;
}

/**
* @deprecated Use {@link io.trino.sql.query.QueryAssertions#function(String, String...)}
*/
@Deprecated
protected void assertFunction(@Language("SQL") String projection, Type expectedType, Object expected)
{
functionAssertions.assertFunction(projection, expectedType, expected);
}

/**
* @deprecated Use {@link io.trino.sql.query.QueryAssertions#operator(OperatorType, String...)}
*/
@Deprecated
protected void assertOperator(OperatorType operator, String value, Type expectedType, Object expected)
{
functionAssertions.assertFunction(format("\"%s\"(%s)", mangleOperatorName(operator), value), expectedType, expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ public void installPlugin(Plugin plugin)
runner.installPlugin(plugin);
}

/**
* @deprecated Use {@link io.trino.sql.query.QueryAssertions#function(String, String...)}
*/
@Deprecated
public void assertFunction(String projection, Type expectedType, Object expected)
{
if (expected instanceof Slice) {
Expand All @@ -301,17 +305,29 @@ public void assertFunction(String projection, Type expectedType, Object expected
assertEquals(actual, expected);
}

/**
* @deprecated Use {@link io.trino.sql.query.QueryAssertions#function(String, String...)}
*/
@Deprecated
public void assertFunctionString(String projection, Type expectedType, String expected)
{
Object actual = selectSingleValue(projection, expectedType, runner.getExpressionCompiler());
assertEquals(actual.toString(), expected);
}

/**
* @deprecated Use {@link io.trino.sql.query.QueryAssertions#expression(String)}
*/
@Deprecated
public void tryEvaluate(String expression, Type expectedType)
{
tryEvaluate(expression, expectedType, session);
}

/**
* @deprecated Use {@link io.trino.sql.query.QueryAssertions#expression(String)}
*/
@Deprecated
public void tryEvaluate(String expression, Type expectedType, Session session)
{
selectUniqueValue(expression, expectedType, session, runner.getExpressionCompiler());
Expand All @@ -322,6 +338,10 @@ public void tryEvaluateWithAll(String expression, Type expectedType)
tryEvaluateWithAll(expression, expectedType, session);
}

/**
* @deprecated Use {@link io.trino.sql.query.QueryAssertions#expression(String)}
*/
@Deprecated
public void tryEvaluateWithAll(String expression, Type expectedType, Session session)
{
executeProjectionWithAll(expression, expectedType, session, runner.getExpressionCompiler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,72 @@
*/
package io.trino.operator.scalar;

import org.testng.annotations.Test;
import io.trino.sql.query.QueryAssertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import static io.trino.spi.type.BooleanType.BOOLEAN;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;

@TestInstance(PER_CLASS)
public class TestArrayContainsSequence
extends AbstractTestFunctions
{
private QueryAssertions assertions;

@BeforeAll
public void init()
{
assertions = new QueryAssertions();
}

@AfterAll
public void teardown()
{
assertions.close();
assertions = null;
}

@Test
public void testBasic()
{
assertFunction("contains_sequence(ARRAY [1,2,3,4,5,6], ARRAY[1,2])", BOOLEAN, true);
assertFunction("contains_sequence(ARRAY [1,2,3,4,5,6], ARRAY[3,4])", BOOLEAN, true);
assertFunction("contains_sequence(ARRAY [1,2,3,4,5,6], ARRAY[5,6])", BOOLEAN, true);
assertFunction("contains_sequence(ARRAY [1,2,3,4,5,6], ARRAY[1,2,4])", BOOLEAN, false);
assertFunction("contains_sequence(ARRAY [1,2,3,NULL,4,5,6], ARRAY[3,NULL,4])", BOOLEAN, true);
assertFunction("contains_sequence(ARRAY [1,2,3,4,5,6], ARRAY[1,2,3,4,5,6])", BOOLEAN, true);
assertFunction("contains_sequence(ARRAY [1,2,3,4,5,6], ARRAY[])", BOOLEAN, true);
assertFunction("contains_sequence(ARRAY ['1','2','3'], ARRAY['1','2'])", BOOLEAN, true);
assertFunction("contains_sequence(ARRAY [1.1,2.2,3.3], ARRAY[1.1,2.2])", BOOLEAN, true);
assertFunction("contains_sequence(ARRAY [ARRAY[1,2],ARRAY[3],ARRAY[4,5]], ARRAY[ARRAY[1,2],ARRAY[3]])", BOOLEAN, true);
assertFunction("contains_sequence(ARRAY [ARRAY[1,2],ARRAY[3],ARRAY[4,5]], ARRAY[ARRAY[1,2],ARRAY[4]])", BOOLEAN, false);
assertThat(assertions.function("contains_sequence", "ARRAY[1, 2, 3, 4, 5, 6]", "ARRAY[1, 2]"))
.isEqualTo(true);

assertThat(assertions.function("contains_sequence", "ARRAY[1, 2, 3, 4, 5, 6]", "ARRAY[3, 4]"))
.isEqualTo(true);

assertThat(assertions.function("contains_sequence", "ARRAY[1, 2, 3, 4, 5, 6]", "ARRAY[5, 6]"))
.isEqualTo(true);

assertThat(assertions.function("contains_sequence", "ARRAY[1, 2, 3, 4, 5, 6]", "ARRAY[1, 2, 4]"))
.isEqualTo(false);

assertThat(assertions.function("contains_sequence", "ARRAY[1, 2, 3, NULL, 4, 5, 6]", "ARRAY[3, NULL, 4]"))
.isEqualTo(true);

assertThat(assertions.function("contains_sequence", "ARRAY[1, 2, 3, 4, 5, 6]", "ARRAY[1, 2, 3, 4, 5, 6]"))
.isEqualTo(true);

assertThat(assertions.function("contains_sequence", "ARRAY[1, 2, 3, 4, 5, 6]", "ARRAY[]"))
.isEqualTo(true);

assertThat(assertions.function("contains_sequence", "ARRAY['1', '2', '3']", "ARRAY['1', '2']"))
.isEqualTo(true);

assertThat(assertions.function("contains_sequence", "ARRAY[1.1, 2.2, 3.3]", "ARRAY[1.1, 2.2]"))
.isEqualTo(true);

assertThat(assertions.function("contains_sequence", "ARRAY[ARRAY[1,2], ARRAY[3], ARRAY[4,5]]", "ARRAY[ARRAY[1,2], ARRAY[3]]"))
.isEqualTo(true);

assertThat(assertions.function("contains_sequence", "ARRAY[ARRAY[1,2], ARRAY[3], ARRAY[4,5]]", "ARRAY[ARRAY[1,2], ARRAY[4]]"))
.isEqualTo(false);

for (int i = 1; i <= 6; i++) {
assertFunction("contains_sequence(ARRAY [1,2,3,4,5,6], ARRAY[" + i + "])", BOOLEAN, true);
assertThat(assertions.function("contains_sequence", "ARRAY[1, 2, 3, 4, 5, 6]", "ARRAY[%d]".formatted(i)))
.isEqualTo(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,112 @@
*/
package io.trino.operator.scalar;

import com.google.common.collect.ImmutableList;
import io.trino.spi.type.ArrayType;
import org.testng.annotations.Test;
import io.trino.sql.query.QueryAssertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.spi.type.DoubleType.DOUBLE;
import static io.trino.spi.type.IntegerType.INTEGER;
import static io.trino.spi.type.VarcharType.VARCHAR;
import static io.trino.type.UnknownType.UNKNOWN;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;

@TestInstance(PER_CLASS)
public class TestArrayExceptFunction
extends AbstractTestFunctions
{
private QueryAssertions assertions;

@BeforeAll
public void init()
{
assertions = new QueryAssertions();
}

@AfterAll
public void teardown()
{
assertions.close();
assertions = null;
}

@Test
public void testBasic()
{
assertFunction("array_except(ARRAY[1, 5, 3], ARRAY[3])", new ArrayType(INTEGER), ImmutableList.of(1, 5));
assertFunction("array_except(ARRAY[CAST(1 as BIGINT), 5, 3], ARRAY[5])", new ArrayType(BIGINT), ImmutableList.of(1L, 3L));
assertFunction("array_except(ARRAY[VARCHAR 'x', 'y', 'z'], ARRAY['x'])", new ArrayType(VARCHAR), ImmutableList.of("y", "z"));
assertFunction("array_except(ARRAY[true, false, null], ARRAY[true])", new ArrayType(BOOLEAN), asList(false, null));
assertFunction("array_except(ARRAY[1.1E0, 5.4E0, 3.9E0], ARRAY[5, 5.4E0])", new ArrayType(DOUBLE), ImmutableList.of(1.1, 3.9));
assertThat(assertions.function("array_except", "ARRAY[1, 5, 3]", "ARRAY[3]"))
.matches("ARRAY[1, 5]");

assertThat(assertions.function("array_except", "ARRAY[BIGINT '1', 5, 3]", "ARRAY[5]"))
.matches("ARRAY[BIGINT '1', BIGINT '3']");

assertThat(assertions.function("array_except", "ARRAY[VARCHAR 'x', 'y', 'z']", "ARRAY['x']"))
.matches("ARRAY[VARCHAR 'y', VARCHAR 'z']");

assertThat(assertions.function("array_except", "ARRAY[true, false, null]", "ARRAY[true]"))
.matches("ARRAY[false, null]");

assertThat(assertions.function("array_except", "ARRAY[1.1E0, 5.4E0, 3.9E0]", "ARRAY[5, 5.4E0]"))
.matches("ARRAY[1.1E0, 3.9E0]");
}

@Test
public void testEmpty()
{
assertFunction("array_except(ARRAY[], ARRAY[])", new ArrayType(UNKNOWN), ImmutableList.of());
assertFunction("array_except(ARRAY[], ARRAY[1, 3])", new ArrayType(INTEGER), ImmutableList.of());
assertFunction("array_except(ARRAY[VARCHAR 'abc'], ARRAY[])", new ArrayType(VARCHAR), ImmutableList.of("abc"));
assertThat(assertions.function("array_except", "ARRAY[]", "ARRAY[]"))
.matches("ARRAY[]");

assertThat(assertions.function("array_except", "ARRAY[]", "ARRAY[1, 3]"))
.matches("CAST(ARRAY[] AS array(integer))");

assertThat(assertions.function("array_except", "ARRAY[VARCHAR 'abc']", "ARRAY[]"))
.matches("ARRAY[VARCHAR 'abc']");
}

@Test
public void testNull()
{
assertFunction("array_except(ARRAY[NULL], NULL)", new ArrayType(UNKNOWN), null);
assertFunction("array_except(NULL, NULL)", new ArrayType(UNKNOWN), null);
assertFunction("array_except(NULL, ARRAY[NULL])", new ArrayType(UNKNOWN), null);
assertFunction("array_except(ARRAY[NULL], ARRAY[NULL])", new ArrayType(UNKNOWN), ImmutableList.of());
assertFunction("array_except(ARRAY[], ARRAY[NULL])", new ArrayType(UNKNOWN), ImmutableList.of());
assertFunction("array_except(ARRAY[NULL], ARRAY[])", new ArrayType(UNKNOWN), singletonList(null));
assertThat(assertions.function("array_except", "ARRAY[NULL]", "NULL"))
.isNull(new ArrayType(UNKNOWN));

assertThat(assertions.function("array_except", "NULL", "NULL"))
.isNull(new ArrayType(UNKNOWN));

assertThat(assertions.function("array_except", "NULL", "ARRAY[NULL]"))
.isNull(new ArrayType(UNKNOWN));

assertThat(assertions.function("array_except", "ARRAY[NULL]", "ARRAY[NULL]"))
.matches("ARRAY[]");

assertThat(assertions.function("array_except", "ARRAY[]", "ARRAY[NULL]"))
.matches("ARRAY[]");

assertThat(assertions.function("array_except", "ARRAY[NULL]", "ARRAY[]"))
.matches("ARRAY[NULL]");
}

@Test
public void testDuplicates()
{
assertFunction("array_except(ARRAY[1, 5, 3, 5, 1], ARRAY[3])", new ArrayType(INTEGER), ImmutableList.of(1, 5));
assertFunction("array_except(ARRAY[CAST(1 as BIGINT), 5, 5, 3, 3, 3, 1], ARRAY[3, 5])", new ArrayType(BIGINT), ImmutableList.of(1L));
assertFunction("array_except(ARRAY[VARCHAR 'x', 'x', 'y', 'z'], ARRAY['x', 'y', 'x'])", new ArrayType(VARCHAR), ImmutableList.of("z"));
assertFunction("array_except(ARRAY[true, false, null, true, false, null], ARRAY[true, true, true])", new ArrayType(BOOLEAN), asList(false, null));
assertThat(assertions.function("array_except", "ARRAY[1, 5, 3, 5, 1]", "ARRAY[3]"))
.matches("ARRAY[1, 5]");

assertThat(assertions.function("array_except", "ARRAY[BIGINT '1', 5, 5, 3, 3, 3, 1]", "ARRAY[3, 5]"))
.matches("ARRAY[BIGINT '1']");

assertThat(assertions.function("array_except", "ARRAY[VARCHAR 'x', 'x', 'y', 'z']", "ARRAY['x', 'y', 'x']"))
.matches("ARRAY[VARCHAR 'z']");

assertThat(assertions.function("array_except", "ARRAY[true, false, null, true, false, null]", "ARRAY[true, true, true]"))
.matches("ARRAY[false, null]");
}

@Test
public void testNonDistinctNonEqualValues()
{
assertFunction("array_except(ARRAY[NaN()], ARRAY[NaN()])", new ArrayType(DOUBLE), ImmutableList.of());
assertFunction("array_except(ARRAY[1, NaN(), 3], ARRAY[NaN(), 3])", new ArrayType(DOUBLE), ImmutableList.of(1.0));
assertThat(assertions.function("array_except", "ARRAY[NaN()]", "ARRAY[NaN()]"))
.matches("CAST(ARRAY[] AS array(double))");

assertThat(assertions.function("array_except", "ARRAY[1, NaN(), 3]", "ARRAY[NaN(), 3]"))
.matches("ARRAY[1E0]");
}
}
Loading