Search in sources :

Example 6 with SubscriptExpression

use of io.trino.sql.tree.SubscriptExpression in project trino by trinodb.

the class UnwrapSingleColumnRowInApply method unwrapSingleColumnRow.

private Optional<Unwrapping> unwrapSingleColumnRow(Context context, Expression value, Expression list, BiFunction<Symbol, Symbol, Expression> function) {
    Type type = typeAnalyzer.getType(context.getSession(), context.getSymbolAllocator().getTypes(), value);
    if (type instanceof RowType) {
        RowType rowType = (RowType) type;
        if (rowType.getFields().size() == 1) {
            Type elementType = rowType.getTypeParameters().get(0);
            Symbol valueSymbol = context.getSymbolAllocator().newSymbol("input", elementType);
            Symbol listSymbol = context.getSymbolAllocator().newSymbol("subquery", elementType);
            Assignment inputAssignment = new Assignment(valueSymbol, new SubscriptExpression(value, new LongLiteral("1")));
            Assignment nestedPlanAssignment = new Assignment(listSymbol, new SubscriptExpression(list, new LongLiteral("1")));
            Expression comparison = function.apply(valueSymbol, listSymbol);
            return Optional.of(new Unwrapping(comparison, inputAssignment, nestedPlanAssignment));
        }
    }
    return Optional.empty();
}
Also used : Assignment(io.trino.sql.planner.plan.Assignments.Assignment) RowType(io.trino.spi.type.RowType) Type(io.trino.spi.type.Type) LongLiteral(io.trino.sql.tree.LongLiteral) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Expression(io.trino.sql.tree.Expression) Symbol(io.trino.sql.planner.Symbol) RowType(io.trino.spi.type.RowType) SubscriptExpression(io.trino.sql.tree.SubscriptExpression)

Example 7 with SubscriptExpression

use of io.trino.sql.tree.SubscriptExpression in project trino by trinodb.

the class TestPushProjectionIntoTableScan method testDoesNotFire.

@Test
public void testDoesNotFire() {
    try (RuleTester ruleTester = defaultRuleTester()) {
        String columnName = "input_column";
        Type columnType = ROW_TYPE;
        ColumnHandle inputColumnHandle = column(columnName, columnType);
        MockConnectorFactory factory = createMockFactory(ImmutableMap.of(columnName, inputColumnHandle), Optional.empty());
        ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, factory, ImmutableMap.of());
        PushProjectionIntoTableScan optimizer = createRule(ruleTester);
        ruleTester.assertThat(optimizer).on(p -> {
            Symbol symbol = p.symbol(columnName, columnType);
            return p.project(Assignments.of(p.symbol("symbol_dereference", BIGINT), new SubscriptExpression(symbol.toSymbolReference(), new LongLiteral("1"))), p.tableScan(TEST_TABLE_HANDLE, ImmutableList.of(symbol), ImmutableMap.of(symbol, inputColumnHandle)));
        }).withSession(MOCK_SESSION).doesNotFire();
    }
}
Also used : TpchColumnHandle(io.trino.plugin.tpch.TpchColumnHandle) ColumnHandle(io.trino.spi.connector.ColumnHandle) MockConnectorFactory(io.trino.connector.MockConnectorFactory) RowType(io.trino.spi.type.RowType) Type(io.trino.spi.type.Type) LongLiteral(io.trino.sql.tree.LongLiteral) RuleTester.defaultRuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester) RuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester) Symbol(io.trino.sql.planner.Symbol) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) Test(org.testng.annotations.Test)

Example 8 with SubscriptExpression

use of io.trino.sql.tree.SubscriptExpression in project trino by trinodb.

the class TestPushLimitThroughProject method testPushDownLimitThroughOverlappingDereferences.

@Test
public void testPushDownLimitThroughOverlappingDereferences() {
    RowType rowType = RowType.from(ImmutableList.of(new RowType.Field(Optional.of("x"), BIGINT), new RowType.Field(Optional.of("y"), BIGINT)));
    tester().assertThat(new PushLimitThroughProject(tester().getTypeAnalyzer())).on(p -> {
        Symbol a = p.symbol("a", rowType);
        return p.limit(1, p.project(Assignments.of(p.symbol("b"), new SubscriptExpression(a.toSymbolReference(), new LongLiteral("1")), p.symbol("c", rowType), a.toSymbolReference()), p.values(a)));
    }).matches(project(ImmutableMap.of("b", expression("a[1]"), "c", expression("a")), limit(1, values("a"))));
}
Also used : ExpressionMatcher(io.trino.sql.planner.assertions.ExpressionMatcher) Symbol(io.trino.sql.planner.Symbol) PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) RowType(io.trino.spi.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Assignments(io.trino.sql.planner.plan.Assignments) Test(org.testng.annotations.Test) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) TRUE_LITERAL(io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL) FIRST(io.trino.sql.tree.SortItem.NullOrdering.FIRST) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) PlanMatchPattern.limit(io.trino.sql.planner.assertions.PlanMatchPattern.limit) PlanMatchPattern.strictProject(io.trino.sql.planner.assertions.PlanMatchPattern.strictProject) ADD(io.trino.sql.tree.ArithmeticBinaryExpression.Operator.ADD) ImmutableList(com.google.common.collect.ImmutableList) BIGINT(io.trino.spi.type.BigintType.BIGINT) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) SymbolReference(io.trino.sql.tree.SymbolReference) LongLiteral(io.trino.sql.tree.LongLiteral) PlanMatchPattern.sort(io.trino.sql.planner.assertions.PlanMatchPattern.sort) Optional(java.util.Optional) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ASCENDING(io.trino.sql.tree.SortItem.Ordering.ASCENDING) LongLiteral(io.trino.sql.tree.LongLiteral) Symbol(io.trino.sql.planner.Symbol) RowType(io.trino.spi.type.RowType) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Example 9 with SubscriptExpression

use of io.trino.sql.tree.SubscriptExpression in project trino by trinodb.

the class TestSqlParser method testArraySubscript.

@Test
public void testArraySubscript() {
    assertExpression("ARRAY [1, 2][1]", new SubscriptExpression(new ArrayConstructor(ImmutableList.of(new LongLiteral("1"), new LongLiteral("2"))), new LongLiteral("1")));
    assertExpression("CASE WHEN TRUE THEN ARRAY[1,2] END[1]", new SubscriptExpression(new SearchedCaseExpression(ImmutableList.of(new WhenClause(new BooleanLiteral("true"), new ArrayConstructor(ImmutableList.of(new LongLiteral("1"), new LongLiteral("2"))))), Optional.empty()), new LongLiteral("1")));
}
Also used : WhenClause(io.trino.sql.tree.WhenClause) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) LongLiteral(io.trino.sql.tree.LongLiteral) BooleanLiteral(io.trino.sql.tree.BooleanLiteral) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) ArrayConstructor(io.trino.sql.tree.ArrayConstructor) Test(org.junit.jupiter.api.Test)

Example 10 with SubscriptExpression

use of io.trino.sql.tree.SubscriptExpression in project trino by trinodb.

the class TestSqlParser method testSelectWithRowType.

@Test
public void testSelectWithRowType() {
    assertStatement("SELECT col1.f1, col2, col3.f1.f2.f3 FROM table1", simpleQuery(selectList(new DereferenceExpression(new Identifier("col1"), identifier("f1")), new Identifier("col2"), new DereferenceExpression(new DereferenceExpression(new DereferenceExpression(new Identifier("col3"), identifier("f1")), identifier("f2")), identifier("f3"))), new Table(QualifiedName.of("table1"))));
    assertStatement("SELECT col1.f1[0], col2, col3[2].f2.f3, col4[4] FROM table1", simpleQuery(selectList(new SubscriptExpression(new DereferenceExpression(new Identifier("col1"), identifier("f1")), new LongLiteral("0")), new Identifier("col2"), new DereferenceExpression(new DereferenceExpression(new SubscriptExpression(new Identifier("col3"), new LongLiteral("2")), identifier("f2")), identifier("f3")), new SubscriptExpression(new Identifier("col4"), new LongLiteral("4"))), new Table(QualifiedName.of("table1"))));
    assertStatement("SELECT CAST(ROW(11, 12) AS ROW(COL0 INTEGER, COL1 INTEGER)).col0", simpleQuery(selectList(new DereferenceExpression(new Cast(new Row(Lists.newArrayList(new LongLiteral("11"), new LongLiteral("12"))), rowType(location(1, 26), field(location(1, 30), "COL0", simpleType(location(1, 35), "INTEGER")), field(location(1, 44), "COL1", simpleType(location(1, 49), "INTEGER")))), identifier("col0")))));
}
Also used : Cast(io.trino.sql.tree.Cast) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) QueryUtil.quotedIdentifier(io.trino.sql.QueryUtil.quotedIdentifier) Identifier(io.trino.sql.tree.Identifier) CreateTable(io.trino.sql.tree.CreateTable) DropTable(io.trino.sql.tree.DropTable) Table(io.trino.sql.tree.Table) TruncateTable(io.trino.sql.tree.TruncateTable) RenameTable(io.trino.sql.tree.RenameTable) LongLiteral(io.trino.sql.tree.LongLiteral) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) SkipTo.skipToNextRow(io.trino.sql.tree.SkipTo.skipToNextRow) Row(io.trino.sql.tree.Row) Test(org.junit.jupiter.api.Test)

Aggregations

SubscriptExpression (io.trino.sql.tree.SubscriptExpression)28 Expression (io.trino.sql.tree.Expression)23 SymbolReference (io.trino.sql.tree.SymbolReference)22 Assignments (io.trino.sql.planner.plan.Assignments)20 ImmutableList (com.google.common.collect.ImmutableList)19 Map (java.util.Map)17 TypeAnalyzer (io.trino.sql.planner.TypeAnalyzer)16 Objects.requireNonNull (java.util.Objects.requireNonNull)16 Set (java.util.Set)16 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)15 Capture (io.trino.matching.Capture)15 Capture.newCapture (io.trino.matching.Capture.newCapture)15 Captures (io.trino.matching.Captures)15 Pattern (io.trino.matching.Pattern)15 Rule (io.trino.sql.planner.iterative.Rule)15 Patterns.source (io.trino.sql.planner.plan.Patterns.source)15 ProjectNode (io.trino.sql.planner.plan.ProjectNode)15 HashBiMap (com.google.common.collect.HashBiMap)14 ExpressionNodeInliner.replaceExpression (io.trino.sql.planner.ExpressionNodeInliner.replaceExpression)14 DereferencePushdown.extractRowSubscripts (io.trino.sql.planner.iterative.rule.DereferencePushdown.extractRowSubscripts)14