Search in sources :

Example 16 with RowExpression

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

the class TestScanFilterAndProjectOperator method testRecordCursorYield.

@Test
public void testRecordCursorYield() {
    // create a generic long function that yields for projection on every row
    // verify we will yield #row times totally
    // create a table with 15 rows
    int length = 15;
    Page input = SequencePageBuilder.createSequencePage(ImmutableList.of(BIGINT), length, 0);
    DriverContext driverContext = newDriverContext();
    // set up generic long function with a callback to force yield
    functionAssertions.addFunctions(new InternalFunctionBundle(new GenericLongFunction("record_cursor", value -> {
        driverContext.getYieldSignal().forceYieldForTesting();
        return value;
    })));
    FunctionManager functionManager = functionAssertions.getFunctionManager();
    ExpressionCompiler expressionCompiler = new ExpressionCompiler(functionManager, new PageFunctionCompiler(functionManager, 0));
    List<RowExpression> projections = ImmutableList.of(call(functionAssertions.getMetadata().resolveFunction(session, QualifiedName.of("generic_long_record_cursor"), fromTypes(BIGINT)), field(0, BIGINT)));
    Supplier<CursorProcessor> cursorProcessor = expressionCompiler.compileCursorProcessor(Optional.empty(), projections, "key");
    Supplier<PageProcessor> pageProcessor = expressionCompiler.compilePageProcessor(Optional.empty(), projections);
    ScanFilterAndProjectOperator.ScanFilterAndProjectOperatorFactory factory = new ScanFilterAndProjectOperator.ScanFilterAndProjectOperatorFactory(0, new PlanNodeId("test"), new PlanNodeId("0"), (session, split, table, columns, dynamicFilter) -> new RecordPageSource(new PageRecordSet(ImmutableList.of(BIGINT), input)), cursorProcessor, pageProcessor, TEST_TABLE_HANDLE, ImmutableList.of(), DynamicFilter.EMPTY, ImmutableList.of(BIGINT), DataSize.ofBytes(0), 0);
    SourceOperator operator = factory.createOperator(driverContext);
    operator.addSplit(new Split(new CatalogName("test"), TestingSplit.createLocalSplit(), Lifespan.taskWide()));
    operator.noMoreSplits();
    // start driver; get null value due to yield for the first 15 times
    for (int i = 0; i < length; i++) {
        driverContext.getYieldSignal().setWithDelay(SECONDS.toNanos(1000), driverContext.getYieldExecutor());
        assertNull(operator.getOutput());
        driverContext.getYieldSignal().reset();
    }
    // the 16th yield is not going to prevent the operator from producing a page
    driverContext.getYieldSignal().setWithDelay(SECONDS.toNanos(1000), driverContext.getYieldExecutor());
    Page output = operator.getOutput();
    driverContext.getYieldSignal().reset();
    assertNotNull(output);
    assertEquals(toValues(BIGINT, output.getBlock(0)), toValues(BIGINT, input.getBlock(0)));
}
Also used : PageFunctionCompiler(io.trino.sql.gen.PageFunctionCompiler) CursorProcessor(io.trino.operator.project.CursorProcessor) RowExpression(io.trino.sql.relational.RowExpression) Page(io.trino.spi.Page) PageRecordSet(io.trino.operator.index.PageRecordSet) RecordPageSource(io.trino.spi.connector.RecordPageSource) PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) PageProcessor(io.trino.operator.project.PageProcessor) InternalFunctionBundle(io.trino.metadata.InternalFunctionBundle) ExpressionCompiler(io.trino.sql.gen.ExpressionCompiler) CatalogName(io.trino.connector.CatalogName) Split(io.trino.metadata.Split) TestingSplit(io.trino.testing.TestingSplit) FunctionManager(io.trino.metadata.FunctionManager) Test(org.testng.annotations.Test)

Example 17 with RowExpression

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

the class PageFieldsToInputParametersRewriter method rewritePageFieldsToInputParameters.

public static Result rewritePageFieldsToInputParameters(RowExpression expression) {
    Visitor visitor = new Visitor();
    RowExpression rewrittenProjection = expression.accept(visitor, true);
    InputChannels inputChannels = new InputChannels(visitor.getInputChannels(), visitor.getEagerlyLoadedChannels());
    return new Result(rewrittenProjection, inputChannels);
}
Also used : RowExpressionVisitor(io.trino.sql.relational.RowExpressionVisitor) RowExpression(io.trino.sql.relational.RowExpression)

Example 18 with RowExpression

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

the class BenchmarkPageProcessor2 method rowExpression.

private RowExpression rowExpression(String value) {
    Expression expression = createExpression(value, PLANNER_CONTEXT, TypeProvider.copyOf(symbolTypes));
    Map<NodeRef<Expression>, Type> expressionTypes = TYPE_ANALYZER.getTypes(TEST_SESSION, TypeProvider.copyOf(symbolTypes), expression);
    return SqlToRowExpressionTranslator.translate(expression, expressionTypes, sourceLayout, PLANNER_CONTEXT.getMetadata(), PLANNER_CONTEXT.getFunctionManager(), TEST_SESSION, true);
}
Also used : NodeRef(io.trino.sql.tree.NodeRef) Type(io.trino.spi.type.Type) ExpressionTestUtils.createExpression(io.trino.sql.ExpressionTestUtils.createExpression) RowExpression(io.trino.sql.relational.RowExpression) Expression(io.trino.sql.tree.Expression)

Example 19 with RowExpression

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

the class BenchmarkPageProcessor2 method setup.

@Setup
public void setup() {
    Type type = TYPE_MAP.get(this.type);
    for (int i = 0; i < columnCount; i++) {
        Symbol symbol = new Symbol(type.getDisplayName().toLowerCase(ENGLISH) + i);
        symbolTypes.put(symbol, type);
        sourceLayout.put(symbol, i);
    }
    List<RowExpression> projections = getProjections(type);
    types = projections.stream().map(RowExpression::getType).collect(toList());
    FunctionManager functionManager = createTestingFunctionManager();
    PageFunctionCompiler pageFunctionCompiler = new PageFunctionCompiler(functionManager, 0);
    inputPage = createPage(types, dictionaryBlocks);
    pageProcessor = new ExpressionCompiler(functionManager, pageFunctionCompiler).compilePageProcessor(Optional.of(getFilter(type)), projections).get();
    recordSet = new PageRecordSet(types, inputPage);
    cursorProcessor = new ExpressionCompiler(functionManager, pageFunctionCompiler).compileCursorProcessor(Optional.of(getFilter(type)), projections, "key").get();
}
Also used : Type(io.trino.spi.type.Type) Symbol(io.trino.sql.planner.Symbol) RowExpression(io.trino.sql.relational.RowExpression) PageRecordSet(io.trino.operator.index.PageRecordSet) FunctionManager.createTestingFunctionManager(io.trino.metadata.FunctionManager.createTestingFunctionManager) FunctionManager(io.trino.metadata.FunctionManager) Setup(org.openjdk.jmh.annotations.Setup)

Example 20 with RowExpression

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

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