use of org.apache.calcite.rex.RexCall in project drill by axbaretto.
the class RewriteProjectToFlatten method visitProject.
@Override
public Prel visitProject(ProjectPrel node, Object unused) throws RelConversionException {
ProjectPrel project = node;
List<RexNode> exprList = new ArrayList<>();
boolean rewrite = false;
List<RelDataTypeField> relDataTypes = new ArrayList<>();
int i = 0;
RexNode flatttenExpr = null;
for (RexNode rex : project.getChildExps()) {
RexNode newExpr = rex;
if (rex instanceof RexCall) {
RexCall function = (RexCall) rex;
String functionName = function.getOperator().getName();
if (functionName.equalsIgnoreCase("flatten")) {
rewrite = true;
if (function.getOperands().size() != 1) {
throw new RelConversionException("Flatten expression expects a single input.");
}
newExpr = function.getOperands().get(0);
RexBuilder builder = new RexBuilder(factory);
flatttenExpr = builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), i);
}
}
relDataTypes.add(project.getRowType().getFieldList().get(i));
i++;
exprList.add(newExpr);
}
if (rewrite == true) {
// TODO - figure out what is the right setting for the traits
Prel newChild = ((Prel) project.getInput(0)).accept(this, null);
ProjectPrel newProject = new ProjectPrel(node.getCluster(), project.getTraitSet(), newChild, exprList, new RelRecordType(relDataTypes));
FlattenPrel flatten = new FlattenPrel(project.getCluster(), project.getTraitSet(), newProject, flatttenExpr);
return flatten;
}
Prel child = ((Prel) project.getInput()).accept(this, null);
return (Prel) project.copy(project.getTraitSet(), child, exprList, new RelRecordType(relDataTypes));
}
use of org.apache.calcite.rex.RexCall in project drill by apache.
the class RexVisitorComplexExprSplitter method visitCall.
@Override
public RexNode visitCall(RexCall call) {
final List<RexNode> newOps = new ArrayList<>();
for (RexNode operand : call.operands) {
RexNode newOp = operand.accept(this);
newOps.add(newOp);
}
final RexCall newCall = call.clone(call.getType(), newOps);
String functionName = call.getOperator().getName();
if (funcReg.isFunctionComplexOutput(functionName)) {
RexNode ret = rexBuilder.makeInputRef(newCall.getType(), lastUsedIndex++);
complexExprs.add(newCall);
return ret;
} else {
return newCall;
}
}
use of org.apache.calcite.rex.RexCall in project drill by apache.
the class DrillRelOptUtil method analyzeSimpleEquiJoin.
/**
* Returns whether the join condition is a simple equi-join or not. A simple equi-join is
* defined as an two-table equality join (no self-join)
* @param join input join
* @param joinFieldOrdinals join field ordinal w.r.t. the underlying inputs to the join
* @return TRUE if the join is a simple equi-join (not a self-join), FALSE otherwise
*/
public static boolean analyzeSimpleEquiJoin(Join join, int[] joinFieldOrdinals) {
RexNode joinExp = join.getCondition();
if (joinExp.getKind() != SqlKind.EQUALS) {
return false;
} else {
RexCall binaryExpression = (RexCall) joinExp;
RexNode leftComparand = binaryExpression.operands.get(0);
RexNode rightComparand = binaryExpression.operands.get(1);
if (!(leftComparand instanceof RexInputRef)) {
return false;
} else if (!(rightComparand instanceof RexInputRef)) {
return false;
} else {
int leftFieldCount = join.getLeft().getRowType().getFieldCount();
int rightFieldCount = join.getRight().getRowType().getFieldCount();
RexInputRef leftFieldAccess = (RexInputRef) leftComparand;
RexInputRef rightFieldAccess = (RexInputRef) rightComparand;
if (leftFieldAccess.getIndex() >= leftFieldCount + rightFieldCount || rightFieldAccess.getIndex() >= leftFieldCount + rightFieldCount) {
return false;
}
/* Both columns reference same table */
if ((leftFieldAccess.getIndex() >= leftFieldCount && rightFieldAccess.getIndex() >= leftFieldCount) || (leftFieldAccess.getIndex() < leftFieldCount && rightFieldAccess.getIndex() < leftFieldCount)) {
return false;
} else {
if (leftFieldAccess.getIndex() < leftFieldCount) {
joinFieldOrdinals[0] = leftFieldAccess.getIndex();
joinFieldOrdinals[1] = rightFieldAccess.getIndex() - leftFieldCount;
} else {
joinFieldOrdinals[0] = rightFieldAccess.getIndex();
joinFieldOrdinals[1] = leftFieldAccess.getIndex() - leftFieldCount;
}
return true;
}
}
}
}
use of org.apache.calcite.rex.RexCall in project drill by apache.
the class NumericEquiDepthHistogram method getValuesRange.
private Range<Double> getValuesRange(List<RexNode> filterList, Range<Double> fullRange, List<RexNode> unkownFilterList) {
Range<Double> currentRange = fullRange;
for (RexNode filter : filterList) {
if (filter instanceof RexCall) {
Double value = getLiteralValue(filter);
if (value == null) {
unkownFilterList.add(filter);
} else {
// get the operator
SqlOperator op = ((RexCall) filter).getOperator();
Range range;
switch(op.getKind()) {
case GREATER_THAN:
range = Range.greaterThan(value);
currentRange = currentRange.intersection(range);
break;
case GREATER_THAN_OR_EQUAL:
range = Range.atLeast(value);
currentRange = currentRange.intersection(range);
break;
case LESS_THAN:
range = Range.lessThan(value);
currentRange = currentRange.intersection(range);
break;
case LESS_THAN_OR_EQUAL:
range = Range.atMost(value);
currentRange = currentRange.intersection(range);
break;
default:
break;
}
}
}
}
return currentRange;
}
use of org.apache.calcite.rex.RexCall in project drill by apache.
the class DrillProjectRelBase method getSimpleFieldCount.
private int getSimpleFieldCount() {
int cnt = 0;
final ComplexFieldWithNamedSegmentIdentifier complexFieldIdentifer = new ComplexFieldWithNamedSegmentIdentifier();
// a + b, a * 10 + b, etc are not simple fields, since they are expressions.
for (RexNode expr : this.getProjects()) {
if (expr instanceof RexInputRef) {
// Simple Field reference.
cnt++;
} else if (expr instanceof RexCall && expr.accept(complexFieldIdentifer)) {
// Complex field with named segments only.
cnt++;
}
}
return cnt;
}
Aggregations