/** * 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); } } }
/** * Perform the {@link QueryExecution} once. * * @param action * @param queryExecution * @param query * @param queryStringLog Informational string created from the initial query. * @return */ protected SPARQLResult executeQuery( HttpAction action, QueryExecution queryExecution, Query query, String queryStringLog) { setAnyTimeouts(queryExecution, action); if (query.isSelectType()) { ResultSet rs = queryExecution.execSelect(); // Force some query execution now. // If the timeout-first-row goes off, the output stream has not // been started so the HTTP error code is sent. rs.hasNext(); // If we wanted perfect query time cancellation, we could consume // the result now to see if the timeout-end-of-query goes off. // rs = ResultSetFactory.copyResults(rs) ; // action.log.info(format("[%d] exec/select", action.id)) ; return new SPARQLResult(rs); } if (query.isConstructType()) { Dataset dataset = queryExecution.execConstructDataset(); // action.log.info(format("[%d] exec/construct", action.id)); return new SPARQLResult(dataset); } if (query.isDescribeType()) { Model model = queryExecution.execDescribe(); // action.log.info(format("[%d] exec/describe", action.id)) ; return new SPARQLResult(model); } if (query.isAskType()) { boolean b = queryExecution.execAsk(); // action.log.info(format("[%d] exec/ask", action.id)) ; return new SPARQLResult(b); } ServletOps.errorBadRequest("Unknown query type - " + queryStringLog); return null; }