Example #1
0
  private void parseDecimalColumn(int column) {
    // don't include column number in message because it causes boxing which is expensive here
    checkArgument(!isPartitionColumn[column], "Column is a partition key");

    loaded[column] = true;

    Object fieldData = rowInspector.getStructFieldData(rowData, structFields[column]);

    if (fieldData == null) {
      nulls[column] = true;
    } else {
      Object fieldValue =
          ((PrimitiveObjectInspector) fieldInspectors[column]).getPrimitiveJavaObject(fieldData);
      checkState(fieldValue != null, "fieldValue should not be null");

      HiveDecimal decimal = (HiveDecimal) fieldValue;
      DecimalType columnType = (DecimalType) types[column];
      BigInteger unscaledDecimal =
          rescale(decimal.unscaledValue(), decimal.scale(), columnType.getScale());

      if (columnType.isShort()) {
        longs[column] = unscaledDecimal.longValue();
      } else {
        slices[column] = Decimals.encodeUnscaledValue(unscaledDecimal);
      }
      nulls[column] = false;
    }
  }
Example #2
0
File: Row.java Project: cdma/presto
 private static Object nativeContainerToOrcValue(Type type, Object nativeValue) {
   if (nativeValue == null) {
     return null;
   }
   if (type instanceof DecimalType) {
     BigInteger unscaledValue;
     DecimalType decimalType = (DecimalType) type;
     if (decimalType.isShort()) {
       unscaledValue = BigInteger.valueOf((long) nativeValue);
     } else {
       unscaledValue = Decimals.decodeUnscaledValue((Slice) nativeValue);
     }
     return HiveDecimal.create(unscaledValue, decimalType.getScale());
   }
   if (type.getJavaType() == boolean.class) {
     return nativeValue;
   }
   if (type.getJavaType() == long.class) {
     return nativeValue;
   }
   if (type.getJavaType() == double.class) {
     return nativeValue;
   }
   if (type.getJavaType() == Slice.class) {
     Slice slice = (Slice) nativeValue;
     return type instanceof VarcharType ? slice.toStringUtf8() : slice.getBytes();
   }
   if (isArrayType(type)) {
     Block arrayBlock = (Block) nativeValue;
     Type elementType = type.getTypeParameters().get(0);
     List<Object> list = new ArrayList<>();
     for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
       list.add(
           nativeContainerToOrcValue(
               elementType, getNativeContainerValue(elementType, arrayBlock, i)));
     }
     return list;
   }
   if (isMapType(type)) {
     Block mapBlock = (Block) nativeValue;
     Type keyType = type.getTypeParameters().get(0);
     Type valueType = type.getTypeParameters().get(1);
     Map<Object, Object> map = new HashMap<>();
     for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
       Object key =
           nativeContainerToOrcValue(keyType, getNativeContainerValue(keyType, mapBlock, i));
       Object value =
           nativeContainerToOrcValue(
               valueType, getNativeContainerValue(valueType, mapBlock, i + 1));
       map.put(key, value);
     }
     return map;
   }
   throw new PrestoException(INTERNAL_ERROR, "Unimplemented type: " + type);
 }
 @Override
 protected Object visitDecimalLiteral(DecimalLiteral node, ConnectorSession context) {
   return Decimals.parse(node.getValue()).getObject();
 }
Example #4
0
 public static Slice longDecimalPartitionKey(String value, DecimalType type, String name) {
   return Decimals.encodeUnscaledValue(decimalPartitionKey(value, type, name).unscaledValue());
 }
Example #5
0
  public static NullableValue parsePartitionValue(
      String partitionName, String value, Type type, DateTimeZone timeZone) {
    boolean isNull = HIVE_DEFAULT_DYNAMIC_PARTITION.equals(value);

    if (type instanceof DecimalType) {
      DecimalType decimalType = (DecimalType) type;
      if (isNull) {
        return NullableValue.asNull(decimalType);
      }
      if (decimalType.isShort()) {
        if (value.isEmpty()) {
          return NullableValue.of(decimalType, 0L);
        }
        return NullableValue.of(
            decimalType, shortDecimalPartitionKey(value, decimalType, partitionName));
      } else {
        if (value.isEmpty()) {
          return NullableValue.of(decimalType, Decimals.encodeUnscaledValue(BigInteger.ZERO));
        }
        return NullableValue.of(
            decimalType, longDecimalPartitionKey(value, decimalType, partitionName));
      }
    }

    if (BOOLEAN.equals(type)) {
      if (isNull) {
        return NullableValue.asNull(BOOLEAN);
      }
      if (value.isEmpty()) {
        return NullableValue.of(BOOLEAN, false);
      }
      return NullableValue.of(BOOLEAN, booleanPartitionKey(value, partitionName));
    }

    if (TINYINT.equals(type)) {
      if (isNull) {
        return NullableValue.asNull(TINYINT);
      }
      if (value.isEmpty()) {
        return NullableValue.of(TINYINT, 0L);
      }
      return NullableValue.of(TINYINT, tinyintPartitionKey(value, partitionName));
    }

    if (SMALLINT.equals(type)) {
      if (isNull) {
        return NullableValue.asNull(SMALLINT);
      }
      if (value.isEmpty()) {
        return NullableValue.of(SMALLINT, 0L);
      }
      return NullableValue.of(SMALLINT, smallintPartitionKey(value, partitionName));
    }

    if (INTEGER.equals(type)) {
      if (isNull) {
        return NullableValue.asNull(INTEGER);
      }
      if (value.isEmpty()) {
        return NullableValue.of(INTEGER, 0L);
      }
      return NullableValue.of(INTEGER, integerPartitionKey(value, partitionName));
    }

    if (BIGINT.equals(type)) {
      if (isNull) {
        return NullableValue.asNull(BIGINT);
      }
      if (value.isEmpty()) {
        return NullableValue.of(BIGINT, 0L);
      }
      return NullableValue.of(BIGINT, bigintPartitionKey(value, partitionName));
    }

    if (DATE.equals(type)) {
      if (isNull) {
        return NullableValue.asNull(DATE);
      }
      return NullableValue.of(DATE, datePartitionKey(value, partitionName));
    }

    if (TIMESTAMP.equals(type)) {
      if (isNull) {
        return NullableValue.asNull(TIMESTAMP);
      }
      return NullableValue.of(TIMESTAMP, timestampPartitionKey(value, timeZone, partitionName));
    }

    if (REAL.equals(type)) {
      if (isNull) {
        return NullableValue.asNull(REAL);
      }
      if (value.isEmpty()) {
        return NullableValue.of(REAL, (long) floatToRawIntBits(0.0f));
      }
      return NullableValue.of(REAL, floatPartitionKey(value, partitionName));
    }

    if (DOUBLE.equals(type)) {
      if (isNull) {
        return NullableValue.asNull(DOUBLE);
      }
      if (value.isEmpty()) {
        return NullableValue.of(DOUBLE, 0.0);
      }
      return NullableValue.of(DOUBLE, doublePartitionKey(value, partitionName));
    }

    if (type instanceof VarcharType) {
      if (isNull) {
        return NullableValue.asNull(type);
      }
      return NullableValue.of(type, varcharPartitionKey(value, partitionName, type));
    }

    if (isCharType(type)) {
      if (isNull) {
        return NullableValue.asNull(type);
      }
      return NullableValue.of(type, charPartitionKey(value, partitionName, type));
    }

    throw new PrestoException(
        NOT_SUPPORTED, format("Unsupported Type [%s] for partition: %s", type, partitionName));
  }