use of datawave.query.jexl.functions.arguments.JexlArgumentDescriptor in project datawave by NationalSecurityAgency.
the class FunctionNormalizationRebuildingVisitor method normalize.
public static JexlNode normalize(ASTFunctionNode function, Multimap<String, Type<?>> allNormalizers, MetadataHelper helper, Set<String> datatypeFilter) {
Preconditions.checkNotNull(function);
Preconditions.checkNotNull(allNormalizers);
Preconditions.checkNotNull(helper);
JexlArgumentDescriptor descriptor = JexlFunctionArgumentDescriptorFactory.F.getArgumentDescriptor(function);
List<ASTFunctionNode> functions = Lists.newArrayList();
// create the distinct normalization lists.
List<List<Type<?>>> lists = getNormalizerListsForArgs(function, allNormalizers, descriptor, helper, datatypeFilter);
// now for each unique list of normalizers, lets normalize the function arguments
for (List<Type<?>> list : lists) {
FunctionNormalizationRebuildingVisitor visitor = new FunctionNormalizationRebuildingVisitor(list, descriptor, helper, datatypeFilter);
ASTFunctionNode node = (ASTFunctionNode) function.jjtAccept(visitor, null);
if (!visitor.normalizationFailed) {
functions.add(node);
}
}
// now reduce the set of functions by eliminating those variants with identical normalized values
Comparator<ASTFunctionNode> comparator = new ASTFunctionNodeComparator();
Collections.sort(functions, comparator);
ASTFunctionNode last = null;
for (Iterator<ASTFunctionNode> it = functions.iterator(); it.hasNext(); ) {
ASTFunctionNode test = it.next();
if (last != null && comparator.compare(last, test) == 0) {
it.remove();
} else {
last = test;
}
}
if (functions.isEmpty()) {
return copy(function);
} else if (1 == functions.size()) {
return functions.iterator().next();
} else {
JexlArgumentDescriptor desc = JexlFunctionArgumentDescriptorFactory.F.getArgumentDescriptor(function);
if (desc.useOrForExpansion()) {
return JexlNodeFactory.createOrNode(functions);
} else {
return JexlNodeFactory.createAndNode(functions);
}
}
}
use of datawave.query.jexl.functions.arguments.JexlArgumentDescriptor in project datawave by NationalSecurityAgency.
the class GeoFeatureVisitor method visit.
@Override
public Object visit(ASTFunctionNode node, Object data) {
JexlArgumentDescriptor desc = JexlFunctionArgumentDescriptorFactory.F.getArgumentDescriptor(node);
try {
if (desc instanceof GeoFunctionsDescriptor.GeoJexlArgumentDescriptor) {
JexlNode geowaveNode = ((GeoFunctionsDescriptor.GeoJexlArgumentDescriptor) desc).toGeoWaveFunction(desc.fields(null, null));
geowaveNode.jjtAccept(this, JexlStringBuildingVisitor.buildQuery(node));
} else if (desc instanceof GeoWaveFunctionsDescriptor.GeoWaveJexlArgumentDescriptor) {
String function = null;
if (data != null)
function = (String) data;
else
function = JexlStringBuildingVisitor.buildQuery(node);
// reformat as a lucene function
if (isLuceneQuery) {
int paramsIdx = function.indexOf('(');
String op = function.substring(0, function.indexOf('('));
String params = function.substring(paramsIdx);
if (op.startsWith("geowave:")) {
function = op.replace("geowave:", "#").toUpperCase() + params;
} else if (op.startsWith("geo:")) {
String opParam = op.substring("geo:within_".length());
function = "#GEO(" + opParam + ", " + params.substring(1);
}
}
String geometry = geoJson.toString(wktReader.read(((GeoWaveFunctionsDescriptor.GeoWaveJexlArgumentDescriptor) desc).getWkt()));
geoFeatures.add(new QueryGeometry(function, geometry));
}
} catch (Exception e) {
log.error("Unable to extract geo feature from function", e);
}
return node;
}
use of datawave.query.jexl.functions.arguments.JexlArgumentDescriptor in project datawave by NationalSecurityAgency.
the class GeoWavePruningVisitor method visit.
@Override
public Object visit(ASTAndNode node, Object data) {
Multimap<String, Geometry> fieldToGeometryMap = (data instanceof Multimap) ? (Multimap<String, Geometry>) data : HashMultimap.create();
// if one of the anded nodes is a geowave function, pass down the geometry and field name in the multimap
for (JexlNode child : children(node)) {
child = JexlASTHelper.dereference(child);
if (child instanceof ASTFunctionNode) {
JexlArgumentDescriptor desc = JexlFunctionArgumentDescriptorFactory.F.getArgumentDescriptor((ASTFunctionNode) child);
if (desc instanceof GeoWaveFunctionsDescriptor.GeoWaveJexlArgumentDescriptor) {
GeoWaveFunctionsDescriptor.GeoWaveJexlArgumentDescriptor geoWaveDesc = (GeoWaveFunctionsDescriptor.GeoWaveJexlArgumentDescriptor) desc;
Geometry geom = GeometryNormalizer.parseGeometry(geoWaveDesc.getWkt());
Set<String> fields = geoWaveDesc.fields(metadataHelper, Collections.emptySet());
for (String field : fields) {
fieldToGeometryMap.put(field, geom);
}
}
}
}
return super.visit(node, (fieldToGeometryMap.isEmpty() ? null : fieldToGeometryMap));
}
use of datawave.query.jexl.functions.arguments.JexlArgumentDescriptor in project datawave by NationalSecurityAgency.
the class ContentFunctionsTest method testJexlFunctionArgumentDescriptors.
private void testJexlFunctionArgumentDescriptors(String query, String expected, Set<String> contentFields) throws ParseException {
MockMetadataHelper metadataHelper = new MockMetadataHelper();
metadataHelper.addTermFrequencyFields(Arrays.asList("BODY", "META"));
metadataHelper.setIndexedFields(Sets.newHashSet("BODY", "META"));
if (contentFields != null) {
metadataHelper.addContentFields(contentFields);
}
MockDateIndexHelper dateIndexHelper = new MockDateIndexHelper();
ASTJexlScript script = JexlASTHelper.parseJexlQuery(query);
JexlNode ref = script.jjtGetChild(0);
Assert.assertEquals("First child of ASTJexlScript is not an ASTReference", ASTReference.class, ref.getClass());
JexlNode child = ref.jjtGetChild(0);
Assert.assertEquals("First child of ASTJexlScript is not an AStFunctionNode", ASTFunctionNode.class, child.getClass());
ASTFunctionNode function = (ASTFunctionNode) child;
JexlArgumentDescriptor desc = new ContentFunctionsDescriptor().getArgumentDescriptor(function);
JexlNode indexQuery = desc.getIndexQuery(null, metadataHelper, dateIndexHelper, null);
ASTJexlScript expectedScript = JexlASTHelper.parseJexlQuery(expected);
JexlNode scriptChild = expectedScript.jjtGetChild(0);
Assert.assertTrue("Expected " + JexlStringBuildingVisitor.buildQuery(scriptChild) + " but was " + JexlStringBuildingVisitor.buildQuery(indexQuery), JexlASTHelper.equals(scriptChild, indexQuery));
}
use of datawave.query.jexl.functions.arguments.JexlArgumentDescriptor in project datawave by NationalSecurityAgency.
the class GeoFunctionsDescriptorTest method antiMeridianTest2.
@Test
public void antiMeridianTest2() throws Exception {
String query = "geo:within_bounding_box(LON_FIELD, LAT_FIELD, '170', '40', '-170', '50')";
JexlNode node = JexlASTHelper.parseJexlQuery(query);
JexlArgumentDescriptor argDesc = new GeoFunctionsDescriptor().getArgumentDescriptor((ASTFunctionNode) node.jjtGetChild(0).jjtGetChild(0));
JexlNode queryNode = argDesc.getIndexQuery(null, null, null, null);
Assert.assertEquals("((((_Bounded_ = true) && (LON_FIELD >= '170.0' && LON_FIELD <= '180')) && ((_Bounded_ = true) && (LAT_FIELD >= '40.0' && LAT_FIELD <= '50.0'))) && (((_Bounded_ = true) && (LON_FIELD >= '-180' && LON_FIELD <= '-170.0')) && ((_Bounded_ = true) && (LAT_FIELD >= '40.0' && LAT_FIELD <= '50.0'))))", JexlStringBuildingVisitor.buildQuery(queryNode));
}
Aggregations