Search in sources :

Example 16 with MetadataHelper

use of datawave.query.util.MetadataHelper in project datawave by NationalSecurityAgency.

the class ExecutableExpansionVisitorTest method testMinimumExpansionParse.

@Test
public void testMinimumExpansionParse() throws Exception {
    ASTJexlScript queryTree = JexlASTHelper.parseJexlQuery("UUID == 'capone' && (filter:includeRegex(QUOTE,'.*kind.*') || QUOTE == 'kind' || BIRTH_DATE == '123')");
    ShardQueryConfiguration config = EasyMock.createMock(ShardQueryConfiguration.class);
    MetadataHelper helper = EasyMock.createMock(MetadataHelper.class);
    HashSet<String> indexedFields = new HashSet<>();
    indexedFields.add("UUID");
    indexedFields.add("QUOTE");
    EasyMock.expect(config.getIndexedFields()).andReturn(indexedFields).anyTimes();
    EasyMock.replay(config, helper);
    ASTJexlScript newTree = ExecutableExpansionVisitor.expand(queryTree, config, helper);
    EasyMock.verify(config, helper);
    Assert.assertFalse(JexlStringBuildingVisitor.buildQuery(queryTree).equals(JexlStringBuildingVisitor.buildQuery(newTree)));
    String expected = "(QUOTE == 'kind' && UUID == 'capone') || ((filter:includeRegex(QUOTE, '.*kind.*') || BIRTH_DATE == '123') && UUID == 'capone')";
    Assert.assertEquals(expected, JexlStringBuildingVisitor.buildQuery(newTree));
}
Also used : MetadataHelper(datawave.query.util.MetadataHelper) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) ShardQueryConfiguration(datawave.query.config.ShardQueryConfiguration) HashSet(java.util.HashSet) CompositeFunctionsTest(datawave.query.CompositeFunctionsTest) Test(org.junit.Test)

Example 17 with MetadataHelper

use of datawave.query.util.MetadataHelper in project datawave by NationalSecurityAgency.

the class ExecutableExpansionVisitorTest method testDelayedDoubleExpansion.

@Test
public void testDelayedDoubleExpansion() throws Exception {
    ASTJexlScript queryTree = JexlASTHelper.parseJexlQuery("UUID == 'capone' && (((_Delayed_ = true) && QUOTE == 'kind') || " + "((_Delayed_ = true) && BIRTH_DATE == '123'))");
    ShardQueryConfiguration config = EasyMock.createMock(ShardQueryConfiguration.class);
    MetadataHelper helper = EasyMock.createMock(MetadataHelper.class);
    HashSet<String> indexedFields = new HashSet<>();
    indexedFields.add("UUID");
    indexedFields.add("QUOTE");
    EasyMock.expect(config.getIndexedFields()).andReturn(indexedFields).anyTimes();
    Set<String> dataTypes = new HashSet<>();
    dataTypes.add("test");
    // QUOTE being delayed creates a query that is non-executable we cannot delay a field which is nonEvent
    Set<String> nonEventFields = new HashSet<>();
    nonEventFields.add("QUOTE");
    EasyMock.expect(config.getDatatypeFilter()).andReturn(dataTypes).anyTimes();
    EasyMock.expect(helper.getNonEventFields(dataTypes)).andReturn(nonEventFields).anyTimes();
    EasyMock.replay(config, helper);
    ASTJexlScript newTree = ExecutableExpansionVisitor.expand(queryTree, config, helper);
    EasyMock.verify(config, helper);
    // included ExceededValueThresholdMarker before
    Assert.assertTrue(JexlStringBuildingVisitor.buildQuery(queryTree), JexlStringBuildingVisitor.buildQuery(queryTree).equals("UUID == 'capone' && (((_Delayed_ = true) && QUOTE == 'kind') || " + "((_Delayed_ = true) && BIRTH_DATE == '123'))"));
    // starts off executable
    Assert.assertTrue(ExecutableDeterminationVisitor.isExecutable(queryTree, config, helper));
    // what came out is executable
    Assert.assertTrue(ExecutableDeterminationVisitor.isExecutable(newTree, config, helper));
    // the visitor changed nothing
    Assert.assertTrue(JexlStringBuildingVisitor.buildQuery(newTree), JexlStringBuildingVisitor.buildQuery(newTree).equals(JexlStringBuildingVisitor.buildQuery(queryTree)));
}
Also used : MetadataHelper(datawave.query.util.MetadataHelper) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) ShardQueryConfiguration(datawave.query.config.ShardQueryConfiguration) HashSet(java.util.HashSet) CompositeFunctionsTest(datawave.query.CompositeFunctionsTest) Test(org.junit.Test)

Example 18 with MetadataHelper

use of datawave.query.util.MetadataHelper in project datawave by NationalSecurityAgency.

the class ExecutableExpansionVisitorTest method testArbitraryNodeExpansionFailNoFlatten.

@Test
public void testArbitraryNodeExpansionFailNoFlatten() throws Exception {
    ASTJexlScript queryTree = JexlASTHelper.parseJexlQuery("UUID == 'capone' && (filter:includeRegex(QUOTE,'.*kind.*') || QUOTE == 'kind' || BIRTH_DATE == '123')");
    ShardQueryConfiguration config = EasyMock.createMock(ShardQueryConfiguration.class);
    MetadataHelper helper = EasyMock.createMock(MetadataHelper.class);
    HashSet<String> indexedFields = new HashSet<>();
    indexedFields.add("UUID");
    indexedFields.add("QUOTE");
    EasyMock.expect(config.getIndexedFields()).andReturn(indexedFields).anyTimes();
    EasyMock.replay(config, helper);
    // find an orNode in the tree
    ExecutableExpansionVisitor visitor = new ExecutableExpansionVisitor(config, helper);
    Object data = visitor.visit(queryTree.jjtGetChild(0), null);
    EasyMock.verify(config, helper);
    Assert.assertFalse(data instanceof ExecutableExpansionVisitor.ExpansionTracker);
}
Also used : MetadataHelper(datawave.query.util.MetadataHelper) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) ShardQueryConfiguration(datawave.query.config.ShardQueryConfiguration) HashSet(java.util.HashSet) CompositeFunctionsTest(datawave.query.CompositeFunctionsTest) Test(org.junit.Test)

Example 19 with MetadataHelper

use of datawave.query.util.MetadataHelper in project datawave by NationalSecurityAgency.

the class ExecutableExpansionVisitorTest method testNoReferenceOrReferenceExpressions.

@Test
public void testNoReferenceOrReferenceExpressions() throws Exception {
    ASTJexlScript queryTree = JexlASTHelper.parseJexlQuery("UUID == 'capone' && (filter:includeRegex(QUOTE,'.*kind.*') || QUOTE == 'kind' || BIRTH_DATE == '123')");
    // strip reference/referenceExpressions
    queryTree = TreeFlatteningRebuildingVisitor.flattenAll(queryTree);
    ShardQueryConfiguration config = EasyMock.createMock(ShardQueryConfiguration.class);
    MetadataHelper helper = EasyMock.createMock(MetadataHelper.class);
    HashSet<String> indexedFields = new HashSet<>();
    indexedFields.add("UUID");
    indexedFields.add("QUOTE");
    EasyMock.expect(config.getIndexedFields()).andReturn(indexedFields).anyTimes();
    EasyMock.replay(config, helper);
    ASTJexlScript newTree = ExecutableExpansionVisitor.expand(queryTree, config, helper);
    EasyMock.verify(config, helper);
    // starts executable
    Assert.assertFalse(ExecutableDeterminationVisitor.isExecutable(queryTree, config, helper));
    // what came out is executable
    Assert.assertTrue(ExecutableDeterminationVisitor.isExecutable(newTree, config, helper));
    // the visitor changed nothing
    Assert.assertTrue(JexlStringBuildingVisitor.buildQuery(newTree), JexlStringBuildingVisitor.buildQuery(newTree).equals("(QUOTE == 'kind' && UUID == 'capone') || " + "((filter:includeRegex(QUOTE, '.*kind.*') || BIRTH_DATE == '123') && UUID == 'capone')"));
}
Also used : MetadataHelper(datawave.query.util.MetadataHelper) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) ShardQueryConfiguration(datawave.query.config.ShardQueryConfiguration) HashSet(java.util.HashSet) CompositeFunctionsTest(datawave.query.CompositeFunctionsTest) Test(org.junit.Test)

Example 20 with MetadataHelper

use of datawave.query.util.MetadataHelper in project datawave by NationalSecurityAgency.

the class FieldIndexCountQueryLogic method initialize.

@Override
public GenericQueryConfiguration initialize(Connector connection, Query settings, Set<Authorizations> auths) throws Exception {
    if (logger.isTraceEnabled()) {
        logger.trace("initialize");
    }
    this.scannerFactory = new ScannerFactory(connection);
    MetadataHelper metadataHelper = prepareMetadataHelper(connection, this.getMetadataTableName(), auths);
    String modelName = this.getModelName();
    String modelTableName = this.getModelTableName();
    // Check if the default modelName and modelTableNames have been overriden by custom parameters.
    if (null != settings.findParameter(QueryParameters.PARAMETER_MODEL_NAME) && !settings.findParameter(QueryParameters.PARAMETER_MODEL_NAME).getParameterValue().trim().isEmpty()) {
        modelName = settings.findParameter(QueryParameters.PARAMETER_MODEL_NAME).getParameterValue().trim();
    }
    if (null != settings.findParameter(QueryParameters.PARAMETER_MODEL_TABLE_NAME) && !settings.findParameter(QueryParameters.PARAMETER_MODEL_TABLE_NAME).getParameterValue().trim().isEmpty()) {
        modelTableName = settings.findParameter(QueryParameters.PARAMETER_MODEL_TABLE_NAME).getParameterValue().trim();
    }
    if (null != modelName && null == modelTableName) {
        throw new IllegalArgumentException(QueryParameters.PARAMETER_MODEL_NAME + " has been specified but " + QueryParameters.PARAMETER_MODEL_TABLE_NAME + " is missing. Both are required to use a model");
    }
    if (null != modelName && null != modelTableName) {
        this.queryModel = metadataHelper.getQueryModel(modelTableName, modelName, this.getUnevaluatedFields());
    }
    // I'm using this config object in a pinch, we should probably create a custom one.
    ShardQueryConfiguration config = ShardQueryConfiguration.create(this, settings);
    config.setConnector(connection);
    config.setAuthorizations(auths);
    // the following throw IllegalArgumentExceptions if validation fails.
    parseQuery(config, settings);
    configDate(config, settings);
    configTypeFilter(config, settings);
    Set<String> normalizedFieldValues = null;
    Iterator<String> fieldNameIter = fieldNames.iterator();
    while (fieldNameIter.hasNext()) {
        String fieldName = fieldNameIter.next();
        // check that the field name is actually an indexed field
        Set<Type<?>> normalizerSet = metadataHelper.getDatatypesForField(fieldName, config.getDatatypeFilter());
        if (null != normalizerSet && !normalizerSet.isEmpty()) {
            if (null != this.fieldValues && !this.fieldValues.isEmpty()) {
                for (Type<?> norm : normalizerSet) {
                    if (null == normalizedFieldValues) {
                        normalizedFieldValues = new HashSet<>();
                    }
                    for (String val : this.fieldValues) {
                        try {
                            String normVal = norm.normalize(val);
                            if (null != normVal && !normVal.isEmpty()) {
                                normalizedFieldValues.add(normVal);
                            }
                        } catch (Exception e) {
                            logger.debug(norm + " failed to normalize value: " + val);
                        }
                    }
                }
            }
        } else {
            if (logger.isTraceEnabled()) {
                logger.trace("dropping fieldname " + fieldName + " because it's not indexed.");
            }
            // drop fieldName since it isn't indexed.
            fieldNameIter.remove();
        }
    }
    this.fieldValues = normalizedFieldValues;
    if (this.fieldNames.isEmpty()) {
        throw new IllegalArgumentException("Need at least 1 indexed field to query with.");
    }
    // Generate & set the query ranges
    this.ranges = generateRanges(config);
    // Find out if we need to list unique data types.
    if (null != settings.findParameter(Constants.UNIQ_DATATYPE)) {
        this.uniqueByDataType = Boolean.parseBoolean(settings.findParameter(Constants.UNIQ_DATATYPE).getParameterValue());
        if (logger.isTraceEnabled()) {
            logger.trace("uniqueByDataType: " + uniqueByDataType);
        }
    }
    // Find out if we need to list unique visibilities.
    if (null != settings.findParameter(Constants.UNIQ_VISIBILITY)) {
        this.uniqueByVisibility = Boolean.parseBoolean(settings.findParameter(Constants.UNIQ_VISIBILITY).getParameterValue());
        if (logger.isTraceEnabled()) {
            logger.trace("uniqueByVisibility: " + uniqueByVisibility);
        }
    }
    if (logger.isTraceEnabled()) {
        logger.trace("FieldNames: ");
        for (String f : this.fieldNames) {
            logger.trace("\t" + f);
        }
        logger.trace("FieldValues: ");
        if (null == this.fieldValues) {
            logger.trace("\tnone");
        } else {
            for (String f : this.fieldValues) {
                logger.trace("\t" + f);
            }
        }
        logger.trace("uniqueByDataType: " + uniqueByDataType);
        logger.trace("uniqueByVisibility: " + uniqueByVisibility);
    }
    return config;
}
Also used : MetadataHelper(datawave.query.util.MetadataHelper) Type(datawave.data.type.Type) ScannerFactory(datawave.query.tables.ScannerFactory) ShardQueryConfiguration(datawave.query.config.ShardQueryConfiguration) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) QueryException(datawave.webservice.query.exception.QueryException)

Aggregations

MetadataHelper (datawave.query.util.MetadataHelper)21 ShardQueryConfiguration (datawave.query.config.ShardQueryConfiguration)16 HashSet (java.util.HashSet)14 ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)14 CompositeFunctionsTest (datawave.query.CompositeFunctionsTest)13 Test (org.junit.Test)13 ExceededOrThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededOrThresholdMarkerJexlNode)5 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)5 JexlNode (org.apache.commons.jexl2.parser.JexlNode)5 Type (datawave.data.type.Type)3 ScannerFactory (datawave.query.tables.ScannerFactory)2 QueryException (datawave.webservice.query.exception.QueryException)2 Date (java.util.Date)2 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)2 ASTOrNode (org.apache.commons.jexl2.parser.ASTOrNode)2 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 EdgeExtendedSummaryConfiguration (datawave.query.config.EdgeExtendedSummaryConfiguration)1 ShardIndexQueryConfiguration (datawave.query.config.ShardIndexQueryConfiguration)1 DatawaveKey (datawave.query.data.parsers.DatawaveKey)1