yanxinyi commented on a change in pull request #616: PHOENIX-5432: Refactor 
LiteralExpression to use the builder pattern
URL: https://github.com/apache/phoenix/pull/616#discussion_r345414346
 
 

 ##########
 File path: 
phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
 ##########
 @@ -54,184 +54,218 @@
  * @since 0.1
  */
 public class LiteralExpression extends BaseTerminalExpression {
+
        private static final LiteralExpression[] NULL_EXPRESSIONS = new 
LiteralExpression[Determinism.values().length];
     private static final LiteralExpression[] TYPED_NULL_EXPRESSIONS = new 
LiteralExpression[PDataType.values().length * Determinism.values().length];
     private static final LiteralExpression[] BOOLEAN_EXPRESSIONS = new 
LiteralExpression[2 * Determinism.values().length];
-    
+
     static {
        for (Determinism determinism : Determinism.values()) {
-               NULL_EXPRESSIONS[determinism.ordinal()] = new 
LiteralExpression(null, determinism);
+                NULL_EXPRESSIONS[determinism.ordinal()] = new 
LiteralExpression(null, null, ByteUtil.EMPTY_BYTE_ARRAY,
+                        null, null, SortOrder.getDefault(), determinism);
                for (int i = 0; i < PDataType.values().length; i++) {
-                   
TYPED_NULL_EXPRESSIONS[i+PDataType.values().length*determinism.ordinal()] = new 
LiteralExpression(PDataType.values()[i], determinism);
-               }        
-               BOOLEAN_EXPRESSIONS[determinism.ordinal()] = new 
LiteralExpression(Boolean.FALSE,
-              PBoolean.INSTANCE, PBoolean.INSTANCE.toBytes(Boolean.FALSE), 
determinism);
-               
BOOLEAN_EXPRESSIONS[Determinism.values().length+determinism.ordinal()] = new 
LiteralExpression(Boolean.TRUE, PBoolean.INSTANCE, 
PBoolean.INSTANCE.toBytes(Boolean.TRUE), determinism);
+                   PDataType type = PDataType.values()[i];
+                
TYPED_NULL_EXPRESSIONS[i+PDataType.values().length*determinism.ordinal()] = new 
LiteralExpression(null,
+                        type, ByteUtil.EMPTY_BYTE_ARRAY, !type.isFixedWidth() 
? null : type.getMaxLength(null), null,
+                        SortOrder.getDefault(), determinism);
+               }
+                BOOLEAN_EXPRESSIONS[determinism.ordinal()] = new 
LiteralExpression(Boolean.FALSE, PBoolean.INSTANCE,
+                        PBoolean.INSTANCE.toBytes(Boolean.FALSE), 
!PBoolean.INSTANCE.isFixedWidth() ? null : PBoolean.INSTANCE.getMaxLength(null),
+                        null, SortOrder.getDefault(), determinism);
+
+                BOOLEAN_EXPRESSIONS[Determinism.values().length + 
determinism.ordinal()] = new LiteralExpression(Boolean.TRUE, PBoolean.INSTANCE,
+                        PBoolean.INSTANCE.toBytes(Boolean.TRUE), 
!PBoolean.INSTANCE.isFixedWidth() ? null : PBoolean.INSTANCE.getMaxLength(null),
+                        null, SortOrder.getDefault(), determinism);
        }
     }
-    
+
+    private static LiteralExpression getNullLiteralExpression(Determinism 
determinism) {
+        return NULL_EXPRESSIONS[determinism.ordinal()] ;
+    }
+
+    private static LiteralExpression getTypedNullLiteralExpression(PDataType 
type, Determinism determinism){
+        return 
TYPED_NULL_EXPRESSIONS[type.ordinal()+PDataType.values().length*determinism.ordinal()];
+    }
+
+    private static LiteralExpression getBooleanLiteralExpression(Boolean bool, 
Determinism determinism){
+        return BOOLEAN_EXPRESSIONS[ (Boolean.FALSE.equals(bool) ?  0 : 
Determinism.values().length) + determinism.ordinal()];
+    }
+
     private Object value;
     private PDataType type;
     private Determinism determinism;
     private byte[] byteValue;
     private Integer maxLength;
     private Integer scale;
     private SortOrder sortOrder;
-    
-    private static LiteralExpression getNullLiteralExpression(Determinism 
determinism) {
-       return NULL_EXPRESSIONS[determinism.ordinal()] ;
-    }
-    
-    private static LiteralExpression getTypedNullLiteralExpression(PDataType 
type, Determinism determinism){
-       return 
TYPED_NULL_EXPRESSIONS[type.ordinal()+PDataType.values().length*determinism.ordinal()];
-    }
-    
-    private static LiteralExpression getBooleanLiteralExpression(Boolean bool, 
Determinism determinism){
-       return BOOLEAN_EXPRESSIONS[ (Boolean.FALSE.equals(bool) ?  0 : 
Determinism.values().length) + determinism.ordinal()];
-    }
 
-    public static boolean isFalse(Expression child) {
-        if (child!=null) {
-            return child == 
BOOLEAN_EXPRESSIONS[child.getDeterminism().ordinal()];
-        }
-        return false;
-    }
-    
-    public static boolean isTrue(Expression child) {
-       if (child!=null) {
-               return child == 
BOOLEAN_EXPRESSIONS[Determinism.values().length+child.getDeterminism().ordinal()];
-       }
-       return false;
-    }
+    public static class Builder {
 
-    public static boolean isBooleanNull(Expression child) {
-       if (child!=null) {
-               return child == 
TYPED_NULL_EXPRESSIONS[PBoolean.INSTANCE.ordinal()+PDataType.values().length*child.getDeterminism().ordinal()];
-       }
-       return false;
-    }
+        private Object value;
+        private PDataType type;
+        private Determinism determinism;
+        private byte[] byteValue;
+        private Integer maxLength;
+        private Integer scale;
+        private SortOrder sortOrder;
+        private Boolean rowKeyOrderOptimizable; //Changed type of this field 
from primitive to object since its value should be true if not explicitly set 
in Builder
 
-    public static boolean isBooleanFalseOrNull(Expression child) {
-        if (child!=null) {
-            return child == 
BOOLEAN_EXPRESSIONS[child.getDeterminism().ordinal()]
-                    || child == 
TYPED_NULL_EXPRESSIONS[PBoolean.INSTANCE.ordinal()+PDataType.values().length*child.getDeterminism().ordinal()];
+        public Builder setValue(Object value) {
+            this.value=value;
+            return this;
         }
-        return false;
-    }
-    
-    public static LiteralExpression newConstant(Object value) {
-        return newConstant(value, Determinism.ALWAYS);
-    }
-    
-    // TODO: cache?
-    public static LiteralExpression newConstant(Object value, Determinism 
determinism) {
-        if (value instanceof Boolean) {
-            return getBooleanLiteralExpression((Boolean)value, determinism);
+
+        public Builder setDataType(PDataType type) {
+            this.type=type;
+            return this;
         }
-        else if (value == null) {
-            return getNullLiteralExpression(determinism);
+
+        public Builder setDeterminism(Determinism determinism) {
+            this.determinism=determinism;
+            return this;
         }
-        PDataType type = PDataType.fromLiteral(value);
-        byte[] b = type.toBytes(value);
-        if (type.isNull(b)) {
-            return getTypedNullLiteralExpression(type, determinism);
+
+        public Builder setRowKeyOrderOptimizable(Boolean 
rowKeyOrderOptimizable) {
+            this.rowKeyOrderOptimizable=rowKeyOrderOptimizable;
+            return this;
         }
-        if (type == PVarchar.INSTANCE) {
-            String s = (String) value;
-            if (s.length() == b.length) { // single byte characters only
-                type = PChar.INSTANCE;
-            }
+
+        public Builder setMaxLength(Integer maxLength) {
+            this.maxLength=maxLength;
+            return this;
         }
-        return new LiteralExpression(value, type, b, determinism);
-    }
 
-    public static LiteralExpression newConstant(Object value, PDataType type) 
throws SQLException {
-        return newConstant(value, type, Determinism.ALWAYS);
-    }
-    
-    public static LiteralExpression newConstant(Object value, PDataType type, 
Determinism determinism) throws SQLException {
-        return newConstant(value, type, SortOrder.getDefault(), determinism);
-    }
-    
-    public static LiteralExpression newConstant(Object value, PDataType type, 
SortOrder sortOrder) throws SQLException {
-        return newConstant(value, type, null, null, sortOrder, 
Determinism.ALWAYS);
-    }
-    
-    public static LiteralExpression newConstant(Object value, PDataType type, 
SortOrder sortOrder, Determinism determinism) throws SQLException {
-        return newConstant(value, type, null, null, sortOrder, determinism);
-    }
-    
-    public static LiteralExpression newConstant(Object value, PDataType type, 
Integer maxLength, Integer scale) throws SQLException {
-        return newConstant(value, type, maxLength, scale, 
SortOrder.getDefault(), Determinism.ALWAYS);
-    }
-    
-    public static LiteralExpression newConstant(Object value, PDataType type, 
Integer maxLength, Integer scale, Determinism determinism) throws SQLException 
{ // remove?
-        return newConstant(value, type, maxLength, scale, 
SortOrder.getDefault(), determinism);
-    }
+        public Builder setScale(Integer scale) {
+            this.scale=scale;
+            return this;
+        }
 
-    public static LiteralExpression newConstant(Object value, PDataType type, 
Integer maxLength, Integer scale, SortOrder sortOrder, Determinism determinism) 
-            throws SQLException {
-        return newConstant(value, type, maxLength, scale, sortOrder, 
determinism, true);
-    }
-    
-    // TODO: cache?
-    public static LiteralExpression newConstant(Object value, PDataType type, 
Integer maxLength, Integer scale, SortOrder sortOrder, Determinism determinism, 
boolean rowKeyOrderOptimizable)
-            throws SQLException {
-        if (value == null) {
-            return  (type == null) ?  getNullLiteralExpression(determinism) : 
getTypedNullLiteralExpression(type, determinism);
+        public Builder setSortOrder(SortOrder sortOrder) {
+            this.sortOrder=sortOrder;
+            return this;
         }
-        else if (value instanceof Boolean) {
-            return getBooleanLiteralExpression((Boolean)value, determinism);
+
+        public Builder setByteValue(byte[] byteValue) {
+            this.byteValue=byteValue;
+            return this;
         }
-        PDataType actualType = PDataType.fromLiteral(value);
-        type = type == null ? actualType : type;
-        try {
-            value = type.toObject(value, actualType);
-        } catch (IllegalDataException e) {
-            throw TypeMismatchException.newException(type, actualType, 
value.toString());
+
+        private void setDefaults() {
+            if (this.determinism == null) {
+                this.determinism = Determinism.ALWAYS;
+            }
+            if (this.sortOrder == null) {
+                this.sortOrder = SortOrder.getDefault();
+            }
+            if (this.rowKeyOrderOptimizable == null) {
+                this.rowKeyOrderOptimizable = true;
+            }
         }
-        byte[] b = type.isArrayType() ? ((PArrayDataType)type).toBytes(value, 
PArrayDataType.arrayBaseType(type), sortOrder, rowKeyOrderOptimizable) :
-                type.toBytes(value, sortOrder);
-        if (type == PVarchar.INSTANCE || type == PChar.INSTANCE) {
-            if (type == PChar.INSTANCE && maxLength != null  && b.length < 
maxLength) {
-                if (rowKeyOrderOptimizable) {
-                    b = type.pad(b, maxLength, sortOrder);
-                } else {
-                    b = StringUtil.padChar(b, maxLength);
+
+        public LiteralExpression buildValueAndDeterminism(boolean 
byteValueOnly) {
+            /***
+             * @param byteValueOnly: true if building expression that only has 
byteValue, false otherwise
+             */
+            //for when you only have value (could be null) and determinism 
(can also be null)
+            // all other fields should be null
+            if (byteValueOnly) {
+                return new LiteralExpression(null, null, this.byteValue = 
this.byteValue != null ? this.byteValue : ByteUtil.EMPTY_BYTE_ARRAY, null, 
null, SortOrder.ASC, Determinism.ALWAYS);
+            }
+            setDefaults();
+            if (this.value instanceof Boolean) {
+            return 
LiteralExpression.getBooleanLiteralExpression((Boolean)this.value, 
this.determinism);
+            }
+            else if (this.value == null) {
+                return 
LiteralExpression.getNullLiteralExpression(this.determinism);
+            }
+            PDataType type = PDataType.fromLiteral(this.value);
+            this.byteValue = type.toBytes(this.value);
+            if (type.isNull(this.byteValue)) {
+                return LiteralExpression.getTypedNullLiteralExpression(type, 
this.determinism);
+            }
+            if (type == PVarchar.INSTANCE) {
+                String s = (String) this.value;
+                if (s.length() == this.byteValue.length) { // single byte 
characters only
+                    type = PChar.INSTANCE;
                 }
-            } else if (value != null) {
-                maxLength = ((String)value).length();
             }
-        } else if (type.isArrayType()) {
-            maxLength = ((PhoenixArray)value).getMaxLength();
-        }
-        if (b.length == 0) {
-            return getTypedNullLiteralExpression(type, determinism);
+            return new LiteralExpression(this.value, type, this.byteValue, 
type == null || !type.isFixedWidth() ? null :
+                    type.getMaxLength(this.value), null, 
SortOrder.getDefault(), this.determinism);
         }
-        if (maxLength == null) {
-            maxLength = type.isFixedWidth() ? type.getMaxLength(value) : null;
+
+        public LiteralExpression build() throws SQLException{
+            setDefaults();
+            if (this.value == null) {
+                return  (this.type == null) ?  
LiteralExpression.getNullLiteralExpression(this.determinism) :
+                        
LiteralExpression.getTypedNullLiteralExpression(this.type, this.determinism);
+            }
+            else if (this.value instanceof Boolean){
 
 Review comment:
   nit: inconsistent coding style, most of the time you keep `else if` with the 
same line after `}`, maybe follow the same coding style here?
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to