Search in sources :

Example 1 with FunctionJexlNodeVisitor

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;
}
Also used : ASTStringLiteral(org.apache.commons.jexl2.parser.ASTStringLiteral) JexlArgumentDescriptor(datawave.query.jexl.functions.arguments.JexlArgumentDescriptor) JexlNode(org.apache.commons.jexl2.parser.JexlNode) FunctionJexlNodeVisitor(datawave.query.jexl.functions.FunctionJexlNodeVisitor)

Example 2 with FunctionJexlNodeVisitor

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);
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) JexlArgumentDescriptor(datawave.query.jexl.functions.arguments.JexlArgumentDescriptor) FunctionJexlNodeVisitor(datawave.query.jexl.functions.FunctionJexlNodeVisitor) JexlNode(org.apache.commons.jexl2.parser.JexlNode) GeoFunctionsDescriptor(datawave.query.jexl.functions.GeoFunctionsDescriptor) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with FunctionJexlNodeVisitor

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;
}
Also used : ASTFunctionNode(org.apache.commons.jexl2.parser.ASTFunctionNode) ASTERNode(org.apache.commons.jexl2.parser.ASTERNode) ASTNRNode(org.apache.commons.jexl2.parser.ASTNRNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) FunctionJexlNodeVisitor(datawave.query.jexl.functions.FunctionJexlNodeVisitor)

Example 4 with FunctionJexlNodeVisitor

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;
}
Also used : ASTNENode(org.apache.commons.jexl2.parser.ASTNENode) ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTIdentifier(org.apache.commons.jexl2.parser.ASTIdentifier) List(java.util.List) FunctionJexlNodeVisitor(datawave.query.jexl.functions.FunctionJexlNodeVisitor) ASTNullLiteral(org.apache.commons.jexl2.parser.ASTNullLiteral) ASTAndNode(org.apache.commons.jexl2.parser.ASTAndNode)

Example 5 with FunctionJexlNodeVisitor

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);
}
Also used : JexlASTHelper(datawave.query.jexl.JexlASTHelper) FunctionJexlNodeVisitor(datawave.query.jexl.functions.FunctionJexlNodeVisitor)

Aggregations

FunctionJexlNodeVisitor (datawave.query.jexl.functions.FunctionJexlNodeVisitor)5 JexlNode (org.apache.commons.jexl2.parser.JexlNode)4 JexlArgumentDescriptor (datawave.query.jexl.functions.arguments.JexlArgumentDescriptor)2 List (java.util.List)2 JexlASTHelper (datawave.query.jexl.JexlASTHelper)1 GeoFunctionsDescriptor (datawave.query.jexl.functions.GeoFunctionsDescriptor)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)1 ASTEQNode (org.apache.commons.jexl2.parser.ASTEQNode)1 ASTERNode (org.apache.commons.jexl2.parser.ASTERNode)1 ASTFunctionNode (org.apache.commons.jexl2.parser.ASTFunctionNode)1 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)1 ASTNENode (org.apache.commons.jexl2.parser.ASTNENode)1 ASTNRNode (org.apache.commons.jexl2.parser.ASTNRNode)1 ASTNullLiteral (org.apache.commons.jexl2.parser.ASTNullLiteral)1 ASTStringLiteral (org.apache.commons.jexl2.parser.ASTStringLiteral)1