private PBinaryArray() { super("BINARY ARRAY", PDataType.ARRAY_TYPE_BASE + PBinary.INSTANCE.getSqlType(), PhoenixArray.class, null, 28); }
@Override public byte[] toBytes(Object object, SortOrder sortOrder) { byte[] bytes = toBytes(object); if (sortOrder == SortOrder.DESC) { return SortOrder.invert(bytes, 0, new byte[bytes.length], 0, bytes.length); } return bytes; }
public static PDataType getIndexColumnDataType(boolean isNullable, PDataType dataType) { if (dataType == null || !isNullable || !dataType.isFixedWidth()) { return dataType; } // for fixed length numeric types and boolean if (dataType.isCastableTo(PDecimal.INSTANCE)) { return PDecimal.INSTANCE; } // for CHAR if (dataType.isCoercibleTo(PVarchar.INSTANCE)) { return PVarchar.INSTANCE; } if (PBinary.INSTANCE.equals(dataType)) { return PVarbinary.INSTANCE; } throw new IllegalArgumentException("Unsupported non nullable type " + dataType); }
@Override public byte[] toBytes(Object object) { // Delegate to VARBINARY if (object == null) { throw newIllegalDataException(this + " may not be null"); } return PVarbinary.INSTANCE.toBytes(object); }
@Override public boolean isCoercibleTo(PDataType targetType) { return equalsAny(targetType, this, PVarbinary.INSTANCE); }
@Override public void coerceBytes(ImmutableBytesWritable ptr, Object o, PDataType actualType, Integer actualMaxLength, Integer actualScale, SortOrder actualModifier, Integer desiredMaxLength, Integer desiredScale, SortOrder expectedModifier) { PVarbinary.INSTANCE.coerceBytes(ptr, o, actualType, actualMaxLength, actualScale, actualModifier, desiredMaxLength, desiredScale, expectedModifier); if (null != desiredMaxLength && null != expectedModifier) { pad(ptr, desiredMaxLength, expectedModifier); } }
@Override public Object toObject(byte[] bytes, int offset, int length, PDataType actualType, SortOrder sortOrder, Integer maxLength, Integer scale) { if (!actualType.isCoercibleTo(this)) { throwConstraintViolationException(actualType, this); } return PVarbinary.INSTANCE.toObject(bytes, offset, length, actualType, sortOrder); }
@Override public String toStringLiteral(Object o, Format formatter) { return toStringLiteral((byte[])o, 0, ((byte[]) o).length, formatter); }
@Test public void testExecute_InvalidType() throws Exception { byte[] binaryData=(byte[])PBinary.INSTANCE.getSampleValue(); String encodedBinaryData = Bytes.toString(Base64.getEncoder().encode(binaryData)); R recordWithInvalidType = createRecord(123L, "NameValue", "ThisIsNotANumber", Arrays.asList(1, 2, 3), true, encodedBinaryData, Timestamp.valueOf(TIMESTAMP_WITH_NANOS)); getUpsertExecutor().execute(recordWithInvalidType); verify(upsertListener).errorOnRecord(eq(recordWithInvalidType), any(Throwable.class)); verifyNoMoreInteractions(upsertListener); }
@Override public int toBytes(Object object, byte[] bytes, int offset) { if (object == null) { throw newIllegalDataException(this + " may not be null"); } return PVarbinary.INSTANCE.toBytes(object, bytes, offset); }
@Override public int compareTo(Object lhs, Object rhs, PDataType rhsType) { if (lhs == null && rhs == null) { return 0; } else if (lhs == null) { return -1; } else if (rhs == null) { return 1; } if (equalsAny(rhsType, PVarbinary.INSTANCE, PBinary.INSTANCE)) { return Bytes.compareTo((byte[]) lhs, (byte[]) rhs); } else { byte[] rhsBytes = rhsType.toBytes(rhs); return Bytes.compareTo((byte[]) lhs, rhsBytes); } }
@Override public void coerceBytes(ImmutableBytesWritable ptr, Object o, PDataType actualType, Integer actualMaxLength, Integer actualScale, SortOrder actualModifier, Integer desiredMaxLength, Integer desiredScale, SortOrder expectedModifier) { PVarbinary.INSTANCE.coerceBytes(ptr, o, actualType, actualMaxLength, actualScale, actualModifier, desiredMaxLength, desiredScale, expectedModifier); if (null != desiredMaxLength && null != expectedModifier) { pad(ptr, desiredMaxLength, expectedModifier); } }
@Override public Object toObject(byte[] bytes, int offset, int length, PDataType actualType, SortOrder sortOrder, Integer maxLength, Integer scale) { if (!actualType.isCoercibleTo(this)) { throwConstraintViolationException(actualType, this); } return PVarbinary.INSTANCE.toObject(bytes, offset, length, actualType, sortOrder); }
@Override public String toStringLiteral(Object o, Format formatter) { return toStringLiteral((byte[])o, 0, ((byte[]) o).length, formatter); }
@Test public void testExecute_TooManyFields() throws Exception { byte[] binaryData=(byte[])PBinary.INSTANCE.getSampleValue(); String encodedBinaryData = Bytes.toString(Base64.getEncoder().encode(binaryData)); R recordWithTooManyFields = createRecord(123L, "NameValue", 42, Arrays.asList(1, 2, 3), true, encodedBinaryData, Timestamp.valueOf(TIMESTAMP_WITH_NANOS), "garbage"); getUpsertExecutor().execute(recordWithTooManyFields); verify(upsertListener).upsertDone(1L); verifyNoMoreInteractions(upsertListener); verify(preparedStatement).setObject(1, Long.valueOf(123L)); verify(preparedStatement).setObject(2, "NameValue"); verify(preparedStatement).setObject(3, Integer.valueOf(42)); verify(preparedStatement).setObject(4, PArrayDataType.instantiatePhoenixArray(PInteger.INSTANCE, new Object[]{1,2,3})); verify(preparedStatement).setObject(5, Boolean.TRUE); verify(preparedStatement).setObject(6, binaryData); verify(preparedStatement).setObject(7, DateUtil.parseTimestamp(TIMESTAMP_WITH_NANOS)); verify(preparedStatement).execute(); verifyNoMoreInteractions(preparedStatement); }
private static String getAffectedDataTypes() { StringBuilder buf = new StringBuilder("(" + PVarchar.INSTANCE.getSqlType() + "," + + PChar.INSTANCE.getSqlType() + "," + + PBinary.INSTANCE.getSqlType() + "," + + PFloat.INSTANCE.getSqlType() + "," + + PDouble.INSTANCE.getSqlType() + "," + + PDecimal.INSTANCE.getSqlType() + "," ); for (PDataType type : PDataType.values()) { if (type.isArrayType()) { buf.append(type.getSqlType()); buf.append(','); } } buf.setCharAt(buf.length()-1, ')'); return buf.toString(); }
@Override public byte[] toBytes(Object object, SortOrder sortOrder) { byte[] bytes = toBytes(object); if (sortOrder == SortOrder.DESC) { return SortOrder.invert(bytes, 0, new byte[bytes.length], 0, bytes.length); } return bytes; }
@Override public int toBytes(Object object, byte[] bytes, int offset) { if (object == null) { throw newIllegalDataException(this + " may not be null"); } return PVarbinary.INSTANCE.toBytes(object, bytes, offset); }
@Override public boolean isCoercibleTo(PDataType targetType) { return equalsAny(targetType, this, PVarbinary.INSTANCE); }