You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by gj...@apache.org on 2019/09/17 18:32:17 UTC

[phoenix] branch 4.x-HBase-1.4 updated: PHOENIX-5458 : Refactor dup calls in ExpressionCompiler

This is an automated email from the ASF dual-hosted git repository.

gjacoby pushed a commit to branch 4.x-HBase-1.4
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/4.x-HBase-1.4 by this push:
     new c4e7969  PHOENIX-5458 : Refactor dup calls in ExpressionCompiler
c4e7969 is described below

commit c4e7969e59c59259942da0a29c8a591837958cc5
Author: Viraj Jasani <vi...@gmail.com>
AuthorDate: Wed Sep 4 15:40:59 2019 +0530

    PHOENIX-5458 : Refactor dup calls in ExpressionCompiler
    
    Signed-off-by: Geoffrey Jacoby <gj...@apache.org>
---
 .../apache/phoenix/compile/ExpressionCompiler.java | 215 ++++++++++-----------
 1 file changed, 107 insertions(+), 108 deletions(-)

diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java
index b101024..d430427 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java
@@ -764,28 +764,7 @@ public class ExpressionCompiler extends UnsupportedAllParseNodeVisitor<Expressio
             public PDatum getBindMetaData(int i, List<Expression> children, final Expression expression) {
                 PDataType type = expression.getDataType();
                 if (type != null && type.isCoercibleTo(PDate.INSTANCE)) {
-                    return new PDatum() {
-                        @Override
-                        public boolean isNullable() {
-                            return expression.isNullable();
-                        }
-                        @Override
-                        public PDataType getDataType() {
-                            return PDecimal.INSTANCE;
-                        }
-                        @Override
-                        public Integer getMaxLength() {
-                            return expression.getMaxLength();
-                        }
-                        @Override
-                        public Integer getScale() {
-                            return expression.getScale();
-                        }
-                        @Override
-                        public SortOrder getSortOrder() {
-                            return expression.getSortOrder();
-                        }
-                    };
+                    return getPDatumByExpression(expression, PDecimal.INSTANCE);
                 }
                 return expression;
             }
@@ -862,53 +841,10 @@ public class ExpressionCompiler extends UnsupportedAllParseNodeVisitor<Expressio
                 // we know that the first parameter must be a date type too.
                 if (i == 0 && (type = children.get(1).getDataType()) != null
                         && type.isCoercibleTo(PDate.INSTANCE)) {
-                    return new PDatum() {
-                        @Override
-                        public boolean isNullable() {
-                            return expression.isNullable();
-                        }
-                        @Override
-                        public PDataType getDataType() {
-                            return type;
-                        }
-                        @Override
-                        public Integer getMaxLength() {
-                            return expression.getMaxLength();
-                        }
-                        @Override
-                        public Integer getScale() {
-                            return expression.getScale();
-                        }
-                        @Override
-                        public SortOrder getSortOrder() {
-                            return expression.getSortOrder();
-                        }                        
-                    };
+                  return getPDatumByExpression(expression, type);
                 } else if (expression.getDataType() != null
-                        && expression.getDataType().isCoercibleTo(
-                    PDate.INSTANCE)) {
-                    return new PDatum() { // Same as with addition
-                        @Override
-                        public boolean isNullable() {
-                            return expression.isNullable();
-                        }
-                        @Override
-                        public PDataType getDataType() {
-                            return PDecimal.INSTANCE;
-                        }
-                        @Override
-                        public Integer getMaxLength() {
-                            return expression.getMaxLength();
-                        }
-                        @Override
-                        public Integer getScale() {
-                            return expression.getScale();
-                        }
-                        @Override
-                        public SortOrder getSortOrder() {
-                            return expression.getSortOrder();
-                        }
-                    };
+                        && expression.getDataType().isCoercibleTo(PDate.INSTANCE)) {
+                  return getPDatumByExpression(expression, PDecimal.INSTANCE);
                 }
                 // Otherwise just go with what was calculated for the expression
                 return expression;
@@ -1030,26 +966,10 @@ public class ExpressionCompiler extends UnsupportedAllParseNodeVisitor<Expressio
             public Expression create(ArithmeticParseNode node, List<Expression> children) throws SQLException {
                 PDataType theType = null;
                 Determinism determinism = Determinism.ALWAYS;
-                for(int i = 0; i < children.size(); i++) {
-                    Expression e = children.get(i);
-                    determinism = determinism.combine(e.getDeterminism());
-                    PDataType type = e.getDataType();
-                    if (type == null) {
-                        continue;
-                    } else if (type == PDecimal.INSTANCE) {
-                        theType = PDecimal.INSTANCE;
-                    } else if (type.isCoercibleTo(PLong.INSTANCE)) {
-                        if (theType == null) {
-                            theType = PLong.INSTANCE;
-                        }
-                    } else if (type.isCoercibleTo(PDouble.INSTANCE)) {
-                        if (theType == null) {
-                            theType = PDouble.INSTANCE;
-                        }
-                    } else {
-                        throw TypeMismatchException.newException(type, node.toString());
-                    }
-                }
+                ExpressionDeterminism expressionDeterminism =
+                  new ExpressionDeterminism(node, children, theType, determinism).invoke();
+                theType = expressionDeterminism.getDataType();
+                determinism = expressionDeterminism.getDeterminism();
                 if (theType == PDecimal.INSTANCE) {
                   return new DecimalMultiplyExpression( children);
                 } else if (theType == PLong.INSTANCE) {
@@ -1090,26 +1010,10 @@ public class ExpressionCompiler extends UnsupportedAllParseNodeVisitor<Expressio
             public Expression create(ArithmeticParseNode node, List<Expression> children) throws SQLException {
                 PDataType theType = null;
                 Determinism determinism = Determinism.ALWAYS;
-                for(int i = 0; i < children.size(); i++) {
-                    Expression e = children.get(i);
-                    determinism = determinism.combine(e.getDeterminism());
-                    PDataType type = e.getDataType();
-                    if (type == null) {
-                        continue;
-                    } else if (type == PDecimal.INSTANCE) {
-                        theType = PDecimal.INSTANCE;
-                    } else if (type.isCoercibleTo(PLong.INSTANCE)) {
-                        if (theType == null) {
-                            theType = PLong.INSTANCE;
-                        }
-                    } else if (type.isCoercibleTo(PDouble.INSTANCE)) {
-                        if (theType == null) {
-                            theType = PDouble.INSTANCE;
-                        }
-                    } else {
-                        throw TypeMismatchException.newException(type, node.toString());
-                    }
-                }
+                ExpressionDeterminism expressionDeterminism =
+                  new ExpressionDeterminism(node, children, theType, determinism).invoke();
+                theType = expressionDeterminism.getDataType();
+                determinism = expressionDeterminism.getDeterminism();
                 if (theType == PDecimal.INSTANCE) {
                   return new DecimalDivideExpression( children);
                 } else if (theType == PLong.INSTANCE) {
@@ -1326,4 +1230,99 @@ public class ExpressionCompiler extends UnsupportedAllParseNodeVisitor<Expressio
     public int getTotalNodeCount() {
         return totalNodeCount;
     }
+
+  private PDatum getPDatumByExpression(Expression expression, PDataType pDataTypeInput) {
+    return new PDatumImpl(expression, pDataTypeInput);
+  }
+
+  private class PDatumImpl implements PDatum {
+
+    private final boolean isNullable;
+    private final PDataType dataType;
+    private final Integer maxLength;
+    private final Integer scale;
+    private final SortOrder sortOrder;
+
+    PDatumImpl(Expression expression, PDataType pDataTypeInput) {
+      this.isNullable = expression.isNullable();
+      this.dataType = pDataTypeInput;
+      this.maxLength = expression.getMaxLength();
+      this.scale = expression.getScale();
+      this.sortOrder = expression.getSortOrder();
+    }
+
+    @Override
+    public boolean isNullable() {
+      return isNullable;
+    }
+
+    @Override
+    public PDataType getDataType() {
+      return dataType;
+    }
+
+    @Override
+    public Integer getMaxLength() {
+      return maxLength;
+    }
+
+    @Override
+    public Integer getScale() {
+      return scale;
+    }
+
+    @Override
+    public SortOrder getSortOrder() {
+      return sortOrder;
+    }
+
+  }
+
+  private class ExpressionDeterminism {
+    private ArithmeticParseNode node;
+    private List<Expression> children;
+    private PDataType theType;
+    private Determinism determinism;
+
+    ExpressionDeterminism(ArithmeticParseNode node, List<Expression> children,
+      PDataType theType, Determinism determinism) {
+      this.node = node;
+      this.children = children;
+      this.theType = theType;
+      this.determinism = determinism;
+    }
+
+    PDataType getDataType() {
+      return theType;
+    }
+
+    Determinism getDeterminism() {
+      return determinism;
+    }
+
+    public ExpressionDeterminism invoke() throws TypeMismatchException {
+      for (Expression e : children) {
+        determinism = determinism.combine(e.getDeterminism());
+        PDataType type = e.getDataType();
+        if (type == null) {
+          continue;
+        }
+        if (type == PDecimal.INSTANCE) {
+          theType = PDecimal.INSTANCE;
+        } else if (type.isCoercibleTo(PLong.INSTANCE)) {
+          if (theType == null) {
+            theType = PLong.INSTANCE;
+          }
+        } else if (type.isCoercibleTo(PDouble.INSTANCE)) {
+          if (theType == null) {
+            theType = PDouble.INSTANCE;
+          }
+        } else {
+          throw TypeMismatchException.newException(type, node.toString());
+        }
+      }
+      return this;
+    }
+  }
+
 }