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);
}
}
}
}
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;
}
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;
}
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));
}
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));
}
}
Aggregations