org.apache.calcite.rel.core.Window Java Examples

The following examples show how to use org.apache.calcite.rel.core.Window. 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: 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 #2
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 #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: PostOrderRelNodeVisitor.java    From streamline with Apache License 2.0 5 votes vote down vote up
public final T traverse(RelNode n) throws Exception {
  List<T> inputStreams = new ArrayList<>();
  for (RelNode input : n.getInputs()) {
    inputStreams.add(traverse(input));
  }

  if (n instanceof Aggregate) {
    return visitAggregate((Aggregate) n, inputStreams);
  } else if (n instanceof Calc) {
    return visitCalc((Calc) n, inputStreams);
  } else if (n instanceof Collect) {
    return visitCollect((Collect) n, inputStreams);
  } else if (n instanceof Correlate) {
    return visitCorrelate((Correlate) n, inputStreams);
  } else if (n instanceof Delta) {
    return visitDelta((Delta) n, inputStreams);
  } else if (n instanceof Exchange) {
    return visitExchange((Exchange) n, inputStreams);
  } else if (n instanceof Project) {
    return visitProject((Project) n, inputStreams);
  } else if (n instanceof Filter) {
    return visitFilter((Filter) n, inputStreams);
  } else if (n instanceof Sample) {
    return visitSample((Sample) n, inputStreams);
  } else if (n instanceof Sort) {
    return visitSort((Sort) n, inputStreams);
  } else if (n instanceof TableModify) {
    return visitTableModify((TableModify) n, inputStreams);
  } else if (n instanceof TableScan) {
    return visitTableScan((TableScan) n, inputStreams);
  } else if (n instanceof Uncollect) {
    return visitUncollect((Uncollect) n, inputStreams);
  } else if (n instanceof Window) {
    return visitWindow((Window) n, inputStreams);
  } else if (n instanceof Join) {
    return visitJoin((Join) n, inputStreams);
  } else {
    return defaultValue(n, inputStreams);
  }
}
 
Example #7
Source File: WindowPrule.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private List<DistributionTrait.DistributionField> getDistributionFields(Window.Group window) {
  List<DistributionTrait.DistributionField> groupByFields = Lists.newArrayList();
  for (int group : BitSets.toIter(window.keys)) {
    DistributionTrait.DistributionField field = new DistributionTrait.DistributionField(group);
    groupByFields.add(field);
  }

  return groupByFields;
}
 
Example #8
Source File: WindowPrule.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Create a RelCollation that has partition-by as the leading keys followed by order-by keys
 * @param window The window specification
 * @return a RelCollation with {partition-by keys, order-by keys}
 */
private RelCollation getCollation(Window.Group window) {
  List<RelFieldCollation> fields = Lists.newArrayList();
  for (int group : BitSets.toIter(window.keys)) {
    fields.add(new RelFieldCollation(group));
  }

  fields.addAll(window.orderKeys.getFieldCollations());

  return RelCollations.of(fields);
}
 
Example #9
Source File: WindowRule.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final Window window = call.rel(0);
  final RelNode input = call.rel(1);
  final RelTraitSet traits = window.getTraitSet().plus(Rel.LOGICAL);
  final RelNode convertedInput = convert(input, traits.simplify());
  call.transformTo(
      WindowRel.create(
          window.getCluster(),
          traits,
          convertedInput,
          window.constants,
          window.getRowType(),
          window.groups));
}
 
Example #10
Source File: WindowPrule.java    From Bats with Apache License 2.0 5 votes vote down vote up
private List<DistributionField> getDistributionFieldsFromCollation(Window.Group window) {
    List<DistributionField> distFields = Lists.newArrayList();

    for (RelFieldCollation relField : window.collation().getFieldCollations()) {
        DistributionField field = new DistributionField(relField.getFieldIndex());
        distFields.add(field);
    }

    return distFields;
}
 
Example #11
Source File: WindowPrule.java    From Bats with Apache License 2.0 5 votes vote down vote up
private List<DrillDistributionTrait.DistributionField> getDistributionFields(Window.Group window) {
    List<DrillDistributionTrait.DistributionField> groupByFields = Lists.newArrayList();
    for (int group : BitSets.toIter(window.keys)) {
        DrillDistributionTrait.DistributionField field = new DrillDistributionTrait.DistributionField(group);
        groupByFields.add(field);
    }

    return groupByFields;
}
 
Example #12
Source File: WindowPrule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Create a RelCollation that has partition-by as the leading keys followed by order-by keys
 * @param window The window specification
 * @return a RelCollation with {partition-by keys, order-by keys}
 */
private RelCollation getCollation(Window.Group window) {
    List<RelFieldCollation> fields = Lists.newArrayList();
    for (int group : BitSets.toIter(window.keys)) {
        fields.add(new RelFieldCollation(group));
    }

    fields.addAll(window.orderKeys.getFieldCollations());

    return RelCollations.of(fields);
}
 
Example #13
Source File: DrillReduceAggregatesRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
  final DrillWindowRel oldWinRel = (DrillWindowRel) call.rels[0];
  for (Window.Group group : oldWinRel.groups) {
    for (Window.RexWinAggCall rexWinAggCall : group.aggCalls) {
      if (isConversionToSumZeroNeeded(rexWinAggCall.getOperator(), rexWinAggCall.getType())) {
        return true;
      }
    }
  }
  return false;
}
 
Example #14
Source File: DrillWindowRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final Window window = call.rel(0);
  final RelNode input = call.rel(1);
  final RelTraitSet traits = window.getTraitSet().plus(DrillRel.DRILL_LOGICAL).simplify();
  final RelNode convertedInput = convert(input, traits);
  call.transformTo(
      new DrillWindowRel(
          window.getCluster(),
          traits,
          convertedInput,
          window.constants,
          window.getRowType(),
          window.groups));
}
 
Example #15
Source File: DrillRelMdDistinctRowCount.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Double getDistinctRowCount(RelNode rel, RelMetadataQuery mq, ImmutableBitSet groupKey, RexNode predicate) {
  if (rel instanceof DrillScanRelBase) {                  // Applies to both Drill Logical and Physical Rels
    if (!DrillRelOptUtil.guessRows(rel)) {
      DrillTable table = Utilities.getDrillTable(rel.getTable());
      return getDistinctRowCountInternal(((DrillScanRelBase) rel), mq, table, groupKey, rel.getRowType(), predicate);
    } else {
      /* If we are not using statistics OR there is no table or metadata (stats) table associated with scan,
       * estimate the distinct row count. Consistent with the estimation of Aggregate row count in
       * RelMdRowCount: distinctRowCount = rowCount * 10%.
       */
      if (rel instanceof DrillScanRel) {
        // The existing Drill behavior is to only use this estimation for DrillScanRel and not ScanPrel.
        // TODO: We may potentially do it for ScanPrel (outside the scope of statistics)
        return rel.estimateRowCount(mq) * 0.1;
      }
    }
  } else if (rel instanceof SingleRel && !DrillRelOptUtil.guessRows(rel)) {
    if (rel instanceof Window) {
      int childFieldCount = ((Window) rel).getInput().getRowType().getFieldCount();
      // For window aggregates delegate ndv to parent
      for (int bit : groupKey) {
        if (bit >= childFieldCount) {
          return super.getDistinctRowCount(rel, mq, groupKey, predicate);
        }
      }
    }
    return mq.getDistinctRowCount(((SingleRel) rel).getInput(), groupKey, predicate);
  } else if (rel instanceof DrillJoinRelBase && !DrillRelOptUtil.guessRows(rel)) {
    //Assume ndv is unaffected by the join
    return getDistinctRowCountInternal(((DrillJoinRelBase) rel), mq, groupKey, predicate);
  } else if (rel instanceof RelSubset && !DrillRelOptUtil.guessRows(rel)) {
    if (((RelSubset) rel).getBest() != null) {
      return mq.getDistinctRowCount(((RelSubset) rel).getBest(), groupKey, predicate);
    } else if (((RelSubset) rel).getOriginal() != null) {
      return mq.getDistinctRowCount(((RelSubset) rel).getOriginal(), groupKey, predicate);
    }
  }
  return super.getDistinctRowCount(rel, mq, groupKey, predicate);
}
 
Example #16
Source File: SqlImplementor.java    From calcite with Apache License 2.0 4 votes vote down vote up
public List<SqlNode> toSql(Window.Group group, ImmutableList<RexLiteral> constants,
    int inputFieldCount) {
  final List<SqlNode> rexOvers = new ArrayList<>();
  final List<SqlNode> partitionKeys = new ArrayList<>();
  final List<SqlNode> orderByKeys = new ArrayList<>();
  for (int partition: group.keys) {
    partitionKeys.add(this.field(partition));
  }
  for (RelFieldCollation collation: group.orderKeys.getFieldCollations()) {
    this.addOrderItem(orderByKeys, collation);
  }
  SqlLiteral isRows = SqlLiteral.createBoolean(group.isRows, POS);
  SqlNode lowerBound = null;
  SqlNode upperBound = null;

  final SqlLiteral allowPartial = null;

  for (Window.RexWinAggCall winAggCall: group.aggCalls) {
    SqlAggFunction aggFunction = (SqlAggFunction) winAggCall.getOperator();
    final SqlWindow sqlWindow = SqlWindow.create(null, null,
            new SqlNodeList(partitionKeys, POS), new SqlNodeList(orderByKeys, POS),
            isRows, lowerBound, upperBound, allowPartial, POS);
    if (aggFunction.allowsFraming()) {
      lowerBound = createSqlWindowBound(group.lowerBound);
      upperBound = createSqlWindowBound(group.upperBound);
      sqlWindow.setLowerBound(lowerBound);
      sqlWindow.setUpperBound(upperBound);
    }

    RexShuttle replaceConstants = new RexShuttle() {
      @Override public RexNode visitInputRef(RexInputRef inputRef) {
        int index = inputRef.getIndex();
        RexNode ref;
        if (index > inputFieldCount - 1) {
          ref = constants.get(index - inputFieldCount);
        } else {
          ref = inputRef;
        }
        return ref;
      }
    };
    RexCall aggCall = (RexCall) winAggCall.accept(replaceConstants);
    List<SqlNode> operands = toSql(null, aggCall.operands);
    rexOvers.add(createOverCall(aggFunction, operands, sqlWindow));
  }
  return rexOvers;
}
 
Example #17
Source File: ReduceExpressionsRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
public WindowReduceExpressionsRule(Class<? extends Window> windowClass,
    boolean matchNullability, RelBuilderFactory relBuilderFactory) {
  super(windowClass, matchNullability, relBuilderFactory,
      "ReduceExpressionsRule(Window)");
}
 
Example #18
Source File: Nodes.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void visit(Window window) {
  node = new WindowNode(this, window);
}
 
Example #19
Source File: RelMdCollation.java    From calcite with Apache License 2.0 4 votes vote down vote up
public ImmutableList<RelCollation> collations(Window rel,
    RelMetadataQuery mq) {
  return ImmutableList.copyOf(window(mq, rel.getInput(), rel.groups));
}
 
Example #20
Source File: RelMdNodeTypes.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Multimap<Class<? extends RelNode>, RelNode> getNodeTypes(Window rel,
    RelMetadataQuery mq) {
  return getNodeTypes(rel, Window.class, mq);
}
 
Example #21
Source File: RelMdTableReferences.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Table references from Window.
 */
public Set<RelTableRef> getTableReferences(Window rel, RelMetadataQuery mq) {
  return mq.getTableReferences(rel.getInput());
}
 
Example #22
Source File: WindowNode.java    From calcite with Apache License 2.0 4 votes vote down vote up
WindowNode(Compiler compiler, Window rel) {
  super(compiler, rel);
}
 
Example #23
Source File: PigRelToSqlConverter.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** @see #dispatch */
public Result visit(Window e) {
  final Result x = visitChild(0, e.getInput());
  final Builder builder = x.builder(e, Clause.SELECT);
  final List<SqlNode> selectList =
      new ArrayList<>(builder.context.fieldList());

  for (Window.Group winGroup : e.groups) {
    final List<SqlNode> partitionList = Expressions.list();
    for (int i : winGroup.keys) {
      partitionList.add(builder.context.field(i));
    }

    final List<SqlNode> orderList = Expressions.list();
    for (RelFieldCollation orderKey : winGroup.collation().getFieldCollations()) {
      orderList.add(builder.context.toSql(orderKey));
    }

    final SqlNode sqlWindow =  SqlWindow.create(
        null, // Window declaration name
        null, // Window reference name
        new SqlNodeList(partitionList, POS),
        new SqlNodeList(orderList, POS),
        SqlLiteral.createBoolean(winGroup.isRows, POS),
        builder.context.toSql(winGroup.lowerBound),
        builder.context.toSql(winGroup.upperBound),
        null, // allowPartial
        POS);

    for (Window.RexWinAggCall winFunc : winGroup.aggCalls) {
      final List<SqlNode> winFuncOperands = Expressions.list();
      for (RexNode operand : winFunc.getOperands()) {
        winFuncOperands.add(builder.context.toSql(null, operand));
      }
      SqlNode aggFunc = winFunc.getOperator().createCall(new SqlNodeList(winFuncOperands, POS));
      selectList.add(SqlStdOperatorTable.OVER.createCall(POS, aggFunc, sqlWindow));
    }
    builder.setSelect(new SqlNodeList(selectList, POS));
  }
  return builder.result();
}
 
Example #24
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 4 votes vote down vote up
public com.google.common.collect.ImmutableList<RelCollation> collations(Window rel, RelMetadataQuery mq) {
	return com.google.common.collect.ImmutableList.copyOf(window(mq, rel.getInput(), rel.groups));
}
 
Example #25
Source File: RelMdCollation.java    From Bats with Apache License 2.0 4 votes vote down vote up
public ImmutableList<RelCollation> collations(Window rel,
    RelMetadataQuery mq) {
  return ImmutableList.copyOf(window(mq, rel.getInput(), rel.groups));
}
 
Example #26
Source File: OLAPWindowRule.java    From kylin with Apache License 2.0 4 votes vote down vote up
public OLAPWindowRule() {
    super(Window.class, Convention.NONE, OLAPRel.CONVENTION, "OLAPWindowRule");
}
 
Example #27
Source File: OLAPWindowRel.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public Window copy(RelTraitSet traitSet, List<RelNode> inputs) {
    assert inputs.size() == 1;
    return new OLAPWindowRel(getCluster(), traitSet, inputs.get(0), constants, rowType, groups);
}
 
Example #28
Source File: PostOrderRelNodeVisitor.java    From streamline with Apache License 2.0 4 votes vote down vote up
public T visitWindow(Window window, List<T> inputStreams) throws Exception {
  return defaultValue(window, inputStreams);
}
 
Example #29
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public List<SqlNode> toSql(Window.Group group, ImmutableList<RexLiteral> constants,
                           int inputFieldCount) {
  final List<SqlNode> rexOvers = new ArrayList<>();
  final List<SqlNode> partitionKeys = new ArrayList<>();
  final List<SqlNode> orderByKeys = new ArrayList<>();
  for (int partition : group.keys) {
    partitionKeys.add(this.field(partition));
  }
  for (RelFieldCollation collation : group.orderKeys.getFieldCollations()) {
    this.addOrderItem(orderByKeys, collation);
  }
  SqlLiteral isRows = SqlLiteral.createBoolean(group.isRows, POS);
  SqlNode lowerBound = null;
  SqlNode upperBound = null;

  final SqlLiteral allowPartial = null;

  for (Window.RexWinAggCall winAggCall : group.aggCalls) {
    SqlAggFunction aggFunction = (SqlAggFunction) winAggCall.getOperator();
    final SqlWindow sqlWindow = SqlWindow.create(null, null,
      new SqlNodeList(partitionKeys, POS), new SqlNodeList(orderByKeys, POS),
      isRows, lowerBound, upperBound, allowPartial, POS);
    if (aggFunction.allowsFraming()) {
      lowerBound = createSqlWindowBound(group.lowerBound);
      upperBound = createSqlWindowBound(group.upperBound);
      sqlWindow.setLowerBound(lowerBound);
      sqlWindow.setUpperBound(upperBound);
    }

    RexShuttle replaceConstants = new RexShuttle() {
      @Override
      public RexNode visitInputRef(RexInputRef inputRef) {
        int index = inputRef.getIndex();
        RexNode ref;
        if (index > inputFieldCount - 1) {
          ref = constants.get(index - inputFieldCount);
        } else {
          ref = inputRef;
        }
        return ref;
      }
    };
    RexCall aggCall = (RexCall) winAggCall.accept(replaceConstants);
    List<SqlNode> operands = toSql(null, aggCall.operands);
    rexOvers.add(createOverCall(aggFunction, operands, sqlWindow));
  }
  return rexOvers;
}
 
Example #30
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;
}