Search in sources :

Example 11 with CallExpression

use of io.trino.sql.relational.CallExpression in project trino by trinodb.

the class TestExpressionOptimizer method testCastWithJsonParseOptimization.

@Test
public void testCastWithJsonParseOptimization() {
    ResolvedFunction jsonParseFunction = functionResolution.resolveFunction(QualifiedName.of("json_parse"), fromTypes(VARCHAR));
    // constant
    ResolvedFunction jsonCastFunction = functionResolution.getCoercion(JSON, new ArrayType(INTEGER));
    RowExpression jsonCastExpression = new CallExpression(jsonCastFunction, ImmutableList.of(call(jsonParseFunction, constant(utf8Slice("[1, 2]"), VARCHAR))));
    RowExpression resultExpression = optimizer.optimize(jsonCastExpression);
    assertInstanceOf(resultExpression, ConstantExpression.class);
    Object resultValue = ((ConstantExpression) resultExpression).getValue();
    assertInstanceOf(resultValue, IntArrayBlock.class);
    assertEquals(toValues(INTEGER, (IntArrayBlock) resultValue), ImmutableList.of(1, 2));
    // varchar to array
    testCastWithJsonParseOptimization(jsonParseFunction, new ArrayType(VARCHAR), JSON_STRING_TO_ARRAY_NAME);
    // varchar to map
    testCastWithJsonParseOptimization(jsonParseFunction, mapType(INTEGER, VARCHAR), JSON_STRING_TO_MAP_NAME);
    // varchar to row
    testCastWithJsonParseOptimization(jsonParseFunction, RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), JSON_STRING_TO_ROW_NAME);
}
Also used : ArrayType(io.trino.spi.type.ArrayType) IntArrayBlock(io.trino.spi.block.IntArrayBlock) ResolvedFunction(io.trino.metadata.ResolvedFunction) ConstantExpression(io.trino.sql.relational.ConstantExpression) RowExpression(io.trino.sql.relational.RowExpression) CallExpression(io.trino.sql.relational.CallExpression) Test(org.testng.annotations.Test)

Example 12 with CallExpression

use of io.trino.sql.relational.CallExpression in project trino by trinodb.

the class TestExpressionOptimizer method testPossibleExponentialOptimizationTime.

@Test(timeOut = 10_000)
public void testPossibleExponentialOptimizationTime() {
    RowExpression expression = constant(1L, BIGINT);
    for (int i = 0; i < 100; i++) {
        expression = new CallExpression(functionResolution.resolveOperator(ADD, ImmutableList.of(BIGINT, BIGINT)), ImmutableList.of(expression, constant(1L, BIGINT)));
    }
    optimizer.optimize(expression);
}
Also used : RowExpression(io.trino.sql.relational.RowExpression) CallExpression(io.trino.sql.relational.CallExpression) Test(org.testng.annotations.Test)

Example 13 with CallExpression

use of io.trino.sql.relational.CallExpression in project trino by trinodb.

the class TestExpressionOptimizer method testIfConstantOptimization.

@Test
public void testIfConstantOptimization() {
    assertEquals(optimizer.optimize(ifExpression(constant(true, BOOLEAN), 1L, 2L)), constant(1L, BIGINT));
    assertEquals(optimizer.optimize(ifExpression(constant(false, BOOLEAN), 1L, 2L)), constant(2L, BIGINT));
    assertEquals(optimizer.optimize(ifExpression(constant(null, BOOLEAN), 1L, 2L)), constant(2L, BIGINT));
    RowExpression condition = new CallExpression(functionResolution.resolveOperator(EQUAL, ImmutableList.of(BIGINT, BIGINT)), ImmutableList.of(constant(3L, BIGINT), constant(3L, BIGINT)));
    assertEquals(optimizer.optimize(ifExpression(condition, 1L, 2L)), constant(1L, BIGINT));
}
Also used : RowExpression(io.trino.sql.relational.RowExpression) CallExpression(io.trino.sql.relational.CallExpression) Test(org.testng.annotations.Test)

Aggregations

CallExpression (io.trino.sql.relational.CallExpression)13 Test (org.testng.annotations.Test)10 RowExpression (io.trino.sql.relational.RowExpression)7 ResolvedFunction (io.trino.metadata.ResolvedFunction)6 DriverYieldSignal (io.trino.operator.DriverYieldSignal)4 PageProcessor (io.trino.operator.project.PageProcessor)4 Page (io.trino.spi.Page)4 ConstantExpression (io.trino.sql.relational.ConstantExpression)3 InputReferenceExpression (io.trino.sql.relational.InputReferenceExpression)3 Scope (io.airlift.bytecode.Scope)2 BlockAssertions.createLongDictionaryBlock (io.trino.block.BlockAssertions.createLongDictionaryBlock)2 DictionaryBlock (io.trino.spi.block.DictionaryBlock)2 ArrayType (io.trino.spi.type.ArrayType)2 LambdaDefinitionExpression (io.trino.sql.relational.LambdaDefinitionExpression)2 RowExpressionVisitor (io.trino.sql.relational.RowExpressionVisitor)2 SpecialForm (io.trino.sql.relational.SpecialForm)2 VariableReferenceExpression (io.trino.sql.relational.VariableReferenceExpression)2 ArrayList (java.util.ArrayList)2 ImmutableList (com.google.common.collect.ImmutableList)1 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)1