Search in sources :

Example 41 with PDataType

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(")");
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType)

Example 42 with PDataType

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;
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) SQLException(java.sql.SQLException) DelegateSQLException(org.apache.phoenix.schema.DelegateSQLException) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) DelegateSQLException(org.apache.phoenix.schema.DelegateSQLException) PDataType(org.apache.phoenix.schema.types.PDataType) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Expression(org.apache.phoenix.expression.Expression) ConstraintViolationException(org.apache.phoenix.schema.ConstraintViolationException) ExpressionCompiler(org.apache.phoenix.compile.ExpressionCompiler) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo)

Example 43 with PDataType

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");
    }
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) FloorDateExpression(org.apache.phoenix.expression.function.FloorDateExpression) FloorDecimalExpression(org.apache.phoenix.expression.function.FloorDecimalExpression)

Example 44 with PDataType

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;
}
Also used : ArrayList(java.util.ArrayList) FunctionNotFoundException(org.apache.phoenix.schema.FunctionNotFoundException) PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) UDFExpression(org.apache.phoenix.expression.function.UDFExpression) FunctionExpression(org.apache.phoenix.expression.function.FunctionExpression)

Example 45 with PDataType

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);
        */
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType)

Aggregations

PDataType (org.apache.phoenix.schema.types.PDataType)152 Expression (org.apache.phoenix.expression.Expression)54 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)31 SortOrder (org.apache.phoenix.schema.SortOrder)29 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)21 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)18 Test (org.junit.Test)15 PDatum (org.apache.phoenix.schema.PDatum)12 BigDecimal (java.math.BigDecimal)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)11 List (java.util.List)10 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)10 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)10 SingleCellConstructorExpression (org.apache.phoenix.expression.SingleCellConstructorExpression)10 IOException (java.io.IOException)9 PreparedStatement (java.sql.PreparedStatement)7 Pair (org.apache.hadoop.hbase.util.Pair)7 Date (java.sql.Date)6 AndExpression (org.apache.phoenix.expression.AndExpression)6