/** * Tests whether an algebra expression gives the same results when run both with and without a * given optimizer * * @param algStr Algebra * @param ds Dataset * @param opt Optimizer * @param expected Expected number of results */ public static void testAsAlgebra(String algStr, Dataset ds, Symbol opt, int expected) { Op op = SSE.parseOp(algStr); List<String> vars = new ArrayList<>(); for (Var v : OpVars.visibleVars(op)) { vars.add(v.getName()); } // Track current state boolean isEnabled = ARQ.isTrue(opt); boolean isDisabled = ARQ.isFalse(opt); try { // Run first without optimization ARQ.set(opt, false); QueryEngineMain engine = new QueryEngineMain(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext()); QueryIterator iter = engine.eval(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext()); ResultSetRewindable rs = ResultSetFactory.makeRewindable( new ResultSetStream(vars, ModelFactory.createDefaultModel(), iter)); 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()); iter.close(); // Run with optimization ARQ.set(opt, true); engine = new QueryEngineMain(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext()); QueryIterator iterOpt = engine.eval(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext()); ResultSetRewindable rsOpt = ResultSetFactory.makeRewindable( new ResultSetStream(vars, ModelFactory.createDefaultModel(), iterOpt)); 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()); iterOpt.close(); 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); } } }
private void buildHashTable(QueryIterator iter1) { state = Phase.HASH; for (; iter1.hasNext(); ) { Binding row1 = iter1.next(); s_countProbe++; hashTable.put(row1); } iter1.close(); state = Phase.STREAM; }
@Override protected void closeSubIterator() { if (JoinLib.JOIN_EXPLAIN) { String x = String.format( "HashJoin: LHS=%d RHS=%d Results=%d RightMisses=%d MaxBucket=%d NoKeyBucket=%d", s_countProbe, s_countScan, s_countResults, hashTable.s_countScanMiss, hashTable.s_maxBucketSize, hashTable.s_noKeyBucketSize); System.out.println(x); } // In case it's a peek iterator. iterStream.close(); hashTable.clear(); }
public QueryIteratorLogging(QueryIterator input) { super(input); log = LoggerFactory.getLogger(input.getClass()); }
private static List<Binding> toList(QueryIterator qIter) { List<Binding> x = new ArrayList<>(); for (; qIter.hasNext(); ) x.add(qIter.nextBinding()); return x; }
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; } } }