use of datawave.query.function.JexlEvaluation in project datawave by NationalSecurityAgency.
the class QueryPruningVisitorTest method falseAndRewriteTest.
@Test
public void falseAndRewriteTest() throws ParseException {
String query = "FIELD1 == 'x' && _NOFIELD_ == 'y'";
ASTJexlScript script = JexlASTHelper.parseJexlQuery(query);
JexlNode reduced = QueryPruningVisitor.reduce(script, true);
JexlEvaluation jexlEvaluation = new JexlEvaluation(JexlStringBuildingVisitor.buildQuery(reduced), new DefaultArithmetic());
boolean jexlState = jexlEvaluation.apply(new Tuple3<>(new Key(), new Document(), new DatawaveJexlContext()));
Assert.assertFalse(jexlState);
Assert.assertEquals("false", JexlStringBuildingVisitor.buildQuery(reduced));
Assert.assertEquals("false", JexlStringBuildingVisitor.buildQuery(QueryPruningVisitor.reduce(script, false)));
Assert.assertTrue(logAppender.getMessages().size() == 2);
Assert.assertEquals("Pruning FIELD1 == 'x' && _NOFIELD_ == 'y' to false", logAppender.getMessages().get(0));
Assert.assertEquals("Query before prune: FIELD1 == 'x' && _NOFIELD_ == 'y'\nQuery after prune: false", logAppender.getMessages().get(1));
}
use of datawave.query.function.JexlEvaluation in project datawave by NationalSecurityAgency.
the class QueryIterator method getEvaluation.
protected Iterator<Entry<Key, Document>> getEvaluation(NestedQueryIterator<Key> documentSource, SortedKeyValueIterator<Key, Value> sourceDeepCopy, Iterator<Entry<Key, Document>> documents, CompositeMetadata compositeMetadata, TypeMetadata typeMetadataForEval, Collection<ByteSequence> columnFamilies, boolean inclusive) {
// Filter the Documents by testing them against the JEXL query
if (!this.disableEvaluation) {
JexlEvaluation jexlEvaluationFunction = getJexlEvaluation(documentSource);
Collection<String> variables = null;
if (null != documentSource && null != documentSource.getQuery()) {
variables = VariableNameVisitor.parseQuery(jexlEvaluationFunction.parse(documentSource.getQuery()));
} else {
variables = VariableNameVisitor.parseQuery(jexlEvaluationFunction.parse(query));
}
final Iterator<Tuple2<Key, Document>> tupleItr = Iterators.transform(documents, new EntryToTuple<>());
// get the function we use for the tf functionality. Note we are
// getting an additional source deep copy for this function
final Iterator<Tuple3<Key, Document, Map<String, Object>>> itrWithContext;
if (this.isTermFrequenciesRequired()) {
// The TFFunction can only prune non index-only fields
Set<String> tfIndexOnlyFields = Sets.intersection(getTermFrequencyFields(), getIndexOnlyFields());
Function<Tuple2<Key, Document>, Tuple3<Key, Document, Map<String, Object>>> tfFunction;
tfFunction = TFFactory.getFunction(getScript(documentSource), getContentExpansionFields(), getTermFrequencyFields(), this.getTypeMetadata(), super.equality, getEvaluationFilter(), sourceDeepCopy.deepCopy(myEnvironment), tfIndexOnlyFields);
itrWithContext = TraceIterators.transform(tupleItr, tfFunction, "Term Frequency Lookup");
} else {
itrWithContext = Iterators.transform(tupleItr, new EmptyContext<>());
}
try {
IteratorBuildingVisitor iteratorBuildingVisitor = createIteratorBuildingVisitor(getDocumentRange(documentSource), false, this.sortedUIDs);
Multimap<String, JexlNode> delayedNonEventFieldMap = DelayedNonEventSubTreeVisitor.getDelayedNonEventFieldMap(iteratorBuildingVisitor, script, getNonEventFields());
IndexOnlyContextCreatorBuilder contextCreatorBuilder = new IndexOnlyContextCreatorBuilder().setSource(sourceDeepCopy).setRange(getDocumentRange(documentSource)).setTypeMetadata(typeMetadataForEval).setCompositeMetadata(compositeMetadata).setOptions(this).setVariables(variables).setIteratorBuildingVisitor(iteratorBuildingVisitor).setDelayedNonEventFieldMap(delayedNonEventFieldMap).setEquality(equality).setColumnFamilies(columnFamilies).setInclusive(inclusive).setComparatorFactory(this);
final IndexOnlyContextCreator contextCreator = contextCreatorBuilder.build();
if (exceededOrEvaluationCache != null) {
contextCreator.addAdditionalEntries(exceededOrEvaluationCache);
}
final Iterator<Tuple3<Key, Document, DatawaveJexlContext>> itrWithDatawaveJexlContext = Iterators.transform(itrWithContext, contextCreator);
Iterator<Tuple3<Key, Document, DatawaveJexlContext>> matchedDocuments = statelessFilter(itrWithDatawaveJexlContext, jexlEvaluationFunction);
if (log.isTraceEnabled()) {
log.trace("arithmetic:" + arithmetic + " range:" + getDocumentRange(documentSource) + ", thread:" + Thread.currentThread());
}
return Iterators.transform(matchedDocuments, new TupleToEntry<>());
} catch (InstantiationException | MalformedURLException | IllegalAccessException | ConfigException e) {
throw new IllegalStateException("Could not perform delayed index only evaluation", e);
}
} else if (log.isTraceEnabled()) {
log.trace("Evaluation is disabled, not instantiating Jexl evaluation logic");
}
return documents;
}
use of datawave.query.function.JexlEvaluation in project datawave by NationalSecurityAgency.
the class DynamicFacetIterator method validateOptions.
@Override
public boolean validateOptions(Map<String, String> options) {
boolean res = super.validateOptions(options);
configuration = new FacetedConfiguration();
FacetedSearchType type = FacetedSearchType.DAY_COUNT;
if (options.containsKey(FACETED_SEARCH_TYPE)) {
type = FacetedSearchType.valueOf(options.get(FACETED_SEARCH_TYPE));
}
configuration.setType(type);
if (options.containsKey(FACETED_MINIMUM)) {
try {
configuration.setMinimumCount(Integer.parseInt(options.get(FACETED_MINIMUM)));
} catch (NumberFormatException nfe) {
log.error(nfe);
// defaulting to 1
}
}
String fields = "";
if (options.containsKey(FACETED_SEARCH_FIELDS)) {
fields = options.get(FACETED_SEARCH_FIELDS);
}
switch(type) {
case SHARD_COUNT:
case DAY_COUNT:
try {
// Parse & flatten the query tree.
script = JexlASTHelper.parseAndFlattenJexlQuery(this.getQuery());
myEvaluationFunction = new JexlEvaluation(this.getQuery(), arithmetic);
} catch (Exception e) {
throw new RuntimeException("Could not parse the JEXL query: '" + this.getQuery() + "'", e);
}
break;
default:
break;
}
SortedSet<String> facetedFields = Sets.newTreeSet(Splitter.on(",").split(fields));
// indexed fields from the faceted field list.
if (!facetedFields.isEmpty())
configuration.setHasFieldLimits(true);
configuration.setFacetedFields(facetedFields);
fiAggregator = new CardinalityAggregator(getAllIndexOnlyFields(), !merge);
// assign the options for later use by the document iterator
documenIteratorOptions = options;
return res;
}
Aggregations