Search in sources :

Example 6 with JexlEvaluation

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));
}
Also used : DefaultArithmetic(datawave.query.jexl.DefaultArithmetic) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) JexlEvaluation(datawave.query.function.JexlEvaluation) JexlNode(org.apache.commons.jexl2.parser.JexlNode) Document(datawave.query.attributes.Document) DatawaveJexlContext(datawave.query.jexl.DatawaveJexlContext) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Example 7 with JexlEvaluation

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;
}
Also used : EmptyContext(datawave.query.util.EmptyContext) MalformedURLException(java.net.MalformedURLException) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IndexOnlyContextCreator(datawave.query.function.IndexOnlyContextCreator) JexlEvaluation(datawave.query.function.JexlEvaluation) IteratorBuildingVisitor(datawave.query.jexl.visitors.IteratorBuildingVisitor) IndexOnlyContextCreatorBuilder(datawave.query.function.IndexOnlyContextCreatorBuilder) Tuple2(datawave.query.util.Tuple2) Tuple3(datawave.query.util.Tuple3) JexlNode(org.apache.commons.jexl2.parser.JexlNode)

Example 8 with JexlEvaluation

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;
}
Also used : JexlEvaluation(datawave.query.function.JexlEvaluation) FacetedConfiguration(datawave.query.tables.facets.FacetedConfiguration) FacetedSearchType(datawave.query.tables.facets.FacetedSearchType) MalformedURLException(java.net.MalformedURLException) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IOException(java.io.IOException) CardinalityAggregator(datawave.query.jexl.functions.CardinalityAggregator)

Aggregations

JexlEvaluation (datawave.query.function.JexlEvaluation)8 Key (org.apache.accumulo.core.data.Key)5 DefaultArithmetic (datawave.query.jexl.DefaultArithmetic)4 MalformedURLException (java.net.MalformedURLException)4 ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)4 JexlNode (org.apache.commons.jexl2.parser.JexlNode)4 ConfigException (org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException)4 Document (datawave.query.attributes.Document)3 DatawaveJexlContext (datawave.query.jexl.DatawaveJexlContext)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 FileNotFoundException (java.io.FileNotFoundException)2 InterruptedIOException (java.io.InterruptedIOException)2 IterationInterruptedException (org.apache.accumulo.core.iterators.IterationInterruptedException)2 TabletClosedException (org.apache.accumulo.tserver.tablet.TabletClosedException)2 IndexOnlyContextCreator (datawave.query.function.IndexOnlyContextCreator)1 IndexOnlyContextCreatorBuilder (datawave.query.function.IndexOnlyContextCreatorBuilder)1 EvaluationTrackingNestedIterator (datawave.query.iterator.profile.EvaluationTrackingNestedIterator)1 MultiThreadedQuerySpan (datawave.query.iterator.profile.MultiThreadedQuerySpan)1 SourceTrackingIterator (datawave.query.iterator.profile.SourceTrackingIterator)1