예제 #1
0
  /** Variant of {@link #trimFields(RelNode, BitSet, Set)} for {@link TableModificationRel}. */
  public TrimResult trimFields(
      TableModificationRel modifier, BitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
    // Ignore what consumer wants. We always project all columns.
    Util.discard(fieldsUsed);

    final RelDataType rowType = modifier.getRowType();
    final int fieldCount = rowType.getFieldCount();
    RelNode input = modifier.getChild();

    // We want all fields from the child.
    final int inputFieldCount = input.getRowType().getFieldCount();
    BitSet inputFieldsUsed = Util.bitSetBetween(0, inputFieldCount);

    // Create input with trimmed columns.
    final Set<RelDataTypeField> inputExtraFields = Collections.emptySet();
    TrimResult trimResult = trimChild(modifier, input, inputFieldsUsed, inputExtraFields);
    RelNode newInput = trimResult.left;
    final Mapping inputMapping = trimResult.right;
    if (!inputMapping.isIdentity()) {
      // We asked for all fields. Can't believe that the child decided
      // to permute them!
      throw Util.newInternal("Expected identity mapping, got " + inputMapping);
    }

    TableModificationRel newModifier = modifier;
    if (newInput != input) {
      newModifier = modifier.copy(modifier.getTraitSet(), Collections.singletonList(newInput));
    }
    assert newModifier.getClass() == modifier.getClass();

    // Always project all fields.
    Mapping mapping = Mappings.createIdentity(fieldCount);
    return new TrimResult(newModifier, mapping);
  }
예제 #2
0
 /**
  * Trims the fields of an input relational expression.
  *
  * @param rel Relational expression
  * @param input Input relational expression, whose fields to trim
  * @param fieldsUsed Bitmap of fields needed by the consumer
  * @return New relational expression and its field mapping
  */
 protected TrimResult trimChild(
     RelNode rel, RelNode input, BitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
   Util.discard(rel);
   if (input.getClass().getName().endsWith("MedMdrClassExtentRel")) {
     // MedMdrJoinRule cannot handle Join of Project of
     // MedMdrClassExtentRel, only naked MedMdrClassExtentRel.
     // So, disable trimming.
     fieldsUsed = Util.bitSetBetween(0, input.getRowType().getFieldCount());
   }
   return dispatchTrimFields(input, fieldsUsed, extraFields);
 }
예제 #3
0
  /** Variant of {@link #trimFields(RelNode, BitSet, Set)} for {@link TableFunctionRel}. */
  public TrimResult trimFields(
      TableFunctionRel tabFun, BitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
    final RelDataType rowType = tabFun.getRowType();
    final int fieldCount = rowType.getFieldCount();
    List<RelNode> newInputs = new ArrayList<RelNode>();

    for (RelNode input : tabFun.getInputs()) {
      final int inputFieldCount = input.getRowType().getFieldCount();
      BitSet inputFieldsUsed = Util.bitSetBetween(0, inputFieldCount);

      // Create input with trimmed columns.
      final Set<RelDataTypeField> inputExtraFields = Collections.emptySet();
      TrimResult trimResult = trimChildRestore(tabFun, input, inputFieldsUsed, inputExtraFields);
      assert trimResult.right.isIdentity();
      newInputs.add(trimResult.left);
    }

    TableFunctionRel newTabFun = tabFun;
    if (!tabFun.getInputs().equals(newInputs)) {
      newTabFun = tabFun.copy(tabFun.getTraitSet(), newInputs);
    }
    assert newTabFun.getClass() == tabFun.getClass();

    // Always project all fields.
    Mapping mapping = Mappings.createIdentity(fieldCount);
    return new TrimResult(newTabFun, mapping);
  }
예제 #4
0
 private Mapping createMapping(BitSet fieldsUsed, int fieldCount) {
   final Mapping mapping =
       Mappings.create(MappingType.InverseSurjection, fieldCount, fieldsUsed.cardinality());
   int i = 0;
   for (int field : Util.toIter(fieldsUsed)) {
     mapping.set(field, i++);
   }
   return mapping;
 }
예제 #5
0
 /**
  * Trims unused fields from a relational expression.
  *
  * <p>We presume that all fields of the relational expression are wanted by its consumer, so only
  * trim fields that are not used within the tree.
  *
  * @param root Root node of relational expression
  * @return Trimmed relational expression
  */
 public RelNode trim(RelNode root) {
   final int fieldCount = root.getRowType().getFieldCount();
   final BitSet fieldsUsed = Util.bitSetBetween(0, fieldCount);
   final Set<RelDataTypeField> extraFields = Collections.emptySet();
   final TrimResult trimResult = dispatchTrimFields(root, fieldsUsed, extraFields);
   if (!trimResult.right.isIdentity()) {
     throw new IllegalArgumentException();
   }
   return trimResult.left;
 }
예제 #6
0
 /**
  * Variant of {@link #trimFields(RelNode, BitSet, Set)} for {@link
  * org.eigenbase.rel.TableAccessRel}.
  */
 public TrimResult trimFields(
     final TableAccessRelBase tableAccessRel,
     BitSet fieldsUsed,
     Set<RelDataTypeField> extraFields) {
   final int fieldCount = tableAccessRel.getRowType().getFieldCount();
   if (fieldsUsed.equals(Util.bitSetBetween(0, fieldCount)) && extraFields.isEmpty()) {
     return trimFields((RelNode) tableAccessRel, fieldsUsed, extraFields);
   }
   final RelNode newTableAccessRel = tableAccessRel.project(fieldsUsed, extraFields);
   final Mapping mapping = createMapping(fieldsUsed, fieldCount);
   return new TrimResult(newTableAccessRel, mapping);
 }
예제 #7
0
파일: RexUtil.java 프로젝트: kunlqt/optiq
 /** Returns whether an array of expressions has any common sub-expressions. */
 public static boolean containCommonExprs(RexNode[] exprs, boolean fail) {
   final ExpressionNormalizer visitor = new ExpressionNormalizer(false);
   for (int i = 0; i < exprs.length; i++) {
     try {
       exprs[i].accept(visitor);
     } catch (ExpressionNormalizer.SubExprExistsException e) {
       Util.swallow(e, null);
       assert !fail;
     }
   }
   return false;
 }
예제 #8
0
파일: RexUtil.java 프로젝트: kunlqt/optiq
 /**
  * Replaces the operands of a call. The new operands' types must match the old operands' types.
  */
 public static RexCall replaceOperands(RexCall call, RexNode[] operands) {
   if (call.operands == operands) {
     return call;
   }
   for (int i = 0; i < operands.length; i++) {
     RelDataType oldType = call.operands[i].getType();
     RelDataType newType = operands[i].getType();
     if (!oldType.isNullable() && newType.isNullable()) {
       throw Util.newInternal("invalid nullability");
     }
     assert (oldType.toString().equals(newType.toString()));
   }
   return new RexCall(call.getType(), call.getOperator(), operands);
 }
예제 #9
0
파일: RexUtil.java 프로젝트: kunlqt/optiq
 /**
  * Returns whether a given tree contains any {@link org.eigenbase.rex.RexFieldAccess} nodes.
  *
  * @param node a RexNode tree
  */
 public static boolean containsFieldAccess(RexNode node) {
   try {
     RexVisitor<Void> visitor =
         new RexVisitorImpl<Void>(true) {
           public Void visitFieldAccess(RexFieldAccess fieldAccess) {
             throw new Util.FoundOne(fieldAccess);
           }
         };
     node.accept(visitor);
     return false;
   } catch (Util.FoundOne e) {
     Util.swallow(e, null);
     return true;
   }
 }
예제 #10
0
  /**
   * Variant of {@link #trimFields(RelNode, BitSet, Set)} for {@link org.eigenbase.rel.ValuesRel}.
   */
  public TrimResult trimFields(
      ValuesRel values, BitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
    final RelDataType rowType = values.getRowType();
    final int fieldCount = rowType.getFieldCount();

    // If they are asking for no fields, we can't give them what they want,
    // because zero-column records are illegal. Give them the last field,
    // which is unlikely to be a system field.
    if (fieldsUsed.isEmpty()) {
      fieldsUsed = Util.bitSetBetween(fieldCount - 1, fieldCount);
    }

    // If all fields are used, return unchanged.
    if (fieldsUsed.equals(Util.bitSetBetween(0, fieldCount))) {
      Mapping mapping = Mappings.createIdentity(fieldCount);
      return new TrimResult(values, mapping);
    }

    List<List<RexLiteral>> newTuples = new ArrayList<List<RexLiteral>>();
    for (List<RexLiteral> tuple : values.getTuples()) {
      List<RexLiteral> newTuple = new ArrayList<RexLiteral>();
      for (int field : Util.toIter(fieldsUsed)) {
        newTuple.add(tuple.get(field));
      }
      newTuples.add(newTuple);
    }

    final Mapping mapping = createMapping(fieldsUsed, fieldCount);
    RelDataType newRowType =
        values
            .getCluster()
            .getTypeFactory()
            .createStructType(Mappings.apply3(mapping, rowType.getFieldList()));
    final ValuesRel newValues = new ValuesRel(values.getCluster(), newRowType, newTuples);
    return new TrimResult(newValues, mapping);
  }
예제 #11
0
파일: RexUtil.java 프로젝트: kunlqt/optiq
 /**
  * Returns whether a given tree contains any {link RexInputRef} nodes.
  *
  * @param node a RexNode tree
  */
 public static boolean containsInputRef(RexNode node) {
   try {
     RexVisitor<Void> visitor =
         new RexVisitorImpl<Void>(true) {
           public Void visitInputRef(RexInputRef inputRef) {
             throw new Util.FoundOne(inputRef);
           }
         };
     node.accept(visitor);
     return false;
   } catch (Util.FoundOne e) {
     Util.swallow(e, null);
     return true;
   }
 }
예제 #12
0
파일: RexUtil.java 프로젝트: kunlqt/optiq
 /**
  * Returns whether an array of expressions contains a forward reference. That is, if expression #i
  * contains a {@link RexInputRef} referencing field i or greater.
  *
  * @param exprs Array of expressions
  * @param inputRowType Input row type
  * @param fail Whether to assert if there is a forward reference
  * @return Whether there is a forward reference
  */
 public static boolean containForwardRefs(
     RexNode[] exprs, RelDataType inputRowType, boolean fail) {
   final ForwardRefFinder visitor = new ForwardRefFinder(inputRowType);
   for (int i = 0; i < exprs.length; i++) {
     RexNode expr = exprs[i];
     visitor.setLimit(i); // field cannot refer to self or later field
     try {
       expr.accept(visitor);
     } catch (ForwardRefFinder.IllegalForwardRefException e) {
       Util.swallow(e, null);
       assert !fail : "illegal forward reference in " + expr;
       return true;
     }
   }
   return false;
 }
예제 #13
0
파일: RexUtil.java 프로젝트: kunlqt/optiq
 /**
  * Returns whether a given node contains a RexCall with a specified operator
  *
  * @param operator to look for
  * @param node a RexNode tree
  */
 public static RexCall findOperatorCall(final SqlOperator operator, RexNode node) {
   try {
     RexVisitor<Void> visitor =
         new RexVisitorImpl<Void>(true) {
           public Void visitCall(RexCall call) {
             if (call.getOperator().equals(operator)) {
               throw new Util.FoundOne(call);
             }
             return super.visitCall(call);
           }
         };
     node.accept(visitor);
     return null;
   } catch (Util.FoundOne e) {
     Util.swallow(e, null);
     return (RexCall) e.getNode();
   }
 }
예제 #14
0
파일: RexUtil.java 프로젝트: kunlqt/optiq
 SubExprExistsException(RexNode expr) {
   Util.discard(expr);
 }
예제 #15
0
  /** Variant of {@link #trimFields(RelNode, BitSet, Set)} for {@link JoinRel}. */
  public TrimResult trimFields(JoinRel join, BitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
    final RelDataType rowType = join.getRowType();
    final int fieldCount = rowType.getFieldCount();
    final RexNode conditionExpr = join.getCondition();
    final int systemFieldCount = join.getSystemFieldList().size();

    // Add in fields used in the condition.
    BitSet fieldsUsedPlus = (BitSet) fieldsUsed.clone();
    final Set<RelDataTypeField> combinedInputExtraFields =
        new LinkedHashSet<RelDataTypeField>(extraFields);
    RelOptUtil.InputFinder inputFinder =
        new RelOptUtil.InputFinder(fieldsUsedPlus, combinedInputExtraFields);
    conditionExpr.accept(inputFinder);

    // If no system fields are used, we can remove them.
    int systemFieldUsedCount = 0;
    for (int i = 0; i < systemFieldCount; ++i) {
      if (fieldsUsed.get(i)) {
        ++systemFieldUsedCount;
      }
    }
    final int newSystemFieldCount;
    if (systemFieldUsedCount == 0) {
      newSystemFieldCount = 0;
    } else {
      newSystemFieldCount = systemFieldCount;
    }

    int offset = systemFieldCount;
    int changeCount = 0;
    int newFieldCount = newSystemFieldCount;
    List<RelNode> newInputs = new ArrayList<RelNode>(2);
    List<Mapping> inputMappings = new ArrayList<Mapping>();
    List<Integer> inputExtraFieldCounts = new ArrayList<Integer>();
    for (RelNode input : join.getInputs()) {
      final RelDataType inputRowType = input.getRowType();
      final int inputFieldCount = inputRowType.getFieldCount();

      // Compute required mapping.
      BitSet inputFieldsUsed = new BitSet(inputFieldCount);
      for (int bit : Util.toIter(fieldsUsedPlus)) {
        if (bit >= offset && bit < offset + inputFieldCount) {
          inputFieldsUsed.set(bit - offset);
        }
      }

      // If there are system fields, we automatically use the
      // corresponding field in each input.
      if (newSystemFieldCount > 0) {
        // calling with newSystemFieldCount == 0 should be safe but hits
        // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6222207
        inputFieldsUsed.set(0, newSystemFieldCount);
      }

      // FIXME: We ought to collect extra fields for each input
      // individually. For now, we assume that just one input has
      // on-demand fields.
      Set<RelDataTypeField> inputExtraFields =
          input.getRowType().getField("_extra") == null
              ? Collections.<RelDataTypeField>emptySet()
              : combinedInputExtraFields;
      inputExtraFieldCounts.add(inputExtraFields.size());
      TrimResult trimResult = trimChild(join, input, inputFieldsUsed, inputExtraFields);
      newInputs.add(trimResult.left);
      if (trimResult.left != input) {
        ++changeCount;
      }

      final Mapping inputMapping = trimResult.right;
      inputMappings.add(inputMapping);

      // Move offset to point to start of next input.
      offset += inputFieldCount;
      newFieldCount += inputMapping.getTargetCount() + inputExtraFields.size();
    }

    Mapping mapping = Mappings.create(MappingType.InverseSurjection, fieldCount, newFieldCount);
    for (int i = 0; i < newSystemFieldCount; ++i) {
      mapping.set(i, i);
    }
    offset = systemFieldCount;
    int newOffset = newSystemFieldCount;
    for (int i = 0; i < inputMappings.size(); i++) {
      Mapping inputMapping = inputMappings.get(i);
      for (IntPair pair : inputMapping) {
        mapping.set(pair.source + offset, pair.target + newOffset);
      }
      offset += inputMapping.getSourceCount();
      newOffset += inputMapping.getTargetCount() + inputExtraFieldCounts.get(i);
    }

    if (changeCount == 0 && mapping.isIdentity()) {
      return new TrimResult(join, Mappings.createIdentity(fieldCount));
    }

    // Build new join.
    final RexVisitor<RexNode> shuttle =
        new RexPermuteInputsShuttle(mapping, newInputs.get(0), newInputs.get(1));
    RexNode newConditionExpr = conditionExpr.accept(shuttle);

    final JoinRel newJoin =
        join.copy(join.getTraitSet(), newConditionExpr, newInputs.get(0), newInputs.get(1));

    return new TrimResult(newJoin, mapping);
  }
예제 #16
0
 /**
  * Visit method, per {@link org.eigenbase.util.ReflectiveVisitor}.
  *
  * <p>This method is invoked reflectively, so there may not be any apparent calls to it. The class
  * (or derived classes) may contain overloads of this method with more specific types for the
  * {@code rel} parameter.
  *
  * <p>Returns a pair: the relational expression created, and the mapping between the original
  * fields and the fields of the newly created relational expression.
  *
  * @param rel Relational expression
  * @param fieldsUsed Fields needed by the consumer
  * @return relational expression and mapping
  */
 public TrimResult trimFields(RelNode rel, BitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
   // We don't know how to trim this kind of relational expression, so give
   // it back intact.
   Util.discard(fieldsUsed);
   return new TrimResult(rel, Mappings.createIdentity(rel.getRowType().getFieldCount()));
 }
예제 #17
0
  /** Variant of {@link #trimFields(RelNode, BitSet, Set)} for {@link AggregateRel}. */
  public TrimResult trimFields(
      AggregateRel aggregate, BitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
    // Fields:
    //
    // | sys fields | group fields | agg functions |
    //
    // Two kinds of trimming:
    //
    // 1. If agg rel has system fields but none of these are used, create an
    // agg rel with no system fields.
    //
    // 2. If aggregate functions are not used, remove them.
    //
    // But grouping fields stay, even if they are not used.

    final RelDataType rowType = aggregate.getRowType();

    // Compute which input fields are used.
    BitSet inputFieldsUsed = new BitSet();
    // 1. group fields are always used
    for (int i : Util.toIter(aggregate.getGroupSet())) {
      inputFieldsUsed.set(i);
    }
    // 2. agg functions
    for (AggregateCall aggCall : aggregate.getAggCallList()) {
      for (int i : aggCall.getArgList()) {
        inputFieldsUsed.set(i);
      }
    }

    // Create input with trimmed columns.
    final RelNode input = aggregate.getInput(0);
    final Set<RelDataTypeField> inputExtraFields = Collections.emptySet();
    final TrimResult trimResult = trimChild(aggregate, input, inputFieldsUsed, inputExtraFields);
    final RelNode newInput = trimResult.left;
    final Mapping inputMapping = trimResult.right;

    // If the input is unchanged, and we need to project all columns,
    // there's nothing to do.
    if (input == newInput && fieldsUsed.equals(Util.bitSetBetween(0, rowType.getFieldCount()))) {
      return new TrimResult(aggregate, Mappings.createIdentity(rowType.getFieldCount()));
    }

    // Which agg calls are used by our consumer?
    final int groupCount = aggregate.getGroupSet().cardinality();
    int j = groupCount;
    int usedAggCallCount = 0;
    for (int i = 0; i < aggregate.getAggCallList().size(); i++) {
      if (fieldsUsed.get(j++)) {
        ++usedAggCallCount;
      }
    }

    // Offset due to the number of system fields having changed.
    Mapping mapping =
        Mappings.create(
            MappingType.InverseSurjection, rowType.getFieldCount(), groupCount + usedAggCallCount);

    final BitSet newGroupSet = Mappings.apply(inputMapping, aggregate.getGroupSet());

    // Populate mapping of where to find the fields. System and grouping
    // fields first.
    for (IntPair pair : inputMapping) {
      if (pair.source < groupCount) {
        mapping.set(pair.source, pair.target);
      }
    }

    // Now create new agg calls, and populate mapping for them.
    final List<AggregateCall> newAggCallList = new ArrayList<AggregateCall>();
    j = groupCount;
    for (AggregateCall aggCall : aggregate.getAggCallList()) {
      if (fieldsUsed.get(j)) {
        AggregateCall newAggCall =
            new AggregateCall(
                aggCall.getAggregation(),
                aggCall.isDistinct(),
                Mappings.apply2(inputMapping, aggCall.getArgList()),
                aggCall.getType(),
                aggCall.getName());
        if (newAggCall.equals(aggCall)) {
          newAggCall = aggCall; // immutable -> canonize to save space
        }
        mapping.set(j, groupCount + newAggCallList.size());
        newAggCallList.add(newAggCall);
      }
      ++j;
    }

    RelNode newAggregate =
        new AggregateRel(aggregate.getCluster(), newInput, newGroupSet, newAggCallList);

    assert newAggregate.getClass() == aggregate.getClass();

    return new TrimResult(newAggregate, mapping);
  }
예제 #18
0
 /**
  * Creates a RelFieldTrimmer.
  *
  * @param validator Validator
  */
 public RelFieldTrimmer(SqlValidator validator) {
   Util.discard(validator); // may be useful one day
   this.trimFieldsDispatcher =
       ReflectUtil.createMethodDispatcher(
           TrimResult.class, this, "trimFields", RelNode.class, BitSet.class, Set.class);
 }