use of io.trino.sql.tree.SymbolReference in project trino by trinodb.
the class TestScalarStatsCalculator method testCastDoubleToBigint.
@Test
public void testCastDoubleToBigint() {
PlanNodeStatsEstimate inputStatistics = PlanNodeStatsEstimate.builder().addSymbolStatistics(new Symbol("a"), SymbolStatsEstimate.builder().setNullsFraction(0.3).setLowValue(1.6).setHighValue(17.3).setDistinctValuesCount(10).setAverageRowSize(2.0).build()).build();
assertCalculate(new Cast(new SymbolReference("a"), toSqlType(BIGINT)), inputStatistics, TypeProvider.viewOf(ImmutableMap.of(new Symbol("a"), BIGINT))).lowValue(2.0).highValue(17.0).distinctValuesCount(10).nullsFraction(0.3).dataSizeUnknown();
}
use of io.trino.sql.tree.SymbolReference in project trino by trinodb.
the class TestHttpRemoteTask method testOutboundDynamicFilters.
@Test(timeOut = 30_000)
public void testOutboundDynamicFilters() throws Exception {
DynamicFilterId filterId1 = new DynamicFilterId("df1");
DynamicFilterId filterId2 = new DynamicFilterId("df2");
SymbolAllocator symbolAllocator = new SymbolAllocator();
Symbol symbol1 = symbolAllocator.newSymbol("DF_SYMBOL1", BIGINT);
Symbol symbol2 = symbolAllocator.newSymbol("DF_SYMBOL2", BIGINT);
SymbolReference df1 = symbol1.toSymbolReference();
SymbolReference df2 = symbol2.toSymbolReference();
ColumnHandle handle1 = new TestingColumnHandle("column1");
ColumnHandle handle2 = new TestingColumnHandle("column2");
QueryId queryId = new QueryId("test");
TestingTaskResource testingTaskResource = new TestingTaskResource(new AtomicLong(System.nanoTime()), FailureScenario.NO_FAILURE);
DynamicFilterService dynamicFilterService = new DynamicFilterService(PLANNER_CONTEXT.getMetadata(), PLANNER_CONTEXT.getFunctionManager(), new TypeOperators(), newDirectExecutorService());
dynamicFilterService.registerQuery(queryId, TEST_SESSION, ImmutableSet.of(filterId1, filterId2), ImmutableSet.of(filterId1, filterId2), ImmutableSet.of());
dynamicFilterService.stageCannotScheduleMoreTasks(new StageId(queryId, 1), 0, 1);
DynamicFilter dynamicFilter = dynamicFilterService.createDynamicFilter(queryId, ImmutableList.of(new DynamicFilters.Descriptor(filterId1, df1), new DynamicFilters.Descriptor(filterId2, df2)), ImmutableMap.of(symbol1, handle1, symbol2, handle2), symbolAllocator.getTypes());
// make sure initial dynamic filter is collected
CompletableFuture<?> future = dynamicFilter.isBlocked();
dynamicFilterService.addTaskDynamicFilters(new TaskId(new StageId(queryId.getId(), 1), 1, 0), ImmutableMap.of(filterId1, Domain.singleValue(BIGINT, 1L)));
future.get();
assertEquals(dynamicFilter.getCurrentPredicate(), TupleDomain.withColumnDomains(ImmutableMap.of(handle1, Domain.singleValue(BIGINT, 1L))));
// Create remote task after dynamic filter is created to simulate new nodes joining
HttpRemoteTaskFactory httpRemoteTaskFactory = createHttpRemoteTaskFactory(testingTaskResource, dynamicFilterService);
RemoteTask remoteTask = createRemoteTask(httpRemoteTaskFactory, ImmutableSet.of(filterId1, filterId2));
testingTaskResource.setInitialTaskInfo(remoteTask.getTaskInfo());
remoteTask.start();
assertEventually(new Duration(10, SECONDS), () -> assertEquals(testingTaskResource.getDynamicFiltersSentCounter(), 1L));
assertEquals(testingTaskResource.getCreateOrUpdateCounter(), 1L);
// schedule a couple of splits to trigger task updates
addSplit(remoteTask, testingTaskResource, 1);
addSplit(remoteTask, testingTaskResource, 2);
// make sure dynamic filter was sent in task updates only once
assertEquals(testingTaskResource.getDynamicFiltersSentCounter(), 1L);
assertEquals(testingTaskResource.getCreateOrUpdateCounter(), 3L);
assertEquals(testingTaskResource.getLatestDynamicFilterFromCoordinator(), ImmutableMap.of(filterId1, Domain.singleValue(BIGINT, 1L)));
future = dynamicFilter.isBlocked();
dynamicFilterService.addTaskDynamicFilters(new TaskId(new StageId(queryId.getId(), 1), 1, 0), ImmutableMap.of(filterId2, Domain.singleValue(BIGINT, 2L)));
future.get();
assertEquals(dynamicFilter.getCurrentPredicate(), TupleDomain.withColumnDomains(ImmutableMap.of(handle1, Domain.singleValue(BIGINT, 1L), handle2, Domain.singleValue(BIGINT, 2L))));
// dynamic filter should be sent even though there were no further splits scheduled
assertEventually(new Duration(10, SECONDS), () -> assertEquals(testingTaskResource.getDynamicFiltersSentCounter(), 2L));
assertEquals(testingTaskResource.getCreateOrUpdateCounter(), 4L);
// previously sent dynamic filter should not be repeated
assertEquals(testingTaskResource.getLatestDynamicFilterFromCoordinator(), ImmutableMap.of(filterId2, Domain.singleValue(BIGINT, 2L)));
httpRemoteTaskFactory.stop();
dynamicFilterService.stop();
}
use of io.trino.sql.tree.SymbolReference in project trino by trinodb.
the class TestExpressionVerifier method testSymmetry.
@Test
public void testSymmetry() {
SymbolAliases symbolAliases = SymbolAliases.builder().put("a", new SymbolReference("x")).put("b", new SymbolReference("y")).build();
ExpressionVerifier verifier = new ExpressionVerifier(symbolAliases);
assertTrue(verifier.process(expression("x > y"), expression("a > b")));
assertTrue(verifier.process(expression("x > y"), expression("b < a")));
assertTrue(verifier.process(expression("y < x"), expression("a > b")));
assertTrue(verifier.process(expression("y < x"), expression("b < a")));
assertFalse(verifier.process(expression("x < y"), expression("a > b")));
assertFalse(verifier.process(expression("x < y"), expression("b < a")));
assertFalse(verifier.process(expression("y > x"), expression("a > b")));
assertFalse(verifier.process(expression("y > x"), expression("b < a")));
assertTrue(verifier.process(expression("x >= y"), expression("a >= b")));
assertTrue(verifier.process(expression("x >= y"), expression("b <= a")));
assertTrue(verifier.process(expression("y <= x"), expression("a >= b")));
assertTrue(verifier.process(expression("y <= x"), expression("b <= a")));
assertFalse(verifier.process(expression("x <= y"), expression("a >= b")));
assertFalse(verifier.process(expression("x <= y"), expression("b <= a")));
assertFalse(verifier.process(expression("y >= x"), expression("a >= b")));
assertFalse(verifier.process(expression("y >= x"), expression("b <= a")));
assertTrue(verifier.process(expression("x = y"), expression("a = b")));
assertTrue(verifier.process(expression("x = y"), expression("b = a")));
assertTrue(verifier.process(expression("y = x"), expression("a = b")));
assertTrue(verifier.process(expression("y = x"), expression("b = a")));
assertTrue(verifier.process(expression("x <> y"), expression("a <> b")));
assertTrue(verifier.process(expression("x <> y"), expression("b <> a")));
assertTrue(verifier.process(expression("y <> x"), expression("a <> b")));
assertTrue(verifier.process(expression("y <> x"), expression("b <> a")));
assertTrue(verifier.process(expression("x IS DISTINCT FROM y"), expression("a IS DISTINCT FROM b")));
assertTrue(verifier.process(expression("x IS DISTINCT FROM y"), expression("b IS DISTINCT FROM a")));
assertTrue(verifier.process(expression("y IS DISTINCT FROM x"), expression("a IS DISTINCT FROM b")));
assertTrue(verifier.process(expression("y IS DISTINCT FROM x"), expression("b IS DISTINCT FROM a")));
}
use of io.trino.sql.tree.SymbolReference in project trino by trinodb.
the class TestExpressionVerifier method testBetween.
@Test
public void testBetween() {
SymbolAliases symbolAliases = SymbolAliases.builder().put("X", new SymbolReference("orderkey")).put("Y", new SymbolReference("custkey")).build();
ExpressionVerifier verifier = new ExpressionVerifier(symbolAliases);
// Complete match
assertTrue(verifier.process(expression("orderkey BETWEEN 1 AND 2"), expression("X BETWEEN 1 AND 2")));
// Different value
assertFalse(verifier.process(expression("orderkey BETWEEN 1 AND 2"), expression("Y BETWEEN 1 AND 2")));
assertFalse(verifier.process(expression("custkey BETWEEN 1 AND 2"), expression("X BETWEEN 1 AND 2")));
// Different min or max
assertFalse(verifier.process(expression("orderkey BETWEEN 2 AND 4"), expression("X BETWEEN 1 AND 2")));
assertFalse(verifier.process(expression("orderkey BETWEEN 1 AND 2"), expression("X BETWEEN '1' AND '2'")));
assertFalse(verifier.process(expression("orderkey BETWEEN 1 AND 2"), expression("X BETWEEN 4 AND 7")));
}
use of io.trino.sql.tree.SymbolReference in project trino by trinodb.
the class TestComparisonStatsCalculator method symbolToLiteralGreaterThanStats.
@Test
public void symbolToLiteralGreaterThanStats() {
// Simple case
assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("y"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls times range coverage (50%)
250.0).symbolStats("y", symbolAssert -> {
symbolAssert.averageRowSize(4.0).distinctValuesCount(10.0).lowValue(2.5).highValue(5.0).nullsFraction(0.0);
});
// Literal on the edge of symbol range (whole range included)
assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("x"), new DoubleLiteral("-10.0"))).outputRowsCount(// all rows minus nulls times range coverage (100%)
750.0).symbolStats("x", symbolAssert -> {
symbolAssert.averageRowSize(4.0).distinctValuesCount(40.0).lowValue(-10.0).highValue(10.0).nullsFraction(0.0);
});
// Literal on the edge of symbol range (whole range excluded)
assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("x"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls divided by NDV (one value from edge is included as approximation)
18.75).symbolStats("x", symbolAssert -> {
symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(10.0).highValue(10.0).nullsFraction(0.0);
});
// Literal range out of symbol range
assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("y"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls times range coverage (0%)
0.0).symbolStats("y", symbolAssert -> {
symbolAssert.averageRowSize(0.0).distinctValuesCount(0.0).emptyRange().nullsFraction(1.0);
});
// Literal in left open range
assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("leftOpen"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls times range coverage (25% - heuristic)
225.0).symbolStats("leftOpen", symbolAssert -> {
symbolAssert.averageRowSize(4.0).distinctValuesCount(// (25% heuristic)
12.5).lowValue(0.0).highValue(15.0).nullsFraction(0.0);
});
// Literal in right open range
assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("rightOpen"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls times range coverage (50% - heuristic)
450.0).symbolStats("rightOpen", symbolAssert -> {
symbolAssert.averageRowSize(4.0).distinctValuesCount(// (50% heuristic)
25.0).lowValue(0.0).highValueUnknown().nullsFraction(0.0);
});
// Literal in unknown range
assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("unknownRange"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls times range coverage (50% - heuristic)
450.0).symbolStats("unknownRange", symbolAssert -> {
symbolAssert.averageRowSize(4.0).distinctValuesCount(// (50% heuristic)
25.0).lowValue(0.0).highValueUnknown().nullsFraction(0.0);
});
// Literal in empty range
assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("emptyRange"), new DoubleLiteral("0.0"))).outputRowsCount(0.0).symbolStats("emptyRange", equalTo(emptyRangeStats));
}
Aggregations