Github user jihoonson commented on a diff in the pull request:
https://github.com/apache/tajo/pull/772#discussion_r39945794
--- Diff:
tajo-plan/src/main/java/org/apache/tajo/plan/expr/AlgebraicUtil.java ---
@@ -493,4 +497,190 @@ public Object visitTimeLiteral(Object ctx,
Stack<Expr> stack, TimeLiteral expr)
return super.visitTimeLiteral(ctx, stack, expr);
}
}
+
+ /**
+ * Find the top expr matched to type from the given expr
+ *
+ * @param expr start expr
+ * @param type to find
+ * @return a found expr
+ */
+ public static <T extends Expr> T findTopExpr(Expr expr, OpType type)
throws TajoException {
+ Preconditions.checkNotNull(expr);
+ Preconditions.checkNotNull(type);
+
+ ExprFinder finder = new ExprFinder(type);
+ finder.visit(null, new Stack<Expr>(), expr);
+
+ if (finder.getFoundExprs().size() == 0) {
+ return null;
+ }
+ return (T) finder.getFoundExprs().get(0);
+ }
+
+ private static class ExprFinder extends SimpleAlgebraVisitor<Object,
Expr> {
+ private List<Expr> list = new ArrayList<Expr>();
+ private final OpType[] tofind;
+ private boolean topmost = false;
+ private boolean finished = false;
+
+ public ExprFinder(OpType... type) {
+
+ this.tofind = type;
+ }
+
+ public ExprFinder(OpType[] type, boolean topmost) {
+ this(type);
+ this.topmost = topmost;
+ }
+
+ @Override
+ public Expr visit(Object ctx, Stack<Expr> stack, Expr expr) throws
TajoException {
+ if (!finished) {
+ for (OpType type : tofind) {
+ if (expr.getType() == type) {
+ list.add(expr);
+ }
+ if (topmost && list.size() > 0) {
+ finished = true;
+ }
+ }
+ }
+ return super.visit(ctx, stack, expr);
+ }
+
+ public List<Expr> getFoundExprs() {
+ return list;
+ }
+
+ }
+
+ public static Expr[] toConjunctiveNormalFormArray(Expr expr) {
+ List<Expr> list = new ArrayList<Expr>();
+ toConjunctiveNormalFormArrayRecursive(expr, list);
+ return list.toArray(new Expr[list.size()]);
+ }
+
+ private static void toConjunctiveNormalFormArrayRecursive(Expr node,
List<Expr> found) {
--- End diff --
Unnecessary change.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---