Repository: incubator-impala
Updated Branches:
  refs/heads/master e1a6db760 -> 1e683d4ee


IMPALA-2057: Better error message for incorrect avro decimal column
declaration

Adding a better error message when logical type is specified at a wrong
level or is not not specified in an avro decimal column declaration.

Change-Id: Iad23706128223b6537d565471ef5d8faa91b0b5a
Reviewed-on: http://gerrit.cloudera.org:8080/5255
Reviewed-by: Bharath Vissapragada <[email protected]>
Tested-by: Internal Jenkins


Project: http://git-wip-us.apache.org/repos/asf/incubator-impala/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-impala/commit/02fc53d5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-impala/tree/02fc53d5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-impala/diff/02fc53d5

Branch: refs/heads/master
Commit: 02fc53d5f0a4d39040ca710c00bae4ca62b73fcd
Parents: e1a6db7
Author: aphadke <[email protected]>
Authored: Tue Nov 29 02:14:04 2016 -0800
Committer: Internal Jenkins <[email protected]>
Committed: Fri Dec 9 23:14:24 2016 +0000

----------------------------------------------------------------------
 .../apache/impala/util/AvroSchemaParser.java    | 42 +++++++++++---------
 1 file changed, 24 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-impala/blob/02fc53d5/fe/src/main/java/org/apache/impala/util/AvroSchemaParser.java
----------------------------------------------------------------------
diff --git a/fe/src/main/java/org/apache/impala/util/AvroSchemaParser.java 
b/fe/src/main/java/org/apache/impala/util/AvroSchemaParser.java
index 9840766..a92305f 100644
--- a/fe/src/main/java/org/apache/impala/util/AvroSchemaParser.java
+++ b/fe/src/main/java/org/apache/impala/util/AvroSchemaParser.java
@@ -125,9 +125,20 @@ public class AvroSchemaParser {
         }
         return structType;
       case BYTES:
+        String logicalType = schema.getProp("logicalType");
+        if (logicalType == null) {
+          throw new AnalysisException(String.format(
+            "logicalType for column '%s' specified at wrong level or was not 
specified",
+             colName));
+        }
         // Decimal is stored in Avro as a BYTE.
-        Type decimalType = getDecimalType(schema);
-        if (decimalType != null) return decimalType;
+        if (logicalType.equalsIgnoreCase("decimal")) {
+          return getDecimalType(schema);
+        } else {
+          throw new AnalysisException(String.format(
+            "Unsupported logicalType: '%s' for column '%s' with type BYTES",
+             logicalType, colName));
+        }
       // TODO: Add support for stored Avro UNIONs by exposing them as STRUCTs 
in Impala.
       case UNION:
       case ENUM:
@@ -161,8 +172,7 @@ public class AvroSchemaParser {
 
   /**
    * Attempts to parse decimal type information from the Avro schema, returning
-   * a decimal ColumnType if successful or null if this schema does not map
-   * to a decimal type.
+   * a decimal ColumnType if successful.
    * Decimal is defined in Avro as a BYTE type with the logicalType property
    * set to "decimal" and a specified scale/precision.
    * Throws a SchemaParseException if the logicType=decimal, but 
scale/precision
@@ -170,22 +180,18 @@ public class AvroSchemaParser {
    */
   private static Type getDecimalType(Schema schema) {
     Preconditions.checkState(schema.getType() == Schema.Type.BYTES);
-    String logicalType = schema.getProp("logicalType");
-    if (logicalType != null && logicalType.equalsIgnoreCase("decimal")) {
-      // Parse the scale/precision of the decimal type.
-      Integer scale = getDecimalProp(schema, "scale");
-      // The Avro spec states that scale should default to zero if not set.
-      if (scale == null) scale = 0;
+    // Parse the scale/precision of the decimal type.
+    Integer scale = getDecimalProp(schema, "scale");
+    // The Avro spec states that scale should default to zero if not set.
+    if (scale == null) scale = 0;
 
-      // Precision is a required property according to the Avro spec.
-      Integer precision = getDecimalProp(schema, "precision");
-      if (precision == null) {
-        throw new SchemaParseException(
-            "No 'precision' property specified for 'decimal' logicalType");
-      }
-      return ScalarType.createDecimalType(precision, scale);
+    // Precision is a required property according to the Avro spec.
+    Integer precision = getDecimalProp(schema, "precision");
+    if (precision == null) {
+      throw new SchemaParseException(
+          "No 'precision' property specified for 'decimal' logicalType");
     }
-    return null;
+    return ScalarType.createDecimalType(precision, scale);
   }
 
   /**

Reply via email to