Skip to content

Commit

Permalink
Updated jexl parsing to disable some features which we aren't using t…
Browse files Browse the repository at this point in the history
…o improve parsing performance.
  • Loading branch information
jwomeara committed Mar 8, 2024
1 parent df43e0a commit 1290186
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,54 @@ public static ASTJexlScript parseAndFlattenJexlQuery(String query) throws ParseE
return TreeFlatteningRebuildingVisitor.flatten(script);
}

protected static JexlFeatures jexlFeatures() {
// @formatter:off
return new JexlFeatures()
// mostly used internally by Jexl
.register(false) // default false
// allow usage of let, const, and var variables
.localVar(false) // default true
// allow side-effect operators (e.g. +=, -=, |=, <<=, etc) for global vars. needed to assign values (e.g. _Value_ = true)
.sideEffectGlobal(true) // default true
// allow side-effect operators (e.g. +=, -=, |=, <<=, etc). needed to assign values (e.g. _Value_ = true)
.sideEffect(true) // default true
// allow array indexing via reference expression (e.g. array[some expression])
.arrayReferenceExpr(false) // default true
// allow method calls on expressions
.methodCall(true) // default true
// allow array/map/set literal expressions (e.g. [1, 2, "three"], {"one": 1, "two": 2}, {"one", 2, "more"}
.structuredLiteral(false) // default true
// allow creation of new instances
.newInstance(false) // default true
// allow loop constructs
.loops(false) // default true
// allow lambda/function constructs (not the same as namespaced functions)
.lambda(false) // default true
// allow thin-arrow lambda syntax (e.g. ->)
.thinArrow(false) // default true
// allow fat-arrow lambda syntax (e.g. =>)
.fatArrow(false) // default false
// allow comparator names (e.g. eq, ne, lt, gt, etc)
.comparatorNames(false) // default true
// allow pragma constructs (e.g. #pragma some.option some-value)
.pragma(false) // default true
// allow pragma constructs anywhere in the code
.pragmaAnywhere(false) // default true
// allow namespace pragmas (e.g. '#pragma jexl.namespace.str java.lang.String' to use 'str' in place of 'java.lang.String')
.namespacePragma(false) // default true
// allow import pragmas (e.g. '#pragma jexl.import java.net' to use new URL() instead of new java.net.URL())
.importPragma(false) // default true
// allow annotations
.annotation(false) // default true
// allow multiple semicolon-terminated expressions per jexl string
.script(false) // default true
// whether redefining local variables is an error
.lexical(false) // default false
// whether local variables shade global variables
.lexicalShade(false); // default false
// @formatter: on
}

/**
* Parse a query string using a JEXL parser and transform it into a parse tree of our RefactoredDatawaveTreeNodes. This also sets all convenience maps that
* the analyzer provides.
Expand Down Expand Up @@ -161,7 +209,7 @@ public static ASTJexlScript parseJexlQuery(String query) throws ParseException {
} else {
// Parse the original query
try {
return parser.parse(null, new JexlFeatures(), caseFixQuery, null);
return parser.parse(null, jexlFeatures(), caseFixQuery, null);
} catch (TokenMgrException | JexlException e) {
throw new ParseException(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,6 @@ public void testASTStringLiteral() {
assertEquals(1, count(JexlNodes.makeStringLiteral()).getTotal(ASTStringLiteral.class));
}

@Test
public void testASTArrayLiteral() throws ParseException {
assertEquals(1, count("[1, 2, 3]").getTotal(ASTArrayLiteral.class));
}

@Test
public void testASTMapLiteral() throws ParseException {
assertEquals(1, count("{'one':1, 'two':2, 'three':3}").getTotal(ASTMapLiteral.class));
}

@Test
public void testASTMapEntry() {
assertEquals(1, count(new ASTMapEntry(ParserTreeConstants.JJTMAPENTRY)).getTotal(ASTMapEntry.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,27 +502,6 @@ public void nestedMarkerBoundedRangeTest() throws ParseException {
assertEquals(query, JexlStringBuildingVisitor.buildQuery(QueryPruningVisitor.reduce(script, false)));
}

@Test
public void dualStatementQueryTest() throws ParseException {
String query = "(Expression = 'somevalue'); FIELD == 'x'";
ASTJexlScript script = JexlASTHelper.parseJexlQuery(query);

assertEquals(QueryPruningVisitor.TruthState.UNKNOWN, QueryPruningVisitor.getState(script));
assertEquals(query, JexlStringBuildingVisitor.buildQuery(QueryPruningVisitor.reduce(script, false)));

query = "(Expression = 'somevalue'); FIELD == 'x' || true";
script = JexlASTHelper.parseJexlQuery(query);

assertEquals(QueryPruningVisitor.TruthState.TRUE, QueryPruningVisitor.getState(script));
assertEquals("(Expression = 'somevalue'); true", JexlStringBuildingVisitor.buildQuery(QueryPruningVisitor.reduce(script, false)));

query = "(Expression = 'somevalue'); FIELD == 'x' && false";
script = JexlASTHelper.parseJexlQuery(query);

assertEquals(QueryPruningVisitor.TruthState.UNKNOWN, QueryPruningVisitor.getState(script));
assertEquals("(Expression = 'somevalue'); false", JexlStringBuildingVisitor.buildQuery(QueryPruningVisitor.reduce(script, false)));
}

@Test
public void propertyMarkerTest() throws ParseException {
String query = "((_Value_ = true) && (FIELD = 'x'))";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,6 @@ public void testNonEqualImage() throws ParseException {
"Did not find a matching child for EQNode in [EQNode]: Did not find a matching child for bar in [bat]: Node images differ: bar vs bat");
}

/**
* Verify that two nodes with differing number of children are considered non-equal.
*/
@Test
public void testNonEqualChildrenSize() throws ParseException {
assertNotEquivalent("[1, 2, 3, 4]", "[1, 2]",
"Did not find a matching child for [ 1, 2, 3, 4 ] in [[ 1, 2 ]]: Num children differ: [1, 2, 3, 4] vs [1, 2]");
}

/**
* Verify that two nodes with different children are considered non-equal.
*/
@Test
public void testNonEqualChildren() throws ParseException {
assertNotEquivalent("[1, 2]", "[1, 3]",
"Did not find a matching child for [ 1, 2 ] in [[ 1, 3 ]]: Did not find a matching child for 2 in [3]: Node images differ: 2 vs 3");
}

/**
* Verify that two identical trees are considered equal.
*/
Expand Down

0 comments on commit 1290186

Please sign in to comment.