private static <T> JsonCodec<T> getJsonCodecForType(Type type) {
   if (VarcharType.VARCHAR.equals(type)) {
     return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(String.class);
   }
   if (BooleanType.BOOLEAN.equals(type)) {
     return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Boolean.class);
   }
   if (BigintType.BIGINT.equals(type)) {
     return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Long.class);
   }
   if (DoubleType.DOUBLE.equals(type)) {
     return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Double.class);
   }
   if (type instanceof ArrayType) {
     Type elementType = ((ArrayType) type).getElementType();
     return (JsonCodec<T>) JSON_CODEC_FACTORY.listJsonCodec(getJsonCodecForType(elementType));
   }
   if (type instanceof MapType) {
     Type keyType = ((MapType) type).getKeyType();
     Type valueType = ((MapType) type).getValueType();
     return (JsonCodec<T>)
         JSON_CODEC_FACTORY.mapJsonCodec(getMapKeyType(keyType), getJsonCodecForType(valueType));
   }
   throw new PrestoException(
       INVALID_SESSION_PROPERTY, format("Session property type %s is not supported", type));
 }
 private static Class<?> getMapKeyType(Type type) {
   if (VarcharType.VARCHAR.equals(type)) {
     return String.class;
   }
   if (BooleanType.BOOLEAN.equals(type)) {
     return Boolean.class;
   }
   if (BigintType.BIGINT.equals(type)) {
     return Long.class;
   }
   if (DoubleType.DOUBLE.equals(type)) {
     return Double.class;
   }
   throw new PrestoException(
       INVALID_SESSION_PROPERTY,
       format("Session property map key type %s is not supported", type));
 }
 private static Object deserializeSessionProperty(Type type, String value) {
   if (value == null) {
     throw new PrestoException(INVALID_SESSION_PROPERTY, "Session property can not be null");
   }
   if (VarcharType.VARCHAR.equals(type)) {
     return value;
   }
   if (BooleanType.BOOLEAN.equals(type)) {
     return Boolean.valueOf(value);
   }
   if (BigintType.BIGINT.equals(type)) {
     return Long.valueOf(value);
   }
   if (DoubleType.DOUBLE.equals(type)) {
     return Double.valueOf(value);
   }
   if (type instanceof ArrayType || type instanceof MapType) {
     return getJsonCodecForType(type).fromJson(value);
   }
   throw new PrestoException(
       INVALID_SESSION_PROPERTY, format("Session property type %s is not supported", type));
 }
Exemple #4
0
  private static void serializePrimitive(
      Type type, BlockBuilder builder, Object object, PrimitiveObjectInspector inspector) {
    requireNonNull(builder, "parent builder is null");

    if (object == null) {
      builder.appendNull();
      return;
    }

    switch (inspector.getPrimitiveCategory()) {
      case BOOLEAN:
        BooleanType.BOOLEAN.writeBoolean(builder, ((BooleanObjectInspector) inspector).get(object));
        return;
      case BYTE:
        TinyintType.TINYINT.writeLong(builder, ((ByteObjectInspector) inspector).get(object));
        return;
      case SHORT:
        SmallintType.SMALLINT.writeLong(builder, ((ShortObjectInspector) inspector).get(object));
        return;
      case INT:
        IntegerType.INTEGER.writeLong(builder, ((IntObjectInspector) inspector).get(object));
        return;
      case LONG:
        BigintType.BIGINT.writeLong(builder, ((LongObjectInspector) inspector).get(object));
        return;
      case FLOAT:
        DoubleType.DOUBLE.writeDouble(builder, ((FloatObjectInspector) inspector).get(object));
        return;
      case DOUBLE:
        DoubleType.DOUBLE.writeDouble(builder, ((DoubleObjectInspector) inspector).get(object));
        return;
      case STRING:
        type.writeSlice(
            builder,
            Slices.utf8Slice(((StringObjectInspector) inspector).getPrimitiveJavaObject(object)));
        return;
      case VARCHAR:
        type.writeSlice(
            builder,
            Slices.utf8Slice(
                ((HiveVarcharObjectInspector) inspector)
                    .getPrimitiveJavaObject(object)
                    .getValue()));
        return;
      case CHAR:
        CharType charType = checkType(type, CharType.class, "type");
        HiveChar hiveChar = ((HiveCharObjectInspector) inspector).getPrimitiveJavaObject(object);
        type.writeSlice(
            builder,
            trimSpacesAndTruncateToLength(
                Slices.utf8Slice(hiveChar.getValue()), charType.getLength()));
        return;
      case DATE:
        DateType.DATE.writeLong(builder, formatDateAsLong(object, (DateObjectInspector) inspector));
        return;
      case TIMESTAMP:
        TimestampType.TIMESTAMP.writeLong(
            builder, formatTimestampAsLong(object, (TimestampObjectInspector) inspector));
        return;
      case BINARY:
        VARBINARY.writeSlice(
            builder,
            Slices.wrappedBuffer(
                ((BinaryObjectInspector) inspector).getPrimitiveJavaObject(object)));
        return;
      case DECIMAL:
        DecimalType decimalType = checkType(type, DecimalType.class, "type");
        HiveDecimalWritable hiveDecimal =
            ((HiveDecimalObjectInspector) inspector).getPrimitiveWritableObject(object);
        if (decimalType.isShort()) {
          decimalType.writeLong(
              builder, DecimalUtils.getShortDecimalValue(hiveDecimal, decimalType.getScale()));
        } else {
          decimalType.writeSlice(
              builder, DecimalUtils.getLongDecimalValue(hiveDecimal, decimalType.getScale()));
        }
        return;
    }
    throw new RuntimeException("Unknown primitive type: " + inspector.getPrimitiveCategory());
  }