diff --git a/src/main/java/com/aerospike/dsl/parts/ExpressionContainer.java b/src/main/java/com/aerospike/dsl/parts/ExpressionContainer.java index d5ba17e..7f86c92 100644 --- a/src/main/java/com/aerospike/dsl/parts/ExpressionContainer.java +++ b/src/main/java/com/aerospike/dsl/parts/ExpressionContainer.java @@ -14,6 +14,9 @@ public class ExpressionContainer extends AbstractPart { @Setter() @Accessors(fluent = true) private boolean hasSecondaryIndexFilter; + @Setter() + @Accessors(fluent = true) + private boolean isExclFromSecondaryIndexFilter; public ExpressionContainer() { super(PartType.EXPRESSION_CONTAINER); diff --git a/src/main/java/com/aerospike/dsl/parts/cdt/CdtPart.java b/src/main/java/com/aerospike/dsl/parts/cdt/CdtPart.java index 4e14758..0a96f4a 100644 --- a/src/main/java/com/aerospike/dsl/parts/cdt/CdtPart.java +++ b/src/main/java/com/aerospike/dsl/parts/cdt/CdtPart.java @@ -21,4 +21,9 @@ public CTX getContext() { } public abstract int getReturnType(PathFunction.ReturnParam returnParam); + + public static boolean isCdtPart(AbstractPart part) { + return part.getPartType() == AbstractPart.PartType.LIST_PART + || part.getPartType() == AbstractPart.PartType.MAP_PART; + } } diff --git a/src/main/java/com/aerospike/dsl/parts/path/Path.java b/src/main/java/com/aerospike/dsl/parts/path/Path.java index 328927c..239083c 100644 --- a/src/main/java/com/aerospike/dsl/parts/path/Path.java +++ b/src/main/java/com/aerospike/dsl/parts/path/Path.java @@ -7,11 +7,8 @@ import java.util.List; -import static com.aerospike.dsl.util.PathOperandUtils.processGet; -import static com.aerospike.dsl.util.PathOperandUtils.processPathFunction; -import static com.aerospike.dsl.util.PathOperandUtils.processSize; -import static com.aerospike.dsl.util.PathOperandUtils.processValueType; -import static com.aerospike.dsl.util.PathOperandUtils.updateWithCdtTypeDesignator; +import static com.aerospike.dsl.parts.cdt.CdtPart.isCdtPart; +import static com.aerospike.dsl.util.PathOperandUtils.*; @Getter public class Path extends AbstractPart { @@ -33,8 +30,8 @@ public Exp processPath(BasePath basePath, PathFunction pathFunction) { Exp.Type valueType = processValueType(lastPathPart, pathFunction); int cdtReturnType = 0; - if (lastPathPart instanceof CdtPart lastPart) { - cdtReturnType = lastPart.getReturnType(pathFunction.getReturnParam()); + if (lastPathPart != null && isCdtPart(lastPathPart)) { + cdtReturnType = ((CdtPart) lastPathPart).getReturnType(pathFunction.getReturnParam()); } if (lastPathPart != null) { // only if there are other parts except a bin diff --git a/src/main/java/com/aerospike/dsl/util/PathOperandUtils.java b/src/main/java/com/aerospike/dsl/util/PathOperandUtils.java index 3da3d49..64f046e 100644 --- a/src/main/java/com/aerospike/dsl/util/PathOperandUtils.java +++ b/src/main/java/com/aerospike/dsl/util/PathOperandUtils.java @@ -105,16 +105,16 @@ private static boolean containOnlyCdtDesignator(List parts) { } private static boolean isPrevCdtPartAmbiguous(AbstractPart lastPart) { - if (lastPart instanceof MapPart mapPart) { // check that lastPart is CDT Map + if (lastPart.getPartType() == MAP_PART) { // check that lastPart is CDT Map // check relevant types return List.of(MapPart.MapPartType.INDEX, MapPart.MapPartType.RANK, MapPart.MapPartType.KEY, MapPart.MapPartType.VALUE) - .contains(mapPart.getMapPartType()); + .contains(((MapPart) lastPart).getMapPartType()); } - if (lastPart instanceof ListPart listPart) { // check that lastPart is CDT List + if (lastPart.getPartType() == LIST_PART) { // check that lastPart is CDT List // check relevant types return List.of(INDEX, RANK, ListPart.ListPartType.VALUE) - .contains(listPart.getListPartType()); + .contains(((ListPart) lastPart).getListPartType()); } return false; } @@ -142,11 +142,12 @@ private static Exp doProcessCdtGet(BasePath basePath, AbstractPart lastPathPart, } private static boolean isListTypeDesignator(AbstractPart cdtPart) { - return cdtPart instanceof ListPart listPart && listPart.getListPartType().equals(LIST_TYPE_DESIGNATOR); + return cdtPart.getPartType() == LIST_PART + && ((ListPart) cdtPart).getListPartType().equals(LIST_TYPE_DESIGNATOR); } private static boolean isMapTypeDesignator(AbstractPart cdtPart) { - return cdtPart instanceof MapPart mapPart && mapPart.getMapPartType().equals(MAP_TYPE_DESIGNATOR); + return cdtPart.getPartType() == MAP_PART && ((MapPart) cdtPart).getMapPartType().equals(MAP_TYPE_DESIGNATOR); } private static CTX[] getContextArray(List parts, boolean includeLast) { diff --git a/src/main/java/com/aerospike/dsl/util/TypeUtils.java b/src/main/java/com/aerospike/dsl/util/TypeUtils.java index ac8ea38..3a140e5 100644 --- a/src/main/java/com/aerospike/dsl/util/TypeUtils.java +++ b/src/main/java/com/aerospike/dsl/util/TypeUtils.java @@ -2,7 +2,6 @@ import com.aerospike.client.exp.Exp; import com.aerospike.dsl.parts.AbstractPart; -import com.aerospike.dsl.parts.cdt.map.MapPart; import com.aerospike.dsl.parts.cdt.map.MapTypeDesignator; import lombok.experimental.UtilityClass; @@ -10,7 +9,7 @@ public class TypeUtils { public static Exp.Type getDefaultType(AbstractPart part) { - if (part instanceof MapPart + if (part.getPartType() == AbstractPart.PartType.MAP_PART // MapTypeDesignator is usually combined with int based operations such as size && !(part instanceof MapTypeDesignator)) { // For all other Map parts the default type should be STRING diff --git a/src/main/java/com/aerospike/dsl/visitor/ExpressionConditionVisitor.java b/src/main/java/com/aerospike/dsl/visitor/ExpressionConditionVisitor.java index 3be45a8..1ef198e 100644 --- a/src/main/java/com/aerospike/dsl/visitor/ExpressionConditionVisitor.java +++ b/src/main/java/com/aerospike/dsl/visitor/ExpressionConditionVisitor.java @@ -440,8 +440,8 @@ private AbstractPart overrideType(AbstractPart part, ParseTree ctx) { if (pathFunction != null) { Exp.Type type = pathFunction.getBinType(); if (type != null) { - if (part instanceof BinPart binPart) { - binPart.updateExp(type); + if (part.getPartType() == AbstractPart.PartType.BIN_PART) { + ((BinPart) part).updateExp(type); } else { part.setExpType(type); } @@ -449,11 +449,11 @@ private AbstractPart overrideType(AbstractPart part, ParseTree ctx) { } } else { // Override using Implicit type detection Exp.Type implicitType = detectImplicitTypeFromUpperTree(ctx); - if (part instanceof BinPart binPart) { + if (part.getPartType() == AbstractPart.PartType.BIN_PART) { if (implicitType == null) { implicitType = Exp.Type.INT; } - binPart.updateExp(implicitType); + ((BinPart) part).updateExp(implicitType); } else { // ListPart or MapPart if (implicitType == null) { implicitType = TypeUtils.getDefaultType(part); diff --git a/src/main/java/com/aerospike/dsl/visitor/VisitorUtils.java b/src/main/java/com/aerospike/dsl/visitor/VisitorUtils.java index a954924..408c4e4 100644 --- a/src/main/java/com/aerospike/dsl/visitor/VisitorUtils.java +++ b/src/main/java/com/aerospike/dsl/visitor/VisitorUtils.java @@ -1132,11 +1132,16 @@ private static ExpressionContainer chooseExprForFilter(ExpressionContainer exprC Map> exprsPerCardinality = getExpressionsPerCardinality(exprContainer, indexes); - // Find the entry with the largest key (cardinality) - Map> largestCardinalityMap = exprsPerCardinality.entrySet().stream() - .max(Map.Entry.comparingByKey()) - .map(entry -> Map.of(entry.getKey(), entry.getValue())) - .orElse(Collections.emptyMap()); + Map> largestCardinalityMap; + if (exprsPerCardinality.size() > 1) { + // Find the entry with the largest key (cardinality) + largestCardinalityMap = exprsPerCardinality.entrySet().stream() + .max(Map.Entry.comparingByKey()) + .map(entry -> Map.of(entry.getKey(), entry.getValue())) + .orElse(Collections.emptyMap()); + } else { + largestCardinalityMap = new HashMap<>(exprsPerCardinality); + } List largestCardinalityExprs; if (largestCardinalityMap.isEmpty()) return null; @@ -1249,7 +1254,23 @@ private static BinPart getBinPart(ExpressionContainer expr, int depth) { Predicate stopOnLogicalExpr = part -> { if (part.getPartType() == EXPRESSION_CONTAINER) { ExpressionContainer logicalExpr = (ExpressionContainer) part; - return logicalExpr.getOperationType() == AND || logicalExpr.getOperationType() == OR; + if (logicalExpr.isExclFromSecondaryIndexFilter()) { + // All parts of the tree branch excluded from secondary index Filter building are flagged + if (logicalExpr.getLeft().getPartType() == EXPRESSION_CONTAINER) { + ((ExpressionContainer) logicalExpr.getLeft()).isExclFromSecondaryIndexFilter(true); + } + if (logicalExpr.getRight().getPartType() == EXPRESSION_CONTAINER) { + ((ExpressionContainer) logicalExpr.getRight()).isExclFromSecondaryIndexFilter(true); + } + return true; + } + if (logicalExpr.getOperationType() == AND) return true; + if (logicalExpr.getOperationType() == OR) { + // Both parts of OR-combined query are excluded from secondary index Filter building + ((ExpressionContainer) logicalExpr.getLeft()).isExclFromSecondaryIndexFilter(true); + ((ExpressionContainer) logicalExpr.getRight()).isExclFromSecondaryIndexFilter(true); + return true; + } } return false; }; diff --git a/src/test/java/com/aerospike/dsl/expression/ArithmeticExpressionsTests.java b/src/test/java/com/aerospike/dsl/expression/ArithmeticExpressionsTests.java index ec13080..bde2688 100644 --- a/src/test/java/com/aerospike/dsl/expression/ArithmeticExpressionsTests.java +++ b/src/test/java/com/aerospike/dsl/expression/ArithmeticExpressionsTests.java @@ -2,122 +2,123 @@ import com.aerospike.client.exp.Exp; import com.aerospike.dsl.DslParseException; +import com.aerospike.dsl.util.TestUtils; import org.junit.jupiter.api.Test; -import static com.aerospike.dsl.util.TestUtils.parseExp; -import static com.aerospike.dsl.util.TestUtils.parseExpAndCompare; +import static com.aerospike.dsl.util.TestUtils.parseFilterExp; +import static com.aerospike.dsl.util.TestUtils.parseDslExpressionAndCompare; import static org.assertj.core.api.Assertions.assertThatThrownBy; public class ArithmeticExpressionsTests { @Test void add() { - parseExpAndCompare("($.apples + $.bananas) > 10", + TestUtils.parseFilterExpressionAndCompare("($.apples + $.bananas) > 10", Exp.gt(Exp.add(Exp.intBin("apples"), Exp.intBin("bananas")), Exp.val(10))); - parseExpAndCompare("($.apples + 5) > 10", + TestUtils.parseFilterExpressionAndCompare("($.apples + 5) > 10", Exp.gt(Exp.add(Exp.intBin("apples"), Exp.val(5)), Exp.val(10))); - parseExpAndCompare("(5 + $.bananas) > 10", + TestUtils.parseFilterExpressionAndCompare("(5 + $.bananas) > 10", Exp.gt(Exp.add(Exp.val(5), Exp.intBin("bananas")), Exp.val(10))); - parseExpAndCompare("(5.2 + $.bananas) > 10.2", + TestUtils.parseFilterExpressionAndCompare("(5.2 + $.bananas) > 10.2", Exp.gt(Exp.add(Exp.val(5.2), Exp.floatBin("bananas")), Exp.val(10.2))); } @Test void sub() { - parseExpAndCompare("($.apples - $.bananas) == 10", + TestUtils.parseFilterExpressionAndCompare("($.apples - $.bananas) == 10", Exp.eq(Exp.sub(Exp.intBin("apples"), Exp.intBin("bananas")), Exp.val(10))); - parseExpAndCompare("($.apples - 5) == 10", + TestUtils.parseFilterExpressionAndCompare("($.apples - 5) == 10", Exp.eq(Exp.sub(Exp.intBin("apples"), Exp.val(5)), Exp.val(10))); - parseExpAndCompare("(15 - $.bananas) == 10", + TestUtils.parseFilterExpressionAndCompare("(15 - $.bananas) == 10", Exp.eq(Exp.sub(Exp.val(15), Exp.intBin("bananas")), Exp.val(10))); } @Test void mul() { - parseExpAndCompare("($.apples * $.bananas) != 10", + TestUtils.parseFilterExpressionAndCompare("($.apples * $.bananas) != 10", Exp.ne(Exp.mul(Exp.intBin("apples"), Exp.intBin("bananas")), Exp.val(10))); - parseExpAndCompare("($.apples * 7) != 10", + TestUtils.parseFilterExpressionAndCompare("($.apples * 7) != 10", Exp.ne(Exp.mul(Exp.intBin("apples"), Exp.val(7)), Exp.val(10))); - parseExpAndCompare("(3 * $.bananas) != 10", + TestUtils.parseFilterExpressionAndCompare("(3 * $.bananas) != 10", Exp.ne(Exp.mul(Exp.val(3), Exp.intBin("bananas")), Exp.val(10))); } @Test void div() { - parseExpAndCompare("($.apples / $.bananas) <= 10", + TestUtils.parseFilterExpressionAndCompare("($.apples / $.bananas) <= 10", Exp.le(Exp.div(Exp.intBin("apples"), Exp.intBin("bananas")), Exp.val(10))); - parseExpAndCompare("($.apples / 5) <= 10", + TestUtils.parseFilterExpressionAndCompare("($.apples / 5) <= 10", Exp.le(Exp.div(Exp.intBin("apples"), Exp.val(5)), Exp.val(10))); - parseExpAndCompare("(33 / $.bananas) <= 10", + TestUtils.parseFilterExpressionAndCompare("(33 / $.bananas) <= 10", Exp.le(Exp.div(Exp.val(33), Exp.intBin("bananas")), Exp.val(10))); // Exp should be constructed and equal and therefore the test is passing // but when actually triggered server will throw divide by zero exception - parseExpAndCompare("($.apples / 0) <= 10", + TestUtils.parseFilterExpressionAndCompare("($.apples / 0) <= 10", Exp.le(Exp.div(Exp.intBin("apples"), Exp.val(0)), Exp.val(10))); } @Test void mod() { - parseExpAndCompare("($.apples % $.bananas) != 10", + TestUtils.parseFilterExpressionAndCompare("($.apples % $.bananas) != 10", Exp.ne(Exp.mod(Exp.intBin("apples"), Exp.intBin("bananas")), Exp.val(10))); - parseExpAndCompare("($.apples % 7) != 10", + TestUtils.parseFilterExpressionAndCompare("($.apples % 7) != 10", Exp.ne(Exp.mod(Exp.intBin("apples"), Exp.val(7)), Exp.val(10))); - parseExpAndCompare("(3 % $.bananas) != 10", + TestUtils.parseFilterExpressionAndCompare("(3 % $.bananas) != 10", Exp.ne(Exp.mod(Exp.val(3), Exp.intBin("bananas")), Exp.val(10))); } @Test void intAnd() { -// parseExpAndCompare("($.apples & $.bananas) != 10", -// Exp.ne(Exp.intAnd(Exp.intBin("apples"), Exp.intBin("bananas")), Exp.val(10))); - parseExpAndCompare("($.apples & 7) != 10", + TestUtils.parseFilterExpressionAndCompare("($.apples & $.bananas) != 10", + Exp.ne(Exp.intAnd(Exp.intBin("apples"), Exp.intBin("bananas")), Exp.val(10))); + TestUtils.parseFilterExpressionAndCompare("($.apples & 7) != 10", Exp.ne(Exp.intAnd(Exp.intBin("apples"), Exp.val(7)), Exp.val(10))); - parseExpAndCompare("(3 & $.bananas) != 10", + TestUtils.parseFilterExpressionAndCompare("(3 & $.bananas) != 10", Exp.ne(Exp.intAnd(Exp.val(3), Exp.intBin("bananas")), Exp.val(10))); } @Test void intOr() { - parseExpAndCompare("($.apples | $.bananas) != 10", + TestUtils.parseFilterExpressionAndCompare("($.apples | $.bananas) != 10", Exp.ne(Exp.intOr(Exp.intBin("apples"), Exp.intBin("bananas")), Exp.val(10))); - parseExpAndCompare("($.apples | 7) != 10", + TestUtils.parseFilterExpressionAndCompare("($.apples | 7) != 10", Exp.ne(Exp.intOr(Exp.intBin("apples"), Exp.val(7)), Exp.val(10))); - parseExpAndCompare("(3 | $.bananas) != 10", + TestUtils.parseFilterExpressionAndCompare("(3 | $.bananas) != 10", Exp.ne(Exp.intOr(Exp.val(3), Exp.intBin("bananas")), Exp.val(10))); } @Test void intXor() { - parseExpAndCompare("($.apples ^ $.bananas) != 10", + TestUtils.parseFilterExpressionAndCompare("($.apples ^ $.bananas) != 10", Exp.ne(Exp.intXor(Exp.intBin("apples"), Exp.intBin("bananas")), Exp.val(10))); - parseExpAndCompare("($.apples ^ 7) != 10", + TestUtils.parseFilterExpressionAndCompare("($.apples ^ 7) != 10", Exp.ne(Exp.intXor(Exp.intBin("apples"), Exp.val(7)), Exp.val(10))); - parseExpAndCompare("(3 ^ $.bananas) != 10", + TestUtils.parseFilterExpressionAndCompare("(3 ^ $.bananas) != 10", Exp.ne(Exp.intXor(Exp.val(3), Exp.intBin("bananas")), Exp.val(10))); } @Test void intNot() { - parseExpAndCompare("(~$.apples) != 10", + TestUtils.parseFilterExpressionAndCompare("(~$.apples) != 10", Exp.ne(Exp.intNot(Exp.intBin("apples")), Exp.val(10))); } @Test void intLShift() { - parseExpAndCompare("$.visits << 1", + TestUtils.parseFilterExpressionAndCompare("$.visits << 1", Exp.lshift(Exp.intBin("visits"), Exp.val(1))); } @Test void intRShift() { - parseExpAndCompare("(($.flags >> 6) & 1) == 1", + TestUtils.parseFilterExpressionAndCompare("(($.flags >> 6) & 1) == 1", Exp.eq(Exp.intAnd(Exp.rshift(Exp.intBin("flags"), Exp.val(6)), Exp.val(1)), Exp.val(1))); } @Test void negativeArithmetic() { - assertThatThrownBy(() -> parseExp("($.apples.get(type: STRING) + 5) > 10")) + assertThatThrownBy(() -> parseFilterExp("($.apples.get(type: STRING) + 5) > 10")) .isInstanceOf(DslParseException.class) .hasMessageContaining("Cannot compare STRING to INT"); diff --git a/src/test/java/com/aerospike/dsl/expression/BinExpressionsTests.java b/src/test/java/com/aerospike/dsl/expression/BinExpressionsTests.java index 1342b8b..ae218c9 100644 --- a/src/test/java/com/aerospike/dsl/expression/BinExpressionsTests.java +++ b/src/test/java/com/aerospike/dsl/expression/BinExpressionsTests.java @@ -4,61 +4,60 @@ import com.aerospike.dsl.DslParseException; import org.junit.jupiter.api.Test; -import static com.aerospike.dsl.util.TestUtils.parseExp; -import static com.aerospike.dsl.util.TestUtils.parseExpAndCompare; +import static com.aerospike.dsl.util.TestUtils.*; import static org.assertj.core.api.Assertions.assertThatThrownBy; class BinExpressionsTests { @Test void binGT() { - parseExpAndCompare("$.intBin1 > 100", Exp.gt(Exp.intBin("intBin1"), Exp.val(100))); - parseExpAndCompare("$.stringBin1 > 'text'", Exp.gt(Exp.stringBin("stringBin1"), Exp.val("text"))); - parseExpAndCompare("$.stringBin1 > \"text\"", Exp.gt(Exp.stringBin("stringBin1"), Exp.val("text"))); + parseFilterExpressionAndCompare("$.intBin1 > 100", Exp.gt(Exp.intBin("intBin1"), Exp.val(100))); + parseFilterExpressionAndCompare("$.stringBin1 > 'text'", Exp.gt(Exp.stringBin("stringBin1"), Exp.val("text"))); + parseFilterExpressionAndCompare("$.stringBin1 > \"text\"", Exp.gt(Exp.stringBin("stringBin1"), Exp.val("text"))); - parseExpAndCompare("100 < $.intBin1", Exp.lt(Exp.val(100), Exp.intBin("intBin1"))); - parseExpAndCompare("'text' < $.stringBin1", Exp.lt(Exp.val("text"), Exp.stringBin("stringBin1"))); - parseExpAndCompare("\"text\" < $.stringBin1", Exp.lt(Exp.val("text"), Exp.stringBin("stringBin1"))); + parseFilterExpressionAndCompare("100 < $.intBin1", Exp.lt(Exp.val(100), Exp.intBin("intBin1"))); + parseFilterExpressionAndCompare("'text' < $.stringBin1", Exp.lt(Exp.val("text"), Exp.stringBin("stringBin1"))); + parseFilterExpressionAndCompare("\"text\" < $.stringBin1", Exp.lt(Exp.val("text"), Exp.stringBin("stringBin1"))); } @Test void binGE() { - parseExpAndCompare("$.intBin1 >= 100", Exp.ge(Exp.intBin("intBin1"), Exp.val(100))); - parseExpAndCompare("$.stringBin1 >= 'text'", Exp.ge(Exp.stringBin("stringBin1"), Exp.val("text"))); - parseExpAndCompare("$.stringBin1 >= \"text\"", Exp.ge(Exp.stringBin("stringBin1"), Exp.val("text"))); + parseFilterExpressionAndCompare("$.intBin1 >= 100", Exp.ge(Exp.intBin("intBin1"), Exp.val(100))); + parseFilterExpressionAndCompare("$.stringBin1 >= 'text'", Exp.ge(Exp.stringBin("stringBin1"), Exp.val("text"))); + parseFilterExpressionAndCompare("$.stringBin1 >= \"text\"", Exp.ge(Exp.stringBin("stringBin1"), Exp.val("text"))); } @Test void binLT() { - parseExpAndCompare("$.intBin1 < 100", Exp.lt(Exp.intBin("intBin1"), Exp.val(100))); - parseExpAndCompare("$.stringBin1 < 'text'", Exp.lt(Exp.stringBin("stringBin1"), Exp.val("text"))); - parseExpAndCompare("$.stringBin1 < \"text\"", Exp.lt(Exp.stringBin("stringBin1"), Exp.val("text"))); + parseFilterExpressionAndCompare("$.intBin1 < 100", Exp.lt(Exp.intBin("intBin1"), Exp.val(100))); + parseFilterExpressionAndCompare("$.stringBin1 < 'text'", Exp.lt(Exp.stringBin("stringBin1"), Exp.val("text"))); + parseFilterExpressionAndCompare("$.stringBin1 < \"text\"", Exp.lt(Exp.stringBin("stringBin1"), Exp.val("text"))); } @Test void binLE() { - parseExpAndCompare("$.intBin1 <= 100", Exp.le(Exp.intBin("intBin1"), Exp.val(100))); - parseExpAndCompare("$.stringBin1 <= 'text'", Exp.le(Exp.stringBin("stringBin1"), Exp.val("text"))); - parseExpAndCompare("$.stringBin1 <= \"text\"", Exp.le(Exp.stringBin("stringBin1"), Exp.val("text"))); + parseFilterExpressionAndCompare("$.intBin1 <= 100", Exp.le(Exp.intBin("intBin1"), Exp.val(100))); + parseFilterExpressionAndCompare("$.stringBin1 <= 'text'", Exp.le(Exp.stringBin("stringBin1"), Exp.val("text"))); + parseFilterExpressionAndCompare("$.stringBin1 <= \"text\"", Exp.le(Exp.stringBin("stringBin1"), Exp.val("text"))); } @Test void binEquals() { - parseExpAndCompare("$.intBin1 == 100", Exp.eq(Exp.intBin("intBin1"), Exp.val(100))); - parseExpAndCompare("$.strBin == \"yes\"", Exp.eq(Exp.stringBin("strBin"), Exp.val("yes"))); - parseExpAndCompare("$.strBin == 'yes'", Exp.eq(Exp.stringBin("strBin"), Exp.val("yes"))); + parseFilterExpressionAndCompare("$.intBin1 == 100", Exp.eq(Exp.intBin("intBin1"), Exp.val(100))); + parseFilterExpressionAndCompare("$.strBin == \"yes\"", Exp.eq(Exp.stringBin("strBin"), Exp.val("yes"))); + parseFilterExpressionAndCompare("$.strBin == 'yes'", Exp.eq(Exp.stringBin("strBin"), Exp.val("yes"))); } @Test void binNotEquals() { - parseExpAndCompare("$.intBin1 != 100", Exp.ne(Exp.intBin("intBin1"), Exp.val(100))); - parseExpAndCompare("$.strBin != \"yes\"", Exp.ne(Exp.stringBin("strBin"), Exp.val("yes"))); - parseExpAndCompare("$.strBin != 'yes'", Exp.ne(Exp.stringBin("strBin"), Exp.val("yes"))); + parseFilterExpressionAndCompare("$.intBin1 != 100", Exp.ne(Exp.intBin("intBin1"), Exp.val(100))); + parseFilterExpressionAndCompare("$.strBin != \"yes\"", Exp.ne(Exp.stringBin("strBin"), Exp.val("yes"))); + parseFilterExpressionAndCompare("$.strBin != 'yes'", Exp.ne(Exp.stringBin("strBin"), Exp.val("yes"))); } @Test void negativeStringBinEquals() { - assertThatThrownBy(() -> parseExp("$.strBin == yes")) + assertThatThrownBy(() -> parseFilterExp("$.strBin == yes")) .isInstanceOf(DslParseException.class) .hasMessage("Unable to parse right operand"); } diff --git a/src/test/java/com/aerospike/dsl/expression/CastingTests.java b/src/test/java/com/aerospike/dsl/expression/CastingTests.java index 398664e..f41a000 100644 --- a/src/test/java/com/aerospike/dsl/expression/CastingTests.java +++ b/src/test/java/com/aerospike/dsl/expression/CastingTests.java @@ -2,10 +2,11 @@ import com.aerospike.client.exp.Exp; import com.aerospike.dsl.DslParseException; +import com.aerospike.dsl.util.TestUtils; import org.junit.jupiter.api.Test; -import static com.aerospike.dsl.util.TestUtils.parseExp; -import static com.aerospike.dsl.util.TestUtils.parseExpAndCompare; +import static com.aerospike.dsl.util.TestUtils.parseFilterExp; +import static com.aerospike.dsl.util.TestUtils.parseDslExpressionAndCompare; import static org.assertj.core.api.Assertions.assertThatThrownBy; public class CastingTests { @@ -14,19 +15,19 @@ public class CastingTests { void floatToIntComparison() { Exp expectedExp = Exp.gt(Exp.intBin("intBin1"), Exp.intBin("floatBin1")); // Int is default - parseExpAndCompare("$.intBin1 > $.floatBin1.asInt()", expectedExp); - parseExpAndCompare("$.intBin1.get(type: INT) > $.floatBin1.asInt()", expectedExp); + TestUtils.parseFilterExpressionAndCompare("$.intBin1 > $.floatBin1.asInt()", expectedExp); + TestUtils.parseFilterExpressionAndCompare("$.intBin1.get(type: INT) > $.floatBin1.asInt()", expectedExp); } @Test void intToFloatComparison() { - parseExpAndCompare("$.intBin1.get(type: INT) > $.intBin2.asFloat()", + TestUtils.parseFilterExpressionAndCompare("$.intBin1.get(type: INT) > $.intBin2.asFloat()", Exp.gt(Exp.intBin("intBin1"), Exp.floatBin("intBin2"))); } @Test void negativeInvalidTypesComparison() { - assertThatThrownBy(() -> parseExp("$.stringBin1.get(type: STRING) > $.intBin2.asFloat()")) + assertThatThrownBy(() -> parseFilterExp("$.stringBin1.get(type: STRING) > $.intBin2.asFloat()")) .isInstanceOf(DslParseException.class) .hasMessageContaining("Cannot compare STRING to FLOAT"); } diff --git a/src/test/java/com/aerospike/dsl/expression/ControlStructuresTests.java b/src/test/java/com/aerospike/dsl/expression/ControlStructuresTests.java index 5f89e1b..f8c1660 100644 --- a/src/test/java/com/aerospike/dsl/expression/ControlStructuresTests.java +++ b/src/test/java/com/aerospike/dsl/expression/ControlStructuresTests.java @@ -1,9 +1,10 @@ package com.aerospike.dsl.expression; import com.aerospike.client.exp.Exp; +import com.aerospike.dsl.util.TestUtils; import org.junit.jupiter.api.Test; -import static com.aerospike.dsl.util.TestUtils.parseExpAndCompare; +import static com.aerospike.dsl.util.TestUtils.parseDslExpressionAndCompare; public class ControlStructuresTests { @@ -17,10 +18,10 @@ void whenWithASingleDeclaration() { Exp.val("other") ); - parseExpAndCompare("when ($.who == 1 => \"bob\", default => \"other\")", + TestUtils.parseFilterExpressionAndCompare("when ($.who == 1 => \"bob\", default => \"other\")", expected); // different spacing style - parseExpAndCompare("when($.who == 1 => \"bob\", default => \"other\")", + TestUtils.parseFilterExpressionAndCompare("when($.who == 1 => \"bob\", default => \"other\")", expected); } @@ -40,7 +41,7 @@ void whenUsingTheResult() { // Implicit detect as String //translateAndCompare("$.stringBin1 == (when ($.who == 1 => \"bob\", default => \"other\"))", // expected); - parseExpAndCompare("$.stringBin1.get(type: STRING) == (when ($.who == 1 => \"bob\", default => \"other\"))", + TestUtils.parseFilterExpressionAndCompare("$.stringBin1.get(type: STRING) == (when ($.who == 1 => \"bob\", default => \"other\"))", expected); } @@ -58,7 +59,7 @@ void whenWithMultipleDeclarations() { Exp.val("other") ); - parseExpAndCompare("when ($.who == 1 => \"bob\", $.who == 2 => \"fred\", default => \"other\")", + TestUtils.parseFilterExpressionAndCompare("when ($.who == 1 => \"bob\", $.who == 2 => \"fred\", default => \"other\")", expected); } @@ -74,10 +75,10 @@ void withMultipleVariablesDefinitionAndUsage() { Exp.add(Exp.var("x"), Exp.var("y")) ); - parseExpAndCompare("with (x = 1, y = ${x} + 1) do (${x} + ${y})", + TestUtils.parseFilterExpressionAndCompare("with (x = 1, y = ${x} + 1) do (${x} + ${y})", expected); // different spacing style - parseExpAndCompare("with(x = 1, y = ${x}+1) do(${x}+${y})", + TestUtils.parseFilterExpressionAndCompare("with(x = 1, y = ${x}+1) do(${x}+${y})", expected); } } diff --git a/src/test/java/com/aerospike/dsl/expression/ExplicitTypesTests.java b/src/test/java/com/aerospike/dsl/expression/ExplicitTypesTests.java index d74a480..2e04bb4 100644 --- a/src/test/java/com/aerospike/dsl/expression/ExplicitTypesTests.java +++ b/src/test/java/com/aerospike/dsl/expression/ExplicitTypesTests.java @@ -2,14 +2,15 @@ import com.aerospike.client.exp.Exp; import com.aerospike.dsl.DslParseException; +import com.aerospike.dsl.util.TestUtils; import org.junit.jupiter.api.Test; import java.util.Base64; import java.util.List; import java.util.TreeMap; -import static com.aerospike.dsl.util.TestUtils.parseExp; -import static com.aerospike.dsl.util.TestUtils.parseExpAndCompare; +import static com.aerospike.dsl.util.TestUtils.parseFilterExp; +import static com.aerospike.dsl.util.TestUtils.parseDslExpressionAndCompare; import static org.assertj.core.api.Assertions.assertThatThrownBy; // Explicit types tests, list and map explicit types are tested in their own test classes @@ -17,33 +18,33 @@ public class ExplicitTypesTests { @Test void integerComparison() { - parseExpAndCompare("$.intBin1.get(type: INT) > 5", + TestUtils.parseFilterExpressionAndCompare("$.intBin1.get(type: INT) > 5", Exp.gt(Exp.intBin("intBin1"), Exp.val(5))); - parseExpAndCompare("5 < $.intBin1.get(type: INT)", + TestUtils.parseFilterExpressionAndCompare("5 < $.intBin1.get(type: INT)", Exp.lt(Exp.val(5), Exp.intBin("intBin1"))); } @Test void stringComparison() { // A String constant must contain quoted Strings - parseExpAndCompare("$.stringBin1.get(type: STRING) == \"yes\"", + TestUtils.parseFilterExpressionAndCompare("$.stringBin1.get(type: STRING) == \"yes\"", Exp.eq(Exp.stringBin("stringBin1"), Exp.val("yes"))); - parseExpAndCompare("$.stringBin1.get(type: STRING) == 'yes'", + TestUtils.parseFilterExpressionAndCompare("$.stringBin1.get(type: STRING) == 'yes'", Exp.eq(Exp.stringBin("stringBin1"), Exp.val("yes"))); - parseExpAndCompare("\"yes\" == $.stringBin1.get(type: STRING)", + TestUtils.parseFilterExpressionAndCompare("\"yes\" == $.stringBin1.get(type: STRING)", Exp.eq(Exp.val("yes"), Exp.stringBin("stringBin1"))); - parseExpAndCompare("'yes' == $.stringBin1.get(type: STRING)", + TestUtils.parseFilterExpressionAndCompare("'yes' == $.stringBin1.get(type: STRING)", Exp.eq(Exp.val("yes"), Exp.stringBin("stringBin1"))); } @Test void stringComparisonNegativeTest() { // A String constant must be quoted - assertThatThrownBy(() -> parseExpAndCompare("$.stringBin1.get(type: STRING) == yes", + assertThatThrownBy(() -> TestUtils.parseFilterExpressionAndCompare("$.stringBin1.get(type: STRING) == yes", Exp.eq(Exp.stringBin("stringBin1"), Exp.val("yes")))) .isInstanceOf(DslParseException.class) .hasMessage("Unable to parse right operand"); @@ -53,65 +54,65 @@ void stringComparisonNegativeTest() { void blobComparison() { byte[] data = new byte[]{1, 2, 3}; String encodedString = Base64.getEncoder().encodeToString(data); - parseExpAndCompare("$.blobBin1.get(type: BLOB) == \"" + encodedString + "\"", + TestUtils.parseFilterExpressionAndCompare("$.blobBin1.get(type: BLOB) == \"" + encodedString + "\"", Exp.eq(Exp.blobBin("blobBin1"), Exp.val(data))); // Reverse - parseExpAndCompare("\"" + encodedString + "\"" + " == $.blobBin1.get(type: BLOB)", + TestUtils.parseFilterExpressionAndCompare("\"" + encodedString + "\"" + " == $.blobBin1.get(type: BLOB)", Exp.eq(Exp.val(data), Exp.blobBin("blobBin1"))); } @Test void floatComparison() { - parseExpAndCompare("$.floatBin1.get(type: FLOAT) == 1.5", + TestUtils.parseFilterExpressionAndCompare("$.floatBin1.get(type: FLOAT) == 1.5", Exp.eq(Exp.floatBin("floatBin1"), Exp.val(1.5))); - parseExpAndCompare("1.5 == $.floatBin1.get(type: FLOAT)", + TestUtils.parseFilterExpressionAndCompare("1.5 == $.floatBin1.get(type: FLOAT)", Exp.eq(Exp.val(1.5), Exp.floatBin("floatBin1"))); } @Test void booleanComparison() { - parseExpAndCompare("$.boolBin1.get(type: BOOL) == true", + TestUtils.parseFilterExpressionAndCompare("$.boolBin1.get(type: BOOL) == true", Exp.eq(Exp.boolBin("boolBin1"), Exp.val(true))); - parseExpAndCompare("true == $.boolBin1.get(type: BOOL)", + TestUtils.parseFilterExpressionAndCompare("true == $.boolBin1.get(type: BOOL)", Exp.eq(Exp.val(true), Exp.boolBin("boolBin1"))); } @Test void negativeBooleanComparison() { - assertThatThrownBy(() -> parseExp("$.boolBin1.get(type: BOOL) == 5")) + assertThatThrownBy(() -> parseFilterExp("$.boolBin1.get(type: BOOL) == 5")) .isInstanceOf(DslParseException.class) .hasMessageContaining("Cannot compare BOOL to INT"); } @Test void listComparison_constantOnRightSide() { - parseExpAndCompare("$.listBin1.get(type: LIST) == [100]", + TestUtils.parseFilterExpressionAndCompare("$.listBin1.get(type: LIST) == [100]", Exp.eq(Exp.listBin("listBin1"), Exp.val(List.of(100)))); - parseExpAndCompare("$.listBin1.[] == [100]", + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[] == [100]", Exp.eq(Exp.listBin("listBin1"), Exp.val(List.of(100)))); // integer values are read as long - parseExpAndCompare("$.listBin1.get(type: LIST) == [100, 200, 300, 400]", + TestUtils.parseFilterExpressionAndCompare("$.listBin1.get(type: LIST) == [100, 200, 300, 400]", Exp.eq(Exp.listBin("listBin1"), Exp.val(List.of(100, 200, 300, 400)))); // integer values are read as long - parseExpAndCompare("$.listBin1.get(type: LIST) == [100, 200, 300, 400]", + TestUtils.parseFilterExpressionAndCompare("$.listBin1.get(type: LIST) == [100, 200, 300, 400]", Exp.eq(Exp.listBin("listBin1"), Exp.val(List.of(100L, 200L, 300L, 400L)))); - parseExpAndCompare("$.listBin1.get(type: LIST) == ['yes']", + TestUtils.parseFilterExpressionAndCompare("$.listBin1.get(type: LIST) == ['yes']", Exp.eq(Exp.listBin("listBin1"), Exp.val(List.of("yes")))); - parseExpAndCompare("$.listBin1.get(type: LIST) == ['yes', 'of course']", + TestUtils.parseFilterExpressionAndCompare("$.listBin1.get(type: LIST) == ['yes', 'of course']", Exp.eq(Exp.listBin("listBin1"), Exp.val(List.of("yes", "of course")))); - parseExpAndCompare("$.listBin1.get(type: LIST) == [\"yes\"]", + TestUtils.parseFilterExpressionAndCompare("$.listBin1.get(type: LIST) == [\"yes\"]", Exp.eq(Exp.listBin("listBin1"), Exp.val(List.of("yes")))); - parseExpAndCompare("$.listBin1.get(type: LIST) == [\"yes\", \"of course\"]", + TestUtils.parseFilterExpressionAndCompare("$.listBin1.get(type: LIST) == [\"yes\", \"of course\"]", Exp.eq(Exp.listBin("listBin1"), Exp.val(List.of("yes", "of course")))); } @@ -119,7 +120,7 @@ void listComparison_constantOnRightSide() { void listComparison_constantOnRightSide_NegativeTest() { // A String constant must be quoted assertThatThrownBy(() -> - parseExpAndCompare("$.listBin1.get(type: LIST) == [yes, of course]", + TestUtils.parseFilterExpressionAndCompare("$.listBin1.get(type: LIST) == [yes, of course]", Exp.eq(Exp.listBin("listBin1"), Exp.val(List.of("yes", "of course")))) ) .isInstanceOf(DslParseException.class) @@ -128,30 +129,30 @@ void listComparison_constantOnRightSide_NegativeTest() { @Test void listComparison_constantOnLeftSide() { - parseExpAndCompare("[100] == $.listBin1.get(type: LIST)", + TestUtils.parseFilterExpressionAndCompare("[100] == $.listBin1.get(type: LIST)", Exp.eq(Exp.val(List.of(100)), Exp.listBin("listBin1"))); - parseExpAndCompare("[100] == $.listBin1.[]", + TestUtils.parseFilterExpressionAndCompare("[100] == $.listBin1.[]", Exp.eq(Exp.val(List.of(100)), Exp.listBin("listBin1"))); // integer values are read as long - parseExpAndCompare("[100, 200, 300, 400] == $.listBin1.get(type: LIST)", + TestUtils.parseFilterExpressionAndCompare("[100, 200, 300, 400] == $.listBin1.get(type: LIST)", Exp.eq(Exp.val(List.of(100, 200, 300, 400)), Exp.listBin("listBin1"))); // integer values are read as long - parseExpAndCompare("[100, 200, 300, 400] == $.listBin1.get(type: LIST)", + TestUtils.parseFilterExpressionAndCompare("[100, 200, 300, 400] == $.listBin1.get(type: LIST)", Exp.eq(Exp.val(List.of(100L, 200L, 300L, 400L)), Exp.listBin("listBin1"))); - parseExpAndCompare("['yes'] == $.listBin1.get(type: LIST)", + TestUtils.parseFilterExpressionAndCompare("['yes'] == $.listBin1.get(type: LIST)", Exp.eq(Exp.val(List.of("yes")), Exp.listBin("listBin1"))); - parseExpAndCompare("['yes', 'of course'] == $.listBin1.get(type: LIST)", + TestUtils.parseFilterExpressionAndCompare("['yes', 'of course'] == $.listBin1.get(type: LIST)", Exp.eq(Exp.val(List.of("yes", "of course")), Exp.listBin("listBin1"))); - parseExpAndCompare("[\"yes\"] == $.listBin1.get(type: LIST)", + TestUtils.parseFilterExpressionAndCompare("[\"yes\"] == $.listBin1.get(type: LIST)", Exp.eq(Exp.val(List.of("yes")), Exp.listBin("listBin1"))); - parseExpAndCompare("[\"yes\", \"of course\"] == $.listBin1.get(type: LIST)", + TestUtils.parseFilterExpressionAndCompare("[\"yes\", \"of course\"] == $.listBin1.get(type: LIST)", Exp.eq(Exp.val(List.of("yes", "of course")), Exp.listBin("listBin1"))); } @@ -159,7 +160,7 @@ void listComparison_constantOnLeftSide() { void listComparison_constantOnLeftSide_NegativeTest() { // A String constant must be quoted assertThatThrownBy(() -> - parseExpAndCompare("[yes, of course] == $.listBin1.get(type: LIST)", + TestUtils.parseFilterExpressionAndCompare("[yes, of course] == $.listBin1.get(type: LIST)", Exp.eq(Exp.val(List.of("yes", "of course")), Exp.listBin("listBin1"))) ) .isInstanceOf(DslParseException.class) @@ -185,39 +186,39 @@ public static TreeMap treeMapOf(Object... entries) { @Test void mapComparison_constantOnRightSide() { // Prerequisite for comparing maps: both sides must be ordered maps - parseExpAndCompare("$.mapBin1.get(type: MAP) == {100:100}", + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.get(type: MAP) == {100:100}", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(treeMapOf(100, 100)))); - parseExpAndCompare("$.mapBin1.get(type: MAP) == {100 : 100}", + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.get(type: MAP) == {100 : 100}", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(treeMapOf(100, 100)))); - parseExpAndCompare("$.mapBin1.{} == {100:100}", + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{} == {100:100}", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(treeMapOf(100, 100)))); byte[] blobKey = new byte[]{1, 2, 3}; String encodedBlobKey = Base64.getEncoder().encodeToString(blobKey); // encoded blob key must be quoted as it is a String - parseExpAndCompare("$.mapBin1.{} == {'" + encodedBlobKey + "':100}", + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{} == {'" + encodedBlobKey + "':100}", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(treeMapOf(encodedBlobKey, 100)))); // integer values are read as long - parseExpAndCompare("$.mapBin1.get(type: MAP) == {100:200, 300:400}", + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.get(type: MAP) == {100:200, 300:400}", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(treeMapOf(100L, 200L, 300L, 400L)))); - parseExpAndCompare("$.mapBin1.get(type: MAP) == {100:200, 300:400}", + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.get(type: MAP) == {100:200, 300:400}", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(treeMapOf(100, 200, 300, 400)))); - parseExpAndCompare("$.mapBin1.get(type: MAP) == {'yes?':'yes!'}", + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.get(type: MAP) == {'yes?':'yes!'}", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(treeMapOf("yes?", "yes!")))); - parseExpAndCompare("$.mapBin1.get(type: MAP) == {\"yes\" : \"yes\"}", + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.get(type: MAP) == {\"yes\" : \"yes\"}", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(treeMapOf("yes", "yes")))); - parseExpAndCompare( + TestUtils.parseFilterExpressionAndCompare( "$.mapBin1.get(type: MAP) == {\"yes of course\" : \"yes of course\"}", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(treeMapOf("yes of course", "yes of course")))); - parseExpAndCompare("$.mapBin1.get(type: MAP) == {\"yes\" : [\"yes\", \"of course\"]}", + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.get(type: MAP) == {\"yes\" : [\"yes\", \"of course\"]}", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(treeMapOf("yes", List.of("yes", "of course"))))); } @@ -225,14 +226,14 @@ void mapComparison_constantOnRightSide() { void mapComparison_constantOnRightSide_NegativeTest() { // A String constant must be quoted assertThatThrownBy(() -> - parseExpAndCompare("$.mapBin1.get(type: MAP) == {yes, of course}", + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.get(type: MAP) == {yes, of course}", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(treeMapOf("yes", "of course")))) ) .isInstanceOf(DslParseException.class) .hasMessage("Unable to parse map operand"); assertThatThrownBy(() -> - parseExpAndCompare("$.mapBin1.get(type: MAP) == ['yes', 'of course']", + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.get(type: MAP) == ['yes', 'of course']", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(List.of("yes", "of course")))) ) .isInstanceOf(DslParseException.class) @@ -240,7 +241,7 @@ void mapComparison_constantOnRightSide_NegativeTest() { // Map key can only be Integer or String assertThatThrownBy(() -> - parseExpAndCompare("$.mapBin1.get(type: MAP) == {[100]:[100]}", + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.get(type: MAP) == {[100]:[100]}", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(List.of("yes", "of course")))) ) .isInstanceOf(DslParseException.class) @@ -250,39 +251,39 @@ void mapComparison_constantOnRightSide_NegativeTest() { @Test void mapComparison_constantOnLeftSide() { // Prerequisite for comparing maps: both sides must be ordered maps - parseExpAndCompare("{100:100} == $.mapBin1.get(type: MAP)", + TestUtils.parseFilterExpressionAndCompare("{100:100} == $.mapBin1.get(type: MAP)", Exp.eq(Exp.val(treeMapOf(100, 100)), Exp.mapBin("mapBin1"))); - parseExpAndCompare("{100 : 100} == $.mapBin1.get(type: MAP)", + TestUtils.parseFilterExpressionAndCompare("{100 : 100} == $.mapBin1.get(type: MAP)", Exp.eq(Exp.val(treeMapOf(100, 100)), Exp.mapBin("mapBin1"))); - parseExpAndCompare("{100:100} == $.mapBin1.{}", + TestUtils.parseFilterExpressionAndCompare("{100:100} == $.mapBin1.{}", Exp.eq(Exp.val(treeMapOf(100, 100)), Exp.mapBin("mapBin1"))); byte[] blobKey = new byte[]{1, 2, 3}; String encodedBlobKey = Base64.getEncoder().encodeToString(blobKey); // encoded blob key must be quoted as it is a String - parseExpAndCompare("{'" + encodedBlobKey + "':100} == $.mapBin1.{}", + TestUtils.parseFilterExpressionAndCompare("{'" + encodedBlobKey + "':100} == $.mapBin1.{}", Exp.eq(Exp.val(treeMapOf(encodedBlobKey, 100)), Exp.mapBin("mapBin1"))); // integer values are read as long - parseExpAndCompare("{100:200, 300:400} == $.mapBin1.get(type: MAP)", + TestUtils.parseFilterExpressionAndCompare("{100:200, 300:400} == $.mapBin1.get(type: MAP)", Exp.eq(Exp.val(treeMapOf(100L, 200L, 300L, 400L)), Exp.mapBin("mapBin1"))); - parseExpAndCompare("{100:200, 300:400} == $.mapBin1.get(type: MAP)", + TestUtils.parseFilterExpressionAndCompare("{100:200, 300:400} == $.mapBin1.get(type: MAP)", Exp.eq(Exp.val(treeMapOf(100, 200, 300, 400)), Exp.mapBin("mapBin1"))); - parseExpAndCompare("{'yes?':'yes!'} == $.mapBin1.get(type: MAP)", + TestUtils.parseFilterExpressionAndCompare("{'yes?':'yes!'} == $.mapBin1.get(type: MAP)", Exp.eq(Exp.val(treeMapOf("yes?", "yes!")), Exp.mapBin("mapBin1"))); - parseExpAndCompare("{\"yes\" : \"yes\"} == $.mapBin1.get(type: MAP)", + TestUtils.parseFilterExpressionAndCompare("{\"yes\" : \"yes\"} == $.mapBin1.get(type: MAP)", Exp.eq(Exp.val(treeMapOf("yes", "yes")), Exp.mapBin("mapBin1"))); - parseExpAndCompare( + TestUtils.parseFilterExpressionAndCompare( "{\"yes of course\" : \"yes of course\"} == $.mapBin1.get(type: MAP)", Exp.eq(Exp.val(treeMapOf("yes of course", "yes of course")), Exp.mapBin("mapBin1"))); - parseExpAndCompare("{\"yes\" : [\"yes\", \"of course\"]} == $.mapBin1.get(type: MAP)", + TestUtils.parseFilterExpressionAndCompare("{\"yes\" : [\"yes\", \"of course\"]} == $.mapBin1.get(type: MAP)", Exp.eq(Exp.val(treeMapOf("yes", List.of("yes", "of course"))), Exp.mapBin("mapBin1"))); } @@ -290,14 +291,14 @@ void mapComparison_constantOnLeftSide() { void mapComparison_constantOnLeftSide_NegativeTest() { // A String constant must be quoted assertThatThrownBy(() -> - parseExpAndCompare("{yes, of course} == $.mapBin1.get(type: MAP)", + TestUtils.parseFilterExpressionAndCompare("{yes, of course} == $.mapBin1.get(type: MAP)", Exp.eq(Exp.mapBin("mapBin1"), Exp.val(treeMapOf("of course", "yes")))) ) .isInstanceOf(DslParseException.class) .hasMessage("Could not parse given DSL expression input"); assertThatThrownBy(() -> - parseExpAndCompare("['yes', 'of course'] == $.mapBin1.get(type: MAP)", // incorrect: must be {} + TestUtils.parseFilterExpressionAndCompare("['yes', 'of course'] == $.mapBin1.get(type: MAP)", // incorrect: must be {} Exp.eq(Exp.val(List.of("yes", "of course")), Exp.mapBin("mapBin1"))) ) .isInstanceOf(DslParseException.class) @@ -305,7 +306,7 @@ void mapComparison_constantOnLeftSide_NegativeTest() { // Map key can only be Integer or String assertThatThrownBy(() -> - parseExpAndCompare("{[100]:[100]} == $.mapBin1.get(type: MAP)", + TestUtils.parseFilterExpressionAndCompare("{[100]:[100]} == $.mapBin1.get(type: MAP)", Exp.eq(Exp.val(List.of("yes", "of course")), Exp.mapBin("mapBin1"))) ) .isInstanceOf(DslParseException.class) @@ -314,44 +315,44 @@ void mapComparison_constantOnLeftSide_NegativeTest() { @Test void twoStringBinsComparison() { - parseExpAndCompare("$.stringBin1.get(type: STRING) == $.stringBin2.get(type: STRING)", + TestUtils.parseFilterExpressionAndCompare("$.stringBin1.get(type: STRING) == $.stringBin2.get(type: STRING)", Exp.eq(Exp.stringBin("stringBin1"), Exp.stringBin("stringBin2"))); } @Test void twoIntegerBinsComparison() { - parseExpAndCompare("$.intBin1.get(type: INT) == $.intBin2.get(type: INT)", + TestUtils.parseFilterExpressionAndCompare("$.intBin1.get(type: INT) == $.intBin2.get(type: INT)", Exp.eq(Exp.intBin("intBin1"), Exp.intBin("intBin2"))); } @Test void twoFloatBinsComparison() { - parseExpAndCompare("$.floatBin1.get(type: FLOAT) == $.floatBin2.get(type: FLOAT)", + TestUtils.parseFilterExpressionAndCompare("$.floatBin1.get(type: FLOAT) == $.floatBin2.get(type: FLOAT)", Exp.eq(Exp.floatBin("floatBin1"), Exp.floatBin("floatBin2"))); } @Test void twoBlobBinsComparison() { - parseExpAndCompare("$.blobBin1.get(type: BLOB) == $.blobBin2.get(type: BLOB)", + TestUtils.parseFilterExpressionAndCompare("$.blobBin1.get(type: BLOB) == $.blobBin2.get(type: BLOB)", Exp.eq(Exp.blobBin("blobBin1"), Exp.blobBin("blobBin2"))); } @Test void negativeTwoDifferentBinTypesComparison() { - assertThatThrownBy(() -> parseExp("$.stringBin1.get(type: STRING) == $.floatBin2.get(type: FLOAT)")) + assertThatThrownBy(() -> parseFilterExp("$.stringBin1.get(type: STRING) == $.floatBin2.get(type: FLOAT)")) .isInstanceOf(DslParseException.class) .hasMessageContaining("Cannot compare STRING to FLOAT"); } @Test void secondDegreeExplicitFloat() { - parseExpAndCompare("($.apples.get(type: FLOAT) + $.bananas.get(type: FLOAT)) > 10.5", + TestUtils.parseFilterExpressionAndCompare("($.apples.get(type: FLOAT) + $.bananas.get(type: FLOAT)) > 10.5", Exp.gt(Exp.add(Exp.floatBin("apples"), Exp.floatBin("bananas")), Exp.val(10.5))); } @Test void forthDegreeComplicatedExplicitFloat() { - parseExpAndCompare("(($.apples.get(type: FLOAT) + $.bananas.get(type: FLOAT))" + + TestUtils.parseFilterExpressionAndCompare("(($.apples.get(type: FLOAT) + $.bananas.get(type: FLOAT))" + " + ($.oranges.get(type: FLOAT) + $.acai.get(type: FLOAT))) > 10.5", Exp.gt( Exp.add( @@ -382,7 +383,7 @@ void complicatedWhenExplicitTypeIntDefault() { ) ); - parseExpAndCompare("$.a.get(type: INT) == " + + TestUtils.parseFilterExpressionAndCompare("$.a.get(type: INT) == " + "(when($.b.get(type: INT) == 1 => $.a1.get(type: INT)," + " $.b.get(type: INT) == 2 => $.a2.get(type: INT)," + " $.b.get(type: INT) == 3 => $.a3.get(type: INT)," + @@ -411,7 +412,7 @@ void complicatedWhenExplicitTypeString() { ) ); - parseExpAndCompare("$.a.get(type: STRING) == " + + TestUtils.parseFilterExpressionAndCompare("$.a.get(type: STRING) == " + "(when($.b == 1 => $.a1.get(type: STRING)," + " $.b == 2 => $.a2.get(type: STRING)," + " $.b == 3 => $.a3.get(type: STRING)," + diff --git a/src/test/java/com/aerospike/dsl/expression/ImplicitTypesTests.java b/src/test/java/com/aerospike/dsl/expression/ImplicitTypesTests.java index 2d657bb..9665f20 100644 --- a/src/test/java/com/aerospike/dsl/expression/ImplicitTypesTests.java +++ b/src/test/java/com/aerospike/dsl/expression/ImplicitTypesTests.java @@ -1,25 +1,26 @@ package com.aerospike.dsl.expression; import com.aerospike.client.exp.Exp; +import com.aerospike.dsl.util.TestUtils; import org.junit.jupiter.api.Test; -import static com.aerospike.dsl.util.TestUtils.parseExpAndCompare; +import static com.aerospike.dsl.util.TestUtils.parseDslExpressionAndCompare; public class ImplicitTypesTests { @Test void floatComparison() { - parseExpAndCompare("$.floatBin1 >= 100.25", + TestUtils.parseFilterExpressionAndCompare("$.floatBin1 >= 100.25", Exp.ge(Exp.floatBin("floatBin1"), Exp.val(100.25))); } @Test void booleanComparison() { - parseExpAndCompare("$.boolBin1 == true", + TestUtils.parseFilterExpressionAndCompare("$.boolBin1 == true", Exp.eq(Exp.boolBin("boolBin1"), Exp.val(true))); - parseExpAndCompare("false == $.boolBin1", + TestUtils.parseFilterExpressionAndCompare("false == $.boolBin1", Exp.eq(Exp.val(false), Exp.boolBin("boolBin1"))); - parseExpAndCompare("$.boolBin1 != false", + TestUtils.parseFilterExpressionAndCompare("$.boolBin1 != false", Exp.ne(Exp.boolBin("boolBin1"), Exp.val(false))); } @@ -27,31 +28,31 @@ void booleanComparison() { // this can also be an expression that evaluates to a boolean result @Test void binBooleanImplicitLogicalComparison() { -// parseFilterExpAndCompare("$.boolBin1 and $.boolBin2", -// Exp.and(Exp.boolBin("boolBin1"), Exp.boolBin("boolBin2"))); -// parseFilterExpAndCompare("$.boolBin1 or $.boolBin2", -// Exp.or(Exp.boolBin("boolBin1"), Exp.boolBin("boolBin2"))); - parseExpAndCompare("not($.boolBin1)", + TestUtils.parseFilterExpressionAndCompare("$.boolBin1 and $.boolBin2", + Exp.and(Exp.boolBin("boolBin1"), Exp.boolBin("boolBin2"))); + TestUtils.parseFilterExpressionAndCompare("$.boolBin1 or $.boolBin2", + Exp.or(Exp.boolBin("boolBin1"), Exp.boolBin("boolBin2"))); + TestUtils.parseFilterExpressionAndCompare("not($.boolBin1)", Exp.not(Exp.boolBin("boolBin1"))); -// parseFilterExpAndCompare("exclusive($.boolBin1, $.boolBin2)", -// Exp.exclusive(Exp.boolBin("boolBin1"), Exp.boolBin("boolBin2"))); + TestUtils.parseFilterExpressionAndCompare("exclusive($.boolBin1, $.boolBin2)", + Exp.exclusive(Exp.boolBin("boolBin1"), Exp.boolBin("boolBin2"))); } @Test void implicitDefaultIntComparison() { - parseExpAndCompare("$.intBin1 < $.intBin2", + TestUtils.parseFilterExpressionAndCompare("$.intBin1 < $.intBin2", Exp.lt(Exp.intBin("intBin1"), Exp.intBin("intBin2"))); } @Test void secondDegreeImplicitCastingFloat() { - parseExpAndCompare("($.apples + $.bananas) > 10.5", + TestUtils.parseFilterExpressionAndCompare("($.apples + $.bananas) > 10.5", Exp.gt(Exp.add(Exp.floatBin("apples"), Exp.floatBin("bananas")), Exp.val(10.5))); } @Test void secondDegreeComplicatedFloatFirstImplicitCastingFloat() { - parseExpAndCompare("($.apples + $.bananas) > 10.5 and ($.oranges + $.grapes) <= 5", + TestUtils.parseFilterExpressionAndCompare("($.apples + $.bananas) > 10.5 and ($.oranges + $.grapes) <= 5", Exp.and( Exp.gt(Exp.add(Exp.floatBin("apples"), Exp.floatBin("bananas")), Exp.val(10.5)), Exp.le(Exp.add(Exp.intBin("oranges"), Exp.intBin("grapes")), Exp.val(5))) @@ -60,7 +61,7 @@ void secondDegreeComplicatedFloatFirstImplicitCastingFloat() { @Test void secondDegreeComplicatedIntFirstImplicitCastingFloat() { - parseExpAndCompare("($.apples + $.bananas) > 5 and ($.oranges + $.grapes) <= 10.5", + TestUtils.parseFilterExpressionAndCompare("($.apples + $.bananas) > 5 and ($.oranges + $.grapes) <= 10.5", Exp.and( Exp.gt(Exp.add(Exp.intBin("apples"), Exp.intBin("bananas")), Exp.val(5)), Exp.le(Exp.add(Exp.floatBin("oranges"), Exp.floatBin("grapes")), Exp.val(10.5))) @@ -69,7 +70,7 @@ void secondDegreeComplicatedIntFirstImplicitCastingFloat() { @Test void thirdDegreeComplicatedDefaultInt() { - parseExpAndCompare("(($.apples + $.bananas) + $.oranges) > 10", + TestUtils.parseFilterExpressionAndCompare("(($.apples + $.bananas) + $.oranges) > 10", Exp.gt( Exp.add(Exp.add(Exp.intBin("apples"), Exp.intBin("bananas")), Exp.intBin("oranges")), Exp.val(10)) @@ -78,7 +79,7 @@ void thirdDegreeComplicatedDefaultInt() { @Test void thirdDegreeComplicatedImplicitCastingFloat() { - parseExpAndCompare("(($.apples + $.bananas) + $.oranges) > 10.5", + TestUtils.parseFilterExpressionAndCompare("(($.apples + $.bananas) + $.oranges) > 10.5", Exp.gt( Exp.add(Exp.add(Exp.floatBin("apples"), Exp.floatBin("bananas")), Exp.floatBin("oranges")), Exp.val(10.5)) @@ -87,7 +88,7 @@ void thirdDegreeComplicatedImplicitCastingFloat() { @Test void forthDegreeComplicatedDefaultInt() { - parseExpAndCompare("(($.apples + $.bananas) + ($.oranges + $.acai)) > 10", + TestUtils.parseFilterExpressionAndCompare("(($.apples + $.bananas) + ($.oranges + $.acai)) > 10", Exp.gt( Exp.add( Exp.add(Exp.intBin("apples"), Exp.intBin("bananas")), @@ -98,7 +99,7 @@ void forthDegreeComplicatedDefaultInt() { @Test void forthDegreeComplicatedImplicitCastingFloat() { - parseExpAndCompare("(($.apples + $.bananas) + ($.oranges + $.acai)) > 10.5", + TestUtils.parseFilterExpressionAndCompare("(($.apples + $.bananas) + ($.oranges + $.acai)) > 10.5", Exp.gt( Exp.add( Exp.add(Exp.floatBin("apples"), Exp.floatBin("bananas")), @@ -128,7 +129,7 @@ void complicatedWhenImplicitTypeInt() { ) ); - parseExpAndCompare("$.a == (when($.b == 1 => $.a1, $.b == 2 => $.a2, $.b == 3 => $.a3, default => $.a4+1))", + TestUtils.parseFilterExpressionAndCompare("$.a == (when($.b == 1 => $.a1, $.b == 2 => $.a2, $.b == 3 => $.a3, default => $.a4+1))", expected); } @@ -154,7 +155,7 @@ void complicatedWhenImplicitTypeString() { ) ); - parseExpAndCompare("$.a == (when($.b == 1 => $.a1, $.b == 2 => $.a2, $.b == 3 => $.a3, default => \"hello\"))", + TestUtils.parseFilterExpressionAndCompare("$.a == (when($.b == 1 => $.a1, $.b == 2 => $.a2, $.b == 3 => $.a3, default => \"hello\"))", expected); } } diff --git a/src/test/java/com/aerospike/dsl/expression/ListExpressionsTests.java b/src/test/java/com/aerospike/dsl/expression/ListExpressionsTests.java index 1f5d96f..32382d6 100644 --- a/src/test/java/com/aerospike/dsl/expression/ListExpressionsTests.java +++ b/src/test/java/com/aerospike/dsl/expression/ListExpressionsTests.java @@ -5,12 +5,13 @@ import com.aerospike.client.exp.Exp; import com.aerospike.client.exp.ListExp; import com.aerospike.dsl.DslParseException; +import com.aerospike.dsl.util.TestUtils; import org.junit.jupiter.api.Test; import java.util.List; -import static com.aerospike.dsl.util.TestUtils.parseExp; -import static com.aerospike.dsl.util.TestUtils.parseExpAndCompare; +import static com.aerospike.dsl.util.TestUtils.parseFilterExp; +import static com.aerospike.dsl.util.TestUtils.parseDslExpressionAndCompare; import static org.assertj.core.api.Assertions.assertThatThrownBy; class ListExpressionsTests { @@ -26,10 +27,10 @@ void listByIndexInteger() { ), Exp.val(100)); // Implicit detect as Int - parseExpAndCompare("$.listBin1.[0] == 100", expected); - parseExpAndCompare("$.listBin1.[0].get(type: INT) == 100", expected); - parseExpAndCompare("$.listBin1.[0].get(type: INT, return: VALUE) == 100", expected); - parseExpAndCompare("$.listBin1.[0].asInt() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0] == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].get(type: INT) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].get(type: INT, return: VALUE) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].asInt() == 100", expected); } @Test @@ -43,9 +44,9 @@ void listByIndexOtherTypes() { ), Exp.val("stringVal")); // Implicit detect as string - parseExpAndCompare("$.listBin1.[0] == \"stringVal\"", expected); - parseExpAndCompare("$.listBin1.[0].get(type: STRING) == \"stringVal\"", expected); - parseExpAndCompare("$.listBin1.[0].get(type: STRING, return: VALUE) == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0] == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].get(type: STRING) == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].get(type: STRING, return: VALUE) == \"stringVal\"", expected); expected = Exp.eq( ListExp.getByIndex( @@ -56,9 +57,9 @@ void listByIndexOtherTypes() { ), Exp.val(true)); // Implicit detect as boolean - parseExpAndCompare("$.listBin1.[0] == true", expected); - parseExpAndCompare("$.listBin1.[0].get(type: BOOL) == true", expected); - parseExpAndCompare("$.listBin1.[0].get(type: BOOL, return: VALUE) == true", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0] == true", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].get(type: BOOL) == true", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].get(type: BOOL, return: VALUE) == true", expected); } @Test @@ -70,10 +71,10 @@ void listByValue() { Exp.listBin("listBin1") ), Exp.val(100)); - parseExpAndCompare("$.listBin1.[=100] == 100", expected); - parseExpAndCompare("$.listBin1.[=100].get(type: INT) == 100", expected); - parseExpAndCompare("$.listBin1.[=100].get(type: INT, return: VALUE) == 100", expected); - parseExpAndCompare("$.listBin1.[=100].asInt() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[=100] == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[=100].get(type: INT) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[=100].get(type: INT, return: VALUE) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[=100].asInt() == 100", expected); } @Test @@ -84,8 +85,8 @@ void listByValueCount() { Exp.listBin("listBin1")), Exp.val(0) ); - parseExpAndCompare("$.listBin1.[=100].count() > 0", expected); - parseExpAndCompare("$.listBin1.[=100].[].count() > 0", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[=100].count() > 0", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[=100].[].count() > 0", expected); } @Test @@ -98,10 +99,10 @@ void listByRank() { Exp.listBin("listBin1") ), Exp.val(100)); - parseExpAndCompare("$.listBin1.[#-1] == 100", expected); - parseExpAndCompare("$.listBin1.[#-1].get(type: INT) == 100", expected); - parseExpAndCompare("$.listBin1.[#-1].get(type: INT, return: VALUE) == 100", expected); - parseExpAndCompare("$.listBin1.[#-1].asInt() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[#-1] == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[#-1].get(type: INT) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[#-1].get(type: INT, return: VALUE) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[#-1].asInt() == 100", expected); } @Test @@ -116,9 +117,9 @@ void listBinElementEquals_Nested() { CTX.listIndex(0) ), Exp.val(100)); - parseExpAndCompare("$.listBin1.[0].[0].[0] == 100", expected); - parseExpAndCompare("$.listBin1.[0].[0].[0].get(type: INT) == 100", expected); - parseExpAndCompare("$.listBin1.[0].[0].[0].get(type: INT, return: VALUE) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].[0].[0] == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].[0].[0].get(type: INT) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].[0].[0].get(type: INT, return: VALUE) == 100", expected); } @Test @@ -126,10 +127,10 @@ void listSize() { Exp expected = Exp.eq( ListExp.size(Exp.listBin("listBin1")), Exp.val(1)); - parseExpAndCompare("$.listBin1.[].count() == 1", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[].count() == 1", expected); // the default behaviour for count() without List '[]' or Map '{}' designators is List - parseExpAndCompare("$.listBin1.count() == 1", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.count() == 1", expected); } @Test @@ -143,10 +144,10 @@ void nestedListSize() { Exp.listBin("listBin1")) ), Exp.val(100)); - parseExpAndCompare("$.listBin1.[1].[].count() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[1].[].count() == 100", expected); // the default behaviour for count() without List '[]' or Map '{}' designators is List - parseExpAndCompare("$.listBin1.[1].count() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[1].count() == 100", expected); } @@ -162,10 +163,10 @@ void nestedListSizeWithContext() { CTX.listIndex(1)) ), Exp.val(100)); - parseExpAndCompare("$.listBin1.[1].[2].[].count() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[1].[2].[].count() == 100", expected); // the default behaviour for count() without List '[]' or Map '{}' designators is List - parseExpAndCompare("$.listBin1.[1].[2].count() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[1].[2].count() == 100", expected); } @Test @@ -179,7 +180,7 @@ void nestedLists() { CTX.listIndex(5) ), Exp.val("stringVal")); - parseExpAndCompare("$.listBin1.[5].[1].get(type: STRING) == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[5].[1].get(type: STRING) == \"stringVal\"", expected); } @Test @@ -195,8 +196,8 @@ void nestedListsWithDifferentContextTypes() { ), Exp.val("stringVal")); // Implicit detect as String - parseExpAndCompare("$.listBin1.[5].[#-1] == \"stringVal\"", expected); - parseExpAndCompare("$.listBin1.[5].[#-1].get(type: STRING) == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[5].[#-1] == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[5].[#-1].get(type: STRING) == \"stringVal\"", expected); // Nested List Rank Value expected = Exp.eq( @@ -209,7 +210,7 @@ void nestedListsWithDifferentContextTypes() { ), Exp.val(200)); // Implicit detect as Int - parseExpAndCompare("$.listBin1.[5].[#-1].[=100] == 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[5].[#-1].[=100] == 200", expected); } @Test @@ -223,21 +224,21 @@ void listBinElementCount() { ), Exp.val(100) ); - parseExpAndCompare("$.listBin1.[0].count() == 100", expected); - parseExpAndCompare("$.listBin1.[0].[].count() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].count() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].[].count() == 100", expected); } @Test void negativeSyntaxList() { // TODO: throw meaningful exception (by ANTLR?) - assertThatThrownBy(() -> parseExp("$.listBin1.[stringValue] == 100")) + assertThatThrownBy(() -> parseFilterExp("$.listBin1.[stringValue] == 100")) .isInstanceOf(DslParseException.class); } //@Test void negativeTypeComparisonList() { // TODO: should fail? Exp is successfully created but comparing int to a string value (validations on List) - assertThatThrownBy(() -> parseExp("$.listBin1.[#-1].get(type: INT) == \"stringValue\"")) + assertThatThrownBy(() -> parseFilterExp("$.listBin1.[#-1].get(type: INT) == \"stringValue\"")) .isInstanceOf(NullPointerException.class); } @@ -248,7 +249,7 @@ void listIndexRange() { Exp.val(1), Exp.val(2), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[1:3]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[1:3]", expected); // Negative expected = ListExp.getByIndexRange( @@ -256,7 +257,7 @@ void listIndexRange() { Exp.val(-3), Exp.val(4), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[-3:1]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[-3:1]", expected); // Inverted expected = ListExp.getByIndexRange( @@ -264,14 +265,14 @@ void listIndexRange() { Exp.val(2), Exp.val(2), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[!2:4]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[!2:4]", expected); // From start till the end expected = ListExp.getByIndexRange( ListReturnType.VALUE, Exp.val(1), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[1:]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[1:]", expected); } @Test @@ -280,23 +281,23 @@ void listValueList() { ListReturnType.VALUE, Exp.val(List.of("a", "b", "c")), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[=a,b,c]", expected); - parseExpAndCompare("$.listBin1.[=\"a\",\"b\",\"c\"]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[=a,b,c]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[=\"a\",\"b\",\"c\"]", expected); // Integer expected = ListExp.getByValueList( ListReturnType.VALUE, Exp.val(List.of(1, 2, 3)), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[=1,2,3]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[=1,2,3]", expected); // Inverted expected = ListExp.getByValueList( ListReturnType.VALUE | ListReturnType.INVERTED, Exp.val(List.of("a", "b", "c")), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[!=a,b,c]", expected); - parseExpAndCompare("$.listBin1.[!=\"a\",\"b\",\"c\"]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[!=a,b,c]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[!=\"a\",\"b\",\"c\"]", expected); } @Test @@ -307,7 +308,7 @@ void listValueRange() { Exp.val(111), Exp.val(334), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[=111:334]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[=111:334]", expected); // Inverted expected = ListExp.getByValueRange( @@ -315,7 +316,7 @@ void listValueRange() { Exp.val(10), Exp.val(20), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[!=10:20]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[!=10:20]", expected); // From start till the end expected = ListExp.getByValueRange( @@ -323,7 +324,7 @@ void listValueRange() { Exp.val(111), null, Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[=111:]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[=111:]", expected); } @Test @@ -333,7 +334,7 @@ void listRankRange() { Exp.val(0), Exp.val(3), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[#0:3]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[#0:3]", expected); // Inverted expected = ListExp.getByRankRange( @@ -341,14 +342,14 @@ void listRankRange() { Exp.val(0), Exp.val(3), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[!#0:3]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[!#0:3]", expected); // From start till the end expected = ListExp.getByRankRange( ListReturnType.VALUE, Exp.val(-3), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[#-3:]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[#-3:]", expected); // From start till the end with context expected = ListExp.getByRankRange( @@ -356,7 +357,7 @@ void listRankRange() { Exp.val(-3), Exp.listBin("listBin1"), CTX.listIndex(5)); - parseExpAndCompare("$.listBin1.[5].[#-3:]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[5].[#-3:]", expected); } @Test @@ -367,7 +368,7 @@ void listRankRangeRelative() { Exp.val("b"), Exp.val(2), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[#-3:-1~b]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[#-3:-1~b]", expected); // Inverted expected = ListExp.getByValueRelativeRankRange( @@ -376,7 +377,7 @@ void listRankRangeRelative() { Exp.val("b"), Exp.val(2), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[!#-3:-1~b]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[!#-3:-1~b]", expected); // From start till the end expected = ListExp.getByValueRelativeRankRange( @@ -384,7 +385,7 @@ void listRankRangeRelative() { Exp.val(-3), Exp.val("b"), Exp.listBin("listBin1")); - parseExpAndCompare("$.listBin1.[#-3:~b]", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[#-3:~b]", expected); } @Test @@ -398,7 +399,7 @@ void listReturnTypes() { ), Exp.val(5)); // Implicit detect as Int - parseExpAndCompare("$.listBin1.[0].get(return: COUNT) == 5", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].get(return: COUNT) == 5", expected); expected = Exp.eq( ListExp.getByIndex( @@ -409,7 +410,7 @@ void listReturnTypes() { ), Exp.val(true)); // Implicit detect as Int - parseExpAndCompare("$.listBin1.[0].get(return: EXISTS) == true", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].get(return: EXISTS) == true", expected); expected = Exp.eq( ListExp.getByIndex( @@ -420,6 +421,6 @@ void listReturnTypes() { ), Exp.val(1)); // Implicit detect as Int - parseExpAndCompare("$.listBin1.[0].get(return: INDEX) == 1", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[0].get(return: INDEX) == 1", expected); } } diff --git a/src/test/java/com/aerospike/dsl/expression/LogicalExpressionsTests.java b/src/test/java/com/aerospike/dsl/expression/LogicalExpressionsTests.java index ff6c344..18638b7 100644 --- a/src/test/java/com/aerospike/dsl/expression/LogicalExpressionsTests.java +++ b/src/test/java/com/aerospike/dsl/expression/LogicalExpressionsTests.java @@ -2,11 +2,12 @@ import com.aerospike.client.exp.Exp; import com.aerospike.dsl.DslParseException; +import com.aerospike.dsl.util.TestUtils; import org.junit.jupiter.api.Test; import org.opentest4j.AssertionFailedError; -import static com.aerospike.dsl.util.TestUtils.parseExp; -import static com.aerospike.dsl.util.TestUtils.parseExpAndCompare; +import static com.aerospike.dsl.util.TestUtils.parseFilterExp; +import static com.aerospike.dsl.util.TestUtils.parseDslExpressionAndCompare; import static org.assertj.core.api.Assertions.assertThatThrownBy; public class LogicalExpressionsTests { @@ -15,7 +16,7 @@ public class LogicalExpressionsTests { void binLogicalAndOrCombinations() { Exp expected1 = Exp.and(Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), Exp.gt(Exp.intBin("intBin2"), Exp.val(100))); - parseExpAndCompare("$.intBin1 > 100 and $.intBin2 > 100", expected1); + TestUtils.parseFilterExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100", expected1); Exp expected2 = Exp.or( Exp.and( @@ -25,8 +26,9 @@ void binLogicalAndOrCombinations() { Exp.lt(Exp.intBin("intBin3"), Exp.val(100)) ); // TODO: what should be the default behaviour with no parentheses? - parseExpAndCompare("$.intBin1 > 100 and $.intBin2 > 100 or $.intBin3 < 100", expected2); - parseExpAndCompare("($.intBin1 > 100 and $.intBin2 > 100) or $.intBin3 < 100", expected2); + TestUtils.parseFilterExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 or $.intBin3 < 100", expected2); + TestUtils.parseFilterExpressionAndCompare("($.intBin1 > 100 and $.intBin2 > 100) or $.intBin3 < 100", expected2); + TestUtils.parseFilterExpressionAndCompare("(($.intBin1 > 100 and $.intBin2 > 100) or $.intBin3 < 100)", expected2); Exp expected3 = Exp.and( Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), @@ -35,27 +37,28 @@ void binLogicalAndOrCombinations() { Exp.lt(Exp.intBin("intBin3"), Exp.val(100)) ) ); - parseExpAndCompare("($.intBin1 > 100 and ($.intBin2 > 100 or $.intBin3 < 100))", expected3); - + TestUtils.parseFilterExpressionAndCompare("($.intBin1 > 100 and ($.intBin2 > 100 or $.intBin3 < 100))", expected3); + TestUtils.parseFilterExpressionAndCompare("$.intBin1 > 100 and ($.intBin2 > 100 or $.intBin3 < 100)", expected3); // check that parentheses make difference - assertThatThrownBy(() -> parseExpAndCompare("($.intBin1 > 100 and ($.intBin2 > 100 or $.intBin3 < 100))", expected2)) - .isInstanceOf(AssertionFailedError.class); + assertThatThrownBy( + () -> TestUtils.parseFilterExpressionAndCompare("($.intBin1 > 100 and ($.intBin2 > 100 or $.intBin3 < 100))", expected2) + ).isInstanceOf(AssertionFailedError.class); } @Test void logicalNot() { - parseExpAndCompare("not($.keyExists())", Exp.not(Exp.keyExists())); + TestUtils.parseFilterExpressionAndCompare("not($.keyExists())", Exp.not(Exp.keyExists())); } @Test void binLogicalExclusive() { - parseExpAndCompare("exclusive($.hand == \"hook\", $.leg == \"peg\")", + TestUtils.parseFilterExpressionAndCompare("exclusive($.hand == \"hook\", $.leg == \"peg\")", Exp.exclusive( Exp.eq(Exp.stringBin("hand"), Exp.val("hook")), Exp.eq(Exp.stringBin("leg"), Exp.val("peg")))); // More than 2 expressions exclusive - parseExpAndCompare("exclusive($.a == \"aVal\", $.b == \"bVal\", $.c == \"cVal\", $.d == 4)", + TestUtils.parseFilterExpressionAndCompare("exclusive($.a == \"aVal\", $.b == \"bVal\", $.c == \"cVal\", $.d == 4)", Exp.exclusive( Exp.eq(Exp.stringBin("a"), Exp.val("aVal")), Exp.eq(Exp.stringBin("b"), Exp.val("bVal")), @@ -66,7 +69,7 @@ void binLogicalExclusive() { //TODO: FMWK-488 //@Test void flatHierarchyAnd() { - parseExpAndCompare("$.intBin1 > 100 and $.intBin2 > 100 and $.intBin3 < 100", + TestUtils.parseFilterExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 and $.intBin3 < 100", Exp.and( Exp.gt( Exp.intBin("intBin1"), @@ -81,26 +84,26 @@ void flatHierarchyAnd() { @Test void negativeSyntaxLogicalOperators() { - assertThatThrownBy(() -> parseExp("($.intBin1 > 100 and ($.intBin2 > 100) or")) + assertThatThrownBy(() -> parseFilterExp("($.intBin1 > 100 and ($.intBin2 > 100) or")) .isInstanceOf(DslParseException.class) .hasMessageContaining("Could not parse given DSL expression input"); - assertThatThrownBy(() -> parseExp("and ($.intBin1 > 100 and ($.intBin2 > 100)")) + assertThatThrownBy(() -> parseFilterExp("and ($.intBin1 > 100 and ($.intBin2 > 100)")) .isInstanceOf(DslParseException.class) .hasMessageContaining("Could not parse given DSL expression input"); - assertThatThrownBy(() -> parseExp("($.intBin1 > 100 and ($.intBin2 > 100) not")) + assertThatThrownBy(() -> parseFilterExp("($.intBin1 > 100 and ($.intBin2 > 100) not")) .isInstanceOf(DslParseException.class) .hasMessageContaining("Could not parse given DSL expression input"); - assertThatThrownBy(() -> parseExp("($.intBin1 > 100 and ($.intBin2 > 100) exclusive")) + assertThatThrownBy(() -> parseFilterExp("($.intBin1 > 100 and ($.intBin2 > 100) exclusive")) .isInstanceOf(DslParseException.class) .hasMessageContaining("Could not parse given DSL expression input"); } @Test void negativeBinLogicalExclusiveWithOneParam() { - assertThatThrownBy(() -> parseExpAndCompare("exclusive($.hand == \"hook\")", + assertThatThrownBy(() -> TestUtils.parseFilterExpressionAndCompare("exclusive($.hand == \"hook\")", Exp.exclusive( Exp.eq(Exp.stringBin("hand"), Exp.val("hook"))))) .isInstanceOf(DslParseException.class) diff --git a/src/test/java/com/aerospike/dsl/expression/MapAndListExpressionsTests.java b/src/test/java/com/aerospike/dsl/expression/MapAndListExpressionsTests.java index 0c2926d..524f20d 100644 --- a/src/test/java/com/aerospike/dsl/expression/MapAndListExpressionsTests.java +++ b/src/test/java/com/aerospike/dsl/expression/MapAndListExpressionsTests.java @@ -7,9 +7,10 @@ import com.aerospike.client.exp.Exp; import com.aerospike.client.exp.ListExp; import com.aerospike.client.exp.MapExp; +import com.aerospike.dsl.util.TestUtils; import org.junit.jupiter.api.Test; -import static com.aerospike.dsl.util.TestUtils.parseExpAndCompare; +import static com.aerospike.dsl.util.TestUtils.parseDslExpressionAndCompare; public class MapAndListExpressionsTests { @@ -24,7 +25,7 @@ void listInsideAMap() { CTX.mapKey(Value.get("a")) ), Exp.val(100)); - parseExpAndCompare("$.mapBin1.a.[0] == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.[0] == 100", expected); expected = Exp.gt( ListExp.getByIndex( @@ -35,7 +36,7 @@ void listInsideAMap() { CTX.mapKey(Value.get("a")), CTX.mapKey(Value.get("cc")) ), Exp.val(100)); - parseExpAndCompare("$.mapBin1.a.cc.[2].get(type: INT) > 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.cc.[2].get(type: INT) > 100", expected); } @Test @@ -50,7 +51,7 @@ void mapListList() { CTX.listIndex(0) ), Exp.val(100)); - parseExpAndCompare("$.mapBin1.a.[0].[0] == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.[0].[0] == 100", expected); } @Test @@ -63,7 +64,7 @@ void mapInsideAList() { Exp.listBin("listBin1"), CTX.listIndex(2) ), Exp.val(100)); - parseExpAndCompare("$.listBin1.[2].cc.get(type: INT) > 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[2].cc.get(type: INT) > 100", expected); } @Test @@ -77,8 +78,8 @@ void listMapMap() { CTX.listIndex(2), CTX.mapKey(Value.get("aa")) ), Exp.val(100)); - parseExpAndCompare("$.listBin1.[2].aa.cc > 100", expected); - parseExpAndCompare("$.listBin1.[2].aa.cc.get(type: INT) > 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[2].aa.cc > 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[2].aa.cc.get(type: INT) > 100", expected); } @Test @@ -93,7 +94,7 @@ void listMapList() { CTX.mapKey(Value.get("a")) ), Exp.val(100)); - parseExpAndCompare("$.listBin1.[1].a.[0] == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[1].a.[0] == 100", expected); } @Test @@ -110,8 +111,8 @@ void listMapListSize() { ) ), Exp.val(100)); - parseExpAndCompare("$.listBin1.[1].a.[0].count() == 100", expected); - parseExpAndCompare("$.listBin1.[1].a.[0].[].count() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[1].a.[0].count() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.listBin1.[1].a.[0].[].count() == 100", expected); } @Test @@ -125,8 +126,8 @@ void mapListMap() { CTX.mapKey(Value.get("a")), CTX.listIndex(0) ), Exp.val(100)); - parseExpAndCompare("$.mapBin1.a.[0].cc > 100", expected); - parseExpAndCompare("$.mapBin1.a.[0].cc.get(type: INT) > 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.[0].cc > 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.[0].cc.get(type: INT) > 100", expected); } //@Test diff --git a/src/test/java/com/aerospike/dsl/expression/MapExpressionsTests.java b/src/test/java/com/aerospike/dsl/expression/MapExpressionsTests.java index 806f5eb..61ead01 100644 --- a/src/test/java/com/aerospike/dsl/expression/MapExpressionsTests.java +++ b/src/test/java/com/aerospike/dsl/expression/MapExpressionsTests.java @@ -7,11 +7,12 @@ import com.aerospike.client.exp.Exp; import com.aerospike.client.exp.ListExp; import com.aerospike.client.exp.MapExp; +import com.aerospike.dsl.util.TestUtils; import org.junit.jupiter.api.Test; import java.util.List; -import static com.aerospike.dsl.util.TestUtils.parseExpAndCompare; +import static com.aerospike.dsl.util.TestUtils.parseDslExpressionAndCompare; public class MapExpressionsTests { @@ -27,10 +28,10 @@ void mapOneLevelExpressions() { ), Exp.val(200)); // Implicit detect as Int - parseExpAndCompare("$.mapBin1.a == 200", expected); - parseExpAndCompare("$.mapBin1.a.get(type: INT) == 200", expected); - parseExpAndCompare("$.mapBin1.a.get(type: INT, return: VALUE) == 200", expected); - parseExpAndCompare("$.mapBin1.a.asInt() == 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a == 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.get(type: INT) == 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.get(type: INT, return: VALUE) == 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.asInt() == 200", expected); // String expected = Exp.eq( @@ -42,9 +43,9 @@ void mapOneLevelExpressions() { ), Exp.val("stringVal")); // Implicit detect as String - parseExpAndCompare("$.mapBin1.a == \"stringVal\"", expected); - parseExpAndCompare("$.mapBin1.a.get(type: STRING) == \"stringVal\"", expected); - parseExpAndCompare("$.mapBin1.a.get(type: STRING, return: VALUE) == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.get(type: STRING) == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.get(type: STRING, return: VALUE) == \"stringVal\"", expected); } @Test @@ -58,9 +59,9 @@ void mapNestedLevelExpressions() { CTX.mapKey(Value.get("a")), CTX.mapKey(Value.get("bb")) ), Exp.val(200)); - parseExpAndCompare("$.mapBin1.a.bb.bcc > 200", expected); - parseExpAndCompare("$.mapBin1.a.bb.bcc.get(type: INT) > 200", expected); - parseExpAndCompare("$.mapBin1.a.bb.bcc.get(type: INT, return: VALUE) > 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.bb.bcc > 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.bb.bcc.get(type: INT) > 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.bb.bcc.get(type: INT, return: VALUE) > 200", expected); // String expected = Exp.eq( @@ -73,9 +74,9 @@ void mapNestedLevelExpressions() { ), Exp.val("stringVal")); // Implicit detect as String - parseExpAndCompare("$.mapBin1.a.bb.bcc == \"stringVal\"", expected); - parseExpAndCompare("$.mapBin1.a.bb.bcc.get(type: STRING) == \"stringVal\"", expected); - parseExpAndCompare("$.mapBin1.a.bb.bcc.get(type: STRING, return: VALUE) == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.bb.bcc == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.bb.bcc.get(type: STRING) == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.bb.bcc.get(type: STRING, return: VALUE) == \"stringVal\"", expected); } @Test @@ -89,9 +90,9 @@ void quotedStringInExpressionPath() { CTX.mapKey(Value.get("a")), CTX.mapKey(Value.get("bb")) ), Exp.val(200)); - parseExpAndCompare("$.mapBin1.a.bb.bcc.get(type: INT) > 200", expected); - parseExpAndCompare("$.mapBin1.a.\"bb\".bcc.get(type: INT) > 200", expected); - parseExpAndCompare("$.mapBin1.a.'bb'.bcc.get(type: INT) > 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.bb.bcc.get(type: INT) > 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.\"bb\".bcc.get(type: INT) > 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.'bb'.bcc.get(type: INT) > 200", expected); expected = Exp.gt( MapExp.getByKey( @@ -102,8 +103,8 @@ void quotedStringInExpressionPath() { CTX.mapKey(Value.get("127.0.0.1")) ), Exp.val(200)); - parseExpAndCompare("$.mapBin1.\"127.0.0.1\".bcc.get(type: INT) > 200", expected); - parseExpAndCompare("$.mapBin1.'127.0.0.1'.bcc.get(type: INT) > 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.\"127.0.0.1\".bcc.get(type: INT) > 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.'127.0.0.1'.bcc.get(type: INT) > 200", expected); } @Test @@ -113,7 +114,7 @@ void mapSize() { Exp.mapBin("mapBin1") ), Exp.val(200)); - parseExpAndCompare("$.mapBin1.{}.count() > 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{}.count() > 200", expected); // the default behaviour for count() without List '[]' or Map '{}' designators is List Exp expected2 = Exp.gt( @@ -121,7 +122,7 @@ void mapSize() { Exp.listBin("mapBin1") ), Exp.val(200)); - parseExpAndCompare("$.mapBin1.count() > 200", expected2); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.count() > 200", expected2); } @Test @@ -135,7 +136,7 @@ void nestedMapSize() { Exp.mapBin("mapBin1")) ), Exp.val(200)); - parseExpAndCompare("$.mapBin1.a.{}.count() == 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.{}.count() == 200", expected); // the default behaviour for count() without Map '{}' or List '[]' designators is List Exp expected2 = Exp.eq( @@ -146,7 +147,7 @@ void nestedMapSize() { Exp.mapBin("mapBin1")) ), Exp.val(200)); - parseExpAndCompare("$.mapBin1.a.count() == 200", expected2); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.count() == 200", expected2); } @Test @@ -161,7 +162,7 @@ void nestedMapSizeWithContext() { CTX.mapKey(Value.get("a"))) ), Exp.val(200)); - parseExpAndCompare("$.mapBin1.a.b.{}.count() == 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.b.{}.count() == 200", expected); // the default behaviour for count() without Map '{}' or List '[]' designators is List Exp expected2 = Exp.eq( @@ -173,7 +174,7 @@ void nestedMapSizeWithContext() { CTX.mapKey(Value.get("a"))) ), Exp.val(200)); - parseExpAndCompare("$.mapBin1.a.b.count() == 200", expected2); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.b.count() == 200", expected2); } @Test @@ -186,10 +187,10 @@ void mapByIndex() { Exp.mapBin("mapBin1") ), Exp.val(100)); - parseExpAndCompare("$.mapBin1.{0} == 100", expected); - parseExpAndCompare("$.mapBin1.{0}.get(type: INT) == 100", expected); - parseExpAndCompare("$.mapBin1.{0}.get(type: INT, return: VALUE) == 100", expected); - parseExpAndCompare("$.mapBin1.{0}.asInt() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{0} == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{0}.get(type: INT) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{0}.get(type: INT, return: VALUE) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{0}.asInt() == 100", expected); } @Test @@ -201,10 +202,10 @@ void mapByValue() { Exp.mapBin("mapBin1") ), Exp.val(100)); - parseExpAndCompare("$.mapBin1.{=100} == 100", expected); - parseExpAndCompare("$.mapBin1.{=100}.get(type: INT) == 100", expected); - parseExpAndCompare("$.mapBin1.{=100}.get(type: INT, return: VALUE) == 100", expected); - parseExpAndCompare("$.mapBin1.{=100}.asInt() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{=100} == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{=100}.get(type: INT) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{=100}.get(type: INT, return: VALUE) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{=100}.asInt() == 100", expected); } @Test @@ -215,8 +216,8 @@ void mapByValueCount() { Exp.mapBin("mapBin1")), Exp.val(0) ); - parseExpAndCompare("$.mapBin1.{=100}.count() > 0", expected); - parseExpAndCompare("$.mapBin1.{=100}.{}.count() > 0", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{=100}.count() > 0", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{=100}.{}.count() > 0", expected); } @Test @@ -229,10 +230,10 @@ void mapByRank() { Exp.mapBin("mapBin1") ), Exp.val(100)); - parseExpAndCompare("$.mapBin1.{#-1} == 100", expected); - parseExpAndCompare("$.mapBin1.{#-1}.get(type: INT) == 100", expected); - parseExpAndCompare("$.mapBin1.{#-1}.get(type: INT, return: VALUE) == 100", expected); - parseExpAndCompare("$.mapBin1.{#-1}.asInt() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{#-1} == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{#-1}.get(type: INT) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{#-1}.get(type: INT, return: VALUE) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{#-1}.asInt() == 100", expected); } @Test @@ -246,10 +247,10 @@ void mapByRankWithNesting() { CTX.mapKey(Value.get("a")) ), Exp.val(100)); - parseExpAndCompare("$.mapBin1.a.{#-1} == 100", expected); - parseExpAndCompare("$.mapBin1.a.{#-1}.get(type: INT) == 100", expected); - parseExpAndCompare("$.mapBin1.a.{#-1}.get(type: INT, return: VALUE) == 100", expected); - parseExpAndCompare("$.mapBin1.a.{#-1}.asInt() == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.{#-1} == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.{#-1}.get(type: INT) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.{#-1}.get(type: INT, return: VALUE) == 100", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.{#-1}.asInt() == 100", expected); } @Test @@ -265,8 +266,8 @@ void nestedListsWithDifferentContextTypes() { ), Exp.val("stringVal")); // Implicit detect as String - parseExpAndCompare("$.mapBin1.{5}.{#-1} == \"stringVal\"", expected); - parseExpAndCompare("$.mapBin1.{5}.{#-1}.get(type: STRING) == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{5}.{#-1} == \"stringVal\"", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{5}.{#-1}.get(type: STRING) == \"stringVal\"", expected); // Nested List Rank Value expected = Exp.eq( @@ -278,7 +279,7 @@ void nestedListsWithDifferentContextTypes() { CTX.mapRank(-1) ), Exp.val(200)); - parseExpAndCompare("$.mapBin1.{5}.{#-1}.{=100} == 200", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{5}.{#-1}.{=100} == 200", expected); } @Test @@ -288,8 +289,8 @@ void mapKeyRange() { Exp.val("a"), Exp.val("c"), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{a-c}", expected); - parseExpAndCompare("$.mapBin1.{\"a\"-\"c\"}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{a-c}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{\"a\"-\"c\"}", expected); // Inverted expected = MapExp.getByKeyRange( @@ -297,8 +298,8 @@ void mapKeyRange() { Exp.val("a"), Exp.val("c"), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{!a-c}", expected); - parseExpAndCompare("$.mapBin1.{!\"a\"-\"c\"}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{!a-c}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{!\"a\"-\"c\"}", expected); // From start till the end expected = MapExp.getByKeyRange( @@ -306,8 +307,8 @@ void mapKeyRange() { Exp.val("a"), null, Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{a-}", expected); - parseExpAndCompare("$.mapBin1.{\"a\"-}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{a-}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{\"a\"-}", expected); } @Test @@ -316,16 +317,16 @@ void mapKeyList() { MapReturnType.VALUE, Exp.val(List.of("a", "b", "c")), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{a,b,c}", expected); - parseExpAndCompare("$.mapBin1.{\"a\",\"b\",\"c\"}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{a,b,c}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{\"a\",\"b\",\"c\"}", expected); // Inverted expected = MapExp.getByKeyList( MapReturnType.VALUE | MapReturnType.INVERTED, Exp.val(List.of("a", "b", "c")), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{!a,b,c}", expected); - parseExpAndCompare("$.mapBin1.{!\"a\",\"b\",\"c\"}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{!a,b,c}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{!\"a\",\"b\",\"c\"}", expected); } @Test @@ -335,7 +336,7 @@ void mapIndexRange() { Exp.val(1), Exp.val(2), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{1:3}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{1:3}", expected); // Negative expected = MapExp.getByIndexRange( @@ -343,7 +344,7 @@ void mapIndexRange() { Exp.val(-3), Exp.val(4), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{-3:1}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{-3:1}", expected); // Inverted expected = MapExp.getByIndexRange( @@ -351,14 +352,14 @@ void mapIndexRange() { Exp.val(2), Exp.val(2), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{!2:4}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{!2:4}", expected); // From start till the end expected = MapExp.getByIndexRange( MapReturnType.VALUE, Exp.val(1), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{1:}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{1:}", expected); } @Test @@ -367,23 +368,23 @@ void mapValueList() { MapReturnType.VALUE, Exp.val(List.of("a", "b", "c")), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{=a,b,c}", expected); - parseExpAndCompare("$.mapBin1.{=\"a\",\"b\",\"c\"}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{=a,b,c}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{=\"a\",\"b\",\"c\"}", expected); // Integer expected = MapExp.getByValueList( MapReturnType.VALUE, Exp.val(List.of(1, 2, 3)), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{=1,2,3}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{=1,2,3}", expected); // Inverted expected = MapExp.getByValueList( MapReturnType.VALUE | MapReturnType.INVERTED, Exp.val(List.of("a", "b", "c")), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{!=a,b,c}", expected); - parseExpAndCompare("$.mapBin1.{!=\"a\",\"b\",\"c\"}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{!=a,b,c}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{!=\"a\",\"b\",\"c\"}", expected); } @Test @@ -393,7 +394,7 @@ void mapValueRange() { Exp.val(111), Exp.val(334), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{=111:334}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{=111:334}", expected); // Inverted expected = MapExp.getByValueRange( @@ -401,7 +402,7 @@ void mapValueRange() { Exp.val(10), Exp.val(20), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{!=10:20}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{!=10:20}", expected); // From start till the end expected = MapExp.getByValueRange( @@ -409,7 +410,7 @@ void mapValueRange() { Exp.val(111), null, Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{=111:}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{=111:}", expected); } @Test @@ -419,7 +420,7 @@ void mapRankRange() { Exp.val(0), Exp.val(3), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{#0:3}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{#0:3}", expected); // Inverted expected = MapExp.getByRankRange( @@ -427,14 +428,14 @@ void mapRankRange() { Exp.val(0), Exp.val(3), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{!#0:3}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{!#0:3}", expected); // From start till the end expected = MapExp.getByRankRange( MapReturnType.VALUE, Exp.val(-3), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{#-3:}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{#-3:}", expected); // From start till the end with context expected = MapExp.getByRankRange( @@ -442,7 +443,7 @@ void mapRankRange() { Exp.val(-3), Exp.mapBin("mapBin1"), CTX.mapIndex(5)); - parseExpAndCompare("$.mapBin1.{5}.{#-3:}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{5}.{#-3:}", expected); } @Test @@ -453,7 +454,7 @@ void mapRankRangeRelative() { Exp.val(10), Exp.val(2), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{#-1:1~10}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{#-1:1~10}", expected); // Inverted expected = MapExp.getByValueRelativeRankRange( @@ -462,7 +463,7 @@ void mapRankRangeRelative() { Exp.val(10), Exp.val(2), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{!#-1:1~10}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{!#-1:1~10}", expected); // From start till the end expected = MapExp.getByValueRelativeRankRange( @@ -470,7 +471,7 @@ void mapRankRangeRelative() { Exp.val(-2), Exp.val(10), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{#-2:~10}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{#-2:~10}", expected); } @Test @@ -481,7 +482,7 @@ void mapIndexRangeRelative() { Exp.val(0), Exp.val(1), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{0:1~a}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{0:1~a}", expected); // Inverted expected = MapExp.getByKeyRelativeIndexRange( @@ -490,7 +491,7 @@ void mapIndexRangeRelative() { Exp.val(0), Exp.val(1), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{!0:1~a}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{!0:1~a}", expected); // From start till the end expected = MapExp.getByKeyRelativeIndexRange( @@ -498,7 +499,7 @@ void mapIndexRangeRelative() { Exp.val("a"), Exp.val(0), Exp.mapBin("mapBin1")); - parseExpAndCompare("$.mapBin1.{0:~a}", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.{0:~a}", expected); } @Test @@ -512,7 +513,7 @@ void mapReturnTypes() { ), Exp.val(5)); // Implicit detect as Int - parseExpAndCompare("$.mapBin1.a.get(type: INT, return: COUNT) == 5", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.get(type: INT, return: COUNT) == 5", expected); expected = MapExp.getByKey( MapReturnType.ORDERED_MAP, @@ -521,7 +522,7 @@ void mapReturnTypes() { Exp.mapBin("mapBin1") ); // Implicit detect as Int - parseExpAndCompare("$.mapBin1.a.get(return: ORDERED_MAP)", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.get(return: ORDERED_MAP)", expected); expected = Exp.eq( MapExp.getByKey( @@ -532,6 +533,6 @@ void mapReturnTypes() { ), Exp.val(5)); // Implicit detect as Int - parseExpAndCompare("$.mapBin1.a.get(type: INT, return: RANK) == 5", expected); + TestUtils.parseFilterExpressionAndCompare("$.mapBin1.a.get(type: INT, return: RANK) == 5", expected); } } diff --git a/src/test/java/com/aerospike/dsl/expression/RecordMetadataTests.java b/src/test/java/com/aerospike/dsl/expression/RecordMetadataTests.java index 5f7557e..fc9569d 100644 --- a/src/test/java/com/aerospike/dsl/expression/RecordMetadataTests.java +++ b/src/test/java/com/aerospike/dsl/expression/RecordMetadataTests.java @@ -2,10 +2,11 @@ import com.aerospike.client.exp.Exp; import com.aerospike.dsl.DslParseException; +import com.aerospike.dsl.util.TestUtils; import org.junit.jupiter.api.Test; -import static com.aerospike.dsl.util.TestUtils.parseExp; -import static com.aerospike.dsl.util.TestUtils.parseExpAndCompare; +import static com.aerospike.dsl.util.TestUtils.parseFilterExp; +import static com.aerospike.dsl.util.TestUtils.parseDslExpressionAndCompare; import static org.assertj.core.api.Assertions.assertThatThrownBy; class RecordMetadataTests { @@ -15,7 +16,7 @@ void deviceSize() { // Expression to find records that occupy more than 1 MiB of storage space String input = "$.deviceSize() > 1048576"; Exp expected = Exp.gt(Exp.deviceSize(), Exp.val(1024 * 1024)); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); } @Test @@ -23,7 +24,7 @@ void memorySize() { // Expression to find records that occupy more than 1 MiB of memory String input = "$.memorySize() > 1048576"; Exp expected = Exp.gt(Exp.memorySize(), Exp.val(1024 * 1024)); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); } @Test @@ -31,7 +32,7 @@ void recordSize() { // Expression to find records that occupy more than 1 MiB of memory String input = "$.recordSize() > 1048576"; Exp expected = Exp.gt(Exp.recordSize(), Exp.val(1024 * 1024)); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); } @Test @@ -39,12 +40,12 @@ void digestModulo() { // Expression to find records where digest mod 3 equals 0 String input = "$.digestModulo(3) == 0"; Exp expected = Exp.eq(Exp.digestModulo(3), Exp.val(0)); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); // Expression to find records where digest mod 3 equals the value stored in the bin called "digestModulo" String input2 = "$.digestModulo(3) == $.digestModulo"; Exp expected2 = Exp.eq(Exp.digestModulo(3), Exp.intBin("digestModulo")); - parseExpAndCompare(input2, expected2); + TestUtils.parseFilterExpressionAndCompare(input2, expected2); } @Test @@ -52,7 +53,7 @@ void isTombstone() { // Expression to find records that are tombstones String input = "$.isTombstone()"; Exp expected = Exp.isTombstone(); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); } @Test @@ -60,7 +61,7 @@ void keyExists() { // Expression to find records that has a stored key String input = "$.keyExists()"; Exp expected = Exp.keyExists(); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); } // Comparing Metadata to a Bin @@ -69,12 +70,12 @@ void lastUpdate() { // Expression to find records where the last-update-time is less than bin 'updateBy' String inputMetadataLeft = "$.lastUpdate() < $.updateBy"; Exp expectedLeft = Exp.lt(Exp.lastUpdate(), Exp.intBin("updateBy")); - parseExpAndCompare(inputMetadataLeft, expectedLeft); + TestUtils.parseFilterExpressionAndCompare(inputMetadataLeft, expectedLeft); // Expression to find records where the last-update-time is less than bin 'updateBy' String inputMetadataRight = "$.updateBy > $.lastUpdate()"; Exp expectedRight = Exp.gt(Exp.intBin("updateBy"), Exp.lastUpdate()); - parseExpAndCompare(inputMetadataRight, expectedRight); + TestUtils.parseFilterExpressionAndCompare(inputMetadataRight, expectedRight); } @Test @@ -82,22 +83,22 @@ void sinceUpdate() { // Expression to find records that were updated within the last 2 hours String input = "$.sinceUpdate() < 7200000"; Exp expected = Exp.lt(Exp.sinceUpdate(), Exp.val(2 * 60 * 60 * 1000)); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); // Expression to find records that were update within the value stored in the bin called "intBin" String input2 = "$.sinceUpdate() < $.intBin"; Exp expected2 = Exp.lt(Exp.sinceUpdate(), Exp.intBin("intBin")); - parseExpAndCompare(input2, expected2); + TestUtils.parseFilterExpressionAndCompare(input2, expected2); // Expression to find records that were updated within the value stored in the bin called "sinceUpdate" String input3 = "$.sinceUpdate() < $.sinceUpdate"; Exp expected3 = Exp.lt(Exp.sinceUpdate(), Exp.intBin("sinceUpdate")); - parseExpAndCompare(input3, expected3); + TestUtils.parseFilterExpressionAndCompare(input3, expected3); // Expression to find records that were updated within the value stored in the bin called "sinceUpdate" String input4 = "$.sinceUpdate > $.sinceUpdate()"; Exp expected4 = Exp.gt(Exp.intBin("sinceUpdate"), Exp.sinceUpdate()); - parseExpAndCompare(input4, expected4); + TestUtils.parseFilterExpressionAndCompare(input4, expected4); } @Test @@ -108,12 +109,12 @@ void setName() { Exp.eq(Exp.setName(), Exp.val("groupA")), Exp.eq(Exp.setName(), Exp.val("groupB")) ); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); // set name compared with String Bin input = "$.mySetBin == $.setName()"; expected = Exp.eq(Exp.stringBin("mySetBin"), Exp.setName()); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); } @Test @@ -121,13 +122,13 @@ void ttl() { // Expression to find records that will expire within 24 hours String input = "$.ttl() <= 86400"; Exp expected = Exp.le(Exp.ttl(), Exp.val(24 * 60 * 60)); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); } //@Test void negativeTtlAsDifferentType() { // TODO: should be supported when adding operator + metadata validations (requires a refactor) - assertThatThrownBy(() -> parseExp("$.ttl() == true")) + assertThatThrownBy(() -> parseFilterExp("$.ttl() == true")) .isInstanceOf(DslParseException.class) .hasMessageContaining("Expecting non-bin operand, got BOOL_OPERAND"); } @@ -137,7 +138,7 @@ void voidTime() { // Expression to find records where the void-time is set to 'never expire' String input = "$.voidTime() == -1"; Exp expected = Exp.eq(Exp.voidTime(), Exp.val(-1)); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); } @Test @@ -148,7 +149,7 @@ void metadataWithLogicalOperatorsExpressions() { Exp.gt(Exp.deviceSize(), Exp.val(1024)), Exp.lt(Exp.ttl(), Exp.val(300)) ); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); // test OR String input2 = "$.deviceSize() > 1024 or $.ttl() < 300"; @@ -156,7 +157,7 @@ void metadataWithLogicalOperatorsExpressions() { Exp.gt(Exp.deviceSize(), Exp.val(1024)), Exp.lt(Exp.ttl(), Exp.val(300)) ); - parseExpAndCompare(input2, expected2); + TestUtils.parseFilterExpressionAndCompare(input2, expected2); } @Test @@ -166,13 +167,13 @@ void metadataAsExpressionWithLogicalOperator() { Exp.isTombstone(), Exp.lt(Exp.ttl(), Exp.val(300)) ); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); input = "$.ttl() < 300 or $.keyExists()"; expected = Exp.or( Exp.lt(Exp.ttl(), Exp.val(300)), Exp.keyExists() ); - parseExpAndCompare(input, expected); + TestUtils.parseFilterExpressionAndCompare(input, expected); } } diff --git a/src/test/java/com/aerospike/dsl/parsedExpression/LogicalParsedExpressionTests.java b/src/test/java/com/aerospike/dsl/parsedExpression/LogicalParsedExpressionTests.java index 9b545df..53f0106 100644 --- a/src/test/java/com/aerospike/dsl/parsedExpression/LogicalParsedExpressionTests.java +++ b/src/test/java/com/aerospike/dsl/parsedExpression/LogicalParsedExpressionTests.java @@ -5,25 +5,25 @@ import com.aerospike.client.query.IndexType; import com.aerospike.dsl.Index; import com.aerospike.dsl.IndexContext; -import org.junit.jupiter.api.Disabled; +import com.aerospike.dsl.util.TestUtils; import org.junit.jupiter.api.Test; import java.util.List; -import static com.aerospike.dsl.util.TestUtils.parseExpressionAndCompare; +import static com.aerospike.dsl.util.TestUtils.parseDslExpressionAndCompare; public class LogicalParsedExpressionTests { @Test - void binLogical_AND_2_no_indexes() { + void binLogical_AND_no_indexes() { Filter filter = null; Exp exp = Exp.and(Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), Exp.gt(Exp.intBin("intBin2"), Exp.val(100))); - parseExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100", filter, exp); + TestUtils.parseDslExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100", filter, exp); } @Test - void binLogical_AND_2_all_indexes() { + void binLogical_AND_all_indexes() { List indexes = List.of( Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build() @@ -31,12 +31,12 @@ void binLogical_AND_2_all_indexes() { String namespace = "test1"; Filter filter = Filter.range("intBin2", 101, Long.MAX_VALUE); Exp exp = Exp.gt(Exp.intBin("intBin1"), Exp.val(100)); - parseExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100", filter, exp, + parseDslExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100", filter, exp, IndexContext.of(namespace, indexes)); } @Test - void binLogical_AND_2_all_indexes_no_cardinality() { + void binLogical_AND_all_indexes_no_cardinality() { List indexes = List.of( Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).build(), Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).build() @@ -45,23 +45,23 @@ void binLogical_AND_2_all_indexes_no_cardinality() { // Filter is chosen alphabetically because no cardinality is given Filter filter = Filter.range("intBin1", 101, Long.MAX_VALUE); Exp exp = Exp.gt(Exp.intBin("intBin2"), Exp.val(100)); - parseExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100", filter, exp, + parseDslExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100", filter, exp, IndexContext.of(namespace, indexes)); } @Test - void binLogical_AND_2_one_index() { + void binLogical_AND_one_index() { List indexes = List.of( Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build()); String namespace = "test1"; Filter filter = Filter.range("intBin1", 101, Long.MAX_VALUE); Exp exp = Exp.gt(Exp.intBin("intBin2"), Exp.val(100)); - parseExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100", filter, exp, + parseDslExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100", filter, exp, IndexContext.of(namespace, indexes)); } @Test - void binLogical_AND_3_all_indexes() { + void binLogical_AND_AND_all_indexes() { List indexes = List.of( Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), @@ -73,12 +73,12 @@ void binLogical_AND_3_all_indexes() { Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), Exp.gt(Exp.intBin("intBin3"), Exp.val(100)) ); - parseExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 and $.intBin3 > 100", filter, exp, + parseDslExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 and $.intBin3 > 100", filter, exp, IndexContext.of(namespace, indexes)); } @Test - void binLogical_AND_3_all_indexes_same_cardinality() { + void binLogical_AND_AND_all_indexes_same_cardinality() { List indexes = List.of( Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(100).build(), Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(100).build(), @@ -91,12 +91,12 @@ void binLogical_AND_3_all_indexes_same_cardinality() { Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), Exp.gt(Exp.intBin("intBin3"), Exp.val(100)) ); - parseExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 and $.intBin3 > 100", filter, exp, + parseDslExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 and $.intBin3 > 100", filter, exp, IndexContext.of(namespace, indexes)); } @Test - void binLogical_AND_3_all_indexes_no_cardinality() { + void binLogical_AND_AND_all_indexes_no_cardinality() { List indexes = List.of( Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).build(), Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).build(), @@ -109,12 +109,12 @@ void binLogical_AND_3_all_indexes_no_cardinality() { Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), Exp.gt(Exp.intBin("intBin3"), Exp.val(100)) ); - parseExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 and $.intBin3 > 100", filter, exp, + parseDslExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 and $.intBin3 > 100", filter, exp, IndexContext.of(namespace, indexes)); } @Test - void binLogical_AND_3_all_indexes_partial_data() { + void binLogical_AND_AND_all_indexes_partial_data() { List indexes = List.of( Index.builder().bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), Index.builder().namespace("test1").bin("intBin2").binValuesRatio(1).build(), @@ -126,15 +126,15 @@ void binLogical_AND_3_all_indexes_partial_data() { // The only matching index with full data is for intBin3 Filter filter = Filter.range("intBin3", 101, Long.MAX_VALUE); Exp exp = Exp.and( - Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), - Exp.gt(Exp.intBin("intBin2"), Exp.val(100)) + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)) ); - parseExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 and $.intBin3 > 100", filter, exp, + parseDslExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 and $.intBin3 > 100", filter, exp, IndexContext.of(namespace, indexes)); } @Test - void binLogical_AND_3_two_indexes() { + void binLogical_AND_AND_two_indexes() { List indexes = List.of( Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(0).build() @@ -145,20 +145,20 @@ void binLogical_AND_3_two_indexes() { Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), Exp.gt(Exp.intBin("intBin3"), Exp.val(100)) ); - parseExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 and $.intBin3 > 100", filter, exp, + parseDslExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 and $.intBin3 > 100", filter, exp, IndexContext.of(namespace, indexes)); } @Test - void binLogical_OR_2_no_indexes() { + void binLogical_OR_no_indexes() { Filter filter = null; Exp exp = Exp.or(Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), Exp.gt(Exp.intBin("intBin2"), Exp.val(100))); - parseExpressionAndCompare("$.intBin1 > 100 or $.intBin2 > 100", filter, exp); + TestUtils.parseDslExpressionAndCompare("$.intBin1 > 100 or $.intBin2 > 100", filter, exp); } @Test - void binLogical_OR_2_all_indexes() { + void binLogical_OR_all_indexes() { List indexes = List.of( Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build() @@ -169,12 +169,12 @@ void binLogical_OR_2_all_indexes() { Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), Exp.gt(Exp.intBin("intBin2"), Exp.val(100)) ); - parseExpressionAndCompare("$.intBin1 > 100 or $.intBin2 > 100", filter, exp, + parseDslExpressionAndCompare("$.intBin1 > 100 or $.intBin2 > 100", filter, exp, IndexContext.of(namespace, indexes)); } @Test - void binLogical_OR_2_one_index() { + void binLogical_OR_one_index() { List indexes = List.of( Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build() ); @@ -184,12 +184,12 @@ void binLogical_OR_2_one_index() { Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), Exp.gt(Exp.intBin("intBin2"), Exp.val(100)) ); - parseExpressionAndCompare("$.intBin1 > 100 or $.intBin2 > 100", filter, exp, + parseDslExpressionAndCompare("$.intBin1 > 100 or $.intBin2 > 100", filter, exp, IndexContext.of(namespace, indexes)); } @Test - void binLogical_OR_3_all_indexes() { + void binLogical_OR_OR_all_indexes() { List indexes = List.of( Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), @@ -199,12 +199,32 @@ void binLogical_OR_3_all_indexes() { Filter filter = null; Exp exp = Exp.or( Exp.or( - Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), - Exp.gt(Exp.intBin("intBin2"), Exp.val(100)) - ), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)) + ), Exp.gt(Exp.intBin("intBin3"), Exp.val(100)) ); - parseExpressionAndCompare("$.intBin1 > 100 or $.intBin2 > 100 or $.intBin3 > 100", filter, exp, + parseDslExpressionAndCompare("$.intBin1 > 100 or $.intBin2 > 100 or $.intBin3 > 100", filter, exp, + IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_OR_OR_all_indexes_same_cardinality() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build() + ); + String namespace = "test1"; + Filter filter = null; + Exp exp = Exp.or( + Exp.or( + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)) + ), + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)) + ); + parseDslExpressionAndCompare("$.intBin1 > 100 or $.intBin2 > 100 or $.intBin3 > 100", filter, exp, IndexContext.of(namespace, indexes)); } @@ -224,30 +244,476 @@ void binLogical_prioritizedAND_OR_indexed() { ), Exp.gt(Exp.intBin("intBin3"), Exp.val(100)) ); - parseExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 or $.intBin3 > 100", filter, exp, + parseDslExpressionAndCompare("$.intBin1 > 100 and $.intBin2 > 100 or $.intBin3 > 100", filter, exp, IndexContext.of(namespace, indexes)); - parseExpressionAndCompare("($.intBin1 > 100 and $.intBin2 > 100) or $.intBin3 > 100", filter, exp, + parseDslExpressionAndCompare("($.intBin1 > 100 and $.intBin2 > 100) or $.intBin3 > 100", filter, exp, IndexContext.of(namespace, indexes)); } - @Disabled // TODO: complex logical structures, different grouping @Test void binLogical_AND_prioritizedOR_indexed() { List indexes = List.of( Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), - Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(0).build() + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build() + ); + String namespace = "test1"; + Filter filter = Filter.range("intBin3", 101, Long.MAX_VALUE); + Exp exp = Exp.or( + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ); + parseDslExpressionAndCompare("$.intBin3 > 100 and ($.intBin2 > 100 or $.intBin1 > 100)", filter, exp, + IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("($.intBin3 > 100 and ($.intBin2 > 100 or $.intBin1 > 100))", filter, exp, + IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_AND_prioritizedOR_indexed_same_cardinality() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build() + ); + String namespace = "test1"; + // Cardinality is the same, is it correct that intBin3 is chosen because it is the only one filtered? + Filter filter = Filter.range("intBin3", 101, Long.MAX_VALUE); + Exp exp = Exp.or( + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ); + parseDslExpressionAndCompare("$.intBin3 > 100 and ($.intBin2 > 100 or $.intBin1 > 100)", filter, exp, + IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("($.intBin3 > 100 and ($.intBin2 > 100 or $.intBin1 > 100))", filter, exp, + IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_AND_prioritizedOR_indexed_no_cardinality() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).build() + ); + String namespace = "test1"; + // Cardinality is the same, is it correct that intBin3 is chosen because it is the only one filtered? + Filter filter = Filter.range("intBin3", 101, Long.MAX_VALUE); + Exp exp = Exp.or( + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ); + parseDslExpressionAndCompare("$.intBin3 > 100 and ($.intBin2 > 100 or $.intBin1 > 100)", filter, exp, + IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("($.intBin3 > 100 and ($.intBin2 > 100 or $.intBin1 > 100))", filter, exp, + IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_OR_prioritizedOR_indexed() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build() + ); + String namespace = "test1"; + Filter filter = null; + Exp exp = Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.or( + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ) + ); + parseDslExpressionAndCompare("$.intBin3 > 100 or ($.intBin2 > 100 or $.intBin1 > 100)", filter, exp, + IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("($.intBin3 > 100 or ($.intBin2 > 100 or $.intBin1 > 100))", filter, exp, + IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_OR_prioritizedOR_indexed_same_cardinality() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build() + ); + String namespace = "test1"; + Filter filter = null; + Exp exp = Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.or( + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ) + ); + parseDslExpressionAndCompare("$.intBin3 > 100 or ($.intBin2 > 100 or $.intBin1 > 100)", filter, exp, + IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("($.intBin3 > 100 or ($.intBin2 > 100 or $.intBin1 > 100))", filter, exp, + IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_OR_prioritizedAND_indexed() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build() + ); + String namespace = "test1"; + Filter filter = null; + Exp exp = Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.and( + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ) + ); + parseDslExpressionAndCompare("$.intBin3 > 100 or ($.intBin2 > 100 and $.intBin1 > 100)", filter, exp, + IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("($.intBin3 > 100 or ($.intBin2 > 100 and $.intBin1 > 100))", filter, exp, + IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_OR_prioritizedAND_indexed_same_cardinality() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build() + ); + String namespace = "test1"; + Filter filter = null; + Exp exp = Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.and( + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ) + ); + parseDslExpressionAndCompare("$.intBin3 > 100 or ($.intBin2 > 100 and $.intBin1 > 100)", filter, exp, + IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("($.intBin3 > 100 or ($.intBin2 > 100 and $.intBin1 > 100))", filter, exp, + IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_prioritizedAND_OR_prioritizedAND_indexed() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin4").indexType(IndexType.NUMERIC).binValuesRatio(0).build() + ); + String namespace = "test1"; + Filter filter = null; + Exp exp = Exp.or( + Exp.and( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin4"), Exp.val(100)) + ), + Exp.and( + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ) + ); + parseDslExpressionAndCompare("($.intBin3 > 100 and $.intBin4 > 100) or ($.intBin2 > 100 and $.intBin1 > 100)", + filter, exp, IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("(($.intBin3 > 100 and $.intBin4 > 100) or ($.intBin2 > 100 and $.intBin1 > 100))", + filter, exp, IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_prioritizedAND_OR_prioritizedAND_indexed_same_cardinality() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin4").indexType(IndexType.NUMERIC).binValuesRatio(1).build() + ); + String namespace = "test1"; + Filter filter = null; + Exp exp = Exp.or( + Exp.and( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin4"), Exp.val(100)) + ), + Exp.and( + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ) + ); + parseDslExpressionAndCompare("($.intBin3 > 100 and $.intBin4 > 100) or ($.intBin2 > 100 and $.intBin1 > 100)", + filter, exp, IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("(($.intBin3 > 100 and $.intBin4 > 100) or ($.intBin2 > 100 and $.intBin1 > 100))", + filter, exp, IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_prioritizedOR_AND_prioritizedOR_indexed() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin4").indexType(IndexType.NUMERIC).binValuesRatio(0).build() + ); + String namespace = "test1"; + Filter filter = null; + Exp exp = Exp.and( + Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin4"), Exp.val(100)) + ), + Exp.or( + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ) + ); + parseDslExpressionAndCompare("($.intBin3 > 100 or $.intBin4 > 100) and ($.intBin2 > 100 or $.intBin1 > 100)", + filter, exp, IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("(($.intBin3 > 100 or $.intBin4 > 100) and ($.intBin2 > 100 or $.intBin1 > 100))", + filter, exp, IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_prioritizedOR_AND_prioritizedOR_indexed_same_cardinality() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin4").indexType(IndexType.NUMERIC).binValuesRatio(1).build() + ); + String namespace = "test1"; + Filter filter = null; + Exp exp = Exp.and( + Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin4"), Exp.val(100)) + ), + Exp.or( + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ) + ); + parseDslExpressionAndCompare("($.intBin3 > 100 or $.intBin4 > 100) and ($.intBin2 > 100 or $.intBin1 > 100)", + filter, exp, IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("(($.intBin3 > 100 or $.intBin4 > 100) and ($.intBin2 > 100 or $.intBin1 > 100))", + filter, exp, IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_prioritizedOR_AND_prioritizedAND_indexed() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin4").indexType(IndexType.NUMERIC).binValuesRatio(0).build() + ); + String namespace = "test1"; + Filter filter = Filter.range("intBin2", 101, Long.MAX_VALUE); + Exp exp = Exp.and( + Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin4"), Exp.val(100)) + ), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ); + parseDslExpressionAndCompare("($.intBin3 > 100 or $.intBin4 > 100) and ($.intBin2 > 100 and $.intBin1 > 100)", + filter, exp, IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("(($.intBin3 > 100 or $.intBin4 > 100) and ($.intBin2 > 100 and $.intBin1 > 100))", + filter, exp, IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_prioritizedOR_AND_prioritizedAND_indexed_same_cardinality() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin4").indexType(IndexType.NUMERIC).binValuesRatio(1).build() + ); + String namespace = "test1"; + Filter filter = Filter.range("intBin1", 101, Long.MAX_VALUE); + Exp exp = Exp.and( + Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin4"), Exp.val(100)) + ), + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)) + ); + parseDslExpressionAndCompare("($.intBin3 > 100 or $.intBin4 > 100) and ($.intBin2 > 100 and $.intBin1 > 100)", + filter, exp, IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("(($.intBin3 > 100 or $.intBin4 > 100) and ($.intBin2 > 100 and $.intBin1 > 100))", + filter, exp, IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_prioritizedOR_prioritizedAND_AND_indexed_withFilter() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin4").indexType(IndexType.NUMERIC).binValuesRatio(0).build() ); String namespace = "test1"; Filter filter = Filter.range("intBin1", 101, Long.MAX_VALUE); Exp exp = Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), Exp.and( - Exp.gt(Exp.intBin("intBin1"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin4"), Exp.val(100)), Exp.gt(Exp.intBin("intBin2"), Exp.val(100)) + ) + ); + parseDslExpressionAndCompare("($.intBin3 > 100 or $.intBin4 > 100 and $.intBin2 > 100) and $.intBin1 > 100", + filter, exp, IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("(($.intBin3 > 100 or $.intBin4 > 100 and $.intBin2 > 100) and $.intBin1 > 100)", + filter, exp, IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_prioritizedOR_prioritizedAND_AND_indexed_withTheOnlyFilter() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin4").indexType(IndexType.NUMERIC).binValuesRatio(0).build() + ); + String namespace = "test1"; + // This expression part does not have the index with the largest cardinality, but it is the only applicable + // because all other parts participate in an OR-combined query + Filter filter = Filter.range("intBin1", 101, Long.MAX_VALUE); + Exp exp = Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.and( + Exp.gt(Exp.intBin("intBin4"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)) + ) + ); + parseDslExpressionAndCompare("($.intBin3 > 100 or $.intBin4 > 100 and $.intBin2 > 100) and $.intBin1 > 100", + filter, exp, IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("(($.intBin3 > 100 or $.intBin4 > 100 and $.intBin2 > 100) and $.intBin1 > 100)", + filter, exp, IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_prioritizedOR_prioritizedAND_AND_indexed_same_cardinality() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin4").indexType(IndexType.NUMERIC).binValuesRatio(1).build() + ); + String namespace = "test1"; + Filter filter = Filter.range("intBin1", 101, Long.MAX_VALUE); + Exp exp = Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.and( + Exp.gt(Exp.intBin("intBin4"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)) + ) + ); + parseDslExpressionAndCompare("($.intBin3 > 100 or $.intBin4 > 100 and $.intBin2 > 100) and $.intBin1 > 100", + filter, exp, IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("(($.intBin3 > 100 or $.intBin4 > 100 and $.intBin2 > 100) and $.intBin1 > 100)", + filter, exp, IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_prioritizedOR_prioritizedAND_AND_indexed_withFilterPerCardinality() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).binValuesRatio(1).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).binValuesRatio(0).build(), + Index.builder().namespace("test1").bin("intBin4").indexType(IndexType.NUMERIC).binValuesRatio(0).build() + ); + String namespace = "test1"; + // This expression part has the index with the largest cardinality and is applicable for Filter building, + // another applicable expression part is "$.intBin1 > 100", but intBin1 has index with lower cardinality + Filter filter = Filter.range("intBin2", 101, Long.MAX_VALUE); + Exp exp = Exp.and( + Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin4"), Exp.val(100)) ), - Exp.gt(Exp.intBin("intBin3"), Exp.val(100)) + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) ); - parseExpressionAndCompare("$.intBin1 > 100 and ($.intBin2 > 100 or $.intBin3 > 100)", filter, exp, - IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("(($.intBin3 > 100 or $.intBin4 > 100) and $.intBin2 > 100) and $.intBin1 > 100", + filter, exp, IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("((($.intBin3 > 100 or $.intBin4 > 100) and $.intBin2 > 100) and $.intBin1 > 100)", + filter, exp, IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_OR2_OR1_AND2_AND_AND1_indexed_no_cardinality() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin4").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin5").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin6").indexType(IndexType.NUMERIC).build() + ); + String namespace = "test1"; + Filter filter = Filter.range("intBin1", 101, Long.MAX_VALUE); + Exp exp = Exp.and( + Exp.or( + Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin4"), Exp.val(100)) + ), + Exp.and( + Exp.gt(Exp.intBin("intBin5"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin6"), Exp.val(100)) + ) + ), + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)) + ); + String dslString = "(($.intBin3 > 100 or $.intBin4 > 100) or ($.intBin5 > 100 and $.intBin6 > 100)) " + + "and ($.intBin2 > 100 and $.intBin1 > 100)"; + parseDslExpressionAndCompare(dslString, filter, exp, IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("(" + dslString + ")", filter, exp, IndexContext.of(namespace, indexes)); + } + + @Test + void binLogical_OR2_OR1_AND2_AND_AND2_OR1_AND2_indexed_no_cardinality() { + List indexes = List.of( + Index.builder().namespace("test1").bin("intBin1").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin2").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin3").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin4").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin5").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin6").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin7").indexType(IndexType.NUMERIC).build(), + Index.builder().namespace("test1").bin("intBin8").indexType(IndexType.NUMERIC).build() + ); + String namespace = "test1"; + // No Filter can be built as all expression parts participate in OR-combined queries + Filter filter = null; + Exp exp = Exp.and( + Exp.or( + Exp.or( + Exp.gt(Exp.intBin("intBin3"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin4"), Exp.val(100)) + ), + Exp.and( + Exp.gt(Exp.intBin("intBin5"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin6"), Exp.val(100)) + ) + ), + Exp.or( + Exp.and( + Exp.gt(Exp.intBin("intBin2"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin1"), Exp.val(100)) + ), + Exp.and( + Exp.gt(Exp.intBin("intBin7"), Exp.val(100)), + Exp.gt(Exp.intBin("intBin8"), Exp.val(100)) + ) + ) + ); + String dslString = "(($.intBin3 > 100 or $.intBin4 > 100) or ($.intBin5 > 100 and $.intBin6 > 100)) " + + "and (($.intBin2 > 100 and $.intBin1 > 100) or ($.intBin7 > 100 and $.intBin8 > 100))"; + parseDslExpressionAndCompare(dslString, filter, exp, IndexContext.of(namespace, indexes)); + parseDslExpressionAndCompare("(" + dslString + ")", filter, exp, IndexContext.of(namespace, indexes)); } } diff --git a/src/test/java/com/aerospike/dsl/util/TestUtils.java b/src/test/java/com/aerospike/dsl/util/TestUtils.java index 75fd3e1..ce36c79 100644 --- a/src/test/java/com/aerospike/dsl/util/TestUtils.java +++ b/src/test/java/com/aerospike/dsl/util/TestUtils.java @@ -14,12 +14,20 @@ public class TestUtils { private final DSLParserImpl parser = new DSLParserImpl(); - - public static Expression parseExp(String input) { + + public static ParsedExpression parseExpression(String input, IndexContext indexContext) { + return parser.parseExpression(input, indexContext); + } + + public static Exp parseFilterExp(String input) { + return parser.parseExpression(input).getResult().getExp(); + } + + public static Expression parseFilterExpression(String input) { return Exp.build(parser.parseExpression(input).getResult().getExp()); } - public static void parseExpAndCompare(String input, Exp expected) { + public static void parseFilterExpressionAndCompare(String input, Exp expected) { Expression actualExpression = Exp.build(parser.parseExpression(input).getResult().getExp()); Expression expectedExpression = Exp.build(expected); assertEquals(actualExpression, expectedExpression); @@ -43,14 +51,14 @@ public static void parseFilterAndCompare(String input, IndexContext indexContext assertEquals(actualFilter, expected); } - public static void parseExpressionAndCompare(String input, Filter filter, Exp exp) { + public static void parseDslExpressionAndCompare(String input, Filter filter, Exp exp) { ParsedExpression actualExpression = parser.parseExpression(input); assertEquals(actualExpression.getResult().getFilter(), filter); Exp actualExp = actualExpression.getResult().getExp(); assertEquals(actualExp == null ? null : Exp.build(actualExp), exp == null ? null : Exp.build(exp)); } - public static void parseExpressionAndCompare(String input, Filter filter, Exp exp, IndexContext indexContext) { + public static void parseDslExpressionAndCompare(String input, Filter filter, Exp exp, IndexContext indexContext) { ParsedExpression actualExpression = parser.parseExpression(input, indexContext); assertEquals(actualExpression.getResult().getFilter(), filter); Exp actualExp = actualExpression.getResult().getExp();