use of datawave.query.exceptions.EmptyUnfieldedTermExpansionException in project datawave by NationalSecurityAgency.
the class ShardIndexQueryTable method initialize.
@Override
public GenericQueryConfiguration initialize(Connector connection, Query settings, Set<Authorizations> auths) throws Exception {
ShardIndexQueryConfiguration config = new ShardIndexQueryConfiguration(this, settings);
this.scannerFactory = new ScannerFactory(connection);
MetadataHelper metadataHelper = initializeMetadataHelper(connection, config.getMetadataTableName(), auths);
if (StringUtils.isEmpty(settings.getQuery())) {
throw new IllegalArgumentException("Query cannot be null");
}
if (log.isDebugEnabled()) {
log.debug("Query parameters set to " + settings.getParameters());
}
String tModelName = getTrimmedOrNull(settings, QueryParameters.PARAMETER_MODEL_NAME);
if (tModelName != null) {
modelName = tModelName;
}
String tModelTableName = getTrimmedOrNull(settings, QueryParameters.PARAMETER_MODEL_TABLE_NAME);
if (tModelTableName != null) {
modelTableName = tModelTableName;
}
queryModel = metadataHelper.getQueryModel(modelTableName, modelName, null);
String datatypeFilterString = getTrimmedOrNull(settings, QueryParameters.DATATYPE_FILTER_SET);
if (datatypeFilterString != null) {
config.setDatatypeFilter(new HashSet<>(Arrays.asList(datatypeFilterString.split(PARAM_VALUE_SEP_STR))));
if (log.isDebugEnabled()) {
log.debug("Data type filter set to " + config.getDatatypeFilterAsString());
}
}
config.setConnector(connection);
config.setAuthorizations(auths);
if (indexTableName != null) {
config.setIndexTableName(indexTableName);
}
if (reverseIndexTableName != null) {
config.setReverseIndexTableName(reverseIndexTableName);
}
if (settings.getBeginDate() != null) {
config.setBeginDate(settings.getBeginDate());
} else {
config.setBeginDate(new Date(0));
if (log.isDebugEnabled()) {
log.debug("No begin date supplied in settings.");
}
}
if (settings.getEndDate() != null) {
config.setEndDate(settings.getEndDate());
} else {
config.setEndDate(new Date(Long.MAX_VALUE));
if (log.isDebugEnabled()) {
log.debug("No end date supplied in settings.");
}
}
// start with a trimmed version of the query, converted to JEXL
LuceneToJexlQueryParser parser = new LuceneToJexlQueryParser();
parser.setAllowLeadingWildCard(this.isAllowLeadingWildcard());
QueryNode node = parser.parse(settings.getQuery().trim());
// TODO: Validate that this is a simple list of terms type of query
config.setQueryString(node.getOriginalQuery());
if (log.isDebugEnabled()) {
log.debug("Original Query = " + settings.getQuery().trim());
log.debug("JEXL Query = " + node.getOriginalQuery());
}
// Parse & flatten the query.
ASTJexlScript origScript = JexlASTHelper.parseAndFlattenJexlQuery(config.getQueryString());
ASTJexlScript script;
try {
script = UnfieldedIndexExpansionVisitor.expandUnfielded(config, this.scannerFactory, metadataHelper, origScript);
} catch (EmptyUnfieldedTermExpansionException e) {
Multimap<String, String> emptyMap = Multimaps.unmodifiableMultimap(HashMultimap.create());
config.setNormalizedTerms(emptyMap);
config.setNormalizedPatterns(emptyMap);
return config;
}
Set<String> dataTypes = config.getDatatypeFilter();
Set<String> allFields = metadataHelper.getAllFields(dataTypes);
script = QueryModelVisitor.applyModel(script, queryModel, allFields);
if (log.isTraceEnabled()) {
log.trace("fetching dataTypes from FetchDataTypesVisitor");
}
Multimap<String, Type<?>> fieldToDataTypeMap = FetchDataTypesVisitor.fetchDataTypes(metadataHelper, config.getDatatypeFilter(), script);
config.setDataTypes(fieldToDataTypeMap);
config.setQueryFieldsDatatypes(fieldToDataTypeMap);
final Set<String> indexedFields = metadataHelper.getIndexedFields(dataTypes);
config.setIndexedFields(indexedFields);
final Set<String> reverseIndexedFields = metadataHelper.getReverseIndexedFields(dataTypes);
config.setReverseIndexedFields(reverseIndexedFields);
final Multimap<String, Type<?>> normalizedFields = metadataHelper.getFieldsToDatatypes(dataTypes);
config.setNormalizedFieldsDatatypes(normalizedFields);
if (log.isTraceEnabled()) {
log.trace("Normalizers:");
for (String field : fieldToDataTypeMap.keySet()) {
log.trace(field + ": " + fieldToDataTypeMap.get(field));
}
}
script = ExpandMultiNormalizedTerms.expandTerms(config, metadataHelper, script);
Multimap<String, String> literals = LiteralNodeVisitor.getLiterals(script);
Multimap<String, String> patterns = PatternNodeVisitor.getPatterns(script);
Map<Entry<String, String>, Range> rangesForTerms = Maps.newHashMap();
Map<Entry<String, String>, Entry<Range, Boolean>> rangesForPatterns = Maps.newHashMap();
config.setNormalizedTerms(literals);
config.setNormalizedPatterns(patterns);
if (log.isDebugEnabled()) {
log.debug("Normalized Literals = " + literals);
log.debug("Normalized Patterns = " + patterns);
}
for (Entry<String, String> entry : literals.entries()) {
rangesForTerms.put(entry, ShardIndexQueryTableStaticMethods.getLiteralRange(entry));
}
for (Entry<String, String> entry : patterns.entries()) {
ShardIndexQueryTableStaticMethods.RefactoredRangeDescription r = ShardIndexQueryTableStaticMethods.getRegexRange(entry, isFullTableScanEnabled(), metadataHelper, config);
rangesForPatterns.put(entry, Maps.immutableEntry(r.range, r.isForReverseIndex));
}
config.setRangesForTerms(rangesForTerms);
config.setRangesForPatterns(rangesForPatterns);
return config;
}
use of datawave.query.exceptions.EmptyUnfieldedTermExpansionException in project datawave by NationalSecurityAgency.
the class DefaultQueryPlanner method timedExpandAnyFieldRegexNodes.
protected ASTJexlScript timedExpandAnyFieldRegexNodes(QueryStopwatch timers, final ASTJexlScript script, ShardQueryConfiguration config, MetadataHelper metadataHelper, ScannerFactory scannerFactory, String query) throws DatawaveQueryException {
try {
config.setIndexedFields(metadataHelper.getIndexedFields(config.getDatatypeFilter()));
config.setReverseIndexedFields(metadataHelper.getReverseIndexedFields(config.getDatatypeFilter()));
// @formatter:off
return visitorManager.timedVisit(timers, "Expand ANYFIELD Regex Nodes", () -> {
try {
return UnfieldedIndexExpansionVisitor.expandUnfielded(config, scannerFactory, metadataHelper, script);
} catch (InstantiationException | IllegalAccessException | TableNotFoundException e) {
// rethrow as a datawave query exception because method contracts
throw new DatawaveQueryException(e);
}
});
// @formatter:on
} catch (EmptyUnfieldedTermExpansionException e) {
// The visitor will only throw this if we cannot expand anything resulting in empty query
NotFoundQueryException qe = new NotFoundQueryException(DatawaveErrorCode.UNFIELDED_QUERY_ZERO_MATCHES, e, MessageFormat.format("Query: ", query));
log.info(qe);
throw new NoResultsException(qe);
} catch (TableNotFoundException e) {
QueryException qe = new QueryException(DatawaveErrorCode.METADATA_ACCESS_ERROR, e);
log.info(qe);
throw new DatawaveFatalQueryException(qe);
}
}
use of datawave.query.exceptions.EmptyUnfieldedTermExpansionException in project datawave by NationalSecurityAgency.
the class AllTermsIndexedVisitor method visit.
@Override
public Object visit(ASTJexlScript node, Object data) {
JexlNode copy = (JexlNode) super.visit(node, data);
if (copy.jjtGetNumChildren() == 0) {
NotFoundQueryException qe = new NotFoundQueryException(DatawaveErrorCode.NO_ANYFIELD_EXPANSION_MATCH);
log.warn(qe);
throw new EmptyUnfieldedTermExpansionException(qe);
}
return copy;
}
use of datawave.query.exceptions.EmptyUnfieldedTermExpansionException in project datawave by NationalSecurityAgency.
the class UnfieldedIndexExpansionVisitor method ensureTreeNotEmpty.
private static <T extends JexlNode> T ensureTreeNotEmpty(T script) throws EmptyUnfieldedTermExpansionException {
if (script.jjtGetNumChildren() == 0) {
NotFoundQueryException qe = new NotFoundQueryException(DatawaveErrorCode.NO_UNFIELDED_TERM_EXPANSION_MATCH);
log.warn(qe);
throw new EmptyUnfieldedTermExpansionException(qe);
}
return script;
}
Aggregations