@Override public String getBaseTypeName() throws SQLException { return baseType.getSqlTypeName(); }
public LiteralParseNode(Object value, PDataType type) { this.type = type; // This will make the value null if the value passed through represents null for the given type. // For example, an empty string is treated as a null. this.value = this.type == null ? null : this.type.toObject(value, this.type); }
public static PDataType arrayBaseType(PDataType arrayType) { Preconditions.checkArgument(arrayType.isArrayType(), "Not a phoenix array type"); return fromTypeId(arrayType.getSqlType() - ARRAY_TYPE_BASE); } }
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 public boolean isCastableTo(PDataType targetType) { // Allow cast to BOOLEAN so it can be used in an index or group by return super.isCastableTo(targetType) || targetType.equals(PDecimal.INSTANCE); }
public static int sqlArrayType(String sqlTypeName) { PDataType fromSqlTypeName = fromSqlTypeName(sqlTypeName); return fromSqlTypeName.getSqlType() + PDataType.ARRAY_TYPE_BASE; }
@Test public void testForNullsAllNulls() throws Exception { Object element = null; PDataType baseType = PChar.INSTANCE; PhoenixArray arr = null; PhoenixArray expected = null; test(arr, element, PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE), 4, null, baseType, 1, null, expected, SortOrder.ASC, SortOrder.DESC); }
@Override public void toSQL(ColumnResolver resolver, StringBuilder buf) { List<ParseNode> children = getChildren(); buf.append(" CAST("); children.get(0).toSQL(resolver, buf); buf.append(" AS "); boolean isArray = dt.isArrayType(); PDataType type = isArray ? PDataType.arrayBaseType(dt) : dt; buf.append(type.getSqlTypeName()); if (maxLength != null) { buf.append('('); buf.append(maxLength); if (scale != null) { buf.append(','); buf.append(scale); // has both max length and scale. For ex- decimal(10,2) } buf.append(')'); } if (isArray) { buf.append(' '); buf.append(PDataType.ARRAY_TYPE_SUFFIX); } buf.append(")"); } }
@Override public byte[] getViewConstant() { PDataType dataType = autoPartitionCol.getDataType(); Object val = dataType.toObject(autoPartitionNum, PLong.INSTANCE); byte[] bytes = new byte [dataType.getByteSize() + 1]; dataType.toBytes(val, bytes, 0); return bytes; } @Override
String argType = arg.getArgumentType(); boolean isArrayType = arg.hasIsArrayType()?arg.getIsArrayType():false; PDataType dataType = isArrayType ? PDataType.fromTypeId(PDataType .sqlArrayType(SchemaUtil.normalizeIdentifier(SchemaUtil .normalizeIdentifier(argType)))) : PDataType .fromSqlTypeName(SchemaUtil.normalizeIdentifier(argType)); boolean isConstant = arg.hasIsConstant()?arg.getIsConstant():false; String defaultValue = arg.hasDefaultValue()?arg.getDefaultValue():null; String maxValue = arg.hasMaxValue()?arg.getMaxValue():null; args.add(new FunctionArgument(argType, isArrayType, isConstant, defaultValue == null ? null : LiteralExpression.newConstant((new LiteralParseNode(dataType.toObject(defaultValue))).getValue()), minValue == null ? null : LiteralExpression.newConstant((new LiteralParseNode(dataType.toObject(minValue))).getValue()), maxValue == null ? null : LiteralExpression.newConstant((new LiteralParseNode(dataType.toObject(maxValue))).getValue())));
public LiteralParseNode literal(String value, String sqlTypeName) throws SQLException { PDataType expectedType = sqlTypeName == null ? null : PDataType.fromSqlTypeName(SchemaUtil.normalizeIdentifier(sqlTypeName)); if (expectedType == null || !expectedType.isCoercibleTo(PTimestamp.INSTANCE)) { throw TypeMismatchException.newException(expectedType, PTimestamp.INSTANCE); } Object typedValue = expectedType.toObject(value); return new LiteralParseNode(typedValue); }
/** * Helper method to create a {@link Array} for a specific {@link PDataType}, and set it on * the provided {@code stmt}. */ private static void setArrayInStatement(PreparedStatement stmt, PDataType<?> type, Object[] obj, int position) throws SQLException { Array sqlArray = stmt.getConnection().createArrayOf( PDataType.arrayBaseType(type).getSqlTypeName(), obj); stmt.setArray(position, sqlArray); }
public boolean isNull(int pos) { if(this.baseType.toBytes(((Object[])array)[pos]).length == 0) { return true; } else { return false; } }
public static String getSqlTypeName(PDataType dataType, Integer maxLength, Integer scale) { return dataType.isArrayType() ? getArraySqlTypeName(maxLength, scale, dataType) : appendMaxLengthAndScale(maxLength, scale, dataType.getSqlTypeName()); }
@Override public boolean isCoercibleTo(PDataType targetType) { return super.isCoercibleTo(targetType) || targetType.equals(PBinary.INSTANCE); }
public static String toString(PDataType type, byte[] value, int offset, int length) { boolean isString = type.isCoercibleTo(PVarchar.INSTANCE); return isString ? ("'" + type.toObject(value).toString() + "'") : type.toObject(value, offset, length).toString(); }
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(); } }
/** * @return true when {@code lhs} equals any of {@code rhs}. */ public static boolean equalsAny(PDataType lhs, PDataType... rhs) { for (int i = 0; i < rhs.length; i++) { if (lhs.equals(rhs[i])) return true; } return false; }
@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)); } } }