@Test
  public void testGroupByWithDimFilterAndWithFilteredDimSpec() throws Exception {
    GroupByQuery query =
        GroupByQuery.builder()
            .setDataSource("xx")
            .setQuerySegmentSpec(new LegacySegmentSpec("1970/3000"))
            .setGranularity(QueryGranularity.ALL)
            .setDimensions(
                Lists.<DimensionSpec>newArrayList(
                    new RegexFilteredDimensionSpec(new DefaultDimensionSpec("tags", "tags"), "t3")))
            .setAggregatorSpecs(
                Arrays.asList(new AggregatorFactory[] {new CountAggregatorFactory("count")}))
            .setDimFilter(new SelectorDimFilter("tags", "t3"))
            .build();

    Sequence<Row> result =
        helper.runQueryOnSegmentsObjs(
            ImmutableList.of(
                new QueryableIndexSegment("sid1", queryableIndex),
                new IncrementalIndexSegment(incrementalIndex, "sid2")),
            query);

    List<Row> expectedResults =
        Arrays.asList(
            GroupByQueryRunnerTestHelper.createExpectedRow(
                "1970-01-01T00:00:00.000Z", "tags", "t3", "count", 4L));

    TestHelper.assertExpectedObjects(
        expectedResults, Sequences.toList(result, new ArrayList<Row>()), "");
  }
  @BeforeClass
  public static void setupClass() throws Exception {
    incrementalIndex =
        new OnheapIncrementalIndex(
            0,
            QueryGranularity.NONE,
            new AggregatorFactory[] {new CountAggregatorFactory("count")},
            true,
            5000);

    StringInputRowParser parser =
        new StringInputRowParser(
            new CSVParseSpec(
                new TimestampSpec("timestamp", "iso", null),
                new DimensionsSpec(ImmutableList.of("product", "tags"), null, null),
                "\t",
                ImmutableList.of("timestamp", "product", "tags")),
            "UTF-8");

    String[] rows =
        new String[] {
          "2011-01-12T00:00:00.000Z,product_1,t1\tt2\tt3",
          "2011-01-13T00:00:00.000Z,product_2,t3\tt4\tt5",
          "2011-01-14T00:00:00.000Z,product_3,t5\tt6\tt7",
        };

    for (String row : rows) {
      incrementalIndex.add(parser.parse(row));
    }

    persistedSegmentDir = Files.createTempDir();
    TestHelper.getTestIndexMerger()
        .persist(
            incrementalIndex,
            persistedSegmentDir,
            ImmutableMap.<String, Object>of(),
            new IndexSpec());

    queryableIndex = TestHelper.getTestIndexIO().loadIndex(persistedSegmentDir);
  }
  @Test
  public void testTopNWithDimFilterAndWithFilteredDimSpec() throws Exception {
    TopNQuery query =
        new TopNQueryBuilder()
            .dataSource("xx")
            .granularity(QueryGranularity.ALL)
            .dimension(
                new ListFilteredDimensionSpec(
                    new DefaultDimensionSpec("tags", "tags"), ImmutableSet.of("t3"), null))
            .metric("count")
            .intervals(QueryRunnerTestHelper.fullOnInterval)
            .aggregators(
                Arrays.asList(new AggregatorFactory[] {new CountAggregatorFactory("count")}))
            .threshold(5)
            .filters(new SelectorDimFilter("tags", "t3"))
            .build();

    QueryRunnerFactory factory =
        new TopNQueryRunnerFactory(
            TestQueryRunners.getPool(),
            new TopNQueryQueryToolChest(
                new TopNQueryConfig(),
                QueryRunnerTestHelper.NoopIntervalChunkingQueryRunnerDecorator()),
            QueryRunnerTestHelper.NOOP_QUERYWATCHER);
    QueryRunner<Result<TopNResultValue>> runner =
        QueryRunnerTestHelper.makeQueryRunner(
            factory, new QueryableIndexSegment("sid1", queryableIndex));
    Map<String, Object> context = Maps.newHashMap();
    Sequence<Result<TopNResultValue>> result = runner.run(query, context);
    List<Result<TopNResultValue>> expectedResults =
        Arrays.asList(
            new Result<TopNResultValue>(
                new DateTime("2011-01-12T00:00:00.000Z"),
                new TopNResultValue(
                    Arrays.<Map<String, Object>>asList(
                        ImmutableMap.<String, Object>of("tags", "t3", "count", 2L)))));
    TestHelper.assertExpectedObjects(
        expectedResults, Sequences.toList(result, new ArrayList<Result<TopNResultValue>>()), "");
  }
  @Test
  public void testSerdeWithNonDefaults() throws Exception {
    String jsonStr =
        "{\n"
            + "  \"type\": \"realtime\",\n"
            + "  \"maxRowsInMemory\": 100,\n"
            + "  \"intermediatePersistPeriod\": \"PT1H\",\n"
            + "  \"windowPeriod\": \"PT1H\",\n"
            + "  \"basePersistDirectory\": \"/tmp/xxx\",\n"
            + "  \"maxPendingPersists\": 100,\n"
            + "  \"buildV9Directly\": false,\n"
            + "  \"persistThreadPriority\": 100,\n"
            + "  \"mergeThreadPriority\": 100,\n"
            + "  \"reportParseExceptions\": true,\n"
            + "  \"handoffConditionTimeout\": 100\n"
            + "}";

    ObjectMapper mapper = TestHelper.getObjectMapper();
    RealtimeTuningConfig config =
        (RealtimeTuningConfig)
            mapper.readValue(
                mapper.writeValueAsString(mapper.readValue(jsonStr, TuningConfig.class)),
                TuningConfig.class);

    Assert.assertEquals("/tmp/xxx", config.getBasePersistDirectory().toString());
    Assert.assertEquals(false, config.getBuildV9Directly());
    Assert.assertEquals(100, config.getHandoffConditionTimeout());
    Assert.assertEquals(new IndexSpec(), config.getIndexSpec());
    Assert.assertEquals(new Period("PT1H"), config.getIntermediatePersistPeriod());
    Assert.assertEquals(NoneShardSpec.instance(), config.getShardSpec());
    Assert.assertEquals(100, config.getMaxPendingPersists());
    Assert.assertEquals(100, config.getMaxRowsInMemory());
    Assert.assertEquals(100, config.getMergeThreadPriority());
    Assert.assertEquals(100, config.getPersistThreadPriority());
    Assert.assertEquals(new Period("PT1H"), config.getWindowPeriod());
    Assert.assertEquals(true, config.isReportParseExceptions());
  }
  @Test
  public void testSerdeWithDefaults() throws Exception {
    String jsonStr = "{\"type\":\"realtime\"}";

    ObjectMapper mapper = TestHelper.getObjectMapper();
    RealtimeTuningConfig config =
        (RealtimeTuningConfig)
            mapper.readValue(
                mapper.writeValueAsString(mapper.readValue(jsonStr, TuningConfig.class)),
                TuningConfig.class);

    Assert.assertNotNull(config.getBasePersistDirectory());
    Assert.assertEquals(true, config.getBuildV9Directly());
    Assert.assertEquals(0, config.getHandoffConditionTimeout());
    Assert.assertEquals(new IndexSpec(), config.getIndexSpec());
    Assert.assertEquals(new Period("PT10M"), config.getIntermediatePersistPeriod());
    Assert.assertEquals(NoneShardSpec.instance(), config.getShardSpec());
    Assert.assertEquals(0, config.getMaxPendingPersists());
    Assert.assertEquals(75000, config.getMaxRowsInMemory());
    Assert.assertEquals(0, config.getMergeThreadPriority());
    Assert.assertEquals(0, config.getPersistThreadPriority());
    Assert.assertEquals(new Period("PT10M"), config.getWindowPeriod());
    Assert.assertEquals(false, config.isReportParseExceptions());
  }