use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class CastParseNode method toSQL.
@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(',');
// has both max length and scale. For ex- decimal(10,2)
buf.append(scale);
}
buf.append(')');
}
if (isArray) {
buf.append(' ');
buf.append(PDataType.ARRAY_TYPE_SUFFIX);
}
buf.append(")");
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class ColumnDef method validateDefault.
public boolean validateDefault(StatementContext context, PrimaryKeyConstraint pkConstraint) throws SQLException {
String defaultStr = this.getExpression();
if (defaultStr == null) {
return true;
}
ExpressionCompiler compiler = new ExpressionCompiler(context);
ParseNode defaultParseNode = new SQLParser(this.getExpression()).parseExpression();
Expression defaultExpression = defaultParseNode.accept(compiler);
if (!defaultParseNode.isStateless() || defaultExpression.getDeterminism() != Determinism.ALWAYS) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CREATE_DEFAULT).setColumnName(this.getColumnDefName().getColumnName()).build().buildException();
}
if (this.isRowTimestamp() || (pkConstraint != null && pkConstraint.isColumnRowTimestamp(this.getColumnDefName()))) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CREATE_DEFAULT_ROWTIMESTAMP).setColumnName(this.getColumnDefName().getColumnName()).build().buildException();
}
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
// Evaluate the expression to confirm it's validity
LiteralExpression defaultValue = ExpressionUtil.getConstantExpression(defaultExpression, ptr);
// A DEFAULT that evaluates to null should be ignored as it adds nothing
if (defaultValue.getValue() == null) {
return false;
}
PDataType sourceType = defaultExpression.getDataType();
PDataType targetType = this.getDataType();
// Ensure that coercion works (will throw if not)
context.getTempPtr().set(ptr.get(), ptr.getOffset(), ptr.getLength());
try {
targetType.coerceBytes(context.getTempPtr(), defaultValue.getValue(), sourceType, defaultValue.getMaxLength(), defaultValue.getScale(), defaultValue.getSortOrder(), this.getMaxLength(), this.getScale(), this.getSortOrder());
} catch (ConstraintViolationException e) {
if (e.getCause() instanceof SQLException) {
SQLException sqlE = (SQLException) e.getCause();
throw new DelegateSQLException(sqlE, ". DEFAULT " + SQLExceptionInfo.COLUMN_NAME + "=" + this.getColumnDefName().getColumnName());
}
throw e;
}
if (!targetType.isSizeCompatible(ptr, defaultValue.getValue(), sourceType, sortOrder, defaultValue.getMaxLength(), defaultValue.getScale(), this.getMaxLength(), this.getScale())) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.DATA_EXCEEDS_MAX_CAPACITY).setColumnName(this.getColumnDefName().getColumnName()).setMessage("DEFAULT " + this.getExpression()).build().buildException();
}
return true;
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class FloorParseNode method getFloorExpression.
public static Expression getFloorExpression(List<Expression> children) throws SQLException {
final Expression firstChild = children.get(0);
final PDataType firstChildDataType = firstChild.getDataType();
//Which is exactly what FloorDateExpression does too.
if (firstChildDataType.isCoercibleTo(PTimestamp.INSTANCE)) {
return FloorDateExpression.create(children);
} else if (firstChildDataType.isCoercibleTo(PDecimal.INSTANCE)) {
return FloorDecimalExpression.create(children);
} else {
throw TypeMismatchException.newException(firstChildDataType, "1");
}
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class FunctionParseNode method validate.
public List<Expression> validate(List<Expression> children, StatementContext context) throws SQLException {
BuiltInFunctionInfo info = this.getInfo();
BuiltInFunctionArgInfo[] args = info.getArgs();
if (args.length < children.size() || info.getRequiredArgCount() > children.size()) {
throw new FunctionNotFoundException(this.name);
}
if (args.length > children.size()) {
List<Expression> moreChildren = new ArrayList<Expression>(children);
for (int i = children.size(); i < info.getArgs().length; i++) {
moreChildren.add(LiteralExpression.newConstant(null, args[i].allowedTypes.length == 0 ? null : PDataTypeFactory.getInstance().instanceFromClass(args[i].allowedTypes[0]), Determinism.ALWAYS));
}
children = moreChildren;
}
List<ParseNode> nodeChildren = this.getChildren();
for (int i = 0; i < children.size(); i++) {
BindParseNode bindNode = null;
Class<? extends PDataType>[] allowedTypes = args[i].getAllowedTypes();
// values.
if (i < nodeChildren.size() && nodeChildren.get(i) instanceof BindParseNode) {
bindNode = (BindParseNode) nodeChildren.get(i);
}
// If the child type is null, then the expression is unbound.
// Skip any validation, since we either 1) have a default value
// which has already been validated, 2) attempting to get the
// parameter metadata, or 3) have an unbound parameter which
// will be detected futher downstream.
Expression child = children.get(i);
if (child.getDataType() == null || /* null used explicitly in query */
i >= nodeChildren.size()) /* argument left off */
{
// Replace the unbound expression with the default value expression if specified
if (args[i].getDefaultValue() != null) {
Expression defaultValue = args[i].getDefaultValue();
children.set(i, defaultValue);
// Set the parameter metadata if this is a bind parameter
if (bindNode != null) {
context.getBindManager().addParamMetaData(bindNode, defaultValue);
}
} else if (bindNode != null) {
// based on the function argument annonation set the parameter meta data.
if (child.getDataType() == null) {
if (allowedTypes.length > 0) {
context.getBindManager().addParamMetaData(bindNode, LiteralExpression.newConstant(null, PDataTypeFactory.getInstance().instanceFromClass(allowedTypes[0]), Determinism.ALWAYS));
}
} else {
// Use expression as is, since we already have the data type set
context.getBindManager().addParamMetaData(bindNode, child);
}
}
} else {
validateFunctionArguement(info, i, child);
}
}
return children;
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class ParseNodeFactory method literal.
public LiteralParseNode literal(Object value, PDataType expectedType) throws SQLException {
PDataType actualType = PDataType.fromLiteral(value);
if (actualType != null && actualType != expectedType) {
checkTypeMatch(expectedType, actualType);
value = expectedType.toObject(value, actualType);
}
return new LiteralParseNode(value);
/*
Object typedValue = expectedType.toObject(value.toString());
return new LiteralParseNode(typedValue);
*/
}
Aggregations