org.apache.pig.Expression.InExpression Java Examples

The following examples show how to use org.apache.pig.Expression.InExpression. 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 File: IcebergStorage.java    From iceberg with 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 #2
Source File: ParquetLoader.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
private FilterPredicate buildFilter(Expression e) {
  OpType op = e.getOpType();

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

    switch (op) {
      case OP_AND:
        return and(buildFilter(lhs), buildFilter(rhs));
      case OP_OR:
        return or(buildFilter(lhs), buildFilter(rhs));
      case OP_BETWEEN:
        BetweenExpression between = (BetweenExpression) rhs;
        return and(
            buildFilter(OpType.OP_GE, (Column) lhs, (Const) between.getLower()),
            buildFilter(OpType.OP_LE, (Column) lhs, (Const) between.getUpper()));
      case OP_IN:
        FilterPredicate current = null;
        for (Object value : ((InExpression) rhs).getValues()) {
          FilterPredicate next = buildFilter(OpType.OP_EQ, (Column) lhs, (Const) value);
          if (current != null) {
            current = or(current, next);
          } else {
            current = next;
          }
        }
        return current;
    }

    if (lhs instanceof Column && rhs instanceof Const) {
      return buildFilter(op, (Column) lhs, (Const) rhs);
    } else if (lhs instanceof Const && rhs instanceof Column) {
      return buildFilter(op, (Column) rhs, (Const) lhs);
    }
  } else if (e instanceof UnaryExpression && op == OpType.OP_NOT) {
    return LogicalInverseRewriter.rewrite(
        not(buildFilter(((UnaryExpression) e).getExpression())));
  }

  throw new RuntimeException("Could not build filter for expression: " + e);
}
 
Example #3
Source File: OrcStorage.java    From spork with Apache License 2.0 4 votes vote down vote up
private void buildSearchArgument(Expression expr, Builder builder) {
    if (expr instanceof BinaryExpression) {
        Expression lhs = ((BinaryExpression) expr).getLhs();
        Expression rhs = ((BinaryExpression) expr).getRhs();
        switch (expr.getOpType()) {
        case OP_AND:
            builder.startAnd();
            buildSearchArgument(lhs, builder);
            buildSearchArgument(rhs, builder);
            builder.end();
            break;
        case OP_OR:
            builder.startOr();
            buildSearchArgument(lhs, builder);
            buildSearchArgument(rhs, builder);
            builder.end();
            break;
        case OP_EQ:
            builder.equals(getColumnName(lhs), getExpressionValue(rhs));
            break;
        case OP_NE:
            builder.startNot();
            builder.equals(getColumnName(lhs), getExpressionValue(rhs));
            builder.end();
            break;
        case OP_LT:
            builder.lessThan(getColumnName(lhs), getExpressionValue(rhs));
            break;
        case OP_LE:
            builder.lessThanEquals(getColumnName(lhs), getExpressionValue(rhs));
            break;
        case OP_GT:
            builder.startNot();
            builder.lessThanEquals(getColumnName(lhs), getExpressionValue(rhs));
            builder.end();
            break;
        case OP_GE:
            builder.startNot();
            builder.lessThan(getColumnName(lhs), getExpressionValue(rhs));
            builder.end();
            break;
        case OP_BETWEEN:
            BetweenExpression between = (BetweenExpression) rhs;
            builder.between(getColumnName(lhs), getSearchArgObjValue(between.getLower()),  getSearchArgObjValue(between.getUpper()));
        case OP_IN:
            InExpression in = (InExpression) rhs;
            builder.in(getColumnName(lhs), getSearchArgObjValues(in.getValues()).toArray());
        default:
            throw new RuntimeException("Unsupported binary expression type: " + expr.getOpType() + " in " + expr);
        }
    } else if (expr instanceof UnaryExpression) {
        Expression unaryExpr = ((UnaryExpression) expr).getExpression();
        switch (expr.getOpType()) {
        case OP_NULL:
            builder.isNull(getColumnName(unaryExpr));
            break;
        case OP_NOT:
            builder.startNot();
            buildSearchArgument(unaryExpr, builder);
            builder.end();
            break;
        default:
            throw new RuntimeException("Unsupported unary expression type: " +
                    expr.getOpType() + " in " + expr);
        }
    } else {
        throw new RuntimeException("Unsupported expression type: " + expr.getOpType() + " in " + expr);
    }
}