gortiz commented on code in PR #10380:
URL: https://github.com/apache/pinot/pull/10380#discussion_r1143226787
##########
pinot-common/src/main/java/org/apache/pinot/common/request/context/LiteralContext.java:
##########
@@ -37,68 +45,116 @@ public class LiteralContext {
private FieldSpec.DataType _type;
private Object _value;
- // TODO: Support all data types.
- private static FieldSpec.DataType
convertThriftTypeToDataType(Literal._Fields fields) {
- switch (fields) {
- 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;
+ private BigDecimal _bigDecimalValue;
+
+ private static BigDecimal getBigDecimalValue(FieldSpec.DataType type, Object
value) {
+ switch (type){
+ case BIG_DECIMAL:
+ return (BigDecimal) value;
+ case BOOLEAN:
+ return PinotDataType.BOOLEAN.toBigDecimal(value);
+ case TIMESTAMP:
+ return
PinotDataType.TIMESTAMP.toBigDecimal(Timestamp.valueOf(value.toString()));
default:
- throw new UnsupportedOperationException("Unsupported literal type:" +
fields);
+ if(type.isNumeric()){
+ return new BigDecimal(value.toString());
+ }
+ return BigDecimal.ZERO;
}
}
- 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);
+ @VisibleForTesting
+ static Pair<FieldSpec.DataType, Object> inferLiteralDataTypeAndValue(String
literal) {
+ // Try to interpret the literal as number
+ try {
+ Number number = NumberUtils.createNumber(literal);
+ if (number instanceof BigDecimal || number instanceof BigInteger) {
+ return ImmutablePair.of(FieldSpec.DataType.BIG_DECIMAL, new
BigDecimal(literal));
+ } else {
+ return ImmutablePair.of(FieldSpec.DataType.STRING, literal);
+ }
+ } catch (Exception e) {
+ // Ignored
+ }
+
+ // Try to interpret the literal as TIMESTAMP
+ try {
+ Timestamp timestamp = Timestamp.valueOf(literal);
+ return ImmutablePair.of(FieldSpec.DataType.TIMESTAMP, timestamp);
+ } catch (Exception e) {
+ // Ignored
}
+ return ImmutablePair.of(FieldSpec.DataType.STRING, literal);
}
public LiteralContext(Literal literal) {
- _type = convertThriftTypeToDataType(literal.getSetField());
- _value = literal.getFieldValue();
+ Preconditions.checkState(literal.getFieldValue() != null,
+ "Field value cannot be null for field:" + literal.getSetField());
+ switch (literal.getSetField()){
+ case BOOL_VALUE:
+ _type = FieldSpec.DataType.BOOLEAN;
+ _value = literal.getFieldValue();
+ break;
+ case DOUBLE_VALUE:
+ _type = FieldSpec.DataType.DOUBLE;
+ _value = literal.getFieldValue();
+ break;
+ case LONG_VALUE:
+ _type = FieldSpec.DataType.LONG;
+ _value = literal.getFieldValue();
+ break;
+ case NULL_VALUE:
+ _type = FieldSpec.DataType.UNKNOWN;
+ _value = null;
+ break;
+ case STRING_VALUE:
+ Pair<FieldSpec.DataType, Object> typeAndValue =
inferLiteralDataTypeAndValue(literal.getFieldValue().toString());
+ _type = typeAndValue.getLeft();
+ _value = typeAndValue.getRight();
+ break;
+ default:
+ throw new UnsupportedOperationException("Unsupported data type:" +
literal.getSetField());
+ }
+ _bigDecimalValue = getBigDecimalValue(_type, _value);
}
public FieldSpec.DataType getType() {
return _type;
}
+ public int getIntValue(){
+ return _bigDecimalValue.intValue();
+ }
+ public double getDoubleValue(){
+ return _bigDecimalValue.doubleValue();
+ }
+ public BigDecimal getBigDecimalValue() {
+ return _bigDecimalValue;
+ }
+
+ public String getStringValue() {
+ return String.valueOf(_value);
+ }
+
@Nullable
public Object getValue() {
return _value;
}
+ // This ctor is only used for special handling in subquery.
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;
+ if(type == FieldSpec.DataType.UNKNOWN){
+ _value = null;
+ } else {
+ _value = value;
+ }
+ _bigDecimalValue = getBigDecimalValue(type, value);
}
@Override
public int hashCode() {
- if (_value == null) {
- return 31 * _type.hashCode();
- }
- return 31 * _value.hashCode();
+ return 31 * Objects.hashCode(_value) + Objects.hashCode(_type);
Review Comment:
Why this change in the hash code?
--
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]