Java Code Examples for org.apache.calcite.rel.core.Window#getInput()

The following examples show how to use org.apache.calcite.rel.core.Window#getInput() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RelToSqlConverter.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * @see #dispatch
 * https://issues.apache.org/jira/projects/CALCITE/issues/CALCITE-3112
 */
public Result visit(Window e) {
  Result x = visitChild(0, e.getInput());
  Builder builder = x.builder(e);
  RelNode input = e.getInput();
  int inputFieldCount = input.getRowType().getFieldCount();
  final List<SqlNode> rexOvers = new ArrayList<>();
  for (Window.Group group : e.groups) {
    rexOvers.addAll(builder.context.toSql(group, e.constants, inputFieldCount));
  }
  final List<SqlNode> selectList = new ArrayList<>();
  for (RelDataTypeField field : input.getRowType().getFieldList()) {
    addSelect(selectList, builder.context.field(field.getIndex()), e.getRowType());
  }
  for (SqlNode rexOver : rexOvers) {
    addSelect(selectList, rexOver, e.getRowType());
  }
  builder.setSelect(new SqlNodeList(selectList, POS));
  return builder.result();
}
 
Example 2
Source File: DremioRelToSqlConverter.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public SqlImplementor.Result visit(Window e) {
  SqlImplementor.Result x = visitChild(0, e.getInput());
  SqlImplementor.Builder builder = x.builder(e);
  RelNode input = e.getInput();
  List<RexOver> rexOvers = WindowUtil.getOver(e);

  final List<SqlNode> selectList = new ArrayList<>();
  final Set<String> fields = ImmutableSet.copyOf(e.getRowType().getFieldNames());
  for (RelDataTypeField field : input.getRowType().getFieldList()) {
    if (fields.contains(field.getName())) {
      addSelect(selectList, builder.context.field(field.getIndex()), e.getRowType());
    }
  }

  for (RexOver rexOver : rexOvers) {
    addSelect(selectList, builder.context.toSql(null, rexOver), e.getRowType());
  }
  builder.setSelect(new SqlNodeList(selectList, POS));
  return builder.result();
}
 
Example 3
Source File: RelToSqlConverter.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** @see #dispatch */
public Result visit(Window e) {
  Result x = visitChild(0, e.getInput());
  Builder builder = x.builder(e);
  RelNode input = e.getInput();
  int inputFieldCount = input.getRowType().getFieldCount();
  final List<SqlNode> rexOvers = new ArrayList<>();
  for (Window.Group group: e.groups) {
    rexOvers.addAll(builder.context.toSql(group, e.constants, inputFieldCount));
  }
  final List<SqlNode> selectList = new ArrayList<>();

  for (RelDataTypeField field: input.getRowType().getFieldList()) {
    addSelect(selectList, builder.context.field(field.getIndex()), e.getRowType());
  }

  for (SqlNode rexOver: rexOvers) {
    addSelect(selectList, rexOver, e.getRowType());
  }

  builder.setSelect(new SqlNodeList(selectList, POS));
  return builder.result();
}
 
Example 4
Source File: OLAPWindowRule.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode convert(RelNode rel) {
    final Window window = (Window) rel;
    final RelTraitSet traitSet = window.getTraitSet().replace(OLAPRel.CONVENTION);
    final RelNode input = window.getInput();
    return new OLAPWindowRel(rel.getCluster(), traitSet,
            convert(input, input.getTraitSet().replace(OLAPRel.CONVENTION)), window.constants, window.getRowType(),
            window.groups);
}
 
Example 5
Source File: OLAPWindowRule.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode convert(RelNode rel) {
    final Window window = (Window) rel;
    final RelTraitSet traitSet = window.getTraitSet().replace(OLAPRel.CONVENTION);
    final RelNode input = window.getInput();
    return new OLAPWindowRel(rel.getCluster(), traitSet,
            convert(input, input.getTraitSet().replace(OLAPRel.CONVENTION)), window.constants, window.getRowType(),
            window.groups);
}
 
Example 6
Source File: WindowUtil.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public static List<RexOver> getOver(Window w) {
  RelNode input = w.getInput();
  int inputFieldCount = input.getRowType().getFieldCount();
  final List<RexOver> rexOvers = new ArrayList<>();
  for (Window.Group group : w.groups) {
    final List<RexNode> partitionKeys = new ArrayList<>();
    final List<RexFieldCollation> orderKeys = new ArrayList<>();

    // Convert RelFieldCollation to RexFieldCollation
    for (RelFieldCollation collation : group.orderKeys.getFieldCollations()) {
      Set<SqlKind> directions = new HashSet<>();
      if (collation.direction.isDescending()) {
        directions.add(SqlKind.DESCENDING);
      }
      if (collation.nullDirection == RelFieldCollation.NullDirection.LAST) {
        directions.add(SqlKind.NULLS_LAST);

      } else if (collation.nullDirection == RelFieldCollation.NullDirection.FIRST) {
        directions.add(SqlKind.NULLS_FIRST);
      }

      RexFieldCollation rexCollation = new RexFieldCollation(w.getCluster().getRexBuilder().makeInputRef(input, collation.getFieldIndex()), directions);
      orderKeys.add(rexCollation);
    }

    // Add partition keys
    for (int partition : group.keys) {
      partitionKeys.add(w.getCluster().getRexBuilder().makeInputRef(input, partition));
    }

    // Create RexWindow
    RexWindow window = new RexWindow(partitionKeys, orderKeys, group.lowerBound, group.upperBound, group.isRows);

    // For each window agg call, create rex over
    for (Window.RexWinAggCall winAggCall : group.aggCalls) {

      RexShuttle replaceConstants = new RexShuttle() {
        @Override public RexNode visitInputRef(RexInputRef inputRef) {
          int index = inputRef.getIndex();
          RexNode ref;
          if (index > inputFieldCount - 1) {
            ref = w.constants.get(index - inputFieldCount);
          } else {
            ref = inputRef;
          }
          return ref;
        }
      };
      RexCall aggCall = (RexCall) winAggCall.accept(replaceConstants);
      SqlAggFunction aggFunction = (SqlAggFunction) winAggCall.getOperator();
      RexOver over = new RexOver(winAggCall.getType(), aggFunction, aggCall.operands, window, winAggCall.distinct);
      rexOvers.add(over);
    }
  }
  return rexOvers;
}