Search in sources :

Example 6 with ParseException

use of org.apache.commons.jexl2.parser.ParseException in project datawave by NationalSecurityAgency.

the class DefaultQueryPlanner method parseQueryAndValidatePattern.

protected ASTJexlScript parseQueryAndValidatePattern(String query, TraceStopwatch stopwatch) {
    ASTJexlScript queryTree;
    try {
        queryTree = JexlASTHelper.parseAndFlattenJexlQuery(query);
        ValidPatternVisitor.check(queryTree);
        ValidComparisonVisitor.check(queryTree);
    } catch (StackOverflowError soe) {
        if (log.isTraceEnabled()) {
            log.trace("Stack trace for overflow " + soe);
        }
        stopwatch.stop();
        PreConditionFailedQueryException qe = new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_DEPTH_OR_TERM_THRESHOLD_EXCEEDED, soe);
        log.warn(qe);
        throw new DatawaveFatalQueryException(qe);
    } catch (ParseException e) {
        stopwatch.stop();
        BadRequestQueryException qe = new BadRequestQueryException(DatawaveErrorCode.UNPARSEABLE_JEXL_QUERY, e, MessageFormat.format("Query: {0}", query));
        log.warn(qe);
        throw new DatawaveFatalQueryException(qe);
    } catch (PatternSyntaxException e) {
        stopwatch.stop();
        BadRequestQueryException qe = new BadRequestQueryException(DatawaveErrorCode.INVALID_REGEX, e, MessageFormat.format("Query: {0}", query));
        log.warn(qe);
        throw new DatawaveFatalQueryException(qe);
    }
    return queryTree;
}
Also used : PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) ParseException(org.apache.commons.jexl2.parser.ParseException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 7 with ParseException

use of org.apache.commons.jexl2.parser.ParseException in project datawave by NationalSecurityAgency.

the class JexlControlledQueryParser method checkIfQueryAllowed.

private void checkIfQueryAllowed(String query) throws ParseException {
    JexlNode node;
    try {
        node = JexlASTHelper.parseJexlQuery(query);
    } catch (Throwable e) {
        throw new ParseException(e.getMessage());
    }
    Set<String> fields = new TreeSet<>();
    List<ASTIdentifier> idList = JexlASTHelper.getIdentifiers(node);
    for (ASTIdentifier id : idList) {
        String fieldName = id.image;
        if (!StringUtils.isEmpty(fieldName)) {
            fieldName = fieldName.trim().toUpperCase();
            if (fieldName.charAt(0) == '$') {
                fields.add(fieldName.substring(1));
            } else {
                fields.add(fieldName);
            }
        }
    }
    fields.removeAll(allowedFields);
    if (!fields.isEmpty()) {
        throw new ParseException("Unallowed field(s) '" + fields + "' for this type of query");
    }
}
Also used : TreeSet(java.util.TreeSet) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTIdentifier(org.apache.commons.jexl2.parser.ASTIdentifier) ParseException(datawave.query.language.parser.ParseException)

Example 8 with ParseException

use of org.apache.commons.jexl2.parser.ParseException in project datawave by NationalSecurityAgency.

the class PushdownUnexecutableNodesVisitorTest method testFieldIndex.

@Test
public void testFieldIndex() throws ParseException {
    ASTJexlScript query = JexlASTHelper.parseJexlQuery(baseQuery);
    replayAll();
    // field index
    Assert.assertEquals(expectedPreFieldState, ExecutableDeterminationVisitor.getState(query, config, helper, true));
    JexlNode result = PushdownUnexecutableNodesVisitor.pushdownPredicates(query, true, config, indexedFields, indexOnlyFields, nonEventFields, helper);
    Assert.assertEquals(expectedFieldIndexPushdown, JexlStringBuildingVisitor.buildQuery(result));
    Assert.assertEquals(expectedPostFieldState, ExecutableDeterminationVisitor.getState(result, config, helper, true));
    verifyAll();
}
Also used : ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) JexlNode(org.apache.commons.jexl2.parser.JexlNode) Test(org.junit.Test)

Example 9 with ParseException

use of org.apache.commons.jexl2.parser.ParseException in project datawave by NationalSecurityAgency.

the class PushdownUnexecutableNodesVisitorTest method testGlobalIndex.

@Test
public void testGlobalIndex() throws ParseException {
    ASTJexlScript query = JexlASTHelper.parseJexlQuery(baseQuery);
    replayAll();
    // global index
    Assert.assertEquals(expectedPreGlobalState, ExecutableDeterminationVisitor.getState(query, config, helper, false));
    JexlNode result = PushdownUnexecutableNodesVisitor.pushdownPredicates(query, false, config, indexedFields, indexOnlyFields, nonEventFields, helper);
    Assert.assertEquals(expectedGlobalIndexPushdown, JexlStringBuildingVisitor.buildQuery(result));
    Assert.assertEquals(expectedPostGlobalState, ExecutableDeterminationVisitor.getState(result, config, helper, false));
    verifyAll();
}
Also used : ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) JexlNode(org.apache.commons.jexl2.parser.JexlNode) Test(org.junit.Test)

Example 10 with ParseException

use of org.apache.commons.jexl2.parser.ParseException in project datawave by NationalSecurityAgency.

the class QueryModelVisitorTest method testCastesianProduct.

@Test
public void testCastesianProduct() throws ParseException {
    model.addTermToModel("FOO1", "1BAR");
    model.addTermToModel("FOO1", "2BAR");
    String original = "FOO == FOO1";
    ASTJexlScript groomed = JexlASTHelper.InvertNodeVisitor.invertSwappedNodes(JexlASTHelper.parseJexlQuery(original));
    String expected = "(BAR1 == $1BAR || BAR2 == $1BAR || BAR1 == $2BAR || BAR2 == $2BAR)";
    assertResult(JexlStringBuildingVisitor.buildQuery(groomed), expected);
}
Also used : ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) Test(org.junit.Test)

Aggregations

ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)258 Test (org.junit.Test)214 JexlNode (org.apache.commons.jexl2.parser.JexlNode)56 ShardQueryConfiguration (datawave.query.config.ShardQueryConfiguration)36 Date (java.util.Date)33 ParseException (org.apache.commons.jexl2.parser.ParseException)18 HashSet (java.util.HashSet)14 Key (org.apache.accumulo.core.data.Key)9 ArrayList (java.util.ArrayList)7 DatawaveFatalQueryException (datawave.query.exceptions.DatawaveFatalQueryException)6 IOException (java.io.IOException)5 ASTERNode (org.apache.commons.jexl2.parser.ASTERNode)5 MockMetadataHelper (datawave.query.util.MockMetadataHelper)4 PartialKey (org.apache.accumulo.core.data.PartialKey)4 Document (datawave.query.attributes.Document)3 JexlEvaluation (datawave.query.function.JexlEvaluation)3 DatawaveJexlContext (datawave.query.jexl.DatawaveJexlContext)3 DefaultArithmetic (datawave.query.jexl.DefaultArithmetic)3 StringReader (java.io.StringReader)3 AbstractMap (java.util.AbstractMap)3