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