You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@parquet.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/04/03 13:29:00 UTC

[jira] [Commented] (PARQUET-1253) Support for new logical type representation

    [ https://issues.apache.org/jira/browse/PARQUET-1253?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16424018#comment-16424018 ] 

ASF GitHub Bot commented on PARQUET-1253:
-----------------------------------------

gszadovszky commented on a change in pull request #463: PARQUET-1253: Support for new logical type representation
URL: https://github.com/apache/parquet-mr/pull/463#discussion_r178809713
 
 

 ##########
 File path: parquet-column/src/main/java/org/apache/parquet/schema/LogicalTypeAnnotation.java
 ##########
 @@ -0,0 +1,748 @@
+/*
+ * 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.parquet.schema;
+
+import org.apache.parquet.format.BsonType;
+import org.apache.parquet.format.ConvertedType;
+import org.apache.parquet.format.DateType;
+import org.apache.parquet.format.DecimalType;
+import org.apache.parquet.format.EnumType;
+import org.apache.parquet.format.IntType;
+import org.apache.parquet.format.JsonType;
+import org.apache.parquet.format.ListType;
+import org.apache.parquet.format.LogicalType;
+import org.apache.parquet.format.MapType;
+import org.apache.parquet.format.MicroSeconds;
+import org.apache.parquet.format.MilliSeconds;
+import org.apache.parquet.format.NullType;
+import org.apache.parquet.format.StringType;
+import org.apache.parquet.format.TimeType;
+import org.apache.parquet.format.TimestampType;
+
+import java.util.Objects;
+
+public interface LogicalTypeAnnotation {
+  /**
+   * Convert this parquet-mr logical type to parquet-format LogicalType.
+   *
+   * @return the parquet-format LogicalType representation of this logical type implementation
+   */
+  LogicalType toLogicalType();
+
+  /**
+   * Convert this parquet-mr logical type to parquet-format ConvertedType.
+   *
+   * @return the parquet-format ConvertedType representation of this logical type implementation
+   */
+  ConvertedType toConvertedType();
+
+  /**
+   * Convert this logical type to old logical type representation in parquet-mr (if there's any).
+   * Those logical type implementations, which don't have a corresponding mapping should return null.
+   *
+   * @return the OriginalType representation of the new logical type, or null if there's none
+   */
+  OriginalType toOriginalType();
+
+  /**
+   * Helper method to convert the old representation of logical types (OriginalType) to new logical type.
+   */
+  static LogicalTypeAnnotation fromOriginalType(OriginalType originalType) {
+    if (originalType == null) {
+      return null;
+    }
+    switch (originalType) {
+      case UTF8:
+        return StringLogicalTypeAnnotation.create();
+      case MAP:
+        return MapLogicalTypeAnnotation.create();
+      case DECIMAL:
+        return DecimalLogicalTypeAnnotation.create();
+      case LIST:
+        return ListLogicalTypeAnnotation.create();
+      case DATE:
+        return DateLogicalTypeAnnotation.create();
+      case INTERVAL:
+        return IntervalLogicalTypeAnnotation.create();
+      case TIMESTAMP_MILLIS:
+        return TimestampLogicalTypeAnnotation.create(true, LogicalTypeAnnotation.TimeUnit.MILLIS);
+      case TIMESTAMP_MICROS:
+        return TimestampLogicalTypeAnnotation.create(true, LogicalTypeAnnotation.TimeUnit.MICROS);
+      case TIME_MILLIS:
+        return TimeLogicalTypeAnnotation.create(true, LogicalTypeAnnotation.TimeUnit.MILLIS);
+      case TIME_MICROS:
+        return TimeLogicalTypeAnnotation.create(true, LogicalTypeAnnotation.TimeUnit.MICROS);
+      case UINT_8:
+        return IntLogicalTypeAnnotation.create((byte) 8, false);
+      case UINT_16:
+        return IntLogicalTypeAnnotation.create((byte) 16, false);
+      case UINT_32:
+        return IntLogicalTypeAnnotation.create((byte) 32, false);
+      case UINT_64:
+        return IntLogicalTypeAnnotation.create((byte) 64, false);
+      case INT_8:
+        return IntLogicalTypeAnnotation.create((byte) 8, true);
+      case INT_16:
+        return IntLogicalTypeAnnotation.create((byte) 16, true);
+      case INT_32:
+        return IntLogicalTypeAnnotation.create((byte) 32, true);
+      case INT_64:
+        return IntLogicalTypeAnnotation.create((byte) 64, true);
+      case ENUM:
+        return EnumLogicalTypeAnnotation.create();
+      case JSON:
+        return JsonLogicalTypeAnnotation.create();
+      case BSON:
+        return BsonLogicalTypeAnnotation.create();
+      case MAP_KEY_VALUE:
+        return MapKeyValueTypeAnnotation.create();
+      default:
+        return NullLogicalTypeAnnotation.create();
+    }
+  }
+
+  class StringLogicalTypeAnnotation implements LogicalTypeAnnotation {
+    private static final StringLogicalTypeAnnotation INSTANCE = new StringLogicalTypeAnnotation();
+
+    public static LogicalTypeAnnotation create() {
+      return INSTANCE;
+    }
+
+    private StringLogicalTypeAnnotation() {
+    }
+
+    @Override
+    public LogicalType toLogicalType() {
+      return LogicalType.STRING(new StringType());
+    }
+
+    @Override
+    public ConvertedType toConvertedType() {
+      return ConvertedType.UTF8;
+    }
+
+    @Override
+    public OriginalType toOriginalType() {
+      return OriginalType.UTF8;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+      return obj instanceof StringLogicalTypeAnnotation;
+    }
+
+    @Override
+    public int hashCode() {
+      // This type doesn't have any parameters, thus use class hashcode
+      return getClass().hashCode();
+    }
+  }
+
+  class MapLogicalTypeAnnotation implements LogicalTypeAnnotation {
+    private static final MapLogicalTypeAnnotation INSTANCE = new MapLogicalTypeAnnotation();
+
+    public static LogicalTypeAnnotation create() {
+      return INSTANCE;
+    }
+
+    private MapLogicalTypeAnnotation() {
+    }
+
+    @Override
+    public LogicalType toLogicalType() {
+      return LogicalType.MAP(new MapType());
+    }
+
+    @Override
+    public ConvertedType toConvertedType() {
+      return ConvertedType.MAP;
+    }
+
+    @Override
+    public OriginalType toOriginalType() {
+      return OriginalType.MAP;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+      return obj instanceof MapLogicalTypeAnnotation;
+    }
+
+    @Override
+    public int hashCode() {
+      // This type doesn't have any parameters, thus use class hashcode
+      return getClass().hashCode();
+    }
+  }
+
+  class ListLogicalTypeAnnotation implements LogicalTypeAnnotation {
+    private static final ListLogicalTypeAnnotation INSTANCE = new ListLogicalTypeAnnotation();
+
+    public static LogicalTypeAnnotation create() {
+      return INSTANCE;
+    }
+
+    private ListLogicalTypeAnnotation() {
+    }
+
+    @Override
+    public LogicalType toLogicalType() {
+      return LogicalType.LIST(new ListType());
+    }
+
+    @Override
+    public ConvertedType toConvertedType() {
+      return ConvertedType.LIST;
+    }
+
+    @Override
+    public OriginalType toOriginalType() {
+      return OriginalType.LIST;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+      return obj instanceof ListLogicalTypeAnnotation;
+    }
+
+    @Override
+    public int hashCode() {
+      // This type doesn't have any parameters, thus use class hashcode
+      return getClass().hashCode();
+    }
+  }
+
+  class EnumLogicalTypeAnnotation implements LogicalTypeAnnotation {
+    private static final EnumLogicalTypeAnnotation INSTANCE = new EnumLogicalTypeAnnotation();
+
+    public static LogicalTypeAnnotation create() {
+      return INSTANCE;
+    }
+
+    private EnumLogicalTypeAnnotation() {
+    }
+
+    @Override
+    public LogicalType toLogicalType() {
+      return LogicalType.ENUM(new EnumType());
+    }
+
+    @Override
+    public ConvertedType toConvertedType() {
+      return ConvertedType.ENUM;
+    }
+
+    @Override
+    public OriginalType toOriginalType() {
+      return OriginalType.ENUM;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+      return obj instanceof EnumLogicalTypeAnnotation;
+    }
+
+    @Override
+    public int hashCode() {
+      // This type doesn't have any parameters, thus use class hashcode
+      return getClass().hashCode();
+    }
+  }
+
+  class DecimalLogicalTypeAnnotation implements LogicalTypeAnnotation {
+
+    private int scale;
+    private int precision;
+
+    public static LogicalTypeAnnotation create() {
+      return new DecimalLogicalTypeAnnotation(0, 0);
+    }
+
+    public static LogicalTypeAnnotation create(int scale, int precision) {
+      return new DecimalLogicalTypeAnnotation(scale, precision);
+    }
+
+    private DecimalLogicalTypeAnnotation(int scale, int precision) {
+      this.scale = scale;
+      this.precision = precision;
+    }
+
+    public void setPrecision(int precision) {
+      this.precision = precision;
+    }
+
+    public void setScale(int scale) {
+      this.scale = scale;
+    }
+
+    @Override
+    public LogicalType toLogicalType() {
+      return LogicalType.DECIMAL(new DecimalType(scale, precision));
+    }
+
+    @Override
+    public ConvertedType toConvertedType() {
+      return ConvertedType.DECIMAL;
+    }
+
+    @Override
+    public OriginalType toOriginalType() {
+      return OriginalType.DECIMAL;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+      if (!(obj instanceof DecimalLogicalTypeAnnotation)) {
+        return false;
+      }
+      DecimalLogicalTypeAnnotation other = (DecimalLogicalTypeAnnotation) obj;
+      return scale == other.scale && precision == other.precision;
+    }
+
+    @Override
+    public int hashCode() {
+      return Objects.hash(scale, precision);
+    }
+  }
+
+  class DateLogicalTypeAnnotation implements LogicalTypeAnnotation {
+    private static final DateLogicalTypeAnnotation INSTANCE = new DateLogicalTypeAnnotation();
+
+    public static LogicalTypeAnnotation create() {
+      return INSTANCE;
+    }
+
+    private DateLogicalTypeAnnotation() {
+    }
+
+    @Override
+    public LogicalType toLogicalType() {
+      return LogicalType.DATE(new DateType());
+    }
+
+    @Override
+    public ConvertedType toConvertedType() {
+      return ConvertedType.DATE;
+    }
+
+    @Override
+    public OriginalType toOriginalType() {
+      return OriginalType.DATE;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+      return obj instanceof DateLogicalTypeAnnotation;
+    }
+
+    @Override
+    public int hashCode() {
+      // This type doesn't have any parameters, thus use class hashcode
+      return getClass().hashCode();
+    }
+  }
+
+  enum TimeUnit {
+    MILLIS,
+    MICROS
+  }
+
+  static org.apache.parquet.format.TimeUnit convertUnit(TimeUnit unit) {
+    switch (unit) {
+      case MICROS:
+        return org.apache.parquet.format.TimeUnit.MICROS(new MicroSeconds());
+      case MILLIS:
+        return org.apache.parquet.format.TimeUnit.MILLIS(new MilliSeconds());
+      default:
+        throw new RuntimeException("Unknown time unit " + unit);
+    }
+  }
+
+  class TimeLogicalTypeAnnotation implements LogicalTypeAnnotation {
+    private final boolean isAdjustedToUTC;
+    private final TimeUnit unit;
+
+    public static LogicalTypeAnnotation create(boolean isAdjustedToUTC, TimeUnit unit) {
+      return new TimeLogicalTypeAnnotation(isAdjustedToUTC, unit);
+    }
+
+    private TimeLogicalTypeAnnotation(boolean isAdjustedToUTC, TimeUnit unit) {
+      this.isAdjustedToUTC = isAdjustedToUTC;
+      this.unit = unit;
+    }
+
+    @Override
+    public LogicalType toLogicalType() {
+      return LogicalType.TIME(new TimeType(isAdjustedToUTC, convertUnit(unit)));
+    }
+
+    @Override
+    public ConvertedType toConvertedType() {
+      switch (toOriginalType()) {
+        case TIME_MILLIS:
+          return ConvertedType.TIME_MILLIS;
+        case TIME_MICROS:
+          return ConvertedType.TIME_MICROS;
+        default:
+          throw new RuntimeException("Unknown converted type for " + toOriginalType());
+      }
+    }
+
+    @Override
+    public OriginalType toOriginalType() {
+      switch (unit) {
+        case MILLIS:
+          return OriginalType.TIME_MILLIS;
+        case MICROS:
+          return OriginalType.TIME_MICROS;
+        default:
+          throw new RuntimeException("Unknown original type for " + unit);
+      }
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+      if (!(obj instanceof TimeLogicalTypeAnnotation)) {
+        return false;
+      }
+      TimeLogicalTypeAnnotation other = (TimeLogicalTypeAnnotation) obj;
+      return isAdjustedToUTC == other.isAdjustedToUTC && unit == other.unit;
+    }
+
+    @Override
+    public int hashCode() {
+      return Objects.hash(isAdjustedToUTC, unit);
+    }
+  }
+
+  class TimestampLogicalTypeAnnotation implements LogicalTypeAnnotation {
+    private final boolean isAdjustedToUTC;
+    private final TimeUnit unit;
+
+    public static LogicalTypeAnnotation create(boolean isAdjustedToUTC, TimeUnit unit) {
+      return new TimestampLogicalTypeAnnotation(isAdjustedToUTC, unit);
+    }
+
+    private TimestampLogicalTypeAnnotation(boolean isAdjustedToUTC, TimeUnit unit) {
+      this.isAdjustedToUTC = isAdjustedToUTC;
+      this.unit = unit;
+    }
+
+    @Override
+    public LogicalType toLogicalType() {
+      return LogicalType.TIMESTAMP(new TimestampType(isAdjustedToUTC, convertUnit(unit)));
+    }
+
+    @Override
+    public ConvertedType toConvertedType() {
+      switch (toOriginalType()) {
+        case TIMESTAMP_MICROS:
+          return ConvertedType.TIMESTAMP_MICROS;
+        case TIMESTAMP_MILLIS:
+          return ConvertedType.TIMESTAMP_MILLIS;
+        default:
+          throw new RuntimeException("Unknown converted type for " + unit);
+      }
+    }
+
+    @Override
+    public OriginalType toOriginalType() {
+      switch (unit) {
+        case MILLIS:
+          return OriginalType.TIMESTAMP_MILLIS;
+        case MICROS:
+          return OriginalType.TIMESTAMP_MICROS;
+        default:
+          throw new RuntimeException("Unknown original type for " + unit);
+      }
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+      if (!(obj instanceof TimestampLogicalTypeAnnotation)) {
+        return false;
+      }
+      TimestampLogicalTypeAnnotation other = (TimestampLogicalTypeAnnotation) obj;
+      return (isAdjustedToUTC == other.isAdjustedToUTC) && (unit == other.unit);
+    }
+
+    @Override
+    public int hashCode() {
+      return Objects.hash(isAdjustedToUTC, unit);
+    }
+  }
+
+  class IntLogicalTypeAnnotation implements LogicalTypeAnnotation {
+    private final byte bitWidth;
+    private final boolean isSigned;
+
+    public static LogicalTypeAnnotation create(byte bitWidth, boolean isSigned) {
+      return new IntLogicalTypeAnnotation(bitWidth, isSigned);
+    }
+
+    private IntLogicalTypeAnnotation(byte bitWidth, boolean isSigned) {
+      this.bitWidth = bitWidth;
+      this.isSigned = isSigned;
+    }
+
+    @Override
+    public LogicalType toLogicalType() {
+      return LogicalType.INTEGER(new IntType(bitWidth, isSigned));
+    }
+
+    @Override
+    public ConvertedType toConvertedType() {
+      switch (toOriginalType()) {
+        case INT_8:
+          return ConvertedType.INT_8;
+        case INT_16:
+          return ConvertedType.INT_16;
+        case INT_32:
+          return ConvertedType.INT_32;
+        case INT_64:
+          return ConvertedType.INT_64;
+        case UINT_8:
+          return ConvertedType.UINT_8;
+        case UINT_16:
+          return ConvertedType.UINT_16;
+        case UINT_32:
+          return ConvertedType.UINT_32;
+        case UINT_64:
+          return ConvertedType.UINT_64;
+        default:
+          throw new RuntimeException("Unknown original type " + toOriginalType());
+      }
+    }
+
+    @Override
+    public OriginalType toOriginalType() {
+      switch (bitWidth) {
+        case 8:
+          return isSigned ? OriginalType.INT_8 : OriginalType.UINT_8;
+        case 16:
+          return isSigned ? OriginalType.INT_16 : OriginalType.UINT_16;
+        case 32:
+          return isSigned ? OriginalType.INT_32 : OriginalType.UINT_32;
+        case 64:
+          return isSigned ? OriginalType.INT_64 : OriginalType.UINT_64;
+        default:
+          throw new RuntimeException("Unknown original type " + toOriginalType());
+      }
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+      if (!(obj instanceof IntLogicalTypeAnnotation)) {
+        return false;
+      }
+      IntLogicalTypeAnnotation other = (IntLogicalTypeAnnotation) obj;
+      return (bitWidth == other.bitWidth) && (isSigned == other.isSigned);
+    }
+
+    @Override
+    public int hashCode() {
+      return Objects.hash(bitWidth, isSigned);
+    }
+  }
+
+  class NullLogicalTypeAnnotation implements LogicalTypeAnnotation {
 
 Review comment:
   I think, this implementation is not needed. All the cases where this instance is returned an exception should be thrown instead that the related case is unsupported.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


> Support for new logical type representation
> -------------------------------------------
>
>                 Key: PARQUET-1253
>                 URL: https://issues.apache.org/jira/browse/PARQUET-1253
>             Project: Parquet
>          Issue Type: Improvement
>          Components: parquet-mr
>            Reporter: Nandor Kollar
>            Assignee: Nandor Kollar
>            Priority: Major
>
> Latest parquet-format [introduced|https://github.com/apache/parquet-format/commit/863875e0be3237c6aa4ed71733d54c91a51deabe#diff-0f9d1b5347959e15259da7ba8f4b6252] a new representation for logical types. As of now this is not yet supported in parquet-mr, thus there's no way to use parametrized UTC normalized timestamp data types. When reading and writing Parquet files, besides 'converted_type' parquet-mr should use the new 'logicalType' field in SchemaElement to tell the current logical type annotation. To maintain backward compatibility, the semantic of converted_type shouldn't change.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)