use of org.apache.calcite.rex.RexCall in project calcite by apache.
the class RelDecorrelator method findCorrelationEquivalent.
/**
* Finds a {@link RexInputRef} that is equivalent to a {@link CorRef},
* and if found, throws a {@link org.apache.calcite.util.Util.FoundOne}.
*/
private void findCorrelationEquivalent(CorRef correlation, RexNode e) throws Util.FoundOne {
switch(e.getKind()) {
case EQUALS:
final RexCall call = (RexCall) e;
final List<RexNode> operands = call.getOperands();
if (references(operands.get(0), correlation)) {
throw new Util.FoundOne(operands.get(1));
}
if (references(operands.get(1), correlation)) {
throw new Util.FoundOne(operands.get(0));
}
break;
case AND:
for (RexNode operand : ((RexCall) e).getOperands()) {
findCorrelationEquivalent(correlation, operand);
}
}
}
use of org.apache.calcite.rex.RexCall in project calcite by apache.
the class RelBuilder method inferAlias.
/**
* Infers the alias of an expression.
*
* <p>If the expression was created by {@link #alias}, replaces the expression
* in the project list.
*/
private String inferAlias(List<RexNode> exprList, RexNode expr) {
switch(expr.getKind()) {
case INPUT_REF:
final RexInputRef ref = (RexInputRef) expr;
return stack.peek().fields.get(ref.getIndex()).getValue().getName();
case CAST:
return inferAlias(exprList, ((RexCall) expr).getOperands().get(0));
case AS:
final RexCall call = (RexCall) expr;
for (; ; ) {
final int i = exprList.indexOf(expr);
if (i < 0) {
break;
}
exprList.set(i, call.getOperands().get(0));
}
return ((NlsString) ((RexLiteral) call.getOperands().get(1)).getValue()).getValue();
default:
return null;
}
}
use of org.apache.calcite.rex.RexCall in project calcite by apache.
the class RelBuilder method match.
/**
* Creates a {@link org.apache.calcite.rel.core.Match}.
*/
public RelBuilder match(RexNode pattern, boolean strictStart, boolean strictEnd, Map<String, RexNode> patternDefinitions, Iterable<? extends RexNode> measureList, RexNode after, Map<String, ? extends SortedSet<String>> subsets, boolean allRows, Iterable<? extends RexNode> partitionKeys, Iterable<? extends RexNode> orderKeys, RexNode interval) {
final List<RelFieldCollation> fieldCollations = new ArrayList<>();
for (RexNode orderKey : orderKeys) {
final RelFieldCollation.Direction direction;
switch(orderKey.getKind()) {
case DESCENDING:
direction = RelFieldCollation.Direction.DESCENDING;
orderKey = ((RexCall) orderKey).getOperands().get(0);
break;
case NULLS_FIRST:
case NULLS_LAST:
throw new AssertionError();
default:
direction = RelFieldCollation.Direction.ASCENDING;
break;
}
final RelFieldCollation.NullDirection nullDirection = direction.defaultNullDirection();
final RexInputRef ref = (RexInputRef) orderKey;
fieldCollations.add(new RelFieldCollation(ref.getIndex(), direction, nullDirection));
}
final RelDataTypeFactory.Builder typeBuilder = cluster.getTypeFactory().builder();
for (RexNode partitionKey : partitionKeys) {
typeBuilder.add(partitionKey.toString(), partitionKey.getType());
}
if (allRows) {
for (RexNode orderKey : orderKeys) {
if (!typeBuilder.nameExists(orderKey.toString())) {
typeBuilder.add(orderKey.toString(), orderKey.getType());
}
}
final RelDataType inputRowType = peek().getRowType();
for (RelDataTypeField fs : inputRowType.getFieldList()) {
if (!typeBuilder.nameExists(fs.getName())) {
typeBuilder.add(fs);
}
}
}
final ImmutableMap.Builder<String, RexNode> measures = ImmutableMap.builder();
for (RexNode measure : measureList) {
List<RexNode> operands = ((RexCall) measure).getOperands();
String alias = operands.get(1).toString();
typeBuilder.add(alias, operands.get(0).getType());
measures.put(alias, operands.get(0));
}
final RelNode match = matchFactory.createMatch(peek(), pattern, typeBuilder.build(), strictStart, strictEnd, patternDefinitions, measures.build(), after, subsets, allRows, ImmutableList.copyOf(partitionKeys), RelCollations.of(fieldCollations), interval);
stack.push(new Frame(match));
return this;
}
use of org.apache.calcite.rex.RexCall in project calcite by apache.
the class RelStructuredTypeFlattener method flattenProjection.
private void flattenProjection(RewriteRexShuttle shuttle, RexNode exp, String fieldName, List<Pair<RexNode, String>> flattenedExps) {
if (exp.getType().isStruct()) {
if (exp instanceof RexInputRef) {
RexInputRef inputRef = (RexInputRef) exp;
int newOffset = getNewForOldInput(inputRef.getIndex());
// expand to range
RelDataType flattenedType = SqlTypeUtil.flattenRecordType(rexBuilder.getTypeFactory(), exp.getType(), null);
List<RelDataTypeField> fieldList = flattenedType.getFieldList();
int n = fieldList.size();
for (int j = 0; j < n; ++j) {
RelDataTypeField field = fieldList.get(j);
flattenedExps.add(Pair.<RexNode, String>of(new RexInputRef(newOffset + j, field.getType()), fieldName));
}
} else if (isConstructor(exp) || exp.isA(SqlKind.CAST)) {
// REVIEW jvs 27-Feb-2005: for cast, see corresponding note
// in RewriteRexShuttle
RexCall call = (RexCall) exp;
if (exp.isA(SqlKind.NEW_SPECIFICATION)) {
// For object constructors, prepend a FALSE null
// indicator.
flattenedExps.add(Pair.<RexNode, String>of(rexBuilder.makeLiteral(false), fieldName));
} else if (exp.isA(SqlKind.CAST)) {
if (RexLiteral.isNullLiteral(((RexCall) exp).operands.get(0))) {
// Translate CAST(NULL AS UDT) into
// the correct number of null fields.
flattenNullLiteral(exp.getType(), flattenedExps);
return;
}
}
flattenProjections(new RewriteRexShuttle(), call.getOperands(), Collections.<String>nCopies(call.getOperands().size(), null), fieldName, flattenedExps);
} else if (exp instanceof RexCall) {
// NOTE jvs 10-Feb-2005: This is a lame hack to keep special
// functions which return row types working.
int j = 0;
RexNode newExp = exp;
List<RexNode> oldOperands = ((RexCall) exp).getOperands();
if (oldOperands.get(0) instanceof RexInputRef) {
RexInputRef inputRef = (RexInputRef) oldOperands.get(0);
int newOffset = getNewForOldInput(inputRef.getIndex());
newExp = rexBuilder.makeCall(exp.getType(), ((RexCall) exp).getOperator(), ImmutableList.of(rexBuilder.makeInputRef(inputRef.getType(), newOffset), oldOperands.get(1)));
}
for (RelDataTypeField field : newExp.getType().getFieldList()) {
flattenedExps.add(Pair.of(rexBuilder.makeFieldAccess(newExp, field.getIndex()), fieldName + "$" + (j++)));
}
} else {
throw Util.needToImplement(exp);
}
} else {
flattenedExps.add(Pair.of(exp.accept(shuttle), fieldName));
}
}
use of org.apache.calcite.rex.RexCall in project calcite by apache.
the class RelOptUtil method analyzeSimpleEquiJoin.
// to be removed before 2.0
@Deprecated
public static boolean analyzeSimpleEquiJoin(LogicalJoin join, int[] joinFieldOrdinals) {
RexNode joinExp = join.getCondition();
if (joinExp.getKind() != SqlKind.EQUALS) {
return false;
}
RexCall binaryExpression = (RexCall) joinExp;
RexNode leftComparand = binaryExpression.operands.get(0);
RexNode rightComparand = binaryExpression.operands.get(1);
if (!(leftComparand instanceof RexInputRef)) {
return false;
}
if (!(rightComparand instanceof RexInputRef)) {
return false;
}
final int leftFieldCount = join.getLeft().getRowType().getFieldCount();
RexInputRef leftFieldAccess = (RexInputRef) leftComparand;
if (!(leftFieldAccess.getIndex() < leftFieldCount)) {
// left field must access left side of join
return false;
}
RexInputRef rightFieldAccess = (RexInputRef) rightComparand;
if (!(rightFieldAccess.getIndex() >= leftFieldCount)) {
// right field must access right side of join
return false;
}
joinFieldOrdinals[0] = leftFieldAccess.getIndex();
joinFieldOrdinals[1] = rightFieldAccess.getIndex() - leftFieldCount;
return true;
}
Aggregations