Search in sources :

Example 1 with NodeTypeCount

use of datawave.query.jexl.NodeTypeCount in project datawave by NationalSecurityAgency.

the class NodeTypeCountVisitorTest method testSimpleNode.

@Test
public void testSimpleNode() {
    NodeTypeCount count = (NodeTypeCount) new SimpleNode(ParserTreeConstants.JJTREFERENCE).jjtAccept(new NodeTypeCountVisitor(), null);
    assertEquals(1, count.getTotal(SimpleNode.class));
}
Also used : NodeTypeCount(datawave.query.jexl.NodeTypeCount) SimpleNode(org.apache.commons.jexl2.parser.SimpleNode) Test(org.junit.Test)

Example 2 with NodeTypeCount

use of datawave.query.jexl.NodeTypeCount in project datawave by NationalSecurityAgency.

the class NodeTypeCountVisitor method count.

/**
 * Increment the count for the specified type, and count the node's children.
 *
 * @param node
 *            the node whose children must be visited
 * @param type
 *            the type to increment the count for
 * @param data
 *            the data that possibly contains an existing count
 * @return the updated count
 */
private NodeTypeCount count(SimpleNode node, Class<? extends Node> type, Object data) {
    NodeTypeCount count = data instanceof NodeTypeCount ? (NodeTypeCount) data : new NodeTypeCount();
    count.increment(type);
    node.childrenAccept(this, count);
    return count;
}
Also used : NodeTypeCount(datawave.query.jexl.NodeTypeCount)

Example 3 with NodeTypeCount

use of datawave.query.jexl.NodeTypeCount in project datawave by NationalSecurityAgency.

the class DefaultQueryPlanner method processTree.

protected ASTJexlScript processTree(final ASTJexlScript originalQueryTree, ShardQueryConfiguration config, Query settings, MetadataHelper metadataHelper, ScannerFactory scannerFactory, QueryData queryData, QueryStopwatch timers, QueryModel queryModel) throws DatawaveQueryException {
    config.setQueryTree(originalQueryTree);
    TraceStopwatch stopwatch = null;
    if (!disableWhindexFieldMappings) {
        // apply the value-specific field mappings for GeoWave functions
        config.setQueryTree(timedApplyWhindexFieldMappings(timers, config.getQueryTree(), config, metadataHelper, settings));
    }
    if (!disableExpandIndexFunction) {
        // expand the index queries for the functions
        config.setQueryTree(timedExpandIndexQueriesForFunctions(timers, config.getQueryTree(), config, metadataHelper));
    }
    // apply the node transform rules
    // running it here before any unfielded expansions to enable potentially pushing down terms before index lookups
    config.setQueryTree(timedApplyNodeTransformRules(timers, "Apply Node Transform Rules - Pre Unfielded Expansions", config.getQueryTree(), config, metadataHelper, getTransformRules()));
    // left as a regex
    if (!disableAnyFieldLookup) {
        config.setQueryTree(timedExpandAnyFieldRegexNodes(timers, config.getQueryTree(), config, metadataHelper, scannerFactory, settings.getQuery()));
    }
    if (reduceQuery) {
        config.setQueryTree(timedReduce(timers, "Reduce Query After ANYFIELD Expansions", config.getQueryTree()));
    }
    if (!disableTestNonExistentFields) {
        timedTestForNonExistentFields(timers, config.getQueryTree(), config, metadataHelper, queryModel, settings);
    }
    // apply the node transform rules
    // running it here before any regex or range expansions to enable potentially pushing down terms before index lookups
    config.setQueryTree(timedApplyNodeTransformRules(timers, "Apply Node Transform Rules - Pre Regex/Range Expansions", config.getQueryTree(), config, metadataHelper, getTransformRules()));
    timedFetchDatatypes(timers, "Fetch Required Datatypes", config.getQueryTree(), config);
    config.setQueryTree(timedFixUnindexedNumerics(timers, config.getQueryTree(), config));
    config.setQueryTree(timedExpandMultiNormalizedTerms(timers, config.getQueryTree(), config, metadataHelper));
    // if we have any index holes, then mark em
    if (!config.getIndexHoles().isEmpty()) {
        config.setQueryTree(timedMarkIndexHoles(timers, config.getQueryTree(), config, metadataHelper));
    }
    // lets precompute the indexed fields and index only fields for the specific datatype if needed below
    Set<String> indexedFields = null;
    Set<String> indexOnlyFields = null;
    Set<String> nonEventFields = null;
    if (config.getMinSelectivity() > 0 || !disableBoundedLookup) {
        try {
            indexedFields = metadataHelper.getIndexedFields(config.getDatatypeFilter());
            indexOnlyFields = metadataHelper.getIndexOnlyFields(config.getDatatypeFilter());
            nonEventFields = metadataHelper.getNonEventFields(config.getDatatypeFilter());
        } catch (TableNotFoundException te) {
            QueryException qe = new QueryException(DatawaveErrorCode.METADATA_ACCESS_ERROR, te);
            throw new DatawaveFatalQueryException(qe);
        }
    }
    // apply the node transform rules
    config.setQueryTree(timedApplyNodeTransformRules(timers, "Apply Node Transform Rules - Pre Pushdown/Pullup Expansions", config.getQueryTree(), config, metadataHelper, getTransformRules()));
    // push down terms that are over the min selectivity
    if (config.getMinSelectivity() > 0) {
        config.setQueryTree(timedPushdownLowSelectiveTerms(timers, config.getQueryTree(), config, indexedFields, indexOnlyFields, nonEventFields));
    }
    config.setQueryTree(timedForceFieldToFieldComparison(timers, config.getQueryTree()));
    if (!disableCompositeFields) {
        config.setQueryTree(timedExpandCompositeFields(timers, config.getQueryTree(), config));
    }
    if (!disableBoundedLookup) {
        stopwatch = timers.newStartedStopwatch("DefaultQueryPlanner - Expand bounded query ranges (total)");
        // Expand any bounded ranges into a conjunction of discrete terms
        try {
            Map<String, IndexLookup> indexLookupMap = new HashMap<>();
            // Check if there is any regex to expand.
            NodeTypeCount nodeCount = NodeTypeCountVisitor.countNodes(config.getQueryTree());
            if (nodeCount.hasAny(ASTNRNode.class, ASTERNode.class)) {
                config.setQueryTree(timedExpandRegex(timers, "Expand Regex", config.getQueryTree(), config, metadataHelper, scannerFactory, indexLookupMap));
            }
            // Check if there are any bounded ranges to expand.
            if (nodeCount.isPresent(BoundedRange.class)) {
                config.setQueryTree(timedExpandRanges(timers, "Expand Ranges", config.getQueryTree(), config, metadataHelper, scannerFactory));
            }
            // NOTE: GeoWavePruningVisitor should run before QueryPruningVisitor. If it runs after, there is a chance
            // that GeoWavePruningVisitor will prune all of the remaining indexed terms, which would leave a GeoWave
            // function without any indexed terms or ranges, which should evaluate to false. That case won't be handled
            // properly if we run GeoWavePruningVisitor after QueryPruningVisitor.
            config.setQueryTree(timedPruneGeoWaveTerms(timers, config.getQueryTree(), metadataHelper));
            if (reduceQuery) {
                config.setQueryTree(timedReduce(timers, "Reduce Query After Range Expansion", config.getQueryTree()));
            }
            // Check if there are functions that can be pushed into exceeded value ranges.
            if (nodeCount.hasAll(ASTFunctionNode.class, ExceededValueThresholdMarkerJexlNode.class)) {
                config.setQueryTree(timedPushFunctions(timers, config.getQueryTree(), config, metadataHelper));
            }
            if (executableExpansion) {
                config.setQueryTree(timedExecutableExpansion(timers, config.getQueryTree(), config, metadataHelper));
            }
            List<String> debugOutput = null;
            if (log.isDebugEnabled()) {
                debugOutput = new ArrayList<>(32);
            }
            // the terms to be delayed.
            if (!ExecutableDeterminationVisitor.isExecutable(config.getQueryTree(), config, indexedFields, indexOnlyFields, nonEventFields, debugOutput, metadataHelper)) {
                // if we now have an unexecutable tree because of delayed
                // predicates, then remove delayed predicates as needed and
                // reexpand
                config.setQueryTree(timedRemoveDelayedPredicates(timers, "Remove Delayed Predicates", config.getQueryTree(), config, metadataHelper, indexedFields, indexOnlyFields, nonEventFields, indexLookupMap, scannerFactory, metadataHelper, debugOutput));
            }
            // if we now have an unexecutable tree because of missing
            // delayed predicates, then add delayed predicates where
            // possible
            config.setQueryTree(timedAddDelayedPredicates(timers, "Add Delayed Predicates", config.getQueryTree(), config, metadataHelper, indexedFields, indexOnlyFields, nonEventFields, debugOutput));
        } catch (TableNotFoundException e) {
            stopwatch.stop();
            QueryException qe = new QueryException(DatawaveErrorCode.METADATA_ACCESS_ERROR, e);
            throw new DatawaveFatalQueryException(qe);
        } catch (CannotExpandUnfieldedTermFatalException e) {
            if (null != e.getCause() && e.getCause() instanceof DoNotPerformOptimizedQueryException)
                throw (DoNotPerformOptimizedQueryException) e.getCause();
            QueryException qe = new QueryException(DatawaveErrorCode.INDETERMINATE_INDEX_STATUS, e);
            throw new DatawaveFatalQueryException(qe);
        }
        stopwatch.stop();
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Bounded range and regex conversion has been disabled");
        }
    }
    return config.getQueryTree();
}
Also used : HashMap(java.util.HashMap) CannotExpandUnfieldedTermFatalException(datawave.query.exceptions.CannotExpandUnfieldedTermFatalException) IndexLookup(datawave.query.jexl.lookups.IndexLookup) NodeTypeCount(datawave.query.jexl.NodeTypeCount) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) DatawaveQueryException(datawave.query.exceptions.DatawaveQueryException) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) DoNotPerformOptimizedQueryException(datawave.query.exceptions.DoNotPerformOptimizedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) QueryException(datawave.webservice.query.exception.QueryException) InvalidQueryException(datawave.query.exceptions.InvalidQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) TraceStopwatch(datawave.util.time.TraceStopwatch) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) DoNotPerformOptimizedQueryException(datawave.query.exceptions.DoNotPerformOptimizedQueryException)

Example 4 with NodeTypeCount

use of datawave.query.jexl.NodeTypeCount in project datawave by NationalSecurityAgency.

the class DefaultQueryPlanner method timedRemoveDelayedPredicates.

protected ASTJexlScript timedRemoveDelayedPredicates(QueryStopwatch timers, String stage, ASTJexlScript script, ShardQueryConfiguration config, MetadataHelper metadataHelper, Set<String> indexedFields, Set<String> indexOnlyFields, Set<String> nonEventFields, Map<String, IndexLookup> indexLookupMap, ScannerFactory scannerFactory, MetadataHelper helper, List<String> debugOutput) throws TableNotFoundException {
    TraceStopwatch stopwatch = timers.newStartedStopwatch("DefaultQueryPlanner - " + stage);
    config.setQueryTree((ASTJexlScript) PullupUnexecutableNodesVisitor.pullupDelayedPredicates(config.getQueryTree(), false, config, indexedFields, indexOnlyFields, nonEventFields, metadataHelper));
    if (log.isDebugEnabled()) {
        logDebug(debugOutput, "Executable state after expanding ranges:");
        logQuery(config.getQueryTree(), "Query after delayed pullup:");
    }
    boolean expandAllTerms = config.isExpandAllTerms();
    // set the expand all terms flag to avoid any more delayed
    // predicates based on cost...
    config.setExpandAllTerms(true);
    // Check if there is any regex to expand after pulling up delayed predicates.
    NodeTypeCount nodeCount = NodeTypeCountVisitor.countNodes(config.getQueryTree());
    if (nodeCount.hasAny(ASTNRNode.class, ASTERNode.class)) {
        config.setQueryTree(RegexIndexExpansionVisitor.expandRegex(config, scannerFactory, helper, indexLookupMap, config.getQueryTree()));
        if (log.isDebugEnabled()) {
            logQuery(config.getQueryTree(), "Query after expanding regex again:");
        }
    }
    // Check if there are any bounded ranges to expand.
    if (nodeCount.isPresent(BoundedRange.class)) {
        try {
            config.setQueryTree(BoundedRangeIndexExpansionVisitor.expandBoundedRanges(config, scannerFactory, metadataHelper, config.getQueryTree()));
        } catch (TableNotFoundException e) {
            QueryException qe = new QueryException(DatawaveErrorCode.METADATA_ACCESS_ERROR, e);
            throw new DatawaveFatalQueryException(qe);
        }
        if (log.isDebugEnabled()) {
            logQuery(config.getQueryTree(), "Query after expanding ranges again:");
        }
    }
    if (reduceQuery) {
        // only show pruned sections of the tree's via assignments if debug to reduce runtime when possible
        config.setQueryTree((ASTJexlScript) QueryPruningVisitor.reduce(config.getQueryTree(), showReducedQueryPrune));
        if (log.isDebugEnabled()) {
            logQuery(config.getQueryTree(), "Query after range expansion reduction again:");
        }
    }
    // Check if there are functions that can be pushed into exceeded value ranges.
    if (nodeCount.hasAll(ASTFunctionNode.class, ExceededValueThresholdMarkerJexlNode.class)) {
        config.setQueryTree(PushFunctionsIntoExceededValueRanges.pushFunctions(config.getQueryTree(), metadataHelper, config.getDatatypeFilter()));
        if (log.isDebugEnabled()) {
            logQuery(config.getQueryTree(), "Query after expanding pushing functions into exceeded value ranges again:");
        }
    }
    // Reset the original expandAllTerms value.
    config.setExpandAllTerms(expandAllTerms);
    stopwatch.stop();
    return config.getQueryTree();
}
Also used : TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) DatawaveQueryException(datawave.query.exceptions.DatawaveQueryException) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) DoNotPerformOptimizedQueryException(datawave.query.exceptions.DoNotPerformOptimizedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) QueryException(datawave.webservice.query.exception.QueryException) InvalidQueryException(datawave.query.exceptions.InvalidQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) TraceStopwatch(datawave.util.time.TraceStopwatch) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) NodeTypeCount(datawave.query.jexl.NodeTypeCount)

Aggregations

NodeTypeCount (datawave.query.jexl.NodeTypeCount)4 DatawaveFatalQueryException (datawave.query.exceptions.DatawaveFatalQueryException)2 DatawaveQueryException (datawave.query.exceptions.DatawaveQueryException)2 DoNotPerformOptimizedQueryException (datawave.query.exceptions.DoNotPerformOptimizedQueryException)2 InvalidQueryException (datawave.query.exceptions.InvalidQueryException)2 TraceStopwatch (datawave.util.time.TraceStopwatch)2 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)2 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)2 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)2 QueryException (datawave.webservice.query.exception.QueryException)2 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)2 CannotExpandUnfieldedTermFatalException (datawave.query.exceptions.CannotExpandUnfieldedTermFatalException)1 IndexLookup (datawave.query.jexl.lookups.IndexLookup)1 HashMap (java.util.HashMap)1 SimpleNode (org.apache.commons.jexl2.parser.SimpleNode)1 Test (org.junit.Test)1