Esempio n. 1
0
    private CompareTupleFilter mergeToInClause(TupleFilter filter) {
      List<? extends TupleFilter> children = filter.getChildren();
      TblColRef inColumn = null;
      List<String> inValues = new LinkedList<String>();
      for (TupleFilter child : children) {
        if (child.getOperator() == FilterOperatorEnum.EQ) {
          CompareTupleFilter compFilter = (CompareTupleFilter) child;
          TblColRef column = compFilter.getColumn();
          if (inColumn == null) {
            inColumn = column;
          }

          if (column == null || !column.equals(inColumn)) {
            return null;
          }
          inValues.addAll(compFilter.getValues());
        } else {
          return null;
        }
      }

      children.clear();

      CompareTupleFilter inFilter = new CompareTupleFilter(FilterOperatorEnum.IN);
      inFilter.addChild(new ColumnTupleFilter(inColumn));
      inFilter.addChild(new ConstantTupleFilter(inValues));
      return inFilter;
    }
  public static Pair<TupleFilter, Boolean> translate(
      LookupStringTable lookup, DeriveInfo hostInfo, CompareTupleFilter compf) {

    TblColRef derivedCol = compf.getColumn();
    TblColRef[] hostCols = hostInfo.columns;
    TblColRef[] pkCols = hostInfo.dimension.getJoin().getPrimaryKeyColumns();

    if (hostInfo.type == DeriveType.PK_FK) {
      assert hostCols.length == 1;
      CompareTupleFilter newComp = new CompareTupleFilter(compf.getOperator());
      newComp.addChild(new ColumnTupleFilter(hostCols[0]));
      newComp.addChild(new ConstantTupleFilter(compf.getValues()));
      return new Pair<TupleFilter, Boolean>(newComp, false);
    }

    assert hostInfo.type == DeriveType.LOOKUP;
    assert hostCols.length == pkCols.length;

    int di = derivedCol.getColumn().getZeroBasedIndex();
    int[] pi = new int[pkCols.length];
    int hn = hostCols.length;
    for (int i = 0; i < hn; i++) {
      pi[i] = pkCols[i].getColumn().getZeroBasedIndex();
    }

    Set<Array<String>> satisfyingHostRecords = Sets.newHashSet();
    SingleColumnTuple tuple = new SingleColumnTuple(derivedCol);
    for (String[] row : lookup.getAllRows()) {
      tuple.value = row[di];
      if (compf.evaluate(tuple)) {
        collect(row, pi, satisfyingHostRecords);
      }
    }

    TupleFilter translated;
    boolean loosened;
    if (satisfyingHostRecords.size() > IN_THRESHOLD) {
      translated = buildRangeFilter(hostCols, satisfyingHostRecords);
      loosened = true;
    } else {
      translated = buildInFilter(hostCols, satisfyingHostRecords);
      loosened = false;
    }

    return new Pair<TupleFilter, Boolean>(translated, loosened);
  }