Jackie-Jiang commented on code in PR #9389:
URL: https://github.com/apache/pinot/pull/9389#discussion_r975903452


##########
pinot-common/src/main/java/org/apache/pinot/common/request/context/LiteralContext.java:
##########
@@ -0,0 +1,126 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.common.request.context;
+
+import com.google.common.base.Preconditions;
+import java.util.Objects;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.Literal;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+/**
+ * The {@code LiteralContext} class represents a literal in the query.
+ * <p>This includes both value and type information. We translate thrift 
literal to this representation in server.
+ * Currently, only Boolean literal is correctly encoded in thrift and passed 
in.
+ * All integers are encoded as LONG in thrift, and the other numerical types 
are encoded as DOUBLE.
+ * The remaining types are encoded as STRING.
+ */
+public class LiteralContext {
+  // TODO: Support all of the types for sql.
+  private FieldSpec.DataType _type;
+  private Object _value;
+
+  private static FieldSpec.DataType 
convertThriftTypeToDataType(Literal._Fields fields) {
+    switch (fields) {
+      case SHORT_VALUE:
+      case INT_VALUE:
+        return FieldSpec.DataType.INT;
+      case LONG_VALUE:
+        return FieldSpec.DataType.LONG;
+      case BOOL_VALUE:
+        return FieldSpec.DataType.BOOLEAN;
+      case DOUBLE_VALUE:
+        return FieldSpec.DataType.DOUBLE;
+      case STRING_VALUE:
+        return FieldSpec.DataType.STRING;
+      default:
+        throw new UnsupportedOperationException("Unsupported literal type:" + 
fields);
+    }
+  }
+
+  private static Class<?> convertDataTypeToJavaType(FieldSpec.DataType 
dataType) {
+    switch (dataType) {
+      case INT:
+        return Integer.class;
+      case LONG:
+        return Long.class;
+      case BOOLEAN:
+        return Boolean.class;
+      case FLOAT:
+        return Float.class;
+      case DOUBLE:
+        return Double.class;
+      case STRING:
+        return String.class;
+      default:
+        throw new UnsupportedOperationException("Unsupported dataType:" + 
dataType);
+    }
+  }
+
+  public LiteralContext(Literal literal) {
+    _type = convertThriftTypeToDataType(literal.getSetField());
+    _value = literal.getFieldValue();
+  }
+
+  public FieldSpec.DataType getType() {
+    return _type;
+  }
+
+  @Nullable
+  public Object getValue() {
+    return _value;
+  }
+
+  public LiteralContext(FieldSpec.DataType type, Object value) {
+    Preconditions.checkArgument(convertDataTypeToJavaType(type) == 
value.getClass(),
+        "Unmatched data type: " + type + " java type: " + value.getClass());
+    _type = type;
+    _value = value;
+  }
+
+  @Override
+  public int hashCode() {
+    if (_value == null) {
+      return 31 * 31 * _type.hashCode();
+    }
+    return 31 * 31 * _type.hashCode() + 31 * _value.hashCode();
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (!(o instanceof LiteralContext)) {
+      return false;
+    }
+    LiteralContext that = (LiteralContext) o;
+    return _type.equals(that._type) && Objects.equals(_value, that._value);
+  }
+
+  @Override
+  public String toString() {
+    if (_value == null) {

Review Comment:
   Can `value` be null?
   We should probably put single quote around STRING and BYTES only



##########
pinot-common/src/main/java/org/apache/pinot/common/request/context/LiteralContext.java:
##########
@@ -0,0 +1,126 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.common.request.context;
+
+import com.google.common.base.Preconditions;
+import java.util.Objects;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.Literal;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+/**
+ * The {@code LiteralContext} class represents a literal in the query.
+ * <p>This includes both value and type information. We translate thrift 
literal to this representation in server.
+ * Currently, only Boolean literal is correctly encoded in thrift and passed 
in.
+ * All integers are encoded as LONG in thrift, and the other numerical types 
are encoded as DOUBLE.
+ * The remaining types are encoded as STRING.
+ */
+public class LiteralContext {
+  // TODO: Support all of the types for sql.
+  private FieldSpec.DataType _type;
+  private Object _value;
+
+  private static FieldSpec.DataType 
convertThriftTypeToDataType(Literal._Fields fields) {
+    switch (fields) {
+      case SHORT_VALUE:
+      case INT_VALUE:
+        return FieldSpec.DataType.INT;
+      case LONG_VALUE:
+        return FieldSpec.DataType.LONG;
+      case BOOL_VALUE:
+        return FieldSpec.DataType.BOOLEAN;
+      case DOUBLE_VALUE:
+        return FieldSpec.DataType.DOUBLE;
+      case STRING_VALUE:
+        return FieldSpec.DataType.STRING;
+      default:
+        throw new UnsupportedOperationException("Unsupported literal type:" + 
fields);
+    }
+  }
+
+  private static Class<?> convertDataTypeToJavaType(FieldSpec.DataType 
dataType) {
+    switch (dataType) {
+      case INT:
+        return Integer.class;
+      case LONG:
+        return Long.class;
+      case BOOLEAN:
+        return Boolean.class;
+      case FLOAT:
+        return Float.class;
+      case DOUBLE:
+        return Double.class;
+      case STRING:
+        return String.class;
+      default:
+        throw new UnsupportedOperationException("Unsupported dataType:" + 
dataType);
+    }
+  }
+
+  public LiteralContext(Literal literal) {
+    _type = convertThriftTypeToDataType(literal.getSetField());
+    _value = literal.getFieldValue();

Review Comment:
   I think we also need to convert the value to match the data type



##########
pinot-common/src/main/java/org/apache/pinot/common/request/context/ExpressionContext.java:
##########
@@ -31,41 +33,58 @@
  */
 public class ExpressionContext {
   public enum Type {
-    LITERAL, IDENTIFIER, FUNCTION
+    LITERAL, IDENTIFIER, FUNCTION,
   }
 
   private final Type _type;
-  private final String _value;
+  private final String _identifier;
   private final FunctionContext _function;
+  // Only set when the _type is LITERAL
+  private final LiteralContext _literal;
 
-  public static ExpressionContext forLiteral(String literal) {
-    return new ExpressionContext(Type.LITERAL, literal, null);
+  public static ExpressionContext forLiteralContext(Literal literal){
+    return new ExpressionContext(Type.LITERAL, null, null, new 
LiteralContext(literal));
+  }
+  
+  public static ExpressionContext forLiteralContext(FieldSpec.DataType type, 
Object val){
+    return new ExpressionContext(Type.LITERAL, null, null, new 
LiteralContext(type, val));
   }
 
   public static ExpressionContext forIdentifier(String identifier) {
-    return new ExpressionContext(Type.IDENTIFIER, identifier, null);
+    return new ExpressionContext(Type.IDENTIFIER, identifier, null, null);
   }
 
   public static ExpressionContext forFunction(FunctionContext function) {
-    return new ExpressionContext(Type.FUNCTION, null, function);
+    return new ExpressionContext(Type.FUNCTION, null, function, null);
   }
 
-  private ExpressionContext(Type type, String value, FunctionContext function) 
{
+  private ExpressionContext(Type type, String value, FunctionContext function, 
LiteralContext literal) {
     _type = type;
-    _value = value;
+    _identifier = value;
     _function = function;
+    _literal = literal;
+  }
+
+  // TODO: Refactor all of the usage for getLiteralString.
+  @Deprecated
+  public String getLiteralString() {

Review Comment:
   IMO is should be fine as long as we clean them up soon



##########
pinot-common/src/main/java/org/apache/pinot/common/request/context/LiteralContext.java:
##########
@@ -0,0 +1,126 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.common.request.context;
+
+import com.google.common.base.Preconditions;
+import java.util.Objects;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.Literal;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+/**
+ * The {@code LiteralContext} class represents a literal in the query.
+ * <p>This includes both value and type information. We translate thrift 
literal to this representation in server.
+ * Currently, only Boolean literal is correctly encoded in thrift and passed 
in.
+ * All integers are encoded as LONG in thrift, and the other numerical types 
are encoded as DOUBLE.
+ * The remaining types are encoded as STRING.
+ */
+public class LiteralContext {
+  // TODO: Support all of the types for sql.
+  private FieldSpec.DataType _type;
+  private Object _value;
+
+  private static FieldSpec.DataType 
convertThriftTypeToDataType(Literal._Fields fields) {
+    switch (fields) {
+      case SHORT_VALUE:
+      case INT_VALUE:
+        return FieldSpec.DataType.INT;
+      case LONG_VALUE:
+        return FieldSpec.DataType.LONG;
+      case BOOL_VALUE:
+        return FieldSpec.DataType.BOOLEAN;
+      case DOUBLE_VALUE:
+        return FieldSpec.DataType.DOUBLE;
+      case STRING_VALUE:
+        return FieldSpec.DataType.STRING;
+      default:
+        throw new UnsupportedOperationException("Unsupported literal type:" + 
fields);
+    }
+  }
+
+  private static Class<?> convertDataTypeToJavaType(FieldSpec.DataType 
dataType) {
+    switch (dataType) {
+      case INT:
+        return Integer.class;
+      case LONG:
+        return Long.class;
+      case BOOLEAN:
+        return Boolean.class;
+      case FLOAT:
+        return Float.class;
+      case DOUBLE:
+        return Double.class;
+      case STRING:
+        return String.class;
+      default:
+        throw new UnsupportedOperationException("Unsupported dataType:" + 
dataType);
+    }
+  }
+
+  public LiteralContext(Literal literal) {
+    _type = convertThriftTypeToDataType(literal.getSetField());
+    _value = literal.getFieldValue();
+  }
+
+  public FieldSpec.DataType getType() {
+    return _type;
+  }
+
+  @Nullable
+  public Object getValue() {
+    return _value;
+  }
+
+  public LiteralContext(FieldSpec.DataType type, Object value) {
+    Preconditions.checkArgument(convertDataTypeToJavaType(type) == 
value.getClass(),
+        "Unmatched data type: " + type + " java type: " + value.getClass());
+    _type = type;
+    _value = value;
+  }
+
+  @Override
+  public int hashCode() {
+    if (_value == null) {
+      return 31 * 31 * _type.hashCode();
+    }
+    return 31 * 31 * _type.hashCode() + 31 * _value.hashCode();

Review Comment:
   Can `_value` be null?
   ```suggestion
       return 31 * _type.hashCode() + _value.hashCode();
   ```



##########
pinot-common/src/main/java/org/apache/pinot/common/request/context/ExpressionContext.java:
##########
@@ -94,7 +113,7 @@ public boolean equals(Object o) {
       return false;
     }
     ExpressionContext that = (ExpressionContext) o;
-    return _type == that._type && Objects.equals(_value, that._value) && 
Objects.equals(_function, that._function);
+    return _type.equals(that._type) && Objects.equals(_identifier, 
that._identifier) && Objects.equals(_function, that._function) && 
Objects.equals(_literal, that._literal);

Review Comment:
   (nit) We can use `==` for enum type



##########
pinot-common/src/main/java/org/apache/pinot/common/request/context/LiteralContext.java:
##########
@@ -0,0 +1,126 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.common.request.context;
+
+import com.google.common.base.Preconditions;
+import java.util.Objects;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.Literal;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+/**
+ * The {@code LiteralContext} class represents a literal in the query.
+ * <p>This includes both value and type information. We translate thrift 
literal to this representation in server.
+ * Currently, only Boolean literal is correctly encoded in thrift and passed 
in.
+ * All integers are encoded as LONG in thrift, and the other numerical types 
are encoded as DOUBLE.
+ * The remaining types are encoded as STRING.
+ */
+public class LiteralContext {
+  // TODO: Support all of the types for sql.
+  private FieldSpec.DataType _type;
+  private Object _value;
+
+  private static FieldSpec.DataType 
convertThriftTypeToDataType(Literal._Fields fields) {
+    switch (fields) {

Review Comment:
   Several fields are not covered. Consider supporting all of them (BYTE -> INT 
and BINARY -> BYTES is missing)



##########
pinot-common/src/main/java/org/apache/pinot/common/request/context/ExpressionContext.java:
##########
@@ -94,7 +113,7 @@ public boolean equals(Object o) {
       return false;
     }
     ExpressionContext that = (ExpressionContext) o;
-    return _type == that._type && Objects.equals(_value, that._value) && 
Objects.equals(_function, that._function);
+    return _type.equals(that._type) && Objects.equals(_identifier, 
that._identifier) && Objects.equals(_function, that._function) && 
Objects.equals(_literal, that._literal);

Review Comment:
   This should be fine because the context class is not transferred from broker 
to server



##########
pinot-common/src/main/java/org/apache/pinot/common/request/context/ExpressionContext.java:
##########
@@ -103,16 +122,19 @@ public int hashCode() {
     if (_type == Type.FUNCTION) {
       return hash + _function.hashCode();
     }
-    return hash + 31 * _value.hashCode();
+    if (_type == Type.LITERAL) {

Review Comment:
   Using a switch is more readable:
   ```
   int hash = 31 * _type.hashCode();
   switch (_type) {
     case LITERAL:
       return hash + _literal.hashCode();
     case IDENTIFIER:
       return hash + _identifier.hashCode();
     case FUNCTION:
       return hash + _function.hashCode();
     default:
       throw new IllegalStateException();
   }
   ```



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to