public static String convertAvroSchemaToFieldType()

in amoro-format-hudi/src/main/java/org/apache/amoro/formats/hudi/HudiTableUtil.java [39:115]


  public static String convertAvroSchemaToFieldType(Schema schema) {
    switch (schema.getType()) {
      case ENUM:
      case STRING:
        // convert Avro's Utf8/CharSequence to String
        return "STRING";
      case ARRAY:
        return "ARRAY<" + convertAvroSchemaToFieldType(schema.getElementType()) + ">";
      case MAP:
        return "MAP<STRING, " + convertAvroSchemaToFieldType(schema.getValueType()) + ">";
      case UNION:
        final Schema actualSchema;
        if (schema.getTypes().size() == 2
            && schema.getTypes().get(0).getType() == Schema.Type.NULL) {
          actualSchema = schema.getTypes().get(1);
        } else if (schema.getTypes().size() == 2
            && schema.getTypes().get(1).getType() == Schema.Type.NULL) {
          actualSchema = schema.getTypes().get(0);
        } else if (schema.getTypes().size() == 1) {
          actualSchema = schema.getTypes().get(0);
        } else {
          return "";
        }

        return convertAvroSchemaToFieldType(actualSchema);
      case FIXED:
        // logical decimal type
        if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
          final LogicalTypes.Decimal decimalType = (LogicalTypes.Decimal) schema.getLogicalType();
          return "DECIMAL(" + decimalType.getPrecision() + ", " + decimalType.getScale() + ")";
        }
        // convert fixed size binary data to primitive byte arrays
        return "VARBINARY(" + schema.getFixedSize() + ")";
      case BYTES:
        // logical decimal type
        if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
          final LogicalTypes.Decimal decimalType = (LogicalTypes.Decimal) schema.getLogicalType();
          return "DECIMAL(" + decimalType.getPrecision() + ", " + decimalType + ")";
        }
        return "BYTES";
      case INT:
        // logical date and time type
        final org.apache.avro.LogicalType logicalType = schema.getLogicalType();
        if (logicalType == LogicalTypes.date()) {
          return "DATE";
        } else if (logicalType == LogicalTypes.timeMillis()) {
          return "TIME";
        }
        return "INT";
      case LONG:
        // logical timestamp type
        if (schema.getLogicalType() == LogicalTypes.timestampMillis()) {
          return "TIMESTAMP";
        } else if (schema.getLogicalType() == LogicalTypes.localTimestampMillis()) {
          return "TIMESTAMP_WITH_LOCAL_TIME_ZONE";
        } else if (schema.getLogicalType() == LogicalTypes.timestampMicros()) {
          return "TIMESTAMP";
        } else if (schema.getLogicalType() == LogicalTypes.localTimestampMicros()) {
          return "TIMESTAMP_WITH_LOCAL_TIME_ZONE";
        } else if (schema.getLogicalType() == LogicalTypes.timeMillis()) {
          return "TIME";
        } else if (schema.getLogicalType() == LogicalTypes.timeMicros()) {
          return "TIME";
        }
        return "BIGINT";
      case FLOAT:
        return "FLOAT";
      case DOUBLE:
        return "DOUBLE";
      case BOOLEAN:
        return "BOOLEAN";
      case NULL:
        return "NULL";
      default:
        throw new IllegalArgumentException("Unsupported Avro type '" + schema.getType() + "'.");
    }
  }