private static TupleFilter buildInFilter(
      TblColRef[] hostCols, Set<Array<String>> satisfyingHostRecords) {
    if (satisfyingHostRecords.size() == 0) {
      return ConstantTupleFilter.FALSE;
    }

    int hn = hostCols.length;
    if (hn == 1) {
      CompareTupleFilter in = new CompareTupleFilter(FilterOperatorEnum.IN);
      in.addChild(new ColumnTupleFilter(hostCols[0]));
      in.addChild(new ConstantTupleFilter(asValues(satisfyingHostRecords)));
      return in;
    } else {
      LogicalTupleFilter or = new LogicalTupleFilter(FilterOperatorEnum.OR);
      for (Array<String> rec : satisfyingHostRecords) {
        LogicalTupleFilter and = new LogicalTupleFilter(FilterOperatorEnum.AND);
        for (int i = 0; i < hn; i++) {
          CompareTupleFilter eq = new CompareTupleFilter(FilterOperatorEnum.EQ);
          eq.addChild(new ColumnTupleFilter(hostCols[i]));
          eq.addChild(new ConstantTupleFilter(rec.data[i]));
          and.addChild(eq);
        }
        or.addChild(and);
      }
      return or;
    }
  }
Exemplo n.º 2
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);
  }
 private static LogicalTupleFilter buildRangeFilter(
     TblColRef[] hostCols, Set<Array<String>> satisfyingHostRecords) {
   int hn = hostCols.length;
   String[] min = new String[hn];
   String[] max = new String[hn];
   findMinMax(satisfyingHostRecords, hostCols, min, max);
   LogicalTupleFilter and = new LogicalTupleFilter(FilterOperatorEnum.AND);
   for (int i = 0; i < hn; i++) {
     CompareTupleFilter compMin = new CompareTupleFilter(FilterOperatorEnum.GTE);
     compMin.addChild(new ColumnTupleFilter(hostCols[i]));
     compMin.addChild(new ConstantTupleFilter(min[i]));
     and.addChild(compMin);
     CompareTupleFilter compMax = new CompareTupleFilter(FilterOperatorEnum.LTE);
     compMax.addChild(new ColumnTupleFilter(hostCols[i]));
     compMax.addChild(new ConstantTupleFilter(max[i]));
     and.addChild(compMax);
   }
   return and;
 }