Search in sources :

Example 21 with ASTEQNode

use of org.apache.commons.jexl2.parser.ASTEQNode in project datawave-query-metric-service by NationalSecurityAgency.

the class BaseQueryMetricHandler method populateMetricSelectors.

public void populateMetricSelectors(T queryMetric) {
    String type = queryMetric.getQueryType();
    Lifecycle lifecycle = queryMetric.getLifecycle();
    // this is time consuming - we only need to parse the query and write the selectors once
    if (lifecycle.equals(Lifecycle.DEFINED) && type != null && type.equalsIgnoreCase("RunningQuery") && queryMetric.getPositiveSelectors() == null && queryMetric.getNegativeSelectors() == null) {
        try {
            String query = queryMetric.getQuery();
            if (query != null) {
                ASTJexlScript jexlScript = null;
                try {
                    // Parse and flatten here before visitors visit.
                    jexlScript = JexlASTHelper.parseAndFlattenJexlQuery(query);
                } catch (Exception e) {
                    // not JEXL, try LUCENE
                    QueryNode node = this.luceneToJexlQueryParser.parse(query);
                    String jexlQuery = node.getOriginalQuery();
                    jexlScript = JexlASTHelper.parseAndFlattenJexlQuery(jexlQuery);
                }
                if (jexlScript != null) {
                    jexlScript = TreeFlatteningRebuildingVisitor.flatten(jexlScript);
                    List<ASTEQNode> positiveEQNodes = JexlASTHelper.getPositiveEQNodes(jexlScript);
                    List<String> positiveSelectors = new ArrayList<>();
                    for (ASTEQNode pos : positiveEQNodes) {
                        String identifier = JexlASTHelper.getIdentifier(pos);
                        Object literal = JexlASTHelper.getLiteralValue(pos);
                        if (identifier != null && literal != null) {
                            positiveSelectors.add(identifier + ":" + literal);
                        }
                    }
                    if (!positiveSelectors.isEmpty()) {
                        queryMetric.setPositiveSelectors(positiveSelectors);
                    }
                    List<ASTEQNode> negativeEQNodes = JexlASTHelper.getNegativeEQNodes(jexlScript);
                    List<String> negativeSelectors = new ArrayList<>();
                    for (ASTEQNode neg : negativeEQNodes) {
                        String identifier = JexlASTHelper.getIdentifier(neg);
                        Object literal = JexlASTHelper.getLiteralValue(neg);
                        if (identifier != null && literal != null) {
                            negativeSelectors.add(identifier + ":" + literal);
                        }
                    }
                    if (!negativeSelectors.isEmpty()) {
                        queryMetric.setNegativeSelectors(negativeSelectors);
                    }
                }
            }
        } catch (Exception e) {
            log.error("populateMetricSelectors: " + e.getMessage());
        }
    }
}
Also used : QueryNode(datawave.query.language.tree.QueryNode) ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) Lifecycle(datawave.microservice.querymetric.BaseQueryMetric.Lifecycle) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 22 with ASTEQNode

use of org.apache.commons.jexl2.parser.ASTEQNode in project datawave by NationalSecurityAgency.

the class FindLiteralsAndPatternsVisitor method visit.

@Override
public Object visit(ASTStringLiteral node, Object data) {
    // strings are always wrapped in a reference node, so the op is the gparent
    JexlNode op = node.jjtGetParent().jjtGetParent();
    if (op instanceof ASTEQNode) {
        JexlNode field = JexlNodes.otherChild(op, node.jjtGetParent()).jjtGetChild(0);
        if (log.isTraceEnabled()) {
            log.trace("Found field " + JexlASTHelper.deconstructIdentifier(field.image) + "==" + node.getLiteral());
        }
        values.addLiteral(node.getLiteral(), JexlASTHelper.deconstructIdentifier(field.image));
    } else if (op instanceof ASTERNode) {
        JexlNode field = JexlNodes.otherChild(op, node.jjtGetParent()).jjtGetChild(0);
        values.addPattern(node.getLiteral(), JexlASTHelper.deconstructIdentifier(field.image));
    }
    return null;
}
Also used : ASTERNode(org.apache.commons.jexl2.parser.ASTERNode) ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode)

Example 23 with ASTEQNode

use of org.apache.commons.jexl2.parser.ASTEQNode in project datawave by NationalSecurityAgency.

the class CompositeRange method contains.

public boolean contains(JexlNode node) {
    boolean success;
    LiteralRange range = JexlASTHelper.findRange().getRange(node);
    if (range != null)
        success = this.jexlNodeListLowerBound.contains(range.getLowerNode()) && this.jexlNodeListUpperBound.contains(range.getUpperNode());
    else if (node instanceof ASTEQNode)
        success = this.jexlNodeList.contains(node);
    else
        success = this.jexlNodeListLowerBound.contains(node) || this.jexlNodeListUpperBound.contains(node);
    return success;
}
Also used : ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) LiteralRange(datawave.query.jexl.LiteralRange)

Example 24 with ASTEQNode

use of org.apache.commons.jexl2.parser.ASTEQNode in project datawave by NationalSecurityAgency.

the class PushdownLargeFieldedListsVisitor method assignNodeByField.

protected void assignNodeByField(JexlNode origNode, JexlNode subNode, Multimap<String, JexlNode> eqNodes, Multimap<String, JexlNode> rangeNodes, List<JexlNode> otherNodes) {
    QueryPropertyMarker.Instance instance = QueryPropertyMarker.findInstance(subNode);
    if (subNode instanceof ASTEQNode) {
        String identifier = JexlASTHelper.getIdentifier(subNode, false);
        if (identifier != null) {
            eqNodes.put(JexlASTHelper.getIdentifier(subNode, false), origNode);
        } else {
            otherNodes.add(origNode);
        }
    } else if (instance.isType(ExceededValueThresholdMarkerJexlNode.class)) {
        assignNodeByField(origNode, instance.getSource(), eqNodes, rangeNodes, otherNodes);
    } else if (instance.isType(BoundedRange.class)) {
        LiteralRange range = JexlASTHelper.findRange().getRange(subNode);
        rangeNodes.put(JexlASTHelper.rebuildIdentifier(range.getFieldName()), origNode);
    } else if ((subNode.jjtGetNumChildren() == 1) && (subNode instanceof ASTReferenceExpression || subNode instanceof ASTReference || subNode instanceof ASTAndNode)) {
        assignNodeByField(origNode, subNode.jjtGetChild(0), eqNodes, rangeNodes, otherNodes);
    } else {
        otherNodes.add(origNode);
    }
}
Also used : ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) QueryPropertyMarker(datawave.query.jexl.nodes.QueryPropertyMarker) ExceededValueThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode) LiteralRange(datawave.query.jexl.LiteralRange) ASTReference(org.apache.commons.jexl2.parser.ASTReference) ASTAndNode(org.apache.commons.jexl2.parser.ASTAndNode)

Example 25 with ASTEQNode

use of org.apache.commons.jexl2.parser.ASTEQNode in project datawave by NationalSecurityAgency.

the class GeoWavePruningVisitor method visit.

@Override
public Object visit(ASTEQNode node, Object data) {
    if (data instanceof Multimap) {
        Multimap<String, Geometry> fieldToGeometryMap = (Multimap<String, Geometry>) data;
        String field = JexlASTHelper.getIdentifier(node);
        Collection<Geometry> queryGeometries = fieldToGeometryMap.get(field);
        if (queryGeometries != null && !queryGeometries.isEmpty()) {
            String value = (String) JexlASTHelper.getLiteralValue(node);
            if (value != null) {
                Geometry nodeGeometry = GeoWaveUtils.positionToGeometry(value);
                // if the node geometry doesn't intersect the query geometry, get rid of this node
                if (fieldToGeometryMap.get(field).stream().noneMatch(nodeGeometry::intersects)) {
                    if (prunedTerms != null) {
                        prunedTerms.put(field, value);
                    }
                    return new ASTFalseNode(ParserTreeConstants.JJTFALSENODE);
                }
            }
        }
    }
    return super.visit(node, data);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) ASTFalseNode(org.apache.commons.jexl2.parser.ASTFalseNode)

Aggregations

ASTEQNode (org.apache.commons.jexl2.parser.ASTEQNode)43 JexlNode (org.apache.commons.jexl2.parser.JexlNode)25 Test (org.junit.Test)19 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)11 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)11 ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)10 ASTERNode (org.apache.commons.jexl2.parser.ASTERNode)7 ASTNENode (org.apache.commons.jexl2.parser.ASTNENode)7 ASTReference (org.apache.commons.jexl2.parser.ASTReference)7 LiteralRange (datawave.query.jexl.LiteralRange)6 ArrayList (java.util.ArrayList)6 ASTGENode (org.apache.commons.jexl2.parser.ASTGENode)6 ASTGTNode (org.apache.commons.jexl2.parser.ASTGTNode)6 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)6 ASTLENode (org.apache.commons.jexl2.parser.ASTLENode)6 ASTLTNode (org.apache.commons.jexl2.parser.ASTLTNode)6 ASTOrNode (org.apache.commons.jexl2.parser.ASTOrNode)6 ExceededTermThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode)5 ASTNRNode (org.apache.commons.jexl2.parser.ASTNRNode)5 ASTFunctionNode (org.apache.commons.jexl2.parser.ASTFunctionNode)4