How to create an empty expression

Hi,
I am trying to make a Statement using Expression depending on multiple conditions.

   public Statement getStatement() {
        Expression selectExpression = x(bucketName + ".*");
        Expression whereExpression = getEmpty();

        if (!StringUtils.isBlank(searchParams.column1())) {
            whereExpression = whereExpression.and(x("column1").eq(x("$column1")));
        }

        if (!StringUtils.isBlank(searchParams.column2())) {
            whereExpression = whereExpression.and(x("column2").eq(x("$column2")));
        }

        Statement selectStatement;

        selectStatement = select(selectExpression)
                    .from(i(bucketName))
                    .where(whereExpression);

        return selectStatement;
    }

    private Expression getEmpty() {
        return path("1=1");
    }

In order to avoid multiple if and else while creating statement with where.and, I am seeding the statement with an empty condition getEmpty.

Question:

  1. Is this correct way?
  2. Any other alternative?

Thanks,
Ashwani

Hi,

@ingenthr: Any help on this…

I don’t see any harm in this, though it probably costs a trivial amount of parsing/planning time. You might need to have multiple indexes though that have this extra path in it. So, I think it’s fine, but you’ll just have to remember it’s like that. @subhashni has a lot of experience here, so she may be able to offer any additional advice.

@ashwanikumar04 you could also simply try x("") as a shortcut? Also, note that we have selectFromCurrentBucket which might save you some additional logic in your method :slight_smile:

Thanks @daschl

x("") does not work as it generates following query

SELECT * FROM sample where AND column1=1;

which is wrong.

Also, selectFromCurrentBucket. I did not find any reference of it. Can you suggest where it can be found.

Thanks.

@ashwanikumar04 yeah then x(“1=1”) makes sense. Sorry I was not precise enough on the other one, its select(…).fromCurrentBucket()…

Thanks. then I will take the same approach.

1 Like