use of datawave.query.jexl.functions.FunctionJexlNodeVisitor in project datawave by NationalSecurityAgency.
the class ValidPatternVisitor method visit.
/**
* Visit an ASTFunctionNode to catch cases like #INCLUDE or #EXCLUDE that accept a regex as an argument
*
* @param node
* - an ASTFunctionNode
* @param data
* - the data
* @return
*/
@Override
public Object visit(ASTFunctionNode node, Object data) {
// Should pull back an EvaluationPhaseFilterFunctionsDescriptor
JexlArgumentDescriptor descriptor = JexlFunctionArgumentDescriptorFactory.F.getArgumentDescriptor(node);
if (descriptor == null) {
throw new IllegalStateException("Could not get descriptor for ASTFunctionNode");
}
if (descriptor.regexArguments()) {
// Extract the args for this function
FunctionJexlNodeVisitor functionVisitor = new FunctionJexlNodeVisitor();
functionVisitor.visit(node, null);
List<JexlNode> args = functionVisitor.args();
for (JexlNode arg : args) {
// Only take the literals
if (arg instanceof ASTStringLiteral) {
parseAndPutPattern(arg);
}
}
}
// Do not descend to children, the ValidPatternVisitor views a function node as a leaf node.
return data;
}
use of datawave.query.jexl.functions.FunctionJexlNodeVisitor in project datawave by NationalSecurityAgency.
the class SplitGeoWaveFunctionVisitor method visit.
@Override
public Object visit(ASTFunctionNode node, Object data) {
JexlArgumentDescriptor descriptor = JexlFunctionArgumentDescriptorFactory.F.getArgumentDescriptor(node);
if (descriptor instanceof GeoWaveFunctionsDescriptor.GeoWaveJexlArgumentDescriptor) {
Set<String> fields = descriptor.fields(metadataHelper, Collections.emptySet());
if (fields.size() > 1) {
List<JexlNode> functionNodes = new ArrayList<>();
FunctionJexlNodeVisitor functionVisitor = new FunctionJexlNodeVisitor();
node.jjtAccept(functionVisitor, null);
for (String field : fields) {
List<JexlNode> newArgs = new ArrayList<>();
for (int i = 0; i < functionVisitor.args().size(); i++) {
if (i == 0) {
newArgs.add(JexlNodeFactory.buildIdentifier(field));
} else {
newArgs.add(RebuildingVisitor.copy(functionVisitor.args().get(i)));
}
}
functionNodes.add(JexlNodes.makeRef(FunctionJexlNodeVisitor.makeFunctionFrom(functionVisitor.namespace(), functionVisitor.name(), newArgs.toArray(new JexlNode[0]))));
}
return JexlNodeFactory.createUnwrappedOrNode(functionNodes);
}
} else if (descriptor instanceof GeoFunctionsDescriptor.GeoJexlArgumentDescriptor) {
Set<String> fields = descriptor.fields(metadataHelper, Collections.emptySet());
if (fields.size() > 1) {
List<JexlNode> functionNodes = new ArrayList<>();
FunctionJexlNodeVisitor functionVisitor = new FunctionJexlNodeVisitor();
node.jjtAccept(functionVisitor, null);
// geo functions with > 3 args contain separate fields for lat/lon, and should not be considered
if (functionVisitor.args().size() == 3) {
for (String field : fields) {
List<JexlNode> newArgs = new ArrayList<>();
for (int i = 0; i < functionVisitor.args().size(); i++) {
if (i == 0) {
newArgs.add(JexlNodeFactory.buildIdentifier(field));
} else {
newArgs.add(RebuildingVisitor.copy(functionVisitor.args().get(i)));
}
}
functionNodes.add(JexlNodes.makeRef(FunctionJexlNodeVisitor.makeFunctionFrom(functionVisitor.namespace(), functionVisitor.name(), newArgs.toArray(new JexlNode[0]))));
}
return JexlNodeFactory.createUnwrappedOrNode(functionNodes);
}
}
}
return super.visit(node, data);
}
use of datawave.query.jexl.functions.FunctionJexlNodeVisitor in project datawave by NationalSecurityAgency.
the class RegexReplacementTransformRule method apply.
@Override
public JexlNode apply(JexlNode node, ShardQueryConfiguration config, MetadataHelper helper) {
if (node instanceof ASTERNode || node instanceof ASTNRNode) {
JexlNode literal = JexlASTHelper.getLiteral(node);
literal.image = processPattern(literal.image);
} else if (node instanceof ASTFunctionNode) {
FunctionJexlNodeVisitor functionMetadata = new FunctionJexlNodeVisitor();
node.jjtAccept(functionMetadata, null);
if (functionMetadata.namespace().equals(EvaluationPhaseFilterFunctions.EVAL_PHASE_FUNCTION_NAMESPACE) && EvaluationPhaseFilterFunctionsDescriptor.EvaluationPhaseFilterJexlArgumentDescriptor.regexFunctions.contains(functionMetadata.name())) {
JexlNode literal = JexlASTHelper.getLiteral(functionMetadata.args().get(1));
literal.image = processPattern(literal.image);
}
}
return node;
}
use of datawave.query.jexl.functions.FunctionJexlNodeVisitor in project datawave by NationalSecurityAgency.
the class RegexFunctionVisitor method visit.
@Override
public Object visit(ASTFunctionNode node, Object data) {
JexlNode returnNode = copy(node);
FunctionJexlNodeVisitor functionMetadata = new FunctionJexlNodeVisitor();
node.jjtAccept(functionMetadata, null);
if (functionMetadata.name().equals("includeRegex") || functionMetadata.name().equals("excludeRegex")) {
List<JexlNode> arguments = functionMetadata.args();
JexlNode node0 = arguments.get(0);
if (node0 instanceof ASTIdentifier) {
JexlNode regexNode = buildRegexNode((ASTIdentifier) node0, functionMetadata.name(), arguments.get(1).image);
if (regexNode != null) {
returnNode = regexNode;
}
} else {
JexlNode newParent;
if (functionMetadata.name().equals("excludeRegex")) {
newParent = new ASTAndNode(ParserTreeConstants.JJTANDNODE);
} else {
// it is likely an 'or' node...
newParent = JexlNodeFactory.shallowCopy(node0);
}
for (int i = 0; i < node0.jjtGetNumChildren(); i++) {
JexlNode child = node0.jjtGetChild(i);
if (child instanceof ASTIdentifier) {
this.adopt(newParent, buildRegexNode((ASTIdentifier) child, functionMetadata.name(), arguments.get(1).image));
} else {
// probably a Reference
for (int j = 0; j < child.jjtGetNumChildren(); j++) {
JexlNode maybeIdentifier = child.jjtGetChild(j);
if (maybeIdentifier instanceof ASTIdentifier) {
this.adopt(newParent, buildRegexNode((ASTIdentifier) maybeIdentifier, functionMetadata.name(), arguments.get(1).image));
}
}
}
}
if (newParent.jjtGetNumChildren() == node0.jjtGetNumChildren() && newParent.jjtGetNumChildren() != 0) {
returnNode = newParent;
}
}
} else if (functionMetadata.name().equals("isNull")) {
List<JexlNode> arguments = functionMetadata.args();
JexlNode node0 = arguments.get(0);
if (node0 instanceof ASTIdentifier) {
returnNode = JexlNodeFactory.buildNode(new ASTEQNode(ParserTreeConstants.JJTEQNODE), node0.image, new ASTNullLiteral(ParserTreeConstants.JJTNULLLITERAL));
}
} else if (functionMetadata.name().equals("isNotNull")) {
List<JexlNode> arguments = functionMetadata.args();
JexlNode node0 = arguments.get(0);
if (node0 instanceof ASTIdentifier) {
returnNode = JexlNodeFactory.buildNode(new ASTNENode(ParserTreeConstants.JJTNENODE), node0.image, new ASTNullLiteral(ParserTreeConstants.JJTNULLLITERAL));
}
}
return returnNode;
}
use of datawave.query.jexl.functions.FunctionJexlNodeVisitor in project datawave by NationalSecurityAgency.
the class NoExpansionFunctionVisitor method visit.
@Override
public Object visit(ASTFunctionNode node, Object data) {
FunctionJexlNodeVisitor visitor = new FunctionJexlNodeVisitor();
node.jjtAccept(visitor, null);
if (visitor.namespace().equals("filter") && visitor.name().equalsIgnoreCase(EvaluationPhaseFilterFunctionsDescriptor.NO_EXPANSION)) {
noExpansionFields.addAll(visitor.args().stream().map(JexlASTHelper::getIdentifier).collect(Collectors.toSet()));
// prune this query node by returning null
return null;
}
return copy(node);
}
Aggregations