Skip to content

Commit

Permalink
Add a lucene and jexl implementations of a stochastic query builder f…
Browse files Browse the repository at this point in the history
…or use in randomized input testing (#2381)
  • Loading branch information
apmoriarty committed May 3, 2024
1 parent d70fcd1 commit b7d257f
Show file tree
Hide file tree
Showing 9 changed files with 1,323 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package datawave.query.common.grouping;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -13,9 +12,7 @@
import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import static org.apache.commons.jexl3.parser.ParserTreeConstants.JJTORNODE;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -22,12 +20,10 @@
import org.apache.commons.jexl3.parser.ASTStringLiteral;
import org.apache.commons.jexl3.parser.JexlNode;
import org.apache.commons.jexl3.parser.JexlNodes;
import org.apache.commons.lang3.StringUtils;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;

import datawave.query.Constants;
import datawave.query.QueryParameters;
import datawave.query.attributes.UniqueFields;
import datawave.query.attributes.UniqueGranularity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package datawave.query.jexl.util;

import java.util.List;
import java.util.Random;
import java.util.Set;

import com.google.common.collect.Lists;

public abstract class AbstractQueryGenerator implements QueryGenerator {

protected boolean intersectionsEnabled = true;
protected boolean unionsEnabled = true;
protected boolean negationsEnabled = false;
protected boolean regexEnabled = false;
protected boolean filterFunctionsEnabled = false;
protected boolean contentFunctionsEnabled = false;
protected boolean groupingFunctionsEnabled = false;

protected final Random random = new Random();
protected final StringBuilder sb = new StringBuilder();

protected final List<String> fields;
protected final List<String> values;

enum NodeType {
EQ, NE, ER, FILTER_FUNCTION, CONTENT_FUNCTION, GROUPING_FUNCTION
}

public AbstractQueryGenerator(Set<String> fields, Set<String> values) {
this.fields = Lists.newArrayList(fields);
this.values = Lists.newArrayList(values);
}

public void validateOptions() {
if (!intersectionsEnabled && !unionsEnabled) {
throw new IllegalStateException("cannot disable both unions and intersections");
}
}

public QueryGenerator enableIntersections() {
intersectionsEnabled = true;
return this;
}

public QueryGenerator disableIntersections() {
intersectionsEnabled = false;
return this;
}

public QueryGenerator enableUnions() {
unionsEnabled = true;
return this;
}

public QueryGenerator disableUnions() {
unionsEnabled = false;
return this;
}

public QueryGenerator enableNegations() {
negationsEnabled = true;
return this;
}

public QueryGenerator disableNegations() {
negationsEnabled = false;
return this;
}

public QueryGenerator enableRegexes() {
regexEnabled = true;
return this;
}

public QueryGenerator disableRegexes() {
regexEnabled = false;
return this;
}

public QueryGenerator enableFilterFunctions() {
filterFunctionsEnabled = true;
return this;
}

public QueryGenerator disableFilterFunctions() {
filterFunctionsEnabled = false;
return this;
}

public QueryGenerator enableContentFunctions() {
contentFunctionsEnabled = true;
return this;
}

public QueryGenerator disableContentFunctions() {
contentFunctionsEnabled = false;
return this;
}

public QueryGenerator enableGroupingFunctions() {
groupingFunctionsEnabled = true;
return this;
}

public QueryGenerator disableGroupingFunctions() {
groupingFunctionsEnabled = false;
return this;
}

public QueryGenerator enableAllOptions() {
return enableNegations().enableRegexes().enableFilterFunctions().enableContentFunctions().enableGroupingFunctions();
}

public QueryGenerator disableAllOptions() {
return disableNegations().disableRegexes().disableFilterFunctions().disableContentFunctions().disableGroupingFunctions();
}

protected abstract void buildNode();

protected abstract void buildJunction();

protected abstract void buildLeaf();
}
Loading

0 comments on commit b7d257f

Please sign in to comment.