com.carrotsearch.randomizedtesting.RandomizedTest Java Examples

The following examples show how to use com.carrotsearch.randomizedtesting.RandomizedTest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GroupByPlannerTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Before
public void prepare() throws IOException {
    e  = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of())
        .enableDefaultTables()
        .addPartitionedTable(
            "create table doc.clustered_parted (" +
            "   id integer," +
            "   date timestamp with time zone," +
            "   city string" +
            ") clustered by (city) partitioned by (date) ",
            new PartitionName(new RelationName("doc", "clustered_parted"), singletonList("1395874800000")).asIndexName(),
            new PartitionName(new RelationName("doc", "clustered_parted"), singletonList("1395961200000")).asIndexName()
        )
        .addPartitionedTable(
            "create table doc.empty_parted (" +
            "   id integer primary key," +
            "   date timestamp with time zone primary key" +
            ") clustered by (id) partitioned by (date)"
        ).build();
}
 
Example #2
Source File: TestRuleLimitSysouts.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected boolean isEnforced() {
  Class<?> target = RandomizedTest.getContext().getTargetClass();

  if (LuceneTestCase.VERBOSE || 
      LuceneTestCase.INFOSTREAM ||
      target.isAnnotationPresent(Monster.class) ||
      target.isAnnotationPresent(SuppressSysoutChecks.class)) {
    return false;
  }
  
  if (!target.isAnnotationPresent(Limit.class)) {
    return false;
  }

  return true;
}
 
Example #3
Source File: BaseDirectoryTestCase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testSeekPastEOF() throws Exception {
  try (Directory dir = getDirectory(createTempDir("testSeekPastEOF"))) {
    IndexOutput o = dir.createOutput("out", newIOContext(random()));
    final int len = random().nextInt(2048);
    byte[] b = new byte[len];
    o.writeBytes(b, 0, len);
    o.close();
    IndexInput i = dir.openInput("out", newIOContext(random()));

    // Seeking past EOF should always throw EOFException
    expectThrows(EOFException.class, () -> i.seek(len + RandomizedTest.randomIntBetween(1, 2048)));

    // Seeking exactly to EOF should never throw any exception.
    i.seek(len);

    // But any read following the seek(len) should throw an EOFException.
    expectThrows(EOFException.class, i::readByte);
    expectThrows(EOFException.class, () -> {
      i.readBytes(new byte [1], 0, 1);
    });

    i.close();
  }
}
 
Example #4
Source File: WithNestedTests.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected void before() throws Throwable {
  if (!isPropertyEmpty(SysGlobals.SYSPROP_TESTFILTER()) ||
      !isPropertyEmpty(SysGlobals.SYSPROP_TESTCLASS())  ||
      !isPropertyEmpty(SysGlobals.SYSPROP_TESTMETHOD()) ||
      !isPropertyEmpty(SysGlobals.SYSPROP_ITERATIONS())) {
    // We're running with a complex test filter that is properly handled by classes
    // which are executed by RandomizedRunner. The "outer" classes testing LuceneTestCase
    // itself are executed by the default JUnit runner and would be always executed.
    // We thus always skip execution if any filtering is detected.
    Assume.assumeTrue(false);
  }
  
  // Check zombie threads from previous suites. Don't run if zombies are around.
  RandomizedTest.assumeFalse(RandomizedRunner.hasZombieThreads());

  TestRuleIgnoreAfterMaxFailures newRule = new TestRuleIgnoreAfterMaxFailures(Integer.MAX_VALUE);
  prevRule = LuceneTestCase.replaceMaxFailureRule(newRule);
  RandomizedTest.assumeFalse(FailureMarker.hadFailures());
}
 
Example #5
Source File: TestFailIfUnreferencedFiles.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailIfUnreferencedFiles() {
  Result r = JUnitCore.runClasses(Nested1.class);
  RandomizedTest.assumeTrue("Ignoring nested test, very likely zombie threads present.", 
      r.getIgnoreCount() == 0);

  // We are suppressing output anyway so dump the failures.
  for (Failure f : r.getFailures()) {
    System.out.println(f.getTrace());
  }

  Assert.assertEquals("Expected exactly one failure.", 
      1, r.getFailureCount());
  Assert.assertTrue("Expected unreferenced files assertion.", 
      r.getFailures().get(0).getTrace().contains("unreferenced files:"));
}
 
Example #6
Source File: CopyToPlannerTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Before
public void prepare() throws IOException {
    e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of())
        .addTable(TableDefinitions.USER_TABLE_DEFINITION)
        .addPartitionedTable(
            "create table parted (" +
            "   id int," +
            "   name string," +
            "   date timestamp with time zone," +
            "   obj object" +
            ") partitioned by (date) ",
            new PartitionName(new RelationName("doc", "parted"), singletonList("1395874800000")).asIndexName(),
            new PartitionName(new RelationName("doc", "parted"), singletonList("1395961200000")).asIndexName(),
            new PartitionName(new RelationName("doc", "parted"), singletonList(null)).asIndexName()
        )
        .addPartitionedTable(
            "create table parted_generated (" +
            "   ts timestamp with time zone," +
            "   day as date_trunc('day', ts)" +
            ") partitioned by (day) ",
            new PartitionName(new RelationName("doc", "parted_generated"), List.of("1395874800000")).asIndexName(),
            new PartitionName(new RelationName("doc", "parted_generated"), List.of("1395961200000")).asIndexName()
        ).build();
}
 
Example #7
Source File: UpdatePlannerTest.java    From crate with Apache License 2.0 5 votes vote down vote up
private static SQLExecutor buildExecutor(ClusterService clusterService) throws IOException {
    return SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of())
        .enableDefaultTables()
        .addPartitionedTable(
            TableDefinitions.PARTED_PKS_TABLE_DEFINITION,
            new PartitionName(new RelationName("doc", "parted_pks"), singletonList("1395874800000")).asIndexName(),
            new PartitionName(new RelationName("doc", "parted_pks"), singletonList("1395961200000")).asIndexName())
        .addPartitionedTable(TableDefinitions.TEST_EMPTY_PARTITIONED_TABLE_DEFINITION)
        .build();
}
 
Example #8
Source File: InternalTestCluster.java    From crate with Apache License 2.0 5 votes vote down vote up
public static String clusterName(String prefix, long clusterSeed) {
    StringBuilder builder = new StringBuilder(prefix);
    final int childVM = RandomizedTest.systemPropertyAsInt(SysGlobals.CHILDVM_SYSPROP_JVM_ID, 0);
    builder.append("-CHILD_VM=[").append(childVM).append(']');
    builder.append("-CLUSTER_SEED=[").append(clusterSeed).append(']');
    // if multiple maven task run on a single host we better have an identifier that doesn't rely on input params
    builder.append("-HASH=[").append(SeedUtils.formatSeed(System.nanoTime())).append(']');
    return builder.toString();
}
 
Example #9
Source File: UnionPlannerTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Before
public void setUpExecutor() throws Exception {
    e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of())
        .addTable(TableDefinitions.USER_TABLE_DEFINITION)
        .addTable(TableDefinitions.TEST_DOC_LOCATIONS_TABLE_DEFINITION)
        .build();
}
 
Example #10
Source File: TermsByQueryActionTest.java    From siren-join with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Tests that the limit for the number of terms retrieved is properly applied.
 */
@Test
public void testTermsByQueryWithLimit() throws Exception {
  createIndex("test");

  int numDocs = RandomizedTest.randomIntBetween(100, 2000);
  logger.info("--> indexing [" + numDocs + "] docs");
  for (int i = 0; i < numDocs; i++) {
    client().prepareIndex("test", "type", "" + i)
            .setSource(jsonBuilder().startObject()
                                      .field("int", i)
                                    .endObject())
            .execute().actionGet();
  }

  client().admin().indices().prepareRefresh("test").execute().actionGet();

  logger.info("--> lookup terms in field [int]");
  TermsByQueryResponse resp = new TermsByQueryRequestBuilder(client(), TermsByQueryAction.INSTANCE).setIndices("test")
                                                                      .setField("int")
                                                                      .setQuery(QueryBuilders.matchAllQuery())
                                                                      .setOrderBy(TermsByQueryRequest.Ordering.DEFAULT)
                                                                      .setMaxTermsPerShard(50)
                                                                      .setTermsEncoding(TermsByQueryRequest.TermsEncoding.LONG)
                                                                      .execute()
                                                                      .actionGet();

  int expectedMaxResultSize = this.getNumShards("test").totalNumShards * 50;
  ElasticsearchAssertions.assertNoFailures(resp);
  assertThat(resp.getEncodedTermsSet(), notNullValue());
  assertThat(resp.getSize(), lessThanOrEqualTo(expectedMaxResultSize));
  TermsSet lTerms = TermsSet.readFrom(resp.getEncodedTermsSet());
  assertThat(lTerms instanceof LongTermsSet, is(true));
}
 
Example #11
Source File: TermsByQueryActionTest.java    From siren-join with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Tests that the terms by query action returns the correct terms against integer fields
 */
@Test
public void testTermsByQueryIntegerField() throws Exception {
  createIndex("test");

  int numDocs = RandomizedTest.randomIntBetween(100, 2000);
  logger.info("--> indexing [" + numDocs + "] docs");
  for (int i = 0; i < numDocs; i++) {
    client().prepareIndex("test", "type", "" + i)
            .setSource(jsonBuilder().startObject()
                                      .field("int", i)
                                    .endObject())
            .execute().actionGet();
  }

  client().admin().indices().prepareRefresh("test").execute().actionGet();

  logger.info("--> lookup terms in field [int]");
  TermsByQueryResponse resp = new TermsByQueryRequestBuilder(client(), TermsByQueryAction.INSTANCE).setIndices("test")
                                                                      .setField("int")
                                                                      .setQuery(QueryBuilders.matchAllQuery())
                                                                      .setTermsEncoding(TermsByQueryRequest.TermsEncoding.LONG)
                                                                      .execute()
                                                                      .actionGet();

  ElasticsearchAssertions.assertNoFailures(resp);
  assertThat(resp.getEncodedTermsSet(), notNullValue());
  assertThat(resp.getSize(), is(numDocs));
  TermsSet lTerms = TermsSet.readFrom(resp.getEncodedTermsSet());
  assertThat(lTerms instanceof LongTermsSet, is(true));
  assertThat(lTerms.size(), is(numDocs));
  for (int i = 0; i < numDocs; i++) {
    assertThat(((LongTermsSet) lTerms).contains(Long.valueOf(i)), is(true));
  }
}
 
Example #12
Source File: TermsByQueryActionTest.java    From siren-join with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Tests that the terms by query action returns the correct terms against string fields
 */
@Test
public void testTermsByQueryStringField() throws Exception {
  createIndex("test");

  int numDocs = RandomizedTest.randomIntBetween(100, 2000);
  logger.info("--> indexing [" + numDocs + "] docs");
  for (int i = 0; i < numDocs; i++) {
    client().prepareIndex("test", "type", "" + i)
            .setSource(jsonBuilder().startObject()
                                      .field("str", Integer.toString(i))
                                    .endObject())
            .execute().actionGet();
  }

  client().admin().indices().prepareRefresh("test").execute().actionGet();

  logger.info("--> lookup terms in field [str]");
  TermsByQueryResponse resp = new TermsByQueryRequestBuilder(client(), TermsByQueryAction.INSTANCE).setIndices("test")
                                                                      .setField("str")
                                                                      .setQuery(QueryBuilders.matchAllQuery())
                                                                      .setTermsEncoding(TermsByQueryRequest.TermsEncoding.LONG)
                                                                      .execute()
                                                                      .actionGet();

  ElasticsearchAssertions.assertNoFailures(resp);
  assertThat(resp.getEncodedTermsSet(), notNullValue());
  assertThat(resp.getSize(), is(numDocs));
  TermsSet lTerms = NumericTermsSet.readFrom(resp.getEncodedTermsSet());
  assertThat(lTerms instanceof LongTermsSet, is(true));
  for (int i = 0; i < numDocs; i++) {
    BytesRef bytesRef = new BytesRef(Integer.toString(i));
    long termHash = LongBloomFilter.hash3_x64_128(bytesRef.bytes, bytesRef.offset, bytesRef.length, 0);
    assertThat(((LongTermsSet) lTerms).contains(termHash), is(true));
  }
}
 
Example #13
Source File: SparseHLLTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Smoke tests the multisets by adding random values.
 */
@Test
public void randomValuesTest() {
    final int log2m = 11/*arbitrary*/;
    final int regwidth = 5/*arbitrary*/;
    final int sparseThreshold = 256/*arbitrary*/;

    for(int run=0; run<100; run++) {
        final HLL hll = new HLL(log2m, regwidth, 128/*explicitThreshold, arbitrary, unused*/, sparseThreshold, HLLType.SPARSE);

        final IntByteHashMap map = new IntByteHashMap();

        for(int i=0; i<sparseThreshold; i++) {
            final long rawValue = RandomizedTest.randomLong();

            final short registerIndex = ProbabilisticTestUtil.getRegisterIndex(rawValue, log2m);
            final byte registerValue = ProbabilisticTestUtil.getRegisterValue(rawValue, log2m);
            if(map.get(registerIndex) < registerValue) {
                map.put(registerIndex, registerValue);
            }

            hll.addRaw(rawValue);
        }

        for (IntByteCursor c : map) {
            final byte expectedRegisterValue = map.get(c.key);
            assertRegisterPresent(hll, c.key, expectedRegisterValue);
        }
    }
}
 
Example #14
Source File: SSLTestConfig.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method for sanity checking if it's safe to use SSL on this JVM
 *
 * @see <a href="https://issues.apache.org/jira/browse/SOLR-12988">SOLR-12988</a>
 * @throws org.junit.internal.AssumptionViolatedException if this JVM is known to have SSL problems
 */
public static void assumeSslIsSafeToTest() {
  if (Constants.JVM_NAME.startsWith("OpenJDK") ||
      Constants.JVM_NAME.startsWith("Java HotSpot(TM)")) {
    RandomizedTest.assumeFalse("Test (or randomization for this seed) wants to use SSL, " +
                               "but SSL is known to fail on your JVM: " +
                               Constants.JVM_NAME + " / " + Constants.JVM_VERSION, 
                               isOpenJdkJvmVersionKnownToHaveProblems(Constants.JVM_VERSION));
  }
}
 
Example #15
Source File: TestByteBuffersDirectory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildIndex() throws IOException {
  try (Directory dir = getDirectory(null);
       IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(
          new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE))) {
    int docs = RandomizedTest.randomIntBetween(0, 10);
    for (int i = docs; i > 0; i--) {
      Document doc = new Document();
      doc.add(newStringField("content", English.intToEnglish(i).trim(), Field.Store.YES));
      writer.addDocument(doc);
    }
    writer.commit();
    assertEquals(docs, writer.getDocStats().numDocs);
  }
}
 
Example #16
Source File: TestFuzzyQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private String randomRealisticMultiByteUnicode(int length) {
  while (true) {
    // There is 1 single-byte unicode block, and 194 multi-byte blocks
    String value = RandomizedTest.randomRealisticUnicodeOfCodepointLength(length);
    if (value.charAt(0) > Byte.MAX_VALUE) {
      return value;
    }
  }
}
 
Example #17
Source File: TestWorstCaseTestBehavior.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Ignore
public void testProgressiveOutput() throws Exception {
  for (int i = 0; i < 20; i++) {
    System.out.println("Emitting sysout line: " + i);
    System.err.println("Emitting syserr line: " + i);
    System.out.flush();
    System.err.flush();
    RandomizedTest.sleep(1000);
  }
}
 
Example #18
Source File: TestFailIfDirectoryNotClosed.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailIfDirectoryNotClosed() {
  Result r = JUnitCore.runClasses(Nested1.class);
  RandomizedTest.assumeTrue("Ignoring nested test, very likely zombie threads present.", 
      r.getIgnoreCount() == 0);
  assertFailureCount(1, r);
  Assert.assertTrue(r.getFailures().get(0).toString().contains("Resource in scope SUITE failed to close"));
}
 
Example #19
Source File: TestRuleIgnoreAfterMaxFailures.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(final Statement s, final Description d) {
  return new Statement() {
    @Override
    public void evaluate() throws Throwable {
      int failuresSoFar = FailureMarker.getFailures();
      if (failuresSoFar >= maxFailures) {
        RandomizedTest.assumeTrue("Ignored, failures limit reached (" + 
            failuresSoFar + " >= " + maxFailures + ").", false);
      }

      s.evaluate();
    }
  };
}
 
Example #20
Source File: TestRuleLimitSysouts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * We're only interested in failing the suite if it was successful (otherwise
 * just propagate the original problem and don't bother doing anything else).
 */
@Override
protected void afterIfSuccessful() throws Throwable {
  if (isEnforced()) {
    checkCaptureStreams();

    // Flush any buffers.
    capturedSystemOut.flush();
    capturedSystemErr.flush();

    // Check for offenders, but only if everything was successful so far.
    Limit ann = RandomizedTest.getContext().getTargetClass().getAnnotation(Limit.class);
    long limit = ann.bytes();
    long hardLimit = ann.hardLimit();
    long written = bytesWritten.get();
    if (written >= limit && failureMarker.wasSuccessful()) {
      throw new AssertionError(String.format(Locale.ENGLISH, 
          "The test or suite printed %d bytes to stdout and stderr," +
          " even though the limit was set to %d bytes.%s Increase the limit with @%s, ignore it completely" +
          " with @%s or run with -Dtests.verbose=true",
          written,
          limit,
          written <= hardLimit ? "" : "Hard limit was enforced so output is truncated.",
          Limit.class.getSimpleName(),
          SuppressSysoutChecks.class.getSimpleName()));
    }
  }
}
 
Example #21
Source File: TestRuleLimitSysouts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void applyClassAnnotations() {
  Class<?> target = RandomizedTest.getContext().getTargetClass();
  if (target.isAnnotationPresent(Limit.class)) {
    Limit limitAnn = target.getAnnotation(Limit.class);
    long bytes = limitAnn.bytes();
    if (bytes < 0 || bytes > MAX_LIMIT) {
      throw new AssertionError("This sysout limit is very high: " + bytes + ". Did you want to use "
          + "@" + LuceneTestCase.SuppressSysoutChecks.class.getName() + " annotation to "
          + "avoid sysout checks entirely (this is discouraged)?");
    }

    hardLimit.set(limitAnn.hardLimit());
  }
}
 
Example #22
Source File: ESTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
public static String randomRealisticUnicodeOfCodepointLengthBetween(int minCodePoints, int maxCodePoints) {
    return RandomizedTest.randomRealisticUnicodeOfCodepointLengthBetween(minCodePoints, maxCodePoints);
}
 
Example #23
Source File: ESTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
public static String randomRealisticUnicodeOfLengthBetween(int minCodeUnits, int maxCodeUnits) {
    return RandomizedTest.randomRealisticUnicodeOfLengthBetween(minCodeUnits, maxCodeUnits);
}
 
Example #24
Source File: ESTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
public static String randomRealisticUnicodeOfLength(int codeUnits) {
    return RandomizedTest.randomRealisticUnicodeOfLength(codeUnits);
}
 
Example #25
Source File: DataTypeTesting.java    From crate with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> Supplier<T> getDataGenerator(DataType<T> type) {
    Random random = RandomizedContext.current().getRandom();
    switch (type.id()) {
        case ByteType.ID:
            return () -> (T) (Byte) (byte) random.nextInt(Byte.MAX_VALUE);
        case BooleanType.ID:
            return () -> (T) (Boolean) random.nextBoolean();

        case StringType.ID:
            return () -> (T) RandomizedTest.randomAsciiLettersOfLength(random.nextInt(10));

        case IpType.ID:
            return () -> {
                if (random.nextBoolean()) {
                    return (T) randomIPv4Address(random);
                } else {
                    return (T) randomIPv6Address(random);
                }
            };

        case DoubleType.ID:
            return () -> (T) (Double) random.nextDouble();

        case FloatType.ID:
            return () -> (T) (Float) random.nextFloat();

        case ShortType.ID:
            return () -> (T) (Short) (short) random.nextInt(Short.MAX_VALUE);

        case IntegerType.ID:
            return () -> (T) (Integer) random.nextInt();

        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return () -> (T) (Long) random.nextLong();

        case GeoPointType.ID:
            return () -> (T) new PointImpl(
                BiasedNumbers.randomDoubleBetween(random, -180, 180),
                BiasedNumbers.randomDoubleBetween(random, -90, 90),
                JtsSpatialContext.GEO
            );

        case GeoShapeType.ID:
            return () -> {
                // Can't use immutable Collections.singletonMap; insert-analyzer mutates the map
                Map<String, Object> geoShape = new HashMap<>(2);
                geoShape.put("coordinates", Arrays.asList(10.2d, 32.2d));
                geoShape.put("type", "Point");
                return (T) geoShape;
            };

        case ObjectType.ID:
            Supplier<?> innerValueGenerator = getDataGenerator(randomType());
            return () -> {
                // Can't use immutable Collections.singletonMap; insert-analyzer mutates the map
                HashMap<String, Object> map = new HashMap<>();
                map.put("x", innerValueGenerator.get());
                return (T) map;
            };
        case IntervalType.ID:
            return () -> {
                return (T) new Period().withSeconds(RandomNumbers.randomIntBetween(random, 0, Integer.MAX_VALUE));
            };

    }

    throw new AssertionError("No data generator for type " + type.getName());
}
 
Example #26
Source File: ESTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
public static String randomRealisticUnicodeOfCodepointLength(int codePoints) {
    return RandomizedTest.randomRealisticUnicodeOfCodepointLength(codePoints);
}
 
Example #27
Source File: InsertFromSubQueryPlannerTest.java    From crate with Apache License 2.0 4 votes vote down vote up
private static SQLExecutor buildExecutor(ClusterService clusterService) throws IOException {
    return SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of())
        .addTable(TableDefinitions.USER_TABLE_DEFINITION)
        .build();
}
 
Example #28
Source File: GroupByScalarPlannerTest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Before
public void prepare() throws IOException {
    e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of())
        .enableDefaultTables()
        .build();
}
 
Example #29
Source File: LimitTest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Test
public void testLimitOnLimitOperator() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of())
        .addTable(TableDefinitions.USER_TABLE_DEFINITION)
        .build();
    QueriedSelectRelation queriedDocTable = e.analyze("select name from users");

    LogicalPlan plan = Limit.create(
        Limit.create(
            Collect.create(
                ((AbstractTableRelation<?>) queriedDocTable.from().get(0)),
                queriedDocTable.outputs(),
                new WhereClause(queriedDocTable.where()),
                Set.of(),
                new TableStats(),
                null
            ),
            Literal.of(10L),
            Literal.of(5L)
        ),
        Literal.of(20L),
        Literal.of(7L)
    );
    assertThat(plan, isPlan(
        "Limit[20::bigint;7::bigint]\n" +
        "  └ Limit[10::bigint;5::bigint]\n" +
        "    └ Collect[doc.users | [name] | true]"));
    PlannerContext ctx = e.getPlannerContext(clusterService.state());
    Merge merge = (Merge) plan.build(
        ctx,
        new ProjectionBuilder(e.functions()),
        TopN.NO_LIMIT,
        0,
        null,
        null,
        Row.EMPTY,
        SubQueryResults.EMPTY
    );
    io.crate.planner.node.dql.Collect collect = (io.crate.planner.node.dql.Collect) merge.subPlan();
    assertThat(collect.collectPhase().projections(), contains(
        ProjectionMatchers.isTopN(15, 0)
    ));
    //noinspection unchecked
    assertThat(merge.mergePhase().projections(), contains(
        ProjectionMatchers.isTopN(10, 5),
        ProjectionMatchers.isTopN(20, 7)
    ));
}
 
Example #30
Source File: SelectPlannerTest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Before
public void prepare() throws IOException {
    TableStats tableStats = new TableStats();
    tableStats.updateTableStats(
        Map.of(new RelationName("doc", "users"), new Stats(20, 20, Map.of())));
    e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of())
        .addTable(TableDefinitions.USER_TABLE_DEFINITION)
        .addTable(TableDefinitions.TEST_CLUSTER_BY_STRING_TABLE_DEFINITION)
        .addTable(TableDefinitions.USER_TABLE_CLUSTERED_BY_ONLY_DEFINITION)
        .addTable(TableDefinitions.IGNORED_NESTED_TABLE_DEFINITION)
        .addTable(T3.T1_DEFINITION)
        .addTable(T3.T2_DEFINITION)
        .addTable(
            "create table doc.gc_table (" +
            "   revenue integer," +
            "   cost integer," +
            "   profit as revenue - cost" +
            ")"
        )
        .addTable(
            "create table t_pk_part_generated (" +
            "   ts timestamp with time zone," +
            "   p as date_trunc('day', ts)," +
            "   primary key (ts, p))")
        .addPartitionedTable(
            "create table parted (" +
            "   id int," +
            "   name string," +
            "   date timestamp without time zone," +
            "   obj object" +
            ") partitioned by (date) clustered into 1 shards ",
            new PartitionName(new RelationName("doc", "parted"), singletonList("1395874800000")).asIndexName(),
            new PartitionName(new RelationName("doc", "parted"), singletonList("1395961200000")).asIndexName(),
            new PartitionName(new RelationName("doc", "parted"), singletonList(null)).asIndexName()
        )
        .addPartitionedTable(
            TableDefinitions.PARTED_PKS_TABLE_DEFINITION,
            new PartitionName(new RelationName("doc", "parted_pks"), singletonList("1395874800000")).asIndexName(),
            new PartitionName(new RelationName("doc", "parted_pks"), singletonList("1395961200000")).asIndexName())
        .setTableStats(tableStats)
        .build();
}