Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tajo.plan.expr;
import org.apache.tajo.algebra.*;
import org.apache.tajo.catalog.Column;
import org.apache.tajo.exception.TajoException;
import org.apache.tajo.plan.visitor.SimpleAlgebraVisitor;
import java.util.*;
public class AlgebraicUtil {
/**
* Transpose a given comparison expression into the expression
* where the variable corresponding to the target is placed
* on the left-hand side.
*
* @param evalNode
* @param target
* @return Transposed expression
*/
public static EvalNode transpose(EvalNode evalNode, Column target) {
BinaryEval commutated;
if (evalNode instanceof BinaryEval) { // if it is binary
BinaryEval binaryEval = (BinaryEval) evalNode;
// If the variable is in the right term, inverse the expr.
if (!EvalTreeUtil.containColumnRef(binaryEval.getLeftExpr(), target)) {
// the commutate method works with a copy of the expr
commutated = commutate(binaryEval);
} else {
try {
commutated = (BinaryEval) evalNode.clone();
} catch (CloneNotSupportedException e) {
throw new AlgebraicException(e);
}
}
return _transpose(commutated, target);
} else {
return evalNode;
}
}
private static EvalNode _transpose(BinaryEval _expr, Column target) {
EvalNode expr = eliminateConstantExprs(_expr);
if (expr instanceof BinaryEval) { // only if expr is a binary operator
BinaryEval binaryEval = (BinaryEval) expr;
if (isSingleVar(binaryEval.getLeftExpr())) {
return expr;
}
EvalNode left = binaryEval.getLeftExpr();
EvalNode lTerm = null;
EvalNode rTerm = null;
if (EvalType.isArithmeticOperator(left.getType())) { // we can ensure that left is binary.
// If the left-left term is a variable, the left-right term is transposed.
if (EvalTreeUtil.containColumnRef(((BinaryEval)left).getLeftExpr(), target)) {
PartialBinaryExpr tmpTerm = splitRightTerm((BinaryEval) left);
tmpTerm.type = inverseOperator(tmpTerm.type);
tmpTerm.setLeftExpr(((BinaryEval)expr).getRightExpr());
lTerm = ((BinaryEval)left).getLeftExpr();
rTerm = new BinaryEval(tmpTerm);
} else {
// Otherwise, the left-right term is transposed into the left-left term.
PartialBinaryExpr tmpTerm = splitLeftTerm((BinaryEval) left);
tmpTerm.type = inverseOperator(tmpTerm.type);
tmpTerm.setLeftExpr(((BinaryEval)expr).getRightExpr());
lTerm = ((BinaryEval)left).getRightExpr();
rTerm = new BinaryEval(tmpTerm);
}
}
return _transpose(new BinaryEval(expr.getType(), lTerm, rTerm), target);
} else {
return _expr;
}
}
/**
* Inverse a given operator (+, -, *, /)
*
* @param type
* @return inversed operator type
*/
public static EvalType inverseOperator(EvalType type) {
switch (type) {
case PLUS:
return EvalType.MINUS;
case MINUS:
return EvalType.PLUS;
case MULTIPLY:
return EvalType.DIVIDE;
case DIVIDE:
return EvalType.MULTIPLY;
default : throw new AlgebraicException("ERROR: cannot inverse the operator: "
+ type);
}
}
/**
* Examine if a given expr is a variable.
*
* @param node
* @return true if a given expr is a variable.
*/
private static boolean isSingleVar(EvalNode node) {
if (node.getType() == EvalType.FIELD) {
return true;
} else {
return false;
}
}
private static class AlgebraicOptimizer extends SimpleEvalNodeVisitor