use of org.apache.calcite.rex.RexCall in project druid by druid-io.
the class ThetaSketchSetBaseOperatorConversion method toPostAggregator.
@Nullable
@Override
public PostAggregator toPostAggregator(PlannerContext plannerContext, RowSignature rowSignature, RexNode rexNode, PostAggregatorVisitor postAggregatorVisitor) {
final List<RexNode> operands = ((RexCall) rexNode).getOperands();
final List<PostAggregator> inputPostAggs = new ArrayList<>();
Integer size = null;
int operandCounter = 0;
for (RexNode operand : operands) {
final PostAggregator convertedPostAgg = OperatorConversions.toPostAggregator(plannerContext, rowSignature, operand, postAggregatorVisitor);
if (convertedPostAgg == null) {
if (operandCounter == 0) {
try {
if (!operand.isA(SqlKind.LITERAL)) {
return null;
}
size = RexLiteral.intValue(operand);
} catch (ClassCastException cce) {
return null;
}
} else {
return null;
}
} else {
inputPostAggs.add(convertedPostAgg);
operandCounter++;
}
}
return new SketchSetPostAggregator(postAggregatorVisitor.getOutputNamePrefix() + postAggregatorVisitor.getAndIncrementCounter(), getSetOperationName(), size, inputPostAggs);
}
use of org.apache.calcite.rex.RexCall in project hive by apache.
the class HiveCalciteUtil method getTypeSafePred.
public static RexNode getTypeSafePred(RelOptCluster cluster, RexNode rex, RelDataType rType) {
RexNode typeSafeRex = rex;
if ((typeSafeRex instanceof RexCall) && HiveCalciteUtil.isComparisonOp((RexCall) typeSafeRex)) {
RexBuilder rb = cluster.getRexBuilder();
List<RexNode> fixedPredElems = new ArrayList<RexNode>();
RelDataType commonType = cluster.getTypeFactory().leastRestrictive(RexUtil.types(((RexCall) rex).getOperands()));
for (RexNode rn : ((RexCall) rex).getOperands()) {
fixedPredElems.add(rb.ensureType(commonType, rn, true));
}
typeSafeRex = rb.makeCall(((RexCall) typeSafeRex).getOperator(), fixedPredElems);
}
return typeSafeRex;
}
use of org.apache.calcite.rex.RexCall in project hive by apache.
the class HiveCalciteUtil method checkMaterializable.
/**
* Check if the expression is usable for query materialization, returning the first failing expression.
*/
public static RexCall checkMaterializable(RexNode expr) {
RexCall failingCall = null;
if (expr == null) {
return null;
}
RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
@Override
public Void visitCall(org.apache.calcite.rex.RexCall call) {
// non-deterministic functions as well as runtime constants are not materializable.
SqlOperator op = call.getOperator();
if (!op.isDeterministic() || op.isDynamicFunction() || (op instanceof HiveSqlFunction && ((HiveSqlFunction) op).isRuntimeConstant())) {
throw new Util.FoundOne(call);
}
return super.visitCall(call);
}
};
try {
expr.accept(visitor);
} catch (Util.FoundOne e) {
failingCall = (RexCall) e.getNode();
}
return failingCall;
}
use of org.apache.calcite.rex.RexCall in project hive by apache.
the class HiveRelOptUtil method getNewRelFieldCollations.
/**
* Map Sort and SortExchange keys to the specified Project columns.
* @param project the Project
* @param sortCollation current collation in Sort
* @param cluster RelOptCluster
* @return new collation should be used in the Sort
*/
public static List<RelFieldCollation> getNewRelFieldCollations(HiveProject project, RelCollation sortCollation, RelOptCluster cluster) {
// Determine mapping between project input and output fields.
// In Hive, Sort is always based on RexInputRef
// HiveSort*PullUpConstantsRule should remove constants (RexLiteral)
// We only need to check if project can contain all the positions that sortCollation needs.
final Mappings.TargetMapping map = RelOptUtil.permutationIgnoreCast(project.getProjects(), project.getInput().getRowType()).inverse();
Set<Integer> needed = new HashSet<>();
for (RelFieldCollation fc : sortCollation.getFieldCollations()) {
needed.add(fc.getFieldIndex());
final RexNode node = project.getProjects().get(map.getTarget(fc.getFieldIndex()));
if (node.isA(SqlKind.CAST)) {
// Check whether it is a monotonic preserving cast, otherwise we cannot push
final RexCall cast = (RexCall) node;
final RexCallBinding binding = RexCallBinding.create(cluster.getTypeFactory(), cast, ImmutableList.of(RexUtil.apply(map, sortCollation)));
if (cast.getOperator().getMonotonicity(binding) == SqlMonotonicity.NOT_MONOTONIC) {
return null;
}
}
}
Map<Integer, Integer> m = new HashMap<>();
for (int projPos = 0; projPos < project.getProjects().size(); projPos++) {
RexNode expr = project.getProjects().get(projPos);
if (expr instanceof RexInputRef) {
Set<Integer> positions = HiveCalciteUtil.getInputRefs(expr);
if (positions.size() <= 1) {
int parentPos = positions.iterator().next();
if (needed.contains(parentPos)) {
m.put(parentPos, projPos);
needed.remove(parentPos);
}
}
}
}
if (!needed.isEmpty()) {
return null;
}
List<RelFieldCollation> fieldCollations = new ArrayList<>();
for (RelFieldCollation fc : sortCollation.getFieldCollations()) {
fieldCollations.add(new RelFieldCollation(m.get(fc.getFieldIndex()), fc.direction, fc.nullDirection));
}
return fieldCollations;
}
use of org.apache.calcite.rex.RexCall in project hive by apache.
the class HiveRelOptUtil method splitCorrelatedFilterCondition.
private static void splitCorrelatedFilterCondition(Filter filter, RexNode condition, List<RexNode> joinKeys, List<RexNode> correlatedJoinKeys, List<RexNode> nonEquiList, boolean extractCorrelatedFieldAccess) {
if (condition instanceof RexCall) {
RexCall call = (RexCall) condition;
if (call.getOperator().getKind() == SqlKind.AND) {
for (RexNode operand : call.getOperands()) {
splitCorrelatedFilterCondition(filter, operand, joinKeys, correlatedJoinKeys, nonEquiList, extractCorrelatedFieldAccess);
}
return;
}
if (call.getOperator().getKind() == SqlKind.EQUALS) {
final List<RexNode> operands = call.getOperands();
RexNode op0 = operands.get(0);
RexNode op1 = operands.get(1);
if (extractCorrelatedFieldAccess) {
if (!RexUtil.containsFieldAccess(op0) && (op1 instanceof RexFieldAccess)) {
joinKeys.add(op0);
correlatedJoinKeys.add(op1);
return;
} else if ((op0 instanceof RexFieldAccess) && !RexUtil.containsFieldAccess(op1)) {
correlatedJoinKeys.add(op0);
joinKeys.add(op1);
return;
}
} else {
if (!(RexUtil.containsInputRef(op0)) && (op1 instanceof RexInputRef)) {
correlatedJoinKeys.add(op0);
joinKeys.add(op1);
return;
} else if ((op0 instanceof RexInputRef) && !(RexUtil.containsInputRef(op1))) {
joinKeys.add(op0);
correlatedJoinKeys.add(op1);
return;
}
}
}
}
// The operator is not of RexCall type
// So we fail. Fall through.
// Add this condition to the list of non-equi-join conditions.
nonEquiList.add(condition);
}
Aggregations