Search in sources :

Example 31 with QueryPlan

use of datawave.query.planner.QueryPlan in project datawave by NationalSecurityAgency.

the class RangeStreamTest method testShardAndDaysHints6.

@Test
public void testShardAndDaysHints6() throws Exception {
    String originalQuery = "(FOO == 'oreo') && ((filter:include(FOO, 'tardy') && (SHARDS_AND_DAYS = '20190312,20190313,20190314')) || (filter:include(FOO, 'bardy') && (SHARDS_AND_DAYS = '20190312,20190313,20190314')) )";
    ASTJexlScript script = JexlASTHelper.parseJexlQuery(originalQuery);
    config.setBeginDate(new Date(0));
    config.setEndDate(new Date(System.currentTimeMillis()));
    Multimap<String, Type<?>> dataTypes = HashMultimap.create();
    dataTypes.putAll("FOO", Sets.newHashSet(new LcNoDiacriticsType()));
    dataTypes.putAll("NUM", Sets.newHashSet(new NumberType()));
    config.setQueryFieldsDatatypes(dataTypes);
    config.setIndexedFields(dataTypes);
    MockMetadataHelper helper = new MockMetadataHelper();
    helper.setIndexedFields(dataTypes.keySet());
    Set<Range> expectedRanges = Sets.newHashSet();
    for (String shard : Lists.newArrayList("20190314_1")) {
        expectedRanges.add(makeTestRange(shard, "datatype1\u0000123"));
        expectedRanges.add(makeTestRange(shard, "datatype1\u0000345"));
    }
    for (QueryPlan queryPlan : new RangeStream(config, new ScannerFactory(config.getConnector()), helper).streamPlans(script)) {
        // verify the query plan dropped no terms
        JexlNode queryTree = JexlASTHelper.parseJexlQuery(queryPlan.getQueryString());
        JexlNode expectedTree = JexlASTHelper.parseJexlQuery("(((SHARDS_AND_DAYS = '20190314') && filter:include(FOO, 'tardy')) || ((SHARDS_AND_DAYS = '20190314') && filter:include(FOO, 'bardy'))) && FOO == 'oreo'");
        JexlNodeAssert.assertThat(queryTree).isEqualTo(expectedTree);
        // verify the range
        for (Range range : queryPlan.getRanges()) {
            assertTrue("Tried to remove unexpected range " + range.toString() + " from expected ranges: " + expectedRanges.toString(), expectedRanges.remove(range));
        }
    }
    assertTrue(expectedRanges.size() + " expected ranges not found in query plan: " + expectedRanges.toString(), expectedRanges.isEmpty());
}
Also used : ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) RangeFactoryForTests.makeTestRange(datawave.common.test.utils.query.RangeFactoryForTests.makeTestRange) Range(org.apache.accumulo.core.data.Range) QueryPlan(datawave.query.planner.QueryPlan) ScannerFactory(datawave.query.tables.ScannerFactory) LcNoDiacriticsType(datawave.data.type.LcNoDiacriticsType) MockMetadataHelper(datawave.query.util.MockMetadataHelper) NoOpType(datawave.data.type.NoOpType) NumberType(datawave.data.type.NumberType) LcNoDiacriticsType(datawave.data.type.LcNoDiacriticsType) Type(datawave.data.type.Type) NumberType(datawave.data.type.NumberType) JexlNode(org.apache.commons.jexl2.parser.JexlNode) Test(org.junit.Test)

Example 32 with QueryPlan

use of datawave.query.planner.QueryPlan in project datawave by NationalSecurityAgency.

the class RangeStreamTestX method runTest.

private void runTest(RangeStream rangeStream, ASTJexlScript script, List<Range> expectedRanges, List<String> expectedQueries) throws Exception {
    CloseableIterable<QueryPlan> queryPlans = rangeStream.streamPlans(script);
    assertEquals(IndexStream.StreamContext.PRESENT, rangeStream.context());
    Iterator<Range> shardIter = expectedRanges.iterator();
    Iterator<String> queryIter = expectedQueries.iterator();
    // Should have one range per query plan
    int counter = 0;
    for (QueryPlan queryPlan : queryPlans) {
        // Assert proper range
        Iterator<Range> rangeIter = queryPlan.getRanges().iterator();
        Range planRange = rangeIter.next();
        Range expectedRange = shardIter.next();
        assertEquals("Query produced unexpected range: " + planRange.toString(), expectedRange, planRange);
        assertFalse("Query plan had more than one range!", rangeIter.hasNext());
        // Assert proper query string for this range.
        ASTJexlScript expectedScript = JexlASTHelper.parseJexlQuery(queryIter.next());
        ASTJexlScript planScript = JexlNodeFactory.createScript(queryPlan.getQueryTree());
        String expectedString = JexlStringBuildingVisitor.buildQuery(expectedScript);
        String plannedString = JexlStringBuildingVisitor.buildQuery(planScript);
        // Re-parse to avoid weird cases of DelayedPredicates
        expectedScript = JexlASTHelper.parseJexlQuery(expectedString);
        planScript = JexlASTHelper.parseJexlQuery(plannedString);
        assertTrue("Queries did not match for counter: " + counter + " on shard: " + planRange.toString() + "\nExpected: " + expectedString + "\nActual  : " + plannedString, TreeEqualityVisitor.isEqual(expectedScript, planScript));
        counter++;
    }
    // Ensure we didn't miss any expected ranges or queries
    if (shardIter.hasNext())
        fail("Expected ranges still exist after test: " + shardIter.next());
    if (queryIter.hasNext())
        fail("Expected queries still exist after test: " + queryIter.next());
}
Also used : ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) QueryPlan(datawave.query.planner.QueryPlan) RangeFactoryForTests.makeDayRange(datawave.common.test.utils.query.RangeFactoryForTests.makeDayRange) RangeFactoryForTests.makeTestRange(datawave.common.test.utils.query.RangeFactoryForTests.makeTestRange) RangeFactoryForTests.makeShardedRange(datawave.common.test.utils.query.RangeFactoryForTests.makeShardedRange) Range(org.apache.accumulo.core.data.Range)

Aggregations

QueryPlan (datawave.query.planner.QueryPlan)32 Range (org.apache.accumulo.core.data.Range)31 RangeFactoryForTests.makeTestRange (datawave.common.test.utils.query.RangeFactoryForTests.makeTestRange)26 ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)26 LcNoDiacriticsType (datawave.data.type.LcNoDiacriticsType)25 NoOpType (datawave.data.type.NoOpType)25 NumberType (datawave.data.type.NumberType)25 Type (datawave.data.type.Type)25 ScannerFactory (datawave.query.tables.ScannerFactory)25 MockMetadataHelper (datawave.query.util.MockMetadataHelper)25 Test (org.junit.Test)24 SimpleDateFormat (java.text.SimpleDateFormat)8 JexlNode (org.apache.commons.jexl2.parser.JexlNode)3 ArrayList (java.util.ArrayList)2 RangeFactoryForTests.makeDayRange (datawave.common.test.utils.query.RangeFactoryForTests.makeDayRange)1 RangeFactoryForTests.makeShardedRange (datawave.common.test.utils.query.RangeFactoryForTests.makeShardedRange)1 JexlASTHelper (datawave.query.jexl.JexlASTHelper)1 SessionOptions (datawave.query.tables.SessionOptions)1 ScannerChunk (datawave.query.tables.async.ScannerChunk)1 HashMap (java.util.HashMap)1