@Test public void testEmptyInput() { RowPagesBuilder rowPagesBuilder = RowPagesBuilder.rowPagesBuilder(BOOLEAN, VARCHAR, BIGINT); List<Page> input = rowPagesBuilder.build(); MaterializedResult expected = resultBuilder(driverContext.getSession(), VARCHAR, BIGINT, BIGINT).build(); assertOperatorEquals(operatorFactory, driverContext, input, expected); }
@Test public void testPageSourceLazyLoad() DriverContext driverContext = newDriverContext(); List<RowExpression> projections = ImmutableList.of(field(0, VARCHAR)); Supplier<CursorProcessor> cursorProcessor = expressionCompiler.compileCursorProcessor(Optional.empty(), projections, "key"); PageProcessor pageProcessor = new PageProcessor(Optional.of(new SelectAllFilter()), ImmutableList.of(new LazyPagePageProjection())); cursorProcessor, () -> pageProcessor, ImmutableList.of(), ImmutableList.of(BIGINT), new DataSize(0, BYTE), operator.noMoreSplits(); MaterializedResult expected = toMaterializedResult(driverContext.getSession(), ImmutableList.of(BIGINT), ImmutableList.of(new Page(inputBlock))); MaterializedResult actual = toMaterializedResult(driverContext.getSession(), ImmutableList.of(BIGINT), toPages(operator));
@Test public void testSingleColumn() throws Exception { InMemoryRecordSet records = new InMemoryRecordSet(ImmutableList.of(VARCHAR), ImmutableList.copyOf(new List<?>[] {ImmutableList.of("abc"), ImmutableList.of("def"), ImmutableList.of("g")})); OperatorContext operatorContext = driverContext.addOperatorContext(0, new PlanNodeId("test"), RecordProjectOperator.class.getSimpleName()); Operator operator = new RecordProjectOperator(operatorContext, records); MaterializedResult expected = resultBuilder(driverContext.getSession(), VARCHAR) .row("abc") .row("def") .row("g") .build(); OperatorAssertion.assertOperatorEquals(operator, expected); }
@Test public void testSinglePage() { RowPagesBuilder rowPagesBuilder = RowPagesBuilder.rowPagesBuilder(BOOLEAN, VARCHAR, BIGINT); List<Page> input = rowPagesBuilder .row(false, "a", 5) .build(); MaterializedResult expected = resultBuilder(driverContext.getSession(), VARCHAR, BIGINT, BIGINT) .row("a", 1L, 5L) .build(); assertOperatorEquals(operatorFactory, driverContext, input, expected); }
@Test public void testMultiColumn() throws Exception { InMemoryRecordSet records = new InMemoryRecordSet(ImmutableList.of(VARCHAR, BIGINT), ImmutableList.of( ImmutableList.of("abc", 1L), ImmutableList.of("def", 2L), ImmutableList.of("g", 0L))); OperatorContext operatorContext = driverContext.addOperatorContext(0, new PlanNodeId("test"), RecordProjectOperator.class.getSimpleName()); Operator operator = new RecordProjectOperator(operatorContext, records); MaterializedResult expected = resultBuilder(driverContext.getSession(), VARCHAR, BIGINT) .row("abc", 1) .row("def", 2) .row("g", 0) .build(); OperatorAssertion.assertOperatorEquals(operator, expected); }
@Test public void testUniqueGroupingValues() { RowPagesBuilder rowPagesBuilder = RowPagesBuilder.rowPagesBuilder(BOOLEAN, VARCHAR, BIGINT); List<Page> input = rowPagesBuilder .addSequencePage(10, 0, 0, 0) .addSequencePage(10, 0, 10, 10) .build(); MaterializedResult.Builder builder = resultBuilder(driverContext.getSession(), VARCHAR, BIGINT, BIGINT); for (int i = 0; i < 20; i++) { builder.row(format("%s", i), 1L, Long.valueOf(i)); } assertOperatorEquals(operatorFactory, driverContext, input, builder.build()); }
@Test public void testPartiallyPreGroupedPartitionWithEmptyInput() { List<Page> input = rowPagesBuilder(BIGINT, VARCHAR, BIGINT, VARCHAR) .pageBreak() .pageBreak() .build(); WindowOperatorFactory operatorFactory = createFactoryUnbounded( ImmutableList.of(BIGINT, VARCHAR, BIGINT, VARCHAR), Ints.asList(0, 1, 2, 3), ROW_NUMBER, Ints.asList(0, 1), Ints.asList(1), Ints.asList(3), ImmutableList.of(SortOrder.ASC_NULLS_LAST), 0); MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT, VARCHAR, BIGINT, VARCHAR, BIGINT) .build(); assertOperatorEquals(operatorFactory, driverContext, input, expected); }
@Test public void testLimitWithPageAlignment() { List<Page> input = rowPagesBuilder(BIGINT) .addSequencePage(3, 1) .addSequencePage(2, 4) .addSequencePage(2, 6) .build(); OperatorFactory operatorFactory = new LimitOperatorFactory(0, new PlanNodeId("test"), 5); MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT) .page(createSequencePage(ImmutableList.of(BIGINT), 3, 1)) .page(createSequencePage(ImmutableList.of(BIGINT), 2, 4)) .build(); OperatorAssertion.assertOperatorEquals(operatorFactory, driverContext, input, expected); }
@Test(dataProvider = "hashEnabledValues") public void testMarkDistinct(boolean hashEnabled) { RowPagesBuilder rowPagesBuilder = rowPagesBuilder(hashEnabled, Ints.asList(0), BIGINT); List<Page> input = rowPagesBuilder .addSequencePage(100, 0) .addSequencePage(100, 0) .build(); OperatorFactory operatorFactory = new MarkDistinctOperatorFactory(0, new PlanNodeId("test"), rowPagesBuilder.getTypes(), ImmutableList.of(0), rowPagesBuilder.getHashChannel(), joinCompiler); MaterializedResult.Builder expected = resultBuilder(driverContext.getSession(), BIGINT, BOOLEAN); for (long i = 0; i < 100; i++) { expected.row(i, true); expected.row(i, false); } OperatorAssertion.assertOperatorEqualsIgnoreOrder(operatorFactory, driverContext, input, expected.build(), hashEnabled, Optional.of(1)); }
@Test public void testUnnestWithArray() { MetadataManager metadata = createTestMetadataManager(); Type arrayType = metadata.getType(parseTypeSignature("array(array(bigint))")); Type mapType = metadata.getType(parseTypeSignature("map(array(bigint),array(bigint))")); List<Page> input = rowPagesBuilder(BIGINT, arrayType, mapType) .row( 1L, arrayBlockOf(new ArrayType(BIGINT), ImmutableList.of(2, 4), ImmutableList.of(3, 6)), mapBlockOf(new ArrayType(BIGINT), new ArrayType(BIGINT), ImmutableMap.of(ImmutableList.of(4, 8), ImmutableList.of(5, 10)))) .row(2L, arrayBlockOf(new ArrayType(BIGINT), ImmutableList.of(99, 198)), null) .row(3L, null, null) .pageBreak() .row( 6, arrayBlockOf(new ArrayType(BIGINT), ImmutableList.of(7, 14), ImmutableList.of(8, 16)), mapBlockOf(new ArrayType(BIGINT), new ArrayType(BIGINT), ImmutableMap.of(ImmutableList.of(9, 18), ImmutableList.of(10, 20), ImmutableList.of(11, 22), ImmutableList.of(12, 24)))) .build(); OperatorFactory operatorFactory = new UnnestOperator.UnnestOperatorFactory( 0, new PlanNodeId("test"), ImmutableList.of(0), ImmutableList.of(BIGINT), ImmutableList.of(1, 2), ImmutableList.of(arrayType, mapType), false); MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT, new ArrayType(BIGINT), new ArrayType(BIGINT), new ArrayType(BIGINT)) .row(1L, ImmutableList.of(2L, 4L), ImmutableList.of(4L, 8L), ImmutableList.of(5L, 10L)) .row(1L, ImmutableList.of(3L, 6L), null, null) .row(2L, ImmutableList.of(99L, 198L), null, null) .row(6L, ImmutableList.of(7L, 14L), ImmutableList.of(9L, 18L), ImmutableList.of(10L, 20L)) .row(6L, ImmutableList.of(8L, 16L), ImmutableList.of(11L, 22L), ImmutableList.of(12L, 24L)) .build(); assertOperatorEquals(operatorFactory, driverContext, input, expected); }
@Test public void testUnnestNonNumericDoubles() { MetadataManager metadata = createTestMetadataManager(); Type arrayType = metadata.getType(parseTypeSignature("array(double)")); Type mapType = metadata.getType(parseTypeSignature("map(bigint,double)")); List<Page> input = rowPagesBuilder(BIGINT, arrayType, mapType) .row(1L, arrayBlockOf(DOUBLE, NEGATIVE_INFINITY, POSITIVE_INFINITY, NaN), mapBlockOf(BIGINT, DOUBLE, ImmutableMap.of(1, NEGATIVE_INFINITY, 2, POSITIVE_INFINITY, 3, NaN))) .build(); OperatorFactory operatorFactory = new UnnestOperator.UnnestOperatorFactory( 0, new PlanNodeId("test"), ImmutableList.of(0), ImmutableList.of(BIGINT), ImmutableList.of(1, 2), ImmutableList.of(arrayType, mapType), false); MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT, DOUBLE, BIGINT, DOUBLE) .row(1L, NEGATIVE_INFINITY, 1L, NEGATIVE_INFINITY) .row(1L, POSITIVE_INFINITY, 2L, POSITIVE_INFINITY) .row(1L, NaN, 3L, NaN) .build(); assertOperatorEquals(operatorFactory, driverContext, input, expected); }
@Test public void testMergeWithMemorySpill() RowPagesBuilder rowPagesBuilder = rowPagesBuilder(BIGINT); .addSequencePage(smallPagesSpillThresholdSize, 0) .addSequencePage(10, smallPagesSpillThresholdSize) .build(); ImmutableList.of(BIGINT), ImmutableList.of(0), ImmutableList.of(), Step.SINGLE, false, MaterializedResult.Builder resultBuilder = resultBuilder(driverContext.getSession(), BIGINT, BIGINT); for (int i = 0; i < smallPagesSpillThresholdSize + 10; ++i) { resultBuilder.row((long) i, (long) i); assertOperatorEqualsIgnoreOrder(operatorFactory, driverContext, input, resultBuilder.build());
@Test(dataProvider = "hashEnabledValues") public void testDistinctLimitWithPageAlignment(boolean hashEnabled) { RowPagesBuilder rowPagesBuilder = rowPagesBuilder(hashEnabled, Ints.asList(0), BIGINT); List<Page> input = rowPagesBuilder .addSequencePage(3, 1) .addSequencePage(3, 2) .build(); OperatorFactory operatorFactory = new DistinctLimitOperator.DistinctLimitOperatorFactory(0, new PlanNodeId("test"), rowPagesBuilder.getTypes(), Ints.asList(0), 3, rowPagesBuilder.getHashChannel(), joinCompiler); MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT) .row(1L) .row(2L) .row(3L) .build(); assertOperatorEquals(operatorFactory, driverContext, input, expected, hashEnabled, ImmutableList.of(1)); }
@Test public void testAggregation() InternalAggregationFunction maxVarcharColumn = metadata.getFunctionRegistry().getAggregateFunctionImplementation( new Signature("max", AGGREGATE, parseTypeSignature(StandardTypes.VARCHAR), parseTypeSignature(StandardTypes.VARCHAR))); List<Page> input = rowPagesBuilder(VARCHAR, BIGINT, VARCHAR, BIGINT, REAL, DOUBLE, VARCHAR) .addSequencePage(100, 0, 0, 300, 500, 400, 500, 500) .build(); new PlanNodeId("test"), Step.SINGLE, ImmutableList.of(COUNT.bind(ImmutableList.of(0), Optional.empty()), LONG_SUM.bind(ImmutableList.of(1), Optional.empty()), LONG_AVERAGE.bind(ImmutableList.of(1), Optional.empty()), maxVarcharColumn.bind(ImmutableList.of(2), Optional.empty()), .addDriverContext(); MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT, BIGINT, DOUBLE, VARCHAR, BIGINT, BIGINT, REAL, DOUBLE, VARCHAR) .row(100L, 4950L, 49.5, "399", 100L, 54950L, 44950.0f, 54950.0, "599") .build();
@Test public void testUnnestWithArrayOfRows() { MetadataManager metadata = createTestMetadataManager(); Type arrayOfRowType = metadata.getType(parseTypeSignature("array(row(bigint, double, varchar))")); Type elementType = RowType.anonymous(ImmutableList.of(BIGINT, DOUBLE, VARCHAR)); List<Page> input = rowPagesBuilder(BIGINT, arrayOfRowType) .row(1, arrayBlockOf(elementType, ImmutableList.of(2, 4.2, "abc"), ImmutableList.of(3, 6.6, "def"))) .row(2, arrayBlockOf(elementType, ImmutableList.of(99, 3.14, "pi"), null)) .row(3, null) .pageBreak() .row(6, arrayBlockOf(elementType, null, ImmutableList.of(8, 1.111, "tt"))) .build(); OperatorFactory operatorFactory = new UnnestOperator.UnnestOperatorFactory( 0, new PlanNodeId("test"), ImmutableList.of(0), ImmutableList.of(BIGINT), ImmutableList.of(1), ImmutableList.of(arrayOfRowType), false); MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT, BIGINT, DOUBLE, VARCHAR) .row(1L, 2L, 4.2, "abc") .row(1L, 3L, 6.6, "def") .row(2L, 99L, 3.14, "pi") .row(2L, null, null, null) .row(6L, null, null, null) .row(6L, 8L, 1.111, "tt") .build(); assertOperatorEquals(operatorFactory, driverContext, input, expected); } }
@Test public void testMultiFieldKey() { List<Page> input = rowPagesBuilder(VARCHAR, BIGINT) .row("a", 1L) .row("b", 2L) .pageBreak() .row("b", 3L) .row("a", 4L) .build(); OrderByOperatorFactory operatorFactory = new OrderByOperatorFactory( 0, new PlanNodeId("test"), ImmutableList.of(VARCHAR, BIGINT), ImmutableList.of(0, 1), 10, ImmutableList.of(0, 1), ImmutableList.of(ASC_NULLS_LAST, DESC_NULLS_LAST), new PagesIndex.TestingFactory(false)); MaterializedResult expected = MaterializedResult.resultBuilder(driverContext.getSession(), VARCHAR, BIGINT) .row("a", 4L) .row("a", 1L) .row("b", 3L) .row("b", 2L) .build(); assertOperatorEquals(operatorFactory, driverContext, input, expected); }
@Test public void testReverseOrder() { List<Page> input = rowPagesBuilder(BIGINT, DOUBLE) .row(1L, 0.1) .row(2L, 0.2) .pageBreak() .row(-1L, -0.1) .row(4L, 0.4) .build(); OrderByOperatorFactory operatorFactory = new OrderByOperatorFactory( 0, new PlanNodeId("test"), ImmutableList.of(BIGINT, DOUBLE), ImmutableList.of(0), 10, ImmutableList.of(0), ImmutableList.of(DESC_NULLS_LAST), new PagesIndex.TestingFactory(false)); MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT) .row(4L) .row(2L) .row(1L) .row(-1L) .build(); assertOperatorEquals(operatorFactory, driverContext, input, expected); }
@Test public void testSingleFieldKey() { List<Page> input = rowPagesBuilder(BIGINT, DOUBLE) .row(1L, 0.1) .row(2L, 0.2) .pageBreak() .row(-1L, -0.1) .row(4L, 0.4) .build(); OrderByOperatorFactory operatorFactory = new OrderByOperatorFactory( 0, new PlanNodeId("test"), ImmutableList.of(BIGINT, DOUBLE), ImmutableList.of(1), 10, ImmutableList.of(0), ImmutableList.of(ASC_NULLS_LAST), new PagesIndex.TestingFactory(false)); MaterializedResult expected = resultBuilder(driverContext.getSession(), DOUBLE) .row(-0.1) .row(0.1) .row(0.2) .row(0.4) .build(); assertOperatorEquals(operatorFactory, driverContext, input, expected); }
@Test(dataProvider = "hashEnabledValues") public void testDistinctLimitValuesLessThanLimit(boolean hashEnabled) { RowPagesBuilder rowPagesBuilder = rowPagesBuilder(hashEnabled, Ints.asList(0), BIGINT); List<Page> input = rowPagesBuilder .addSequencePage(3, 1) .addSequencePage(3, 2) .build(); OperatorFactory operatorFactory = new DistinctLimitOperator.DistinctLimitOperatorFactory(0, new PlanNodeId("test"), rowPagesBuilder.getTypes(), Ints.asList(0), 5, rowPagesBuilder.getHashChannel(), joinCompiler); MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT) .row(1L) .row(2L) .row(3L) .row(4L) .build(); assertOperatorEquals(operatorFactory, driverContext, input, expected, hashEnabled, ImmutableList.of(1)); }
@Test(dataProvider = "hashEnabledValues") public void testDistinctLimit(boolean hashEnabled) { RowPagesBuilder rowPagesBuilder = rowPagesBuilder(hashEnabled, Ints.asList(0), BIGINT); List<Page> input = rowPagesBuilder .addSequencePage(3, 1) .addSequencePage(5, 2) .build(); OperatorFactory operatorFactory = new DistinctLimitOperator.DistinctLimitOperatorFactory(0, new PlanNodeId("test"), rowPagesBuilder.getTypes(), Ints.asList(0), 5, rowPagesBuilder.getHashChannel(), joinCompiler); MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT) .row(1L) .row(2L) .row(3L) .row(4L) .row(5L) .build(); assertOperatorEquals(operatorFactory, driverContext, input, expected, hashEnabled, ImmutableList.of(1)); }