Search in sources :

Example 1 with ASTEQNode

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

the class CompositeRange method addComponent.

@Override
public void addComponent(JexlNode node) {
    List<JexlNode> nodes = new ArrayList<>();
    LiteralRange range = JexlASTHelper.findRange().getRange(node);
    if (range != null) {
        nodes.add(range.getLowerNode());
        nodes.add(range.getUpperNode());
    } else if (node instanceof ASTEQNode) {
        String fieldName = JexlASTHelper.getIdentifier(node);
        String expression = JexlASTHelper.getLiteralValue(node).toString();
        nodes.add(JexlNodeFactory.buildNode((ASTGENode) null, fieldName, expression));
        nodes.add(JexlNodeFactory.buildNode((ASTLENode) null, fieldName, expression));
    } else {
        nodes.add(node);
    }
    jexlNodeList.add(node);
    fieldNameList.add(JexlASTHelper.getIdentifier(nodes.get(0)));
    for (JexlNode boundNode : nodes) {
        String expression = JexlASTHelper.getLiteralValue(boundNode).toString();
        if (boundNode instanceof ASTGENode || boundNode instanceof ASTGTNode) {
            jexlNodeListLowerBound.add(boundNode);
            expressionListLowerBound.add(expression);
            if (nodes.size() < 2) {
                jexlNodeListUpperBound.add(null);
                expressionListUpperBound.add(null);
            }
        } else if (boundNode instanceof ASTLENode || boundNode instanceof ASTLTNode) {
            jexlNodeListUpperBound.add(boundNode);
            expressionListUpperBound.add(expression);
            if (nodes.size() < 2) {
                jexlNodeListLowerBound.add(null);
                expressionListLowerBound.add(null);
            }
        }
    }
}
Also used : ASTLENode(org.apache.commons.jexl2.parser.ASTLENode) ASTGENode(org.apache.commons.jexl2.parser.ASTGENode) ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) ArrayList(java.util.ArrayList) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTGTNode(org.apache.commons.jexl2.parser.ASTGTNode) LiteralRange(datawave.query.jexl.LiteralRange) ASTLTNode(org.apache.commons.jexl2.parser.ASTLTNode)

Example 2 with ASTEQNode

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

the class ExpandCompositeTerms method createCompositeNode.

/**
 * Attempts to form a jexl node from the composite
 *
 * @param composite
 *            A list of composites from which jexl nodes should be created
 * @return A list of jexl nodes created from the given composite
 */
private JexlNode createCompositeNode(Composite composite) {
    List<Class<? extends JexlNode>> nodeClasses = new ArrayList<>();
    List<String> appendedExpressions = new ArrayList<>();
    boolean includeOldData = false;
    if (config.getCompositeTransitionDates().containsKey(composite.getCompositeName())) {
        Date transitionDate = config.getCompositeTransitionDates().get(composite.getCompositeName());
        if (config.getBeginDate().compareTo(transitionDate) < 0)
            includeOldData = true;
    }
    composite.getNodesAndExpressions(nodeClasses, appendedExpressions, config.getFieldToDiscreteIndexTypes(), includeOldData);
    // if this is true, then it indicates that we are dealing with a query containing an overloaded composite
    // field which only contained the first component term. This means that we are running a query against
    // the base composite term, and thus need to expand our ranges to fully include both the composite and
    // non-composite events in our range.
    boolean expandRangeForBaseTerm = CompositeIngest.isOverloadedCompositeField(config.getCompositeToFieldMap(), composite.getCompositeName()) && composite.getJexlNodeList().size() == 1;
    DiscreteIndexType<?> baseTermDiscreteIndexType = config.getFieldToDiscreteIndexTypes().get(composite.getFieldNameList().get(0));
    List<JexlNode> finalNodes = new ArrayList<>();
    for (int i = 0; i < nodeClasses.size(); i++) {
        Class<? extends JexlNode> nodeClass = nodeClasses.get(i);
        String appendedExpression = appendedExpressions.get(i);
        JexlNode newNode = null;
        if (nodeClass.equals(ASTGTNode.class)) {
            if (expandRangeForBaseTerm)
                newNode = JexlNodeFactory.buildNode((ASTGENode) null, composite.getCompositeName(), CompositeUtils.getInclusiveLowerBound(appendedExpression, baseTermDiscreteIndexType));
            else
                newNode = JexlNodeFactory.buildNode((ASTGTNode) null, composite.getCompositeName(), appendedExpression);
        } else if (nodeClass.equals(ASTGENode.class)) {
            newNode = JexlNodeFactory.buildNode((ASTGENode) null, composite.getCompositeName(), appendedExpression);
        } else if (nodeClass.equals(ASTLTNode.class)) {
            newNode = JexlNodeFactory.buildNode((ASTLTNode) null, composite.getCompositeName(), appendedExpression);
        } else if (nodeClass.equals(ASTLENode.class)) {
            if (expandRangeForBaseTerm)
                newNode = JexlNodeFactory.buildNode((ASTLTNode) null, composite.getCompositeName(), CompositeUtils.getExclusiveUpperBound(appendedExpression, baseTermDiscreteIndexType));
            else
                newNode = JexlNodeFactory.buildNode((ASTLENode) null, composite.getCompositeName(), appendedExpression);
        } else if (nodeClass.equals(ASTERNode.class)) {
            newNode = JexlNodeFactory.buildERNode(composite.getCompositeName(), appendedExpression);
        } else if (nodeClass.equals(ASTNENode.class)) {
            newNode = JexlNodeFactory.buildNode((ASTNENode) null, composite.getCompositeName(), appendedExpression);
        } else if (nodeClass.equals(ASTEQNode.class)) {
            // if this is for an overloaded composite field, which only includes the base term, convert to range
            if (expandRangeForBaseTerm) {
                JexlNode lowerBound = JexlNodeFactory.buildNode((ASTGENode) null, composite.getCompositeName(), appendedExpression);
                JexlNode upperBound = JexlNodeFactory.buildNode((ASTLTNode) null, composite.getCompositeName(), CompositeUtils.getExclusiveUpperBound(appendedExpression, baseTermDiscreteIndexType));
                newNode = createUnwrappedAndNode(Arrays.asList(lowerBound, upperBound));
            } else {
                newNode = JexlNodeFactory.buildEQNode(composite.getCompositeName(), appendedExpression);
            }
        } else {
            log.error("Invalid or unknown node type for composite map.");
        }
        finalNodes.add(newNode);
    }
    JexlNode finalNode;
    if (finalNodes.size() > 1) {
        finalNode = createUnwrappedAndNode(finalNodes);
        if (composite.getJexlNodeList().size() > 1) {
            JexlNode delayedNode = ASTEvaluationOnly.create(createUnwrappedAndNode(composite.getJexlNodeList().stream().map(node -> JexlNodeFactory.wrap(copy(node))).collect(Collectors.toList())));
            finalNode = createUnwrappedAndNode(Arrays.asList(JexlNodeFactory.wrap(finalNode), delayedNode));
        }
    } else {
        finalNode = finalNodes.get(0);
        if (composite.getJexlNodeList().size() > 1 && !(finalNode instanceof ASTEQNode)) {
            JexlNode delayedNode = ASTEvaluationOnly.create(createUnwrappedAndNode(composite.getJexlNodeList().stream().map(node -> JexlNodeFactory.wrap(copy(node))).collect(Collectors.toList())));
            finalNode = createUnwrappedAndNode(Arrays.asList(finalNode, delayedNode));
        }
    }
    if (!CompositeIngest.isOverloadedCompositeField(config.getCompositeToFieldMap(), composite.getCompositeName())) {
        config.getIndexedFields().add(composite.getCompositeName());
        config.getQueryFieldsDatatypes().put(composite.getCompositeName(), new NoOpType());
    }
    // save a mapping of generated composites to their component parts for later processing
    jexlNodeToCompMap.put(finalNode, composite);
    return finalNode;
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ASTLTNode(org.apache.commons.jexl2.parser.ASTLTNode) Arrays(java.util.Arrays) Date(java.util.Date) JexlNodeFactory(datawave.query.jexl.JexlNodeFactory) Logger(org.apache.log4j.Logger) ASTNRNode(org.apache.commons.jexl2.parser.ASTNRNode) ASTNotNode(org.apache.commons.jexl2.parser.ASTNotNode) ASTGTNode(org.apache.commons.jexl2.parser.ASTGTNode) Map(java.util.Map) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) ThreadConfigurableLogger(datawave.webservice.common.logging.ThreadConfigurableLogger) CompositeUtils(datawave.query.composite.CompositeUtils) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) CompositeRange(datawave.query.composite.CompositeRange) ASTDelayedPredicate(org.apache.commons.jexl2.parser.ASTDelayedPredicate) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) ASTNENode(org.apache.commons.jexl2.parser.ASTNENode) List(java.util.List) ASTOrNode(org.apache.commons.jexl2.parser.ASTOrNode) ShardQueryConfiguration(datawave.query.config.ShardQueryConfiguration) DiscreteIndexType(datawave.data.type.DiscreteIndexType) Entry(java.util.Map.Entry) JexlNodes.children(org.apache.commons.jexl2.parser.JexlNodes.children) CompositeTerm(datawave.query.composite.CompositeTerm) ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) JexlASTHelper(datawave.query.jexl.JexlASTHelper) ASTLENode(org.apache.commons.jexl2.parser.ASTLENode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) NoOpType(datawave.data.type.NoOpType) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ASTERNode(org.apache.commons.jexl2.parser.ASTERNode) Lists(com.google.common.collect.Lists) LiteralRange(datawave.query.jexl.LiteralRange) ASTAndNode(org.apache.commons.jexl2.parser.ASTAndNode) CompositeIngest(datawave.ingest.data.config.ingest.CompositeIngest) BoundedRange(datawave.query.jexl.nodes.BoundedRange) QueryPropertyMarker(datawave.query.jexl.nodes.QueryPropertyMarker) Composite(datawave.query.composite.Composite) DatawaveErrorCode(datawave.webservice.query.exception.DatawaveErrorCode) ASTGENode(org.apache.commons.jexl2.parser.ASTGENode) QueryException(datawave.webservice.query.exception.QueryException) ASTEvaluationOnly(org.apache.commons.jexl2.parser.ASTEvaluationOnly) Preconditions(com.google.common.base.Preconditions) ASTFunctionNode(org.apache.commons.jexl2.parser.ASTFunctionNode) ASTReference(org.apache.commons.jexl2.parser.ASTReference) ASTNENode(org.apache.commons.jexl2.parser.ASTNENode) ASTGENode(org.apache.commons.jexl2.parser.ASTGENode) ArrayList(java.util.ArrayList) NoOpType(datawave.data.type.NoOpType) Date(java.util.Date) ASTLTNode(org.apache.commons.jexl2.parser.ASTLTNode) ASTLENode(org.apache.commons.jexl2.parser.ASTLENode) ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode)

Example 3 with ASTEQNode

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

the class WhindexVisitor method isFieldValueMatch.

private boolean isFieldValueMatch(JexlNode node, String mappableField) {
    boolean isFieldValueMatch = false;
    JexlNode dereferencedNode = JexlASTHelper.dereference(node);
    if (dereferencedNode instanceof ASTEQNode) {
        ASTEQNode eqNode = (ASTEQNode) dereferencedNode;
        String field = getStringField(eqNode);
        String value = getStringValue(eqNode);
        if (value != null) {
            value = value.toLowerCase();
        }
        // if this is a term/value match
        isFieldValueMatch = mappingFields.contains(field) && valueSpecificFieldMappings.containsKey(value) && valueSpecificFieldMappings.get(value).containsKey(mappableField);
    }
    return isFieldValueMatch;
}
Also used : ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) ExceededValueThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode)

Example 4 with ASTEQNode

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

the class JexlASTHelperTest method validateJunctionChildrenWithInvalidTree.

// Verify that false is returned for a query with invalid junctions when failHard == false.
@Test
public void validateJunctionChildrenWithInvalidTree() {
    ASTEQNode eqNode = (ASTEQNode) JexlNodeFactory.buildEQNode("FOO", "bar");
    ASTAndNode conjunction = (ASTAndNode) JexlNodeFactory.createAndNode(Collections.singletonList(eqNode));
    assertFalse(JexlASTHelper.validateJunctionChildren(conjunction));
    assertFalse(JexlASTHelper.validateJunctionChildren(conjunction, false));
}
Also used : ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) ASTAndNode(org.apache.commons.jexl2.parser.ASTAndNode) Test(org.junit.Test)

Example 5 with ASTEQNode

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

the class JexlASTHelperTest method test1.

@Test
public void test1() throws Exception {
    ASTJexlScript query = JexlASTHelper.parseJexlQuery("FOO == '1' and (FOO == '2' and (FOO == '3' or FOO == '4'))");
    List<ASTEQNode> eqNodes = JexlASTHelper.getEQNodes(query);
    Map<String, Boolean> expectations = Maps.newHashMap();
    expectations.put("1", false);
    expectations.put("2", false);
    expectations.put("3", true);
    expectations.put("4", true);
    for (JexlNode eqNode : eqNodes) {
        String value = JexlASTHelper.getLiteralValue(eqNode).toString();
        Assert.assertTrue(expectations.containsKey(value));
        Assert.assertEquals(expectations.get(value), JexlASTHelper.isWithinOr(eqNode));
    }
}
Also used : ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) JexlNode(org.apache.commons.jexl2.parser.JexlNode) Test(org.junit.Test)

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