public Optional<PinotQueryGeneratorResult> generate(PlanNode plan, ConnectorSession session)
    {
        try {
            PinotQueryGeneratorContext context = requireNonNull(plan.accept(
                            new PinotQueryPlanVisitor(session),
                            new PinotQueryGeneratorContext()),
                    "Resulting context is null");
            return Optional.of(new PinotQueryGeneratorResult(context.toQuery(pinotConfig, session), context));
        }
        catch (PinotException e) {
            log.debug(e, "Possibly benign error when pushing plan into scan node %s", plan);
            return Optional.empty();
        }
    }
@Test
    public void testDefaultTopNPushdown()
    {
        PlanBuilder planBuilder = createPlanBuilder(defaultSessionHolder);
        TableScanNode tableScanNode = tableScan(planBuilder, pinotTable, city, fare);
        AggregationNode aggregationNode = planBuilder.aggregation(
                aggregationNodeBuilder -> aggregationNodeBuilder
                        .source(tableScanNode)
                        .singleGroupingSet(variable("city"))
                        .addAggregation(planBuilder.variable("sum_fare"), getRowExpression("sum(fare)", defaultSessionHolder)));
        pinotConfig.setPushdownTopNBrokerQueries(true);
        TopNNode topN = new TopNNode(Optional.empty(), planBuilder.getIdAllocator().getNextId(), aggregationNode, 1000,
                new OrderingScheme(ImmutableList.of(new Ordering(variable("sum_fare"), SortOrder.ASC_NULLS_FIRST))),
                TopNNode.Step.SINGLE);
        Optional<PinotQueryGenerator.PinotQueryGeneratorResult> generatedQuery =
                new PinotQueryGenerator(pinotConfig, functionAndTypeManager, functionAndTypeManager, standardFunctionResolution)
                    .generate(topN, defaultSessionHolder.getConnectorSession());
        assertTrue(generatedQuery.isPresent());
        SessionHolder sessionHolder = new SessionHolder(pinotConfig);
        testPinotQuery(
                pinotConfig,
                aggregationNode,
                "SELECT \"city\", sum(\"fare\") FROM realtimeOnly GROUP BY \"city\" LIMIT 10000",
                sessionHolder,
                ImmutableMap.of());

        testPinotQuery(
                pinotConfig,
                topN,
                "SELECT \"city\", sum(\"fare\") FROM realtimeOnly GROUP BY \"city\" ORDER BY sum(\"fare\") LIMIT 1000",
                sessionHolder,
                ImmutableMap.of());
    }