You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2021/03/31 09:58:47 UTC

[GitHub] [incubator-pinot] siddharthteotia commented on a change in pull request #6728: Unify data type conversion and formatting

siddharthteotia commented on a change in pull request #6728:
URL: https://github.com/apache/incubator-pinot/pull/6728#discussion_r604761571



##########
File path: pinot-common/src/main/java/org/apache/pinot/common/utils/DataSchema.java
##########
@@ -241,11 +244,179 @@ public boolean isCompatible(ColumnDataType anotherColumnDataType) {
           this.isNumberArray() && anotherColumnDataType.isNumberArray());
     }
 
-    public static ColumnDataType fromDataType(FieldSpec.DataType dataType, boolean isSingleValue) {
+    public DataType toDataType() {
+      switch (this) {
+        case INT:
+          return DataType.INT;
+        case LONG:
+          return DataType.LONG;
+        case FLOAT:
+          return DataType.FLOAT;
+        case DOUBLE:
+          return DataType.DOUBLE;
+        case STRING:
+          return DataType.STRING;
+        case BYTES:
+          return DataType.BYTES;
+        default:
+          throw new IllegalStateException(String.format("Cannot convert ColumnDataType: %s to DataType", this));
+      }
+    }
+
+    /**
+     * Converts the given internal value to the type for external use (e.g. as UDF argument). The given value should be
+     * compatible with the type.
+     */
+    public Serializable convert(Object value) {
+      switch (this) {
+        case INT:
+          return ((Number) value).intValue();
+        case LONG:
+          return ((Number) value).longValue();
+        case FLOAT:
+          return ((Number) value).floatValue();
+        case DOUBLE:
+          return ((Number) value).doubleValue();
+        case STRING:
+          return value.toString();
+        case BYTES:
+          return ((ByteArray) value).getBytes();
+        case INT_ARRAY:
+          return (int[]) value;
+        case LONG_ARRAY:
+          if (value instanceof long[]) {
+            return (long[]) value;
+          } else {
+            int[] intValues = (int[]) value;
+            int length = intValues.length;
+            long[] longValues = new long[length];
+            for (int i = 0; i < length; i++) {
+              longValues[i] = intValues[i];
+            }
+            return longValues;
+          }
+        case FLOAT_ARRAY:
+          return (float[]) value;
+        case DOUBLE_ARRAY:
+          if (value instanceof double[]) {
+            return (double[]) value;
+          } else if (value instanceof int[]) {
+            int[] intValues = (int[]) value;
+            int length = intValues.length;
+            double[] doubleValues = new double[length];
+            for (int i = 0; i < length; i++) {
+              doubleValues[i] = intValues[i];
+            }
+            return doubleValues;
+          } else if (value instanceof long[]) {
+            long[] longValues = (long[]) value;
+            int length = longValues.length;
+            double[] doubleValues = new double[length];
+            for (int i = 0; i < length; i++) {
+              doubleValues[i] = longValues[i];
+            }
+            return doubleValues;
+          } else {
+            float[] floatValues = (float[]) value;
+            int length = floatValues.length;
+            double[] doubleValues = new double[length];
+            for (int i = 0; i < length; i++) {
+              doubleValues[i] = floatValues[i];
+            }
+            return doubleValues;
+          }
+        case STRING_ARRAY:
+          return (String[]) value;
+        default:
+          throw new IllegalStateException(String.format("Cannot convert: '%s' to type: %s", value, this));
+      }
+    }
+
+    /**
+     * Formats the value to human-readable format based on the type to be used in the query response.
+     */
+    public Serializable format(Object value) {
+      switch (this) {
+        case BYTES:
+          return BytesUtils.toHexString((byte[]) value);
+        default:
+          return (Serializable) value;
+      }
+    }
+
+    /**
+     * Equivalent to {@link #convert(Object)} and {@link #format(Object)} with a single switch statement.
+     */
+    public Serializable convertAndFormat(Object value) {
+      switch (this) {

Review comment:
       Can we reuse the previous two functions ?




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org