use of org.apache.commons.jexl2.parser.ASTLTNode 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.ASTLTNode in project datawave by NationalSecurityAgency.
the class CompositeRange method getUpperBoundExpression.
private String getUpperBoundExpression(Map<String, DiscreteIndexType<?>> discreteIndexTypeMap) {
StringBuilder buf = new StringBuilder();
boolean lastNode = false;
for (int i = 0; i < expressionListUpperBound.size(); i++) {
String expression = expressionListUpperBound.get(i);
JexlNode node = jexlNodeListUpperBound.get(i);
if (expression != null && node != null) {
if (i != (expressionListUpperBound.size() - 1)) {
// Convert LE node to LT node if it is the last valid node in the list
if (node instanceof ASTLENode && expressionListUpperBound.get(i + 1) == null) {
String exclusiveUpperBound = CompositeUtils.getExclusiveUpperBound(expression, discreteIndexTypeMap.get(fieldNameList.get(i)));
// bound, and signal that this is the last expression
if (exclusiveUpperBound.length() != expression.length())
lastNode = true;
else
expression = exclusiveUpperBound;
} else // Convert LT nodes to inclusive LE nodes if they are not the last valid node in the list
if (node instanceof ASTLTNode && expressionListUpperBound.get(i + 1) != null) {
String inclusiveUpperBound = CompositeUtils.getInclusiveUpperBound(expression, discreteIndexTypeMap.get(fieldNameList.get(i)));
// bound, and signal that this is the last expression
if (inclusiveUpperBound.length() != expression.length())
lastNode = true;
else
expression = inclusiveUpperBound;
}
}
if (i > 0)
buf.append(separator);
buf.append(expression);
} else {
break;
}
if (lastNode)
break;
}
return buf.toString();
}
use of org.apache.commons.jexl2.parser.ASTLTNode 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.ASTLTNode in project datawave by NationalSecurityAgency.
the class PushFunctionsIntoExceededValueRanges method isExceededValueRangeNode.
private boolean isExceededValueRangeNode(JexlNode child) {
QueryPropertyMarker.Instance instance = QueryPropertyMarker.findInstance(child);
if (instance.isType(ExceededValueThresholdMarkerJexlNode.class)) {
JexlNode source = instance.getSource();
source = JexlASTHelper.dereference(source);
if (source instanceof ASTAndNode && source.jjtGetNumChildren() == 2) {
JexlNode sourceChild = JexlASTHelper.dereference(source.jjtGetChild(0));
if (sourceChild instanceof ASTLENode || sourceChild instanceof ASTLTNode || sourceChild instanceof ASTGENode || sourceChild instanceof ASTGTNode) {
sourceChild = JexlASTHelper.dereference(source.jjtGetChild(1));
if (sourceChild instanceof ASTLENode || sourceChild instanceof ASTLTNode || sourceChild instanceof ASTGENode || sourceChild instanceof ASTGTNode) {
return true;
}
}
}
}
return false;
}
use of org.apache.commons.jexl2.parser.ASTLTNode in project datawave by NationalSecurityAgency.
the class JexlNodeFactory method shallowCopy.
/**
* A shallow copy of the given JexlNode, creates a new node of the same type with the same parent and image. Children are not copied
*
* @param original
* @return
*/
public static JexlNode shallowCopy(JexlNode original) {
if (null == original) {
throw new IllegalArgumentException();
}
JexlNode copy;
Class<?> clz = original.getClass();
if (ASTAndNode.class.isAssignableFrom(clz)) {
copy = new ASTAndNode(ParserTreeConstants.JJTANDNODE);
} else if (ASTBitwiseAndNode.class.isAssignableFrom(clz)) {
copy = new ASTBitwiseAndNode(ParserTreeConstants.JJTBITWISEANDNODE);
} else if (ASTBitwiseComplNode.class.isAssignableFrom(clz)) {
copy = new ASTBitwiseComplNode(ParserTreeConstants.JJTBITWISECOMPLNODE);
} else if (ASTBitwiseOrNode.class.isAssignableFrom(clz)) {
copy = new ASTBitwiseOrNode(ParserTreeConstants.JJTBITWISEORNODE);
} else if (ASTBitwiseXorNode.class.isAssignableFrom(clz)) {
copy = new ASTBitwiseXorNode(ParserTreeConstants.JJTBITWISEXORNODE);
} else if (ASTEmptyFunction.class.isAssignableFrom(clz)) {
copy = new ASTEmptyFunction(ParserTreeConstants.JJTEMPTYFUNCTION);
} else if (ASTEQNode.class.isAssignableFrom(clz)) {
copy = new ASTEQNode(ParserTreeConstants.JJTEQNODE);
} else if (ASTERNode.class.isAssignableFrom(clz)) {
copy = new ASTERNode(ParserTreeConstants.JJTERNODE);
} else if (ASTFalseNode.class.isAssignableFrom(clz)) {
copy = new ASTFalseNode(ParserTreeConstants.JJTFALSENODE);
} else if (ASTGENode.class.isAssignableFrom(clz)) {
copy = new ASTGENode(ParserTreeConstants.JJTGENODE);
} else if (ASTGTNode.class.isAssignableFrom(clz)) {
copy = new ASTGTNode(ParserTreeConstants.JJTGTNODE);
} else if (ASTIdentifier.class.isAssignableFrom(clz)) {
copy = new ASTIdentifier(ParserTreeConstants.JJTIDENTIFIER);
} else if (ASTLENode.class.isAssignableFrom(clz)) {
copy = new ASTLENode(ParserTreeConstants.JJTLENODE);
} else if (ASTLTNode.class.isAssignableFrom(clz)) {
copy = new ASTLTNode(ParserTreeConstants.JJTLTNODE);
} else if (ASTNENode.class.isAssignableFrom(clz)) {
copy = new ASTNENode(ParserTreeConstants.JJTNENODE);
} else if (ASTNRNode.class.isAssignableFrom(clz)) {
copy = new ASTNRNode(ParserTreeConstants.JJTNRNODE);
} else if (ASTNotNode.class.isAssignableFrom(clz)) {
copy = new ASTNotNode(ParserTreeConstants.JJTNOTNODE);
} else if (ASTNullLiteral.class.isAssignableFrom(clz)) {
copy = new ASTNullLiteral(ParserTreeConstants.JJTNULLLITERAL);
} else if (ASTNumberLiteral.class.isAssignableFrom(clz)) {
copy = new ASTNumberLiteral(ParserTreeConstants.JJTNUMBERLITERAL);
JexlNodes.setLiteral((ASTNumberLiteral) copy, ((ASTNumberLiteral) original).getLiteral());
} else if (ASTOrNode.class.isAssignableFrom(clz)) {
copy = new ASTOrNode(ParserTreeConstants.JJTORNODE);
} else if (ASTStringLiteral.class.isAssignableFrom(clz)) {
copy = new ASTStringLiteral(ParserTreeConstants.JJTSTRINGLITERAL);
JexlNodes.setLiteral((ASTStringLiteral) copy, ((ASTStringLiteral) original).getLiteral());
} else if (ASTTrueNode.class.isAssignableFrom(clz)) {
copy = new ASTTrueNode(ParserTreeConstants.JJTTRUENODE);
} else if (ASTReferenceExpression.class.isAssignableFrom(clz)) {
copy = new ASTReferenceExpression(ParserTreeConstants.JJTREFERENCEEXPRESSION);
} else if (ASTReference.class.isAssignableFrom(clz)) {
copy = new ASTReference(ParserTreeConstants.JJTREFERENCE);
} else if (ASTAdditiveNode.class.isAssignableFrom(clz)) {
copy = new ASTAdditiveNode(ParserTreeConstants.JJTADDITIVENODE);
} else if (ASTMethodNode.class.isAssignableFrom(clz)) {
copy = new ASTMethodNode(ParserTreeConstants.JJTMETHODNODE);
} else if (ASTFunctionNode.class.isAssignableFrom(clz)) {
copy = new ASTFunctionNode(ParserTreeConstants.JJTFUNCTIONNODE);
} else if (ASTMulNode.class.isAssignableFrom(clz)) {
copy = new ASTMulNode(ParserTreeConstants.JJTMULNODE);
} else if (ASTAssignment.class.isAssignableFrom(clz)) {
copy = new ASTAssignment(ParserTreeConstants.JJTASSIGNMENT);
} else {
throw new UnsupportedOperationException();
}
copy.jjtSetParent(original.jjtGetParent());
copy.image = original.image;
return copy;
}
Aggregations