Beispiel #1
0
 private static ResultSet exec(String pattern, Graph graph) {
   Op op = SSE.parseOp(pattern, pmap);
   List<Var> vars = new ArrayList<>();
   vars.addAll(OpVars.visibleVars(op));
   QueryIterator qIter = Algebra.exec(op, graph);
   return ResultSetFactory.create(qIter, Var.varNames(vars));
 }
  /**
   * Tests whether a query gives the same results when run both with and without a given optimizer
   *
   * @param queryStr Query
   * @param ds Dataset
   * @param opt Optimizer
   * @param expected Expected number of results
   */
  public static void test(String queryStr, Dataset ds, Symbol opt, int expected) {
    Query q = QueryFactory.create(queryStr);

    if (!q.isSelectType()) Assert.fail("Only SELECT queries are testable with this method");

    Op op = Algebra.compile(q);
    // Track current state
    boolean isEnabled = ARQ.isTrue(opt);
    boolean isDisabled = ARQ.isFalse(opt);

    try {
      // Run first without optimization
      ARQ.set(opt, false);
      ResultSetRewindable rs;
      try (QueryExecution qe = QueryExecutionFactory.create(q, ds)) {
        rs = ResultSetFactory.makeRewindable(qe.execSelect());
        if (expected != rs.size()) {
          System.err.println("Non-optimized results not as expected");
          TextOutput output = new TextOutput((SerializationContext) null);
          output.format(System.out, rs);
          rs.reset();
        }
        Assert.assertEquals(expected, rs.size());
      }

      // Run with optimization
      ARQ.set(opt, true);
      ResultSetRewindable rsOpt;
      try (QueryExecution qeOpt = QueryExecutionFactory.create(q, ds)) {
        rsOpt = ResultSetFactory.makeRewindable(qeOpt.execSelect());
        if (expected != rsOpt.size()) {
          System.err.println("Optimized results not as expected");
          TextOutput output = new TextOutput((SerializationContext) null);
          output.format(System.out, rsOpt);
          rsOpt.reset();
        }
        Assert.assertEquals(expected, rsOpt.size());
      }
      Assert.assertTrue(ResultSetCompare.isomorphic(rs, rsOpt));
    } finally {
      // Restore previous state
      if (isEnabled) {
        ARQ.set(opt, true);
      } else if (isDisabled) {
        ARQ.set(opt, false);
      } else {
        ARQ.unset(opt);
      }
    }
  }
  protected Binding moveToNextBindingOrNull() {
    // iterCurrent is the iterator of entries in the
    // probe hashed table for the current stream row.
    // iterStream is the stream of incoming rows.

    switch (state) {
      case DONE:
        return null;
      case HASH:
      case INIT:
        throw new IllegalStateException();
      case TRAILER:
        return doOneTail();
      case STREAM:
    }

    for (; ; ) {
      // Ensure we are processing a row.
      while (iterCurrent == null) {
        // Move on to the next row from the right.
        if (!iterStream.hasNext()) {
          state = Phase.TRAILER;
          iterTail = joinFinished();
          if (iterTail != null) return doOneTail();
          return null;
        }
        rowStream = iterStream.next();
        s_countScan++;
        iterCurrent = hashTable.getCandidates(rowStream);
        yielded = false;
      }

      // Emit one row using the rightRow and the current matched left rows.
      if (!iterCurrent.hasNext()) {
        iterCurrent = null;
        if (!yielded) {
          Binding b = noYieldedRows(rowStream);
          if (b != null) {
            s_countScan++;
            return b;
          }
        }
        continue;
      }

      // Nested loop join, only on less.
      // Iterator<Binding> iter = nestedLoop(iterCurrent, rowStream) ;

      Binding rowCurrentProbe = iterCurrent.next();
      Binding r = Algebra.merge(rowCurrentProbe, rowStream);
      Binding r2 = null;

      if (r != null) r2 = yieldOneResult(rowCurrentProbe, rowStream, r);
      if (r2 == null) {
        // Reject
      } else {
        yielded = true;
        s_countResults++;
        return r2;
      }
    }
  }