private static boolean onlyAtEndType(Expression expression) { // Due to the encoding schema of these types, they may only be // used once in a group by and are located at the end of the // group by row key. PDataType type = getGroupByDataType(expression); return type.isArrayType() || type == PVarbinary.INSTANCE; }
@Override public Expression visit(ColumnParseNode node) throws SQLException { Expression expression = super.visit(node); if (expression.getDataType().isArrayType()) { Integer count = arrayExpressionCounts.get(expression); arrayExpressionCounts.put(expression, count != null ? (count + 1) : 1); } return expression; }
public static PDataType arrayBaseType(PDataType arrayType) { Preconditions.checkArgument(arrayType.isArrayType(), "Not a phoenix array type"); return fromTypeId(arrayType.getSqlType() - ARRAY_TYPE_BASE); } }
/** * Estimate the byte size from the type length. For example, for char, byte size would be the same as length. For * decimal, byte size would have no correlation with the length. */ public Integer estimateByteSizeFromLength(Integer length) { if (isFixedWidth()) { return getByteSize(); } if (isArrayType()) { return null; } // If not fixed width, default to say the byte size is the same as length. return length; }
public int estimateByteSize(Object o) { if (isFixedWidth()) { return getByteSize(); } if (isArrayType()) { PhoenixArray array = (PhoenixArray)o; int noOfElements = array.numElements; int totalVarSize = 0; for (int i = 0; i < noOfElements; i++) { totalVarSize += array.estimateByteSize(i); } return totalVarSize; } // Non fixed width types must override this throw new UnsupportedOperationException(); }
public static String getSqlTypeName(PDataType dataType, Integer maxLength, Integer scale) { return dataType.isArrayType() ? getArraySqlTypeName(maxLength, scale, dataType) : appendMaxLengthAndScale(maxLength, scale, dataType.getSqlTypeName()); }
public boolean isCoercibleTo(PDataType targetType, PDataType expectedTargetType) { if (!targetType.isArrayType()) { return false; } else { PDataType targetElementType = PDataType.fromTypeId(targetType.getSqlType() - PDataType.ARRAY_TYPE_BASE); PDataType expectedTargetElementType = PDataType.fromTypeId(expectedTargetType.getSqlType() - PDataType.ARRAY_TYPE_BASE); return expectedTargetElementType.isCoercibleTo(targetElementType); } }
@Override protected Function<Object, Object> createConversionFunction(PDataType dataType) { if (dataType.isArrayType()) { return new ArrayDatatypeConversionFunction( new ObjectToArrayConverter( conn, PDataType.fromTypeId(dataType.getSqlType() - PDataType.ARRAY_TYPE_BASE))); } else { return new SimpleDatatypeConversionFunction(dataType, this.conn); } }
@Override public PDataType getDataType() { PDataType baseType = getAggregatorExpression().getDataType().isArrayType() ? PVarbinary.INSTANCE : getAggregatorExpression().getDataType(); return PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE); }
@Override protected Function<String, Object> createConversionFunction(PDataType dataType) { if (dataType.isArrayType()) { return new ArrayDatatypeConversionFunction( new StringToArrayConverter( conn, arrayElementSeparator, PDataType.fromTypeId(dataType.getSqlType() - PDataType.ARRAY_TYPE_BASE))); } else { return new SimpleDatatypeConversionFunction(dataType, this.conn); } }
private void init() { ImmutableBytesWritable ptr = new ImmutableBytesWritable(); if (getLHSExpr().getDataType().isArrayType()) { isNullArray = ExpressionUtil.isNull(getLHSExpr(), ptr); } else { isNullArray = ExpressionUtil.isNull(getRHSExpr(), ptr); } }
@Override public PDataType getDataType() { if (getLHSExpr().getDataType().isArrayType()) { // Use array of RHS type if we have a null constant since otherwise we'd use binary return isNullArray ? getRHSExpr().getDataType().isArrayType() ? getRHSExpr().getDataType() : PDataType.fromTypeId(getRHSExpr().getDataType().getSqlType() + PDataType.ARRAY_TYPE_BASE) : getLHSExpr().getDataType(); } else { return isNullArray ? getLHSExpr().getDataType().isArrayType() ? getLHSExpr().getDataType() : PDataType.fromTypeId(getLHSExpr().getDataType().getSqlType() + PDataType.ARRAY_TYPE_BASE) : getRHSExpr().getDataType(); } }
@Override public Integer getMaxLength() { if (getLHSExpr().getDataType().isArrayType()) { // Use max length of RHS if we have a null constant since otherwise we'd use null (which breaks fixed types) return getMaxLength(isNullArray ? getRHSExpr() : getLHSExpr()); } else { return getMaxLength(isNullArray ? getLHSExpr() : getRHSExpr()); } }
public PDataType getRHSBaseType() { if (getRHSExpr().getDataType().isArrayType()) { // Use LHS type if we have a null constant to get the correct array type return isNullArray ? getLHSExpr().getDataType() : PDataType.arrayBaseType(getRHSExpr().getDataType()); } else { return getRHSExpr().getDataType(); } }
@Override public DistinctValueWithCountClientAggregator newClientAggregator() { PDataType baseType = getAggregatorExpression().getDataType().isArrayType() ? PVarbinary.INSTANCE : getAggregatorExpression().getDataType(); PDataType resultType = PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE); return new DistinctValueClientAggregator(getAggregatorExpression().getSortOrder(), baseType, resultType); }
public PDataType getLHSBaseType() { if (getLHSExpr().getDataType().isArrayType()) { // Use RHS type if we have a null constant to get the correct array type return isNullArray ? getRHSExpr().getDataType() : PDataType.arrayBaseType(getLHSExpr().getDataType()); } else { return getLHSExpr().getDataType(); } }
@Override public SortOrder getSortOrder() { if (getLHSExpr().getDataType().isArrayType()) { return isNullArray ? getRHSExpr().getSortOrder() : getLHSExpr().getSortOrder(); } else { return isNullArray ? getLHSExpr().getSortOrder() : getRHSExpr().getSortOrder(); } }
@Override public Integer getScale() { if (getLHSExpr().getDataType().isArrayType()) { return isNullArray ? getRHSExpr().getScale() : getLHSExpr().getScale(); } else { return isNullArray ? getLHSExpr().getScale() : getRHSExpr().getScale(); } }
@Test public void testIsCoercibleTo() { PDataTypeFactory typeFactory = PDataTypeFactory.getInstance(); for (PDataType<?> type : typeFactory.getTypes()) { if (type.isArrayType()) { Object arr = type.getSampleValue(); assertTrue(type.isCoercibleTo(type, arr)); } } }
@Test public void testCoerceExpressionSupportsCoercingAllPDataTypesToBinary() throws Exception { for(PDataType p : PDataType.values()) { if (!p.isArrayType()) { LiteralExpression v = LiteralExpression.newConstant( map.get(p.getJavaClass()), p); CoerceExpression e = new CoerceExpression(v, PBinary.INSTANCE); ImmutableBytesWritable ptr = new ImmutableBytesWritable(); e.evaluate(null, ptr); Object obj = e.getDataType().toObject(ptr); assertTrue("Coercing to BINARY failed for PDataType " + p, obj instanceof byte[]); } } }