Github user JamesRTaylor commented on a diff in the pull request:
https://github.com/apache/phoenix/pull/76#discussion_r29200389
--- Diff:
phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarchar.java ---
@@ -30,134 +31,145 @@
public class PVarchar extends PDataType<String> {
- public static final PVarchar INSTANCE = new PVarchar();
-
- private PVarchar() {
- super("VARCHAR", Types.VARCHAR, String.class, null, 0);
- }
-
- @Override
- public byte[] toBytes(Object object) {
- // TODO: consider using avro UTF8 object instead of String
- // so that we get get the size easily
- if (object == null) {
- return ByteUtil.EMPTY_BYTE_ARRAY;
- }
- return Bytes.toBytes((String) object);
- }
-
- @Override
- public int toBytes(Object object, byte[] bytes, int offset) {
- if (object == null) {
- return 0;
- }
- byte[] b = toBytes(object); // TODO: no byte[] allocation: use
CharsetEncoder
- System.arraycopy(b, 0, bytes, offset, b.length);
- return b.length;
- }
-
- @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);
- }
- if (length == 0) {
- return null;
- }
- if (sortOrder == SortOrder.DESC) {
- bytes = SortOrder.invert(bytes, offset, length);
- offset = 0;
- }
- return Bytes.toString(bytes, offset, length);
- }
-
- @Override
- public Object toObject(Object object, PDataType actualType) {
- if (equalsAny(actualType, this, PChar.INSTANCE)) {
- String s = (String) object;
- return s == null || s.length() > 0 ? s : null;
- }
- return throwConstraintViolationException(actualType, this);
- }
-
- @Override
- public boolean isCoercibleTo(PDataType targetType) {
- return equalsAny(targetType, this, PChar.INSTANCE,
PVarbinary.INSTANCE, PBinary.INSTANCE);
- }
-
- @Override
- public boolean isCoercibleTo(PDataType targetType, Object value) {
- if (isCoercibleTo(targetType)) {
- if (targetType.equals(PChar.INSTANCE)) {
- return value != null;
- }
- return true;
- }
- return false;
- }
-
- @Override
- public boolean isSizeCompatible(ImmutableBytesWritable ptr, Object
value, PDataType srcType,
- Integer maxLength, Integer scale, Integer desiredMaxLength,
- Integer desiredScale) {
- if (ptr.getLength() != 0 && maxLength != null && desiredMaxLength !=
null) {
- return maxLength <= desiredMaxLength;
- }
- return true;
- }
-
- @Override
- public boolean isFixedWidth() {
- return false;
- }
-
- @Override
- public int estimateByteSize(Object o) {
- String value = (String) o;
- return value == null ? 1 : value.length();
- }
-
- @Override
- public Integer getByteSize() {
- return null;
- }
-
- @Override
- public int compareTo(Object lhs, Object rhs, PDataType rhsType) {
- return ((String) lhs).compareTo((String) rhs);
- }
-
- @Override
- public Object toObject(String value) {
- return value;
- }
-
- @Override
- public boolean isBytesComparableWith(PDataType otherType) {
- return super.isBytesComparableWith(otherType) || otherType ==
PChar.INSTANCE;
- }
-
- @Override
- public String toStringLiteral(Object o, Format formatter) {
- if (formatter != null) {
- return "'" + formatter.format(o) + "'";
- }
- return "'" + StringUtil.escapeStringConstant(o.toString()) + "'";
- }
-
- private char[] sampleChars = new char[1];
-
- @Override
- public Object getSampleValue(Integer maxLength, Integer arrayLength) {
- Preconditions.checkArgument(maxLength == null || maxLength >= 0);
- int length = maxLength != null ? maxLength : 1;
- if (length != sampleChars.length) {
- sampleChars = new char[length];
- }
- for (int i = 0; i < length; i++) {
- sampleChars[i] = (char) RANDOM.get().nextInt(Byte.MAX_VALUE);
- }
- return new String(sampleChars);
- }
+ public static final PVarchar INSTANCE = new PVarchar();
--- End diff --
Minor nit: indenting looks off here. Should be no tabs and 4 space chars.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---