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();
}
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();
}
}
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"))));
}
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")));
}
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")))));
}
Aggregations