use of io.trino.spi.type.BigintType.BIGINT in project trino by trinodb.
the class TestPushProjectionIntoTableScan method testPushProjection.
@Test
public void testPushProjection() {
try (RuleTester ruleTester = defaultRuleTester()) {
// Building context for input
String columnName = "col0";
Type columnType = ROW_TYPE;
Symbol baseColumn = new Symbol(columnName);
ColumnHandle columnHandle = new TpchColumnHandle(columnName, columnType);
// Create catalog with applyProjection enabled
MockConnectorFactory factory = createMockFactory(ImmutableMap.of(columnName, columnHandle), Optional.of(this::mockApplyProjection));
ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, factory, ImmutableMap.of());
TypeAnalyzer typeAnalyzer = createTestingTypeAnalyzer(ruleTester.getPlannerContext());
// Prepare project node symbols and types
Symbol identity = new Symbol("symbol_identity");
Symbol dereference = new Symbol("symbol_dereference");
Symbol constant = new Symbol("symbol_constant");
Symbol call = new Symbol("symbol_call");
ImmutableMap<Symbol, Type> types = ImmutableMap.of(baseColumn, ROW_TYPE, identity, ROW_TYPE, dereference, BIGINT, constant, BIGINT, call, VARCHAR);
// Prepare project node assignments
ImmutableMap<Symbol, Expression> inputProjections = ImmutableMap.of(identity, baseColumn.toSymbolReference(), dereference, new SubscriptExpression(baseColumn.toSymbolReference(), new LongLiteral("1")), constant, new LongLiteral("5"), call, new FunctionCall(QualifiedName.of("STARTS_WITH"), ImmutableList.of(new StringLiteral("abc"), new StringLiteral("ab"))));
// Compute expected symbols after applyProjection
TransactionId transactionId = ruleTester.getQueryRunner().getTransactionManager().beginTransaction(false);
Session session = MOCK_SESSION.beginTransactionId(transactionId, ruleTester.getQueryRunner().getTransactionManager(), ruleTester.getQueryRunner().getAccessControl());
ImmutableMap<Symbol, String> connectorNames = inputProjections.entrySet().stream().collect(toImmutableMap(Map.Entry::getKey, e -> translate(session, e.getValue(), typeAnalyzer, viewOf(types), ruleTester.getPlannerContext()).get().toString()));
ImmutableMap<Symbol, String> newNames = ImmutableMap.of(identity, "projected_variable_" + connectorNames.get(identity), dereference, "projected_dereference_" + connectorNames.get(dereference), constant, "projected_constant_" + connectorNames.get(constant), call, "projected_call_" + connectorNames.get(call));
Map<String, ColumnHandle> expectedColumns = newNames.entrySet().stream().collect(toImmutableMap(Map.Entry::getValue, e -> column(e.getValue(), types.get(e.getKey()))));
ruleTester.assertThat(createRule(ruleTester)).on(p -> {
// Register symbols
types.forEach((symbol, type) -> p.symbol(symbol.getName(), type));
return p.project(new Assignments(inputProjections), p.tableScan(tableScan -> tableScan.setTableHandle(TEST_TABLE_HANDLE).setSymbols(ImmutableList.copyOf(types.keySet())).setAssignments(types.keySet().stream().collect(Collectors.toMap(Function.identity(), v -> columnHandle))).setStatistics(Optional.of(PlanNodeStatsEstimate.builder().setOutputRowCount(42).addSymbolStatistics(baseColumn, SymbolStatsEstimate.builder().setNullsFraction(0).setDistinctValuesCount(33).build()).build()))));
}).withSession(MOCK_SESSION).matches(project(newNames.entrySet().stream().collect(toImmutableMap(e -> e.getKey().getName(), e -> expression(symbolReference(e.getValue())))), tableScan(new MockConnectorTableHandle(new SchemaTableName(TEST_SCHEMA, "projected_" + TEST_TABLE), TupleDomain.all(), Optional.of(ImmutableList.copyOf(expectedColumns.values())))::equals, TupleDomain.all(), expectedColumns.entrySet().stream().collect(toImmutableMap(Map.Entry::getKey, e -> e.getValue()::equals)), Optional.of(PlanNodeStatsEstimate.builder().setOutputRowCount(42).addSymbolStatistics(new Symbol(newNames.get(constant)), SymbolStatsEstimate.builder().setDistinctValuesCount(1).setNullsFraction(0).setLowValue(5).setHighValue(5).build()).addSymbolStatistics(new Symbol(newNames.get(call).toLowerCase(ENGLISH)), SymbolStatsEstimate.builder().setDistinctValuesCount(1).setNullsFraction(0).build()).addSymbolStatistics(new Symbol(newNames.get(identity)), SymbolStatsEstimate.builder().setDistinctValuesCount(33).setNullsFraction(0).build()).addSymbolStatistics(new Symbol(newNames.get(dereference)), SymbolStatsEstimate.unknown()).build())::equals)));
}
}
use of io.trino.spi.type.BigintType.BIGINT in project trino by trinodb.
the class TestPushTopNIntoTableScan method testPushPartialTopNIntoTableScanNotGuaranteed.
@Test
public void testPushPartialTopNIntoTableScanNotGuaranteed() {
try (RuleTester ruleTester = defaultRuleTester()) {
MockConnectorTableHandle connectorHandle = new MockConnectorTableHandle(TEST_SCHEMA_TABLE);
// make the mock connector return a new connectorHandle
MockConnectorFactory.ApplyTopN applyTopN = (session, handle, topNCount, sortItems, tableAssignments) -> Optional.of(new TopNApplicationResult<>(connectorHandle, false, false));
MockConnectorFactory mockFactory = createMockFactory(assignments, Optional.of(applyTopN));
ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, mockFactory, ImmutableMap.of());
ruleTester.assertThat(new PushTopNIntoTableScan(ruleTester.getMetadata())).on(p -> {
Symbol dimension = p.symbol(dimensionName, VARCHAR);
Symbol metric = p.symbol(metricName, BIGINT);
return p.topN(1, ImmutableList.of(dimension), TopNNode.Step.PARTIAL, p.tableScan(TEST_TABLE_HANDLE, ImmutableList.of(dimension, metric), ImmutableMap.of(dimension, dimensionColumn, metric, metricColumn)));
}).withSession(MOCK_SESSION).matches(topN(1, ImmutableList.of(sort(dimensionName, ASCENDING, FIRST)), TopNNode.Step.PARTIAL, tableScan(connectorHandle::equals, TupleDomain.all(), ImmutableMap.of(dimensionName, dimensionColumn::equals, metricName, metricColumn::equals))));
}
}
use of io.trino.spi.type.BigintType.BIGINT in project trino by trinodb.
the class TestPushTopNIntoTableScan method testPushSingleTopNIntoTableScan.
@Test
public void testPushSingleTopNIntoTableScan() {
try (RuleTester ruleTester = defaultRuleTester()) {
MockConnectorTableHandle connectorHandle = new MockConnectorTableHandle(TEST_SCHEMA_TABLE);
// make the mock connector return a new connectorHandle
MockConnectorFactory.ApplyTopN applyTopN = (session, handle, topNCount, sortItems, tableAssignments) -> Optional.of(new TopNApplicationResult<>(connectorHandle, true, false));
MockConnectorFactory mockFactory = createMockFactory(assignments, Optional.of(applyTopN));
ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, mockFactory, ImmutableMap.of());
ruleTester.assertThat(new PushTopNIntoTableScan(ruleTester.getMetadata())).on(p -> {
Symbol dimension = p.symbol(dimensionName, VARCHAR);
Symbol metric = p.symbol(metricName, BIGINT);
return p.topN(1, ImmutableList.of(dimension), p.tableScan(TEST_TABLE_HANDLE, ImmutableList.of(dimension, metric), ImmutableMap.of(dimension, dimensionColumn, metric, metricColumn)));
}).withSession(MOCK_SESSION).matches(tableScan(connectorHandle::equals, TupleDomain.all(), new HashMap<>()));
}
}
use of io.trino.spi.type.BigintType.BIGINT in project trino by trinodb.
the class TestPushDownDereferencesRules method testMultiLevelPushdown.
@Test
public void testMultiLevelPushdown() {
Type complexType = rowType(field("f1", rowType(field("f1", BIGINT), field("f2", BIGINT))), field("f2", BIGINT));
tester().assertThat(new PushDownDereferenceThroughProject(tester().getTypeAnalyzer())).on(p -> p.project(Assignments.of(p.symbol("expr_1"), expression("a[1]"), p.symbol("expr_2"), expression("a[1][1] + 2 + b[1][1] + b[1][2]")), p.project(Assignments.identity(ImmutableList.of(p.symbol("a", complexType), p.symbol("b", complexType))), p.values(p.symbol("a", complexType), p.symbol("b", complexType))))).matches(strictProject(ImmutableMap.of("expr_1", PlanMatchPattern.expression("a_f1"), "expr_2", PlanMatchPattern.expression("a_f1[1] + 2 + b_f1_f1 + b_f1_f2")), strictProject(ImmutableMap.of("a", PlanMatchPattern.expression("a"), "b", PlanMatchPattern.expression("b"), "a_f1", PlanMatchPattern.expression("a[1]"), "b_f1_f1", PlanMatchPattern.expression("b[1][1]"), "b_f1_f2", PlanMatchPattern.expression("b[1][2]")), values("a", "b"))));
}
use of io.trino.spi.type.BigintType.BIGINT in project trino by trinodb.
the class TestPushLimitThroughProject method testDoesntPushDownLimitThroughExclusiveDereferences.
@Test
public void testDoesntPushDownLimitThroughExclusiveDereferences() {
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"), new SubscriptExpression(a.toSymbolReference(), new LongLiteral("2"))), p.values(a)));
}).doesNotFire();
}
Aggregations