>From Ayush Tripathi <[email protected]>:

Ayush Tripathi has uploaded this change for review. ( 
https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/19045 )


Change subject: Rewrite Like operator with % suffix as AND Operator.
......................................................................

Rewrite Like operator with % suffix as AND Operator.

- user model changes: no
- storage format changes: no
- interface changes: no

Details:
When a pattern ends with %, we can utilize secondary indexes by transforming 
the LIKE into an AND expression.
Change-Id: I79e1bbe69d0401bf799b76b5ddea02aaa496af7e

Change-Id: I22a4a5b94c89ae8f7132b52848cfe300f09b1645
---
M 
asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/OperatorExpressionVisitor.java
1 file changed, 61 insertions(+), 13 deletions(-)



  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb 
refs/changes/45/19045/1

diff --git 
a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/OperatorExpressionVisitor.java
 
b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/OperatorExpressionVisitor.java
index f371881..69f84d2 100644
--- 
a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/OperatorExpressionVisitor.java
+++ 
b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/OperatorExpressionVisitor.java
@@ -34,6 +34,7 @@
 import 
org.apache.asterix.lang.common.expression.QuantifiedExpression.Quantifier;
 import org.apache.asterix.lang.common.expression.VariableExpr;
 import org.apache.asterix.lang.common.literal.FalseLiteral;
+import org.apache.asterix.lang.common.literal.StringLiteral;
 import org.apache.asterix.lang.common.literal.TrueLiteral;
 import org.apache.asterix.lang.common.rewrites.LangRewritingContext;
 import org.apache.asterix.lang.common.struct.OperatorType;
@@ -81,14 +82,55 @@
         }
         return operatorExpr;
     }
+    private Expression createANDExpressionHelper(Expression target,Expression 
left,Expression right,OperatorExpr operatorExpr)
+            throws CompilationException {
+        Expression leftComparison = createOperatorExpression(OperatorType.GE, 
target, left, operatorExpr.getHints(),
+                operatorExpr.getSourceLocation());
+        // Creates the expression target <= right.
+        Expression targetCopy = (Expression) SqlppRewriteUtil.deepCopy(target);

-    private Expression processLikeOperator(OperatorExpr operatorExpr, 
OperatorType opType) {
+        // remove any selectivity hints from operatorExpr; do not want to 
duplicate those hints
+        Expression rightComparison = createOperatorExpression(OperatorType.LE, 
targetCopy, right,
+                removeSelectivityHints(operatorExpr), 
operatorExpr.getSourceLocation());
+
+        Expression andExpr = createOperatorExpression(OperatorType.AND, 
leftComparison, rightComparison, null,
+                operatorExpr.getSourceLocation());
+        return andExpr;
+    }
+
+    private Expression processLikeOperator(OperatorExpr operatorExpr, 
OperatorType opType) throws CompilationException {
         CallExpr likeExpr =
                 new CallExpr(new 
FunctionSignature(BuiltinFunctions.STRING_LIKE), operatorExpr.getExprList());
         likeExpr.addHints(operatorExpr.getHints());
         likeExpr.setSourceLocation(operatorExpr.getSourceLocation());
         switch (opType) {
             case LIKE:
+                Expression target = operatorExpr.getExprList().get(0);
+                Expression value = operatorExpr.getExprList().get(1);
+                String s = null;
+                if (value instanceof LiteralExpr) {
+                    Object literalValue = ((LiteralExpr) value).getValue();
+                    if (literalValue instanceof StringLiteral) {
+                        s = ((StringLiteral) literalValue).getValue();
+                    }
+                }
+                if (s!=null && s.endsWith("%") && s.length()>1) {
+                    String withoutSuffix = s.substring(0, s.length() - 1);
+                    if (!withoutSuffix.contains("%") && 
!withoutSuffix.contains("_")) {
+                        int lastCodePoint = 
withoutSuffix.codePointAt(withoutSuffix.length() - 1);
+                        String incrementedLastChar = new 
String(Character.toChars(lastCodePoint + 1));
+                        String result =
+                                withoutSuffix.substring(0, 
withoutSuffix.length() - 1) + incrementedLastChar;
+                        Expression left = new LiteralExpr(new 
StringLiteral(withoutSuffix));
+                        Expression right = new LiteralExpr(new 
StringLiteral(result));
+                        Expression andExpr = 
createANDExpressionHelper(target,left,right,operatorExpr);
+                        return andExpr;
+                    }
+                }
+                if (s!=null && !s.contains("%") && !s.contains("_")) {
+                    return createOperatorExpression(
+                            OperatorType.EQ, target,value, 
operatorExpr.getHints(), operatorExpr.getSourceLocation());
+                }
                 return likeExpr;
             case NOT_LIKE:
                 CallExpr notLikeExpr = new CallExpr(new 
FunctionSignature(BuiltinFunctions.NOT),
@@ -156,19 +198,8 @@
         Expression target = operatorExpr.getExprList().get(0);
         Expression left = operatorExpr.getExprList().get(1);
         Expression right = operatorExpr.getExprList().get(2);
+        Expression andExpr = 
createANDExpressionHelper(target,left,right,operatorExpr);

-        // Creates the expression target >= left.
-        Expression leftComparison = createOperatorExpression(OperatorType.GE, 
target, left, operatorExpr.getHints(),
-                operatorExpr.getSourceLocation());
-        // Creates the expression target <= right.
-        Expression targetCopy = (Expression) SqlppRewriteUtil.deepCopy(target);
-
-        // remove any selectivity hints from operatorExpr; do not want to 
duplicate those hints
-        Expression rightComparison = createOperatorExpression(OperatorType.LE, 
targetCopy, right,
-                removeSelectivityHints(operatorExpr), 
operatorExpr.getSourceLocation());
-
-        Expression andExpr = createOperatorExpression(OperatorType.AND, 
leftComparison, rightComparison, null,
-                operatorExpr.getSourceLocation());
         switch (opType) {
             case BETWEEN:
                 return andExpr;

--
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/19045
To unsubscribe, or for help writing mail filters, visit 
https://asterix-gerrit.ics.uci.edu/settings

Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Change-Id: I22a4a5b94c89ae8f7132b52848cfe300f09b1645
Gerrit-Change-Number: 19045
Gerrit-PatchSet: 1
Gerrit-Owner: Ayush Tripathi <[email protected]>
Gerrit-MessageType: newchange

Reply via email to