-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft: Add query field rule #2672
Open
hlgp
wants to merge
7
commits into
integration
Choose a base branch
from
feature/fieldRuleVisitor
base: integration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5088473
Add query field rule
hlgp 1404a24
Add QueryPlanningStage (missed in first commit)
hlgp 9f5d399
Merge branch 'integration' into feature/fieldRuleVisitor
hlgp c9cb7a5
Merge branch 'integration' into feature/fieldRuleVisitor
hlgp 0244934
Merge branch 'integration' into feature/fieldRuleVisitor
apmoriarty 2f19c31
Merge branch 'integration' into feature/fieldRuleVisitor
hlgp 877ad29
updates to allow rules to access node lineage
hlgp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
warehouse/query-core/src/main/java/datawave/query/jexl/visitors/QueryFieldsRuleVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package datawave.query.jexl.visitors; | ||
|
||
import java.util.HashSet; | ||
|
||
import org.apache.commons.jexl3.parser.ASTJexlScript; | ||
|
||
import datawave.query.model.QueryModel; | ||
import datawave.query.planner.QueryPlanningStage; | ||
import datawave.query.util.MetadataHelper; | ||
|
||
public class QueryFieldsRuleVisitor extends BaseVisitor { | ||
private final MetadataHelper helper; | ||
private final QueryModel model; | ||
private final QueryPlanningStage stage; | ||
|
||
public QueryFieldsRuleVisitor(MetadataHelper helper, QueryModel model, QueryPlanningStage stage) { | ||
this.helper = helper; | ||
this.model = model; | ||
this.stage = stage; | ||
} | ||
|
||
public static ASTJexlScript applyRules(ASTJexlScript script, MetadataHelper helper, QueryModel model, QueryPlanningStage stage) { | ||
QueryFieldsRuleVisitor ruleVisitor = new QueryFieldsRuleVisitor(helper, model, stage); | ||
return (ASTJexlScript) script.jjtAccept(ruleVisitor, new HashSet<>()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
warehouse/query-core/src/main/java/datawave/query/planner/QueryPlanningStage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package datawave.query.planner; | ||
|
||
public class QueryPlanningStage { | ||
public enum PLAN_STAGE { | ||
PRE_MODEL_EXPANSION, POST_MODEL_EXPANSION, POST_INDEX_EXPANSION | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
warehouse/query-core/src/main/java/datawave/query/planner/rules/FieldRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package datawave.query.planner.rules; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.apache.commons.jexl3.parser.JexlNode; | ||
|
||
import datawave.core.query.configuration.GenericQueryConfiguration; | ||
import datawave.query.util.MetadataHelper; | ||
|
||
public abstract class FieldRule { | ||
Set<String> pruneFields = new HashSet<>(); | ||
Map<String,Set<String>> pruneFVPairs = new HashMap<>(); | ||
|
||
public FieldRule(GenericQueryConfiguration config) { | ||
parseRules(config); | ||
} | ||
|
||
abstract void parseRules(GenericQueryConfiguration config); | ||
|
||
public abstract boolean shouldPrune(JexlNode node, MetadataHelper helper); | ||
|
||
public abstract boolean shouldModify(JexlNode node, MetadataHelper helper); | ||
|
||
public abstract JexlNode modify(JexlNode node, MetadataHelper helper); | ||
} |
37 changes: 37 additions & 0 deletions
37
warehouse/query-core/src/main/java/datawave/query/planner/rules/FieldTransformRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package datawave.query.planner.rules; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
|
||
import org.apache.commons.jexl3.parser.ASTFalseNode; | ||
import org.apache.commons.jexl3.parser.JexlNode; | ||
import org.apache.commons.jexl3.parser.ParserTreeConstants; | ||
|
||
import datawave.core.query.configuration.GenericQueryConfiguration; | ||
import datawave.query.config.ShardQueryConfiguration; | ||
import datawave.query.util.MetadataHelper; | ||
|
||
public class FieldTransformRule implements NodeTransformRule { | ||
FieldRule rule; | ||
JexlNode falseNode = new ASTFalseNode(ParserTreeConstants.JJTFALSENODE); | ||
|
||
@Override | ||
public JexlNode apply(JexlNode node, ShardQueryConfiguration config, MetadataHelper helper) { | ||
if (rule.shouldPrune(node, helper)) { | ||
return falseNode; | ||
} | ||
if (rule.shouldModify(node, helper)) { | ||
node = rule.modify(node, helper); | ||
} | ||
return node; | ||
} | ||
|
||
public void setupRules(ShardQueryConfiguration config) { | ||
try { | ||
Class<? extends FieldRule> ruleClass = Class.forName(config.getFieldRuleClassName()).asSubclass(FieldRule.class); | ||
rule = ruleClass.getDeclaredConstructor(GenericQueryConfiguration.class).newInstance(config); | ||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException e) { | ||
throw new RuntimeException("Unable to load pruning rules for " + config.getFieldRuleClassName()); | ||
} | ||
|
||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
...ouse/query-core/src/main/java/datawave/query/planner/rules/FieldTransformRuleVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package datawave.query.planner.rules; | ||
|
||
import static com.google.common.collect.Lists.newArrayList; | ||
import static org.apache.commons.jexl3.parser.JexlNodes.newInstanceOfType; | ||
import static org.apache.commons.jexl3.parser.JexlNodes.setChildren; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.commons.jexl3.parser.ASTAndNode; | ||
import org.apache.commons.jexl3.parser.ASTEQNode; | ||
import org.apache.commons.jexl3.parser.ASTERNode; | ||
import org.apache.commons.jexl3.parser.ASTFunctionNode; | ||
import org.apache.commons.jexl3.parser.ASTGENode; | ||
import org.apache.commons.jexl3.parser.ASTGTNode; | ||
import org.apache.commons.jexl3.parser.ASTJexlScript; | ||
import org.apache.commons.jexl3.parser.ASTLENode; | ||
import org.apache.commons.jexl3.parser.ASTLTNode; | ||
import org.apache.commons.jexl3.parser.ASTNENode; | ||
import org.apache.commons.jexl3.parser.ASTNRNode; | ||
import org.apache.commons.jexl3.parser.ASTNotNode; | ||
import org.apache.commons.jexl3.parser.ASTOrNode; | ||
import org.apache.commons.jexl3.parser.ASTReferenceExpression; | ||
import org.apache.commons.jexl3.parser.JexlNode; | ||
|
||
import datawave.query.config.ShardQueryConfiguration; | ||
import datawave.query.jexl.nodes.QueryPropertyMarker; | ||
import datawave.query.jexl.visitors.RebuildingVisitor; | ||
import datawave.query.util.MetadataHelper; | ||
|
||
public class FieldTransformRuleVisitor extends NodeTransformVisitor { | ||
|
||
public FieldTransformRuleVisitor(ShardQueryConfiguration config, MetadataHelper helper, List<NodeTransformRule> rules) { | ||
super(config, helper, rules); | ||
} | ||
|
||
public static ASTJexlScript transform(ASTJexlScript tree, List<NodeTransformRule> rules, ShardQueryConfiguration config, MetadataHelper helper) { | ||
FieldTransformRuleVisitor visitor = new FieldTransformRuleVisitor(config, helper, rules); | ||
return visitor.apply(tree); | ||
} | ||
|
||
private <T extends JexlNode> T copy(T node, Object data) { | ||
T newNode = newInstanceOfType(node); | ||
// keep lineage | ||
newNode.jjtSetParent(node.jjtGetParent()); | ||
ArrayList<JexlNode> children = newArrayList(); | ||
for (int i = 0; i < node.jjtGetNumChildren(); i++) { | ||
JexlNode copiedChild = (JexlNode) node.jjtGetChild(i).jjtAccept(this, data); | ||
if (copiedChild != null) { | ||
children.add(copiedChild); | ||
} | ||
} | ||
return setChildren(newNode, children.toArray(new JexlNode[children.size()])); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTOrNode node, Object data) { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTAndNode node, Object data) { | ||
// do not recurse on a marker node | ||
if (QueryPropertyMarker.findInstance(node).isAnyType()) { | ||
return applyTransforms(RebuildingVisitor.copy(node)); | ||
} else { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
} | ||
|
||
@Override | ||
public Object visit(ASTEQNode node, Object data) { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTNENode node, Object data) { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTLTNode node, Object data) { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTGTNode node, Object data) { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTLENode node, Object data) { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTGENode node, Object data) { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTERNode node, Object data) { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTNRNode node, Object data) { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTNotNode node, Object data) { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTFunctionNode node, Object data) { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTReferenceExpression node, Object data) { | ||
return applyTransforms(copy(node, data)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only one? Do we want to be able to have a list of rule classes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to just start with one for now. Pretty easy to update if we need multiple, but haven't seen that use case yet.