示例#1
0
 /**
  * Makes a name distinct from other names which have already been used, adds it to the list, and
  * returns it.
  *
  * @param name Suggested name, may not be unique
  * @param nameList Collection of names already used
  * @param suggester Base for name when input name is null
  * @return Unique name
  */
 public static String uniquify(String name, Set<String> nameList, Suggester suggester) {
   if (name != null) {
     if (nameList.add(name)) {
       return name;
     }
   }
   final String originalName = name;
   for (int j = 0; ; j++) {
     name = suggester.apply(originalName, j, nameList.size());
     if (nameList.add(name)) {
       return name;
     }
   }
 }
示例#2
0
 public void collectVariablesUsed(Set<String> variableSet) {
   final RelOptUtil.VariableUsedVisitor vuv = new RelOptUtil.VariableUsedVisitor();
   for (RexNode expr : program.getExprList()) {
     expr.accept(vuv);
   }
   variableSet.addAll(vuv.variables);
 }
示例#3
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);
 }
示例#4
0
  public boolean canCastFrom(SqlTypeName to, SqlTypeName from, boolean coerce) {
    assert (null != to);
    assert (null != from);

    Map<SqlTypeName, Set<SqlTypeName>> ruleset = coerce ? coerceRules : rules;

    if (to.equals(SqlTypeName.NULL)) {
      return false;
    } else if (from.equals(SqlTypeName.NULL)) {
      return true;
    }

    Set<SqlTypeName> rule = ruleset.get(to);
    if (null == rule) {
      // if you hit this assert, see the constructor of this class on how
      // to add new rule
      throw Util.newInternal("No assign rules for " + to + " defined");
    }

    return rule.contains(from);
  }
示例#5
0
 /**
  * Invokes {@link #trimFields}, or the appropriate method for the type of the rel parameter, using
  * multi-method dispatch.
  *
  * @param rel Relational expression
  * @param fieldsUsed Bitmap of fields needed by the consumer
  * @return New relational expression and its field mapping
  */
 protected final TrimResult dispatchTrimFields(
     RelNode rel, BitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
   final TrimResult trimResult = trimFieldsDispatcher.invoke(rel, fieldsUsed, extraFields);
   final RelNode newRel = trimResult.left;
   final Mapping mapping = trimResult.right;
   final int fieldCount = rel.getRowType().getFieldCount();
   assert mapping.getSourceCount() == fieldCount
       : "source: " + mapping.getSourceCount() + " != " + fieldCount;
   final int newFieldCount = newRel.getRowType().getFieldCount();
   assert mapping.getTargetCount() + extraFields.size() == newFieldCount
       : "target: "
           + mapping.getTargetCount()
           + " + "
           + extraFields.size()
           + " != "
           + newFieldCount;
   if (Bug.TodoFixed) assert newFieldCount > 0 : "rel has no fields after trim: " + rel;
   if (newRel.equals(rel)) {
     return new TrimResult(rel, mapping);
   }
   return trimResult;
 }
示例#6
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);
  }