Search in sources :

Example 1 with JexlEvaluation

use of datawave.query.function.JexlEvaluation in project datawave by NationalSecurityAgency.

the class QueryIterator method getJexlEvaluation.

protected JexlEvaluation getJexlEvaluation(NestedQueryIterator<Key> documentSource) {
    if (null == documentSource) {
        return new JexlEvaluation(query, getArithmetic());
    }
    JexlEvaluation jexlEvaluationFunction = null;
    NestedQuery<Key> nestedQuery = documentSource.getNestedQuery();
    if (null == nestedQuery) {
        jexlEvaluationFunction = new JexlEvaluation(query, getArithmetic());
    } else {
        jexlEvaluationFunction = nestedQuery.getEvaluation();
        if (null == jexlEvaluationFunction) {
            return new JexlEvaluation(query, getArithmetic());
        }
    }
    return jexlEvaluationFunction;
}
Also used : JexlEvaluation(datawave.query.function.JexlEvaluation) Key(org.apache.accumulo.core.data.Key)

Example 2 with JexlEvaluation

use of datawave.query.function.JexlEvaluation in project datawave by NationalSecurityAgency.

the class QueryIterator method init.

@Override
public void init(SortedKeyValueIterator<Key, Value> source, Map<String, String> options, IteratorEnvironment env) throws IOException {
    if (log.isTraceEnabled()) {
        log.trace("QueryIterator init()");
    }
    if (!validateOptions(new SourcedOptions<>(source, env, options))) {
        throw new IllegalArgumentException("Could not initialize QueryIterator with " + options);
    }
    // We want to add in spoofed dataTypes for Aggregation/Evaluation to
    // ensure proper numeric evaluation.
    this.typeMetadata = new TypeMetadata(this.getTypeMetadata());
    this.typeMetadataWithNonIndexed = new TypeMetadata(this.typeMetadata);
    this.typeMetadataWithNonIndexed.addForAllIngestTypes(this.getNonIndexedDataTypeMap());
    this.exceededOrEvaluationCache = new HashMap<>();
    // Parse the query
    try {
        this.script = JexlASTHelper.parseJexlQuery(this.getQuery());
        this.myEvaluationFunction = new JexlEvaluation(this.getQuery(), arithmetic);
    } catch (Exception e) {
        throw new IOException("Could not parse the JEXL query: '" + this.getQuery() + "'", e);
    }
    this.documentOptions = options;
    this.myEnvironment = env;
    if (gatherTimingDetails()) {
        this.trackingSpan = new MultiThreadedQuerySpan(getStatsdClient());
        this.source = new SourceTrackingIterator(trackingSpan, source);
    } else {
        this.source = source;
    }
    this.fiAggregator = new IdentityAggregator(getAllIndexOnlyFields(), getEvaluationFilter(), getEvaluationFilter() != null ? getEvaluationFilter().getMaxNextCount() : -1);
    if (isDebugMultithreadedSources()) {
        this.source = new SourceThreadTrackingIterator(this.source);
    }
    this.sourceForDeepCopies = this.source.deepCopy(this.myEnvironment);
    // update ActiveQueryLog with (potentially) updated config
    if (env != null) {
        ActiveQueryLog.setConfig(env.getConfig());
    }
    DatawaveFieldIndexListIteratorJexl.FSTManager.setHdfsFileSystem(this.getFileSystemCache());
    DatawaveFieldIndexListIteratorJexl.FSTManager.setHdfsFileCompressionCodec(this.getHdfsFileCompressionCodec());
    pruneIvaratorCacheDirs();
}
Also used : TypeMetadata(datawave.query.util.TypeMetadata) SourceTrackingIterator(datawave.query.iterator.profile.SourceTrackingIterator) JexlEvaluation(datawave.query.function.JexlEvaluation) MultiThreadedQuerySpan(datawave.query.iterator.profile.MultiThreadedQuerySpan) IdentityAggregator(datawave.query.jexl.functions.IdentityAggregator) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IterationInterruptedException(org.apache.accumulo.core.iterators.IterationInterruptedException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) TabletClosedException(org.apache.accumulo.tserver.tablet.TabletClosedException) MalformedURLException(java.net.MalformedURLException)

Example 3 with JexlEvaluation

use of datawave.query.function.JexlEvaluation in project datawave by NationalSecurityAgency.

the class QueryIterator method buildDocumentIterator.

/**
 * Build the document iterator
 *
 * @param documentRange
 * @param seekRange
 * @param columnFamilies
 * @param inclusive
 * @return
 * @throws IOException
 */
protected NestedIterator<Key> buildDocumentIterator(Range documentRange, Range seekRange, Collection<ByteSequence> columnFamilies, boolean inclusive) throws IOException, ConfigException, InstantiationException, IllegalAccessException {
    NestedIterator<Key> docIter = null;
    if (log.isTraceEnabled()) {
        log.trace("Batched queries is " + batchedQueries);
    }
    if (batchedQueries >= 1) {
        List<NestedQuery<Key>> nests = Lists.newArrayList();
        for (Entry<Range, String> queries : batchStack) {
            Range myRange = queries.getKey();
            if (log.isTraceEnabled()) {
                log.trace("Adding " + myRange + " from seekrange " + seekRange);
            }
            /*
                 * Only perform the following checks if start key is not infinite and document range is specified
                 */
            if (null != seekRange && !seekRange.isInfiniteStartKey()) {
                Key seekStartKey = seekRange.getStartKey();
                Key myStartKey = myRange.getStartKey();
                /*
                     * if our seek key is greater than our start key we can skip this batched query. myStartKey.compareTo(seekStartKey) must be <= 0, which
                     * means that startKey must be greater than or equal be seekStartKey
                     */
                if (null != myStartKey && null != seekStartKey && !seekRange.contains(myStartKey)) {
                    if (log.isTraceEnabled()) {
                        log.trace("skipping " + myRange);
                    }
                    continue;
                }
            }
            JexlArithmetic myArithmetic;
            if (arithmetic instanceof StatefulArithmetic) {
                myArithmetic = ((StatefulArithmetic) arithmetic).clone();
            } else {
                myArithmetic = new DefaultArithmetic();
            }
            // Parse the query
            ASTJexlScript myScript = null;
            JexlEvaluation eval = null;
            try {
                myScript = JexlASTHelper.parseJexlQuery(queries.getValue());
                eval = new JexlEvaluation(queries.getValue(), myArithmetic);
            } catch (Exception e) {
                throw new IOException("Could not parse the JEXL query: '" + this.getQuery() + "'", e);
            }
            // If we had an event-specific range previously, we need to
            // reset it back
            // to the source we created during init
            NestedIterator<Key> subDocIter = getOrSetKeySource(myRange, myScript);
            if (log.isTraceEnabled()) {
                log.trace("Using init()'ialized source: " + subDocIter.getClass().getName());
            }
            if (gatherTimingDetails()) {
                subDocIter = new EvaluationTrackingNestedIterator(QuerySpan.Stage.FieldIndexTree, trackingSpan, subDocIter, myEnvironment);
            }
            // Seek() the boolean logic stuff
            ((SeekableIterator) subDocIter).seek(myRange, columnFamilies, inclusive);
            NestedQuery<Key> nestedQueryObj = new NestedQuery<>();
            nestedQueryObj.setQuery(queries.getValue());
            nestedQueryObj.setIterator(subDocIter);
            nestedQueryObj.setQueryScript(myScript);
            nestedQueryObj.setEvaluation(eval);
            nestedQueryObj.setRange(queries.getKey());
            nests.add(nestedQueryObj);
        }
        docIter = new NestedQueryIterator<>(nests);
        // now lets start off the nested iterator
        docIter.initialize();
        initKeySource = docIter;
    } else {
        // If we had an event-specific range previously, we need to reset it back
        // to the source we created during init
        docIter = getOrSetKeySource(documentRange, script);
        initKeySource = docIter;
        if (log.isTraceEnabled()) {
            log.trace("Using init()'ialized source: " + this.initKeySource.getClass().getName());
        }
        if (gatherTimingDetails()) {
            docIter = new EvaluationTrackingNestedIterator(QuerySpan.Stage.FieldIndexTree, trackingSpan, docIter, myEnvironment);
        }
        // Seek() the boolean logic stuff
        ((SeekableIterator) docIter).seek(range, columnFamilies, inclusive);
        // now lets start off the nested iterator
        docIter.initialize();
    }
    return docIter;
}
Also used : DefaultArithmetic(datawave.query.jexl.DefaultArithmetic) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) JexlEvaluation(datawave.query.function.JexlEvaluation) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Range(org.apache.accumulo.core.data.Range) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IterationInterruptedException(org.apache.accumulo.core.iterators.IterationInterruptedException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) TabletClosedException(org.apache.accumulo.tserver.tablet.TabletClosedException) MalformedURLException(java.net.MalformedURLException) JexlArithmetic(org.apache.commons.jexl2.JexlArithmetic) StatefulArithmetic(datawave.query.jexl.StatefulArithmetic) EvaluationTrackingNestedIterator(datawave.query.iterator.profile.EvaluationTrackingNestedIterator) Key(org.apache.accumulo.core.data.Key)

Example 4 with JexlEvaluation

use of datawave.query.function.JexlEvaluation in project datawave by NationalSecurityAgency.

the class QueryPruningVisitorTest method falseDoubleAndRewriteTest.

@Test
public void falseDoubleAndRewriteTest() throws ParseException {
    String query = "FIELD1 == 'x' && _NOFIELD_ == 'y' && FIELD2 == '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' && FIELD2 == 'y' to false", logAppender.getMessages().get(0));
    Assert.assertEquals("Query before prune: FIELD1 == 'x' && _NOFIELD_ == 'y' && FIELD2 == '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 5 with JexlEvaluation

use of datawave.query.function.JexlEvaluation in project datawave by NationalSecurityAgency.

the class QueryPruningVisitorTest method trueRewriteTest.

@Test
public void trueRewriteTest() throws ParseException {
    String query = "true || (FIELD1 == '1' && filter:isNull(FIELD2))";
    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.assertTrue(jexlState);
    Assert.assertEquals("true", JexlStringBuildingVisitor.buildQuery(reduced));
    Assert.assertEquals("true", JexlStringBuildingVisitor.buildQuery(QueryPruningVisitor.reduce(script, false)));
    Assert.assertTrue(logAppender.getMessages().size() == 2);
    Assert.assertEquals("Pruning true || (FIELD1 == '1' && filter:isNull(FIELD2)) to true", logAppender.getMessages().get(0));
    Assert.assertEquals("Query before prune: true || (FIELD1 == '1' && filter:isNull(FIELD2))\nQuery after prune: true", 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)

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