Java Code Examples for org.apache.iceberg.expressions.Expressions#and()

The following examples show how to use org.apache.iceberg.expressions.Expressions#and() . These examples are extracted from open source projects. 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 Project: iceberg   File: FindFiles.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Filter results to files in any one of the given partitions.
 *
 * @param spec a spec for the partitions
 * @param partitions a list of StructLike that stores a partition tuple
 * @return this for method chaining
 */
public Builder inPartitions(PartitionSpec spec, List<StructLike> partitions) {
  Preconditions.checkArgument(spec.equals(ops.current().spec(spec.specId())),
      "Partition spec does not belong to table: %s", table);

  Expression partitionSetFilter = Expressions.alwaysFalse();
  for (StructLike partitionData : partitions) {
    Expression partFilter = Expressions.alwaysTrue();
    for (int i = 0; i < spec.fields().size(); i += 1) {
      PartitionField field = spec.fields().get(i);
      partFilter = Expressions.and(
          partFilter,
          Expressions.equal(field.name(), partitionData.get(i, Object.class)));
    }
    partitionSetFilter = Expressions.or(partitionSetFilter, partFilter);
  }

  if (partitionFilter != Expressions.alwaysTrue()) {
    this.partitionFilter = Expressions.or(partitionFilter, partitionSetFilter);
  } else {
    this.partitionFilter = partitionSetFilter;
  }

  return this;
}
 
Example 2
Source Project: iceberg   File: SparkFilters.java    License: Apache License 2.0 5 votes vote down vote up
public static Expression convert(Filter[] filters) {
  Expression expression = Expressions.alwaysTrue();
  for (Filter filter : filters) {
    Expression converted = convert(filter);
    Preconditions.checkArgument(converted != null, "Cannot convert filter to Iceberg: %s", filter);
    expression = Expressions.and(expression, converted);
  }
  return expression;
}
 
Example 3
Source Project: iceberg   File: ManifestReader.java    License: Apache License 2.0 5 votes vote down vote up
private Evaluator evaluator() {
  if (lazyEvaluator == null) {
    Expression projected = Projections.inclusive(spec, caseSensitive).project(rowFilter);
    Expression finalPartFilter = Expressions.and(projected, partFilter);
    if (finalPartFilter != null) {
      this.lazyEvaluator = new Evaluator(spec.partitionType(), finalPartFilter, caseSensitive);
    } else {
      this.lazyEvaluator = new Evaluator(spec.partitionType(), Expressions.alwaysTrue(), caseSensitive);
    }
  }
  return lazyEvaluator;
}
 
Example 4
Source Project: iceberg   File: ScanSummary.java    License: Apache License 2.0 5 votes vote down vote up
static Expression joinFilters(List<Expression> expressions) {
  Expression result = Expressions.alwaysTrue();
  for (Expression expression : expressions) {
    result = Expressions.and(result, expression);
  }
  return result;
}
 
Example 5
Source Project: metacat   File: IcebergFilterGenerator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object visit(final ASTBETWEEN node, final Object data) {
    final Object value = node.jjtGetChild(0).jjtAccept(this, data);
    final Object startValue = node.jjtGetChild(1).jjtAccept(this, data);
    final Object endValue = node.jjtGetChild(2).jjtAccept(this, data);
    final Expression compare1 =
        createIcebergExpression(value, startValue, node.not ? Compare.LT : Compare.GTE);
    final Expression compare2 =
        createIcebergExpression(value, endValue, node.not ? Compare.GT : Compare.LTE);
    return (node.not)
        ? Expressions.or(compare1, compare2) : Expressions.and(compare1, compare2);
}
 
Example 6
Source Project: iceberg   File: IcebergStorage.java    License: Apache License 2.0 4 votes vote down vote up
private org.apache.iceberg.expressions.Expression convert(Expression expression) throws IOException {
  OpType op = expression.getOpType();

  if (expression instanceof BinaryExpression) {
    Expression lhs = ((BinaryExpression) expression).getLhs();
    Expression rhs = ((BinaryExpression) expression).getRhs();

    switch (op) {
      case OP_AND:
        return Expressions.and(convert(lhs), convert(rhs));
      case OP_OR:
        return Expressions.or(convert(lhs), convert(rhs));
      case OP_BETWEEN:
        BetweenExpression between = (BetweenExpression) rhs;
        return Expressions.and(
            convert(OpType.OP_GE, (Column) lhs, (Const) between.getLower()),
            convert(OpType.OP_LE, (Column) lhs, (Const) between.getUpper())
        );
      case OP_IN:
        return ((InExpression) rhs).getValues().stream()
            .map(value -> convert(OpType.OP_EQ, (Column) lhs, (Const) value))
            .reduce(Expressions.alwaysFalse(), Expressions::or);
      default:
        if (lhs instanceof Column && rhs instanceof Const) {
          return convert(op, (Column) lhs, (Const) rhs);
        } else if (lhs instanceof Const && rhs instanceof Column) {
          throw new FrontendException("Invalid expression ordering " + expression);
        }
    }

  } else if (expression instanceof UnaryExpression) {
    Expression unary = ((UnaryExpression) expression).getExpression();

    switch (op) {
      case OP_NOT:  return Expressions.not(convert(unary));
      case OP_NULL: return Expressions.isNull(((Column) unary).getName());
      default: throw new FrontendException("Unsupported unary operator" + op);
    }
  }

  throw new FrontendException("Failed to pushdown expression " + expression);
}
 
Example 7
Source Project: iceberg   File: ManifestReader.java    License: Apache License 2.0 4 votes vote down vote up
public ManifestReader<F> filterPartitions(Expression expr) {
  this.partFilter = Expressions.and(partFilter, expr);
  return this;
}
 
Example 8
Source Project: iceberg   File: ManifestReader.java    License: Apache License 2.0 4 votes vote down vote up
public ManifestReader<F> filterRows(Expression expr) {
  this.rowFilter = Expressions.and(rowFilter, expr);
  return this;
}
 
Example 9
Source Project: iceberg   File: ManifestGroup.java    License: Apache License 2.0 4 votes vote down vote up
ManifestGroup filterData(Expression newDataFilter) {
  this.dataFilter = Expressions.and(dataFilter, newDataFilter);
  return this;
}
 
Example 10
Source Project: iceberg   File: ManifestGroup.java    License: Apache License 2.0 4 votes vote down vote up
ManifestGroup filterFiles(Expression newFileFilter) {
  this.fileFilter = Expressions.and(fileFilter, newFileFilter);
  return this;
}
 
Example 11
Source Project: iceberg   File: ManifestGroup.java    License: Apache License 2.0 4 votes vote down vote up
ManifestGroup filterPartitions(Expression newPartitionFilter) {
  this.partitionFilter = Expressions.and(partitionFilter, newPartitionFilter);
  return this;
}
 
Example 12
Source Project: metacat   File: IcebergFilterGenerator.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Object visit(final ASTAND node, final Object data) {
    return Expressions.and((Expression) node.jjtGetChild(0).jjtAccept(this, data),
        (Expression) node.jjtGetChild(1).jjtAccept(this, data));

}
 
Example 13
Source Project: iceberg   File: RewriteDataFilesAction.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Pass a row Expression to filter DataFiles to be rewritten. Note that all files that may contain data matching the
 * filter may be rewritten.
 *
 * @param expr Expression to filter out DataFiles
 * @return this for method chaining
 */
public RewriteDataFilesAction filter(Expression expr) {
  this.filter = Expressions.and(filter, expr);
  return this;
}
 
Example 14
Source Project: iceberg   File: FindFiles.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Filter results using a record filter. Files that may contain at least one matching record
 * will be returned by {@link #collect()}.
 *
 * @param expr a record filter
 * @return this for method chaining
 */
public Builder withRecordsMatching(Expression expr) {
  this.rowFilter = Expressions.and(rowFilter, expr);
  return this;
}
 
Example 15
Source Project: iceberg   File: FindFiles.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Filter results using a metadata filter for the data in a {@link DataFile}.
 *
 * @param expr a filter for {@link DataFile} metadata columns
 * @return this for method chaining
 */
public Builder withMetadataMatching(Expression expr) {
  this.fileFilter = Expressions.and(fileFilter, expr);
  return this;
}