Refine search
@Test public void testBetween() { assertExpression("1 BETWEEN 2 AND 3", new BetweenPredicate(new LongLiteral("1"), new LongLiteral("2"), new LongLiteral("3"))); assertExpression("1 NOT BETWEEN 2 AND 3", new NotExpression(new BetweenPredicate(new LongLiteral("1"), new LongLiteral("2"), new LongLiteral("3")))); }
@Test public void testArraySubscript() { assertExpression("ARRAY [1, 2][1]", new SubscriptExpression( new ArrayConstructor(ImmutableList.of(new LongLiteral("1"), new LongLiteral("2"))), new LongLiteral("1"))); try { assertExpression("CASE WHEN TRUE THEN ARRAY[1,2] END[1]", null); fail(); } catch (RuntimeException e) { // Expected } }
@Test(timeOut = 10_000) public void testPossibleExponentialOptimizationTime() { Expression expression = new LongLiteral("1"); ImmutableMap.Builder<NodeRef<Expression>, Type> types = ImmutableMap.builder(); types.put(NodeRef.of(expression), BIGINT); for (int i = 0; i < 100; i++) { expression = new CoalesceExpression(expression, new LongLiteral("2")); types.put(NodeRef.of(expression), BIGINT); } translateAndOptimize(expression, types.build()); }
@Test public void testSetSessionWithValidation() { testSetSessionWithValidation(new LongLiteral("0"), "0"); testSetSessionWithValidation(new LongLiteral("2"), "2"); try { testSetSessionWithValidation(new LongLiteral("-1"), "-1"); fail(); } catch (PrestoException e) { assertEquals(e.getMessage(), MUST_BE_POSITIVE); } }
new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3"))); new LongLiteral("1"), new LogicalBinaryExpression(LogicalBinaryExpression.Operator.AND, new LongLiteral("2"), new LongLiteral("3")))); new NotExpression(new LongLiteral("1")), new LongLiteral("2"))); new NotExpression(new LongLiteral("1")), new LongLiteral("2"))); negative(new LongLiteral("1")), new LongLiteral("2"))); new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3"))); new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3"))); new LongLiteral("1"), new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new LongLiteral("2"),
@Test public void testCoalesce() { assertInvalidExpression("coalesce()", "The 'coalesce' function must have at least two arguments"); assertInvalidExpression("coalesce(5)", "The 'coalesce' function must have at least two arguments"); assertInvalidExpression("coalesce(1, 2) filter (where true)", "FILTER not valid for 'coalesce' function"); assertInvalidExpression("coalesce(1, 2) OVER ()", "OVER clause not valid for 'coalesce' function"); assertExpression("coalesce(13, 42)", new CoalesceExpression(new LongLiteral("13"), new LongLiteral("42"))); assertExpression("coalesce(6, 7, 8)", new CoalesceExpression(new LongLiteral("6"), new LongLiteral("7"), new LongLiteral("8"))); assertExpression("coalesce(13, null)", new CoalesceExpression(new LongLiteral("13"), new NullLiteral())); assertExpression("coalesce(null, 13)", new CoalesceExpression(new NullLiteral(), new LongLiteral("13"))); assertExpression("coalesce(null, null)", new CoalesceExpression(new NullLiteral(), new NullLiteral())); }
@Test public void testCall() { assertStatement("CALL foo()", new Call(QualifiedName.of("foo"), ImmutableList.of())); assertStatement("CALL foo(123, a => 1, b => 'go', 456)", new Call(QualifiedName.of("foo"), ImmutableList.of( new CallArgument(new LongLiteral("123")), new CallArgument("a", new LongLiteral("1")), new CallArgument("b", new StringLiteral("go")), new CallArgument(new LongLiteral("456"))))); }
@Test public void testApplyExpressionNotRewritten() { tester().assertThat(applyRewriter.applyExpressionRewrite()) .on(p -> p.apply( Assignments.of( p.symbol("a", BigintType.BIGINT), new InPredicate( new LongLiteral("0"), new InListExpression(ImmutableList.of( new LongLiteral("1"), new LongLiteral("2"))))), ImmutableList.of(), p.values(), p.values())) .doesNotFire(); } }
@Test public void testNormalize() { assertNormalize(new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1"))); assertNormalize(new IsNullPredicate(name("a"))); assertNormalize(new NotExpression(new LikePredicate(name("a"), new StringLiteral("x%"), Optional.empty()))); assertNormalize( new NotExpression(new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1"))), new ComparisonExpression(NOT_EQUAL, name("a"), new LongLiteral("1"))); assertNormalize( new NotExpression(new ComparisonExpression(NOT_EQUAL, name("a"), new LongLiteral("1"))), new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1"))); // Cannot normalize IS DISTINCT FROM yet assertNormalize(new NotExpression(new ComparisonExpression(IS_DISTINCT_FROM, name("a"), new LongLiteral("1")))); }
@Test public void testNullIf() { assertExpression("nullif(42, 87)", new NullIfExpression(new LongLiteral("42"), new LongLiteral("87"))); assertExpression("nullif(42, null)", new NullIfExpression(new LongLiteral("42"), new NullLiteral())); assertExpression("nullif(null, null)", new NullIfExpression(new NullLiteral(), new NullLiteral())); assertInvalidExpression("nullif(1)", "Invalid number of arguments for 'nullif' function"); assertInvalidExpression("nullif(1, 2, 3)", "Invalid number of arguments for 'nullif' function"); assertInvalidExpression("nullif(42, 87) filter (where true)", "FILTER not valid for 'nullif' function"); assertInvalidExpression("nullif(42, 87) OVER ()", "OVER clause not valid for 'nullif' function"); }
@Test public void testApplyExpressionRewrite() { tester().assertThat(applyRewriter.applyExpressionRewrite()) .on(p -> p.apply( Assignments.of( p.symbol("a", BigintType.BIGINT), new InPredicate( new LongLiteral("1"), new InListExpression(ImmutableList.of( new LongLiteral("1"), new LongLiteral("2"))))), ImmutableList.of(), p.values(), p.values())) .matches( apply( ImmutableList.of(), ImmutableMap.of("a", expression("0 IN (1, 2)")), values(), values())); }
@Test public void testSubstringBuiltInFunction() Optional.empty(), new QuerySpecification( selectList(new FunctionCall(QualifiedName.of("substr"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2")))), Optional.empty(), Optional.empty(), Optional.empty(), new QuerySpecification( selectList(new FunctionCall(QualifiedName.of("substr"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2"), new LongLiteral("3")))), Optional.empty(), Optional.empty(),
@Test public void testSubstringRegisteredFunction() Optional.empty(), new QuerySpecification( selectList(new FunctionCall(QualifiedName.of("substring"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2")))), Optional.empty(), Optional.empty(), Optional.empty(), new QuerySpecification( selectList(new FunctionCall(QualifiedName.of("substring"), Lists.newArrayList(new StringLiteral(givenString), new LongLiteral("2"), new LongLiteral("3")))), Optional.empty(), Optional.empty(),
@Test public void testValueExpressionRewrite() { tester().assertThat(zeroRewriter.valuesExpressionRewrite()) .on(p -> p.values( ImmutableList.<Symbol>of(p.symbol("a")), ImmutableList.of((ImmutableList.of(PlanBuilder.expression("1")))))) .matches( values(ImmutableList.of("a"), ImmutableList.of(ImmutableList.of(new LongLiteral("0"))))); }
@Test public void testAggregationFilter() { assertStatement("SELECT SUM(x) FILTER (WHERE x > 4)", new Query( Optional.empty(), new QuerySpecification( selectList( new FunctionCall( QualifiedName.of("SUM"), Optional.empty(), Optional.of(new ComparisonExpression( ComparisonExpression.Operator.GREATER_THAN, new Identifier("x"), new LongLiteral("4"))), Optional.empty(), false, ImmutableList.of(new Identifier("x")))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty())); }
@Test public void testIf() { assertExpression("if(true, 1, 0)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("1"), new LongLiteral("0"))); assertExpression("if(true, 3, null)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("3"), new NullLiteral())); assertExpression("if(false, null, 4)", new IfExpression(new BooleanLiteral("false"), new NullLiteral(), new LongLiteral("4"))); assertExpression("if(false, null, null)", new IfExpression(new BooleanLiteral("false"), new NullLiteral(), new NullLiteral())); assertExpression("if(true, 3)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("3"), null)); assertInvalidExpression("IF(true)", "Invalid number of arguments for 'if' function"); assertInvalidExpression("IF(true, 1, 0) FILTER (WHERE true)", "FILTER not valid for 'if' function"); assertInvalidExpression("IF(true, 1, 0) OVER()", "OVER clause not valid for 'if' function"); }
@Test public void testLimitAll() { Query valuesQuery = query(values( row(new LongLiteral("1"), new StringLiteral("1")), row(new LongLiteral("2"), new StringLiteral("2")))); assertStatement("SELECT * FROM (VALUES (1, '1'), (2, '2')) LIMIT ALL", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of("ALL"))); }
@Test public void testArrayConstructor() { assertExpression("ARRAY []", new ArrayConstructor(ImmutableList.of())); assertExpression("ARRAY [1, 2]", new ArrayConstructor(ImmutableList.of(new LongLiteral("1"), new LongLiteral("2")))); assertExpression("ARRAY [1e0, 2.5e0]", new ArrayConstructor(ImmutableList.of(new DoubleLiteral("1.0"), new DoubleLiteral("2.5")))); assertExpression("ARRAY ['hi']", new ArrayConstructor(ImmutableList.of(new StringLiteral("hi")))); assertExpression("ARRAY ['hi', 'hello']", new ArrayConstructor(ImmutableList.of(new StringLiteral("hi"), new StringLiteral("hello")))); }
@Test public void testShowStatsForQuery() { final String[] tableNames = {"t", "s.t", "c.s.t"}; for (String fullName : tableNames) { QualifiedName qualifiedName = QualifiedName.of(Arrays.asList(fullName.split("\\."))); assertStatement(format("SHOW STATS FOR (SELECT * FROM %s)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.empty())); assertStatement(format("SHOW STATS FOR (SELECT * FROM %s WHERE field > 0)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.of( new ComparisonExpression(GREATER_THAN, new Identifier("field"), new LongLiteral("0"))))); assertStatement(format("SHOW STATS FOR (SELECT * FROM %s WHERE field > 0 or field < 0)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.of( new LogicalBinaryExpression(LogicalBinaryExpression.Operator.OR, new ComparisonExpression(GREATER_THAN, new Identifier("field"), new LongLiteral("0")), new ComparisonExpression(LESS_THAN, new Identifier("field"), new LongLiteral("0")))))); } }
@Test public void testExecuteWithUsing() { assertStatement("EXECUTE myquery USING 1, 'abc', ARRAY ['hello']", new Execute(identifier("myquery"), ImmutableList.of(new LongLiteral("1"), new StringLiteral("abc"), new ArrayConstructor(ImmutableList.of(new StringLiteral("hello")))))); }