in athena-federation-sdk/src/main/java/com/amazonaws/athena/connector/lambda/data/BlockUtils.java [728:883]
protected static void writeListValue(FieldWriter writer, ArrowType type, BufferAllocator allocator, Object value)
{
if (value == null) {
return;
}
try {
//TODO: add all types
switch (Types.getMinorTypeForArrowType(type)) {
case TIMESTAMPMILLITZ:
long dateTimeWithZone;
if (value instanceof ZonedDateTime) {
dateTimeWithZone = DateTimeFormatterUtil.packDateTimeWithZone((ZonedDateTime) value);
}
else if (value instanceof LocalDateTime) {
dateTimeWithZone = DateTimeFormatterUtil.packDateTimeWithZone(
((LocalDateTime) value).atZone(UTC_ZONE_ID).toInstant().toEpochMilli(), UTC_ZONE_ID.getId());
}
else if (value instanceof Date) {
long ldtInLong = Instant.ofEpochMilli(((Date) value).getTime())
.atZone(UTC_ZONE_ID).toInstant().toEpochMilli();
dateTimeWithZone = DateTimeFormatterUtil.packDateTimeWithZone(ldtInLong, UTC_ZONE_ID.getId());
}
else {
dateTimeWithZone = (long) value;
}
writer.writeTimeStampMilliTZ(dateTimeWithZone);
case DATEMILLI:
if (value instanceof Date) {
writer.writeDateMilli(((Date) value).getTime());
}
else {
writer.writeDateMilli((long) value);
}
break;
case DATEDAY:
if (value instanceof Date) {
org.joda.time.Days days = org.joda.time.Days.daysBetween(EPOCH,
new org.joda.time.DateTime(((Date) value).getTime()));
writer.writeDateDay(days.getDays());
}
else if (value instanceof LocalDate) {
int days = (int) ((LocalDate) value).toEpochDay();
writer.writeDateDay(days);
}
else if (value instanceof Long) {
writer.writeDateDay(((Long) value).intValue());
}
else {
writer.writeDateDay((int) value);
}
break;
case FLOAT8:
writer.float8().writeFloat8((double) value);
break;
case FLOAT4:
writer.float4().writeFloat4((float) value);
break;
case INT:
if (value != null && value instanceof Long) {
//This may seem odd at first but many frameworks (like Presto) use long as the preferred
//native java type for representing integers. We do this to keep type conversions simple.
writer.integer().writeInt(((Long) value).intValue());
}
else {
writer.integer().writeInt((int) value);
}
break;
case TINYINT:
writer.tinyInt().writeTinyInt((byte) value);
break;
case SMALLINT:
writer.smallInt().writeSmallInt((short) value);
break;
case UINT1:
writer.uInt1().writeUInt1((byte) value);
break;
case UINT2:
writer.uInt2().writeUInt2((char) value);
break;
case UINT4:
writer.uInt4().writeUInt4((int) value);
break;
case UINT8:
writer.uInt8().writeUInt8((long) value);
break;
case BIGINT:
writer.bigInt().writeBigInt((long) value);
break;
case VARBINARY:
if (value instanceof ArrowBuf) {
ArrowBuf buf = (ArrowBuf) value;
writer.varBinary().writeVarBinary(0, (int) (buf.capacity()), buf);
}
else if (value instanceof byte[]) {
byte[] bytes = (byte[]) value;
try (ArrowBuf buf = allocator.buffer(bytes.length)) {
buf.writeBytes(bytes);
writer.varBinary().writeVarBinary(0, (int) (buf.readableBytes()), buf);
}
}
break;
case DECIMAL:
int scale = ((ArrowType.Decimal) type).getScale();
if (value instanceof Double) {
int precision = ((ArrowType.Decimal) type).getPrecision();
BigDecimal bdVal = new BigDecimal((double) value);
bdVal = bdVal.setScale(scale, RoundingMode.HALF_UP);
writer.decimal().writeDecimal(bdVal);
}
else {
BigDecimal scaledValue = ((BigDecimal) value).setScale(scale, RoundingMode.HALF_UP);
writer.decimal().writeDecimal(scaledValue);
}
break;
case VARCHAR:
if (value instanceof ArrowBuf) {
ArrowBuf buf = (ArrowBuf) value;
writer.varChar().writeVarChar(0, (int) (buf.readableBytes()), buf);
}
else if (value instanceof byte[]) {
byte[] bytes = (byte[]) value;
try (ArrowBuf buf = allocator.buffer(bytes.length)) {
buf.writeBytes(bytes);
writer.varChar().writeVarChar(0, (int) (buf.readableBytes()), buf);
}
}
else {
// always fall back to the object's toString()
byte[] bytes = value.toString().getBytes(Charsets.UTF_8);
try (ArrowBuf buf = allocator.buffer(bytes.length)) {
buf.writeBytes(bytes);
writer.varChar().writeVarChar(0, (int) (buf.readableBytes()), buf);
}
}
break;
case BIT:
if (value instanceof Integer && (int) value > 0) {
writer.bit().writeBit(1);
}
else if (value instanceof Boolean && (boolean) value) {
writer.bit().writeBit(1);
}
else {
writer.bit().writeBit(0);
}
break;
default:
throw new IllegalArgumentException("Unknown type " + type);
}
}
catch (RuntimeException ex) {
String fieldName = (writer.getField() != null) ? writer.getField().getName() : "null_vector";
throw new RuntimeException("Unable to write value for field " + fieldName + " using value " + value, ex);
}
}