Search in sources :

Example 21 with RowExpression

use of io.trino.sql.relational.RowExpression 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 22 with RowExpression

use of io.trino.sql.relational.RowExpression 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)

Example 23 with RowExpression

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

the class FunctionAssertions method assertCachedInstanceHasBoundedRetainedSizeInTx.

private void assertCachedInstanceHasBoundedRetainedSizeInTx(String projection, Session session) {
    requireNonNull(projection, "projection is null");
    Expression projectionExpression = createExpression(session, projection, getPlannerContext(), INPUT_TYPES);
    RowExpression projectionRowExpression = toRowExpression(session, projectionExpression);
    PageProcessor processor = runner.getExpressionCompiler().compilePageProcessor(Optional.empty(), ImmutableList.of(projectionRowExpression)).get();
    // This is a heuristic to detect whether the retained size of cachedInstance is bounded.
    // * The test runs at least 1000 iterations.
    // * The test passes if max retained size doesn't refresh after
    // 4x the number of iterations when max was last updated.
    // * The test fails if retained size reaches 1MB.
    // Note that 1MB is arbitrarily chosen and may be increased if a function implementation
    // legitimately needs more.
    long maxRetainedSize = 0;
    int maxIterationCount = 0;
    for (int iterationCount = 0; iterationCount < Math.max(1000, maxIterationCount * 4); iterationCount++) {
        Iterator<Optional<Page>> output = processor.process(session.toConnectorSession(), new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), SOURCE_PAGE);
        // consume the iterator
        @SuppressWarnings("unused") Optional<Page> ignored = Iterators.getOnlyElement(output);
        long retainedSize = processor.getProjections().stream().mapToLong(this::getRetainedSizeOfCachedInstance).sum();
        if (retainedSize > maxRetainedSize) {
            maxRetainedSize = retainedSize;
            maxIterationCount = iterationCount;
        }
        if (maxRetainedSize >= 1048576) {
            fail(format("The retained size of cached instance of function invocation is likely unbounded: %s", projection));
        }
    }
}
Also used : PageProcessor(io.trino.operator.project.PageProcessor) Optional(java.util.Optional) ExpressionTestUtils.createExpression(io.trino.sql.ExpressionTestUtils.createExpression) Expression(io.trino.sql.tree.Expression) RowExpression(io.trino.sql.relational.RowExpression) RowExpression(io.trino.sql.relational.RowExpression) DriverYieldSignal(io.trino.operator.DriverYieldSignal) Page(io.trino.spi.Page)

Example 24 with RowExpression

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

the class FunctionAssertions method executeProjectionWithAllInTx.

private List<Object> executeProjectionWithAllInTx(String projection, Type expectedType, Session session, ExpressionCompiler compiler) {
    requireNonNull(projection, "projection is null");
    Expression projectionExpression = createExpression(session, projection, getPlannerContext(), INPUT_TYPES);
    RowExpression projectionRowExpression = toRowExpression(session, projectionExpression);
    List<Object> results = new ArrayList<>();
    // If the projection does not need bound values, execute query using full engine
    if (!needsBoundValue(projectionExpression)) {
        MaterializedResult result = runner.execute("SELECT " + projection);
        assertType(result.getTypes(), expectedType);
        assertEquals(result.getTypes().size(), 1);
        assertEquals(result.getMaterializedRows().size(), 1);
        Object queryResult = Iterables.getOnlyElement(result.getMaterializedRows()).getField(0);
        results.add(queryResult);
    }
    // execute as standalone operator
    OperatorFactory operatorFactory = compileFilterProject(Optional.empty(), projectionRowExpression, compiler);
    Object directOperatorValue = selectSingleValue(operatorFactory, expectedType, session);
    results.add(directOperatorValue);
    // interpret
    Object interpretedValue = interpret(projectionExpression, expectedType, session);
    results.add(interpretedValue);
    // execute over normal operator
    SourceOperatorFactory scanProjectOperatorFactory = compileScanFilterProject(Optional.empty(), projectionRowExpression, compiler);
    Object scanOperatorValue = selectSingleValue(scanProjectOperatorFactory, expectedType, createNormalSplit(), session);
    results.add(scanOperatorValue);
    // execute over record set
    Object recordValue = selectSingleValue(scanProjectOperatorFactory, expectedType, createRecordSetSplit(), session);
    results.add(recordValue);
    // If the projection does not need bound values, execute query using full engine
    if (!needsBoundValue(projectionExpression)) {
        MaterializedResult result = runner.execute("SELECT " + projection);
        assertType(result.getTypes(), expectedType);
        assertEquals(result.getTypes().size(), 1);
        assertEquals(result.getMaterializedRows().size(), 1);
        Object queryResult = Iterables.getOnlyElement(result.getMaterializedRows()).getField(0);
        results.add(queryResult);
    }
    // validate type at end since some tests expect failure and for those UNKNOWN is used instead of actual type
    assertEquals(projectionRowExpression.getType(), expectedType);
    return results;
}
Also used : ExpressionTestUtils.createExpression(io.trino.sql.ExpressionTestUtils.createExpression) Expression(io.trino.sql.tree.Expression) RowExpression(io.trino.sql.relational.RowExpression) SourceOperatorFactory(io.trino.operator.SourceOperatorFactory) OperatorFactory(io.trino.operator.OperatorFactory) ArrayList(java.util.ArrayList) RowExpression(io.trino.sql.relational.RowExpression) MaterializedResult(io.trino.testing.MaterializedResult) SourceOperatorFactory(io.trino.operator.SourceOperatorFactory)

Example 25 with RowExpression

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

the class BenchmarkEqualsConjunctsOperator method setup.

@Setup
public void setup() {
    ExpressionCompiler expressionCompiler = functionResolution.getExpressionCompiler();
    RowExpression projection = generateComplexComparisonProjection(FIELDS_COUNT, COMPARISONS_COUNT);
    compiledProcessor = expressionCompiler.compilePageProcessor(Optional.empty(), ImmutableList.of(projection)).get();
}
Also used : RowExpression(io.trino.sql.relational.RowExpression) ExpressionCompiler(io.trino.sql.gen.ExpressionCompiler) Setup(org.openjdk.jmh.annotations.Setup)

Aggregations

RowExpression (io.trino.sql.relational.RowExpression)34 Test (org.testng.annotations.Test)14 PageProcessor (io.trino.operator.project.PageProcessor)13 Page (io.trino.spi.Page)9 PlanNodeId (io.trino.sql.planner.plan.PlanNodeId)9 CursorProcessor (io.trino.operator.project.CursorProcessor)7 CallExpression (io.trino.sql.relational.CallExpression)7 MaterializedResult (io.trino.testing.MaterializedResult)7 ImmutableList (com.google.common.collect.ImmutableList)6 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)6 Variable (io.airlift.bytecode.Variable)6 CatalogName (io.trino.connector.CatalogName)6 Split (io.trino.metadata.Split)6 ExpressionCompiler (io.trino.sql.gen.ExpressionCompiler)6 TestingSplit (io.trino.testing.TestingSplit)6 IfStatement (io.airlift.bytecode.control.IfStatement)5 Type (io.trino.spi.type.Type)5 Expression (io.trino.sql.tree.Expression)5 LabelNode (io.airlift.bytecode.instruction.LabelNode)4 FunctionManager (io.trino.metadata.FunctionManager)4