Search in sources :

Example 1 with FunctionArgumentType

use of org.apache.phoenix.expression.function.FunctionArgumentType in project phoenix by apache.

the class ToCharParseNode method create.

@Override
public FunctionExpression create(List<Expression> children, StatementContext context) throws SQLException {
    PDataType dataType = children.get(0).getDataType();
    // either date or number format string
    String formatString = (String) ((LiteralExpression) children.get(1)).getValue();
    Format formatter;
    FunctionArgumentType type;
    if (dataType.isCoercibleTo(PTimestamp.INSTANCE)) {
        if (formatString == null) {
            formatString = context.getDateFormat();
            formatter = context.getDateFormatter();
        } else {
            formatter = FunctionArgumentType.TEMPORAL.getFormatter(formatString);
        }
        type = FunctionArgumentType.TEMPORAL;
    } else if (dataType.isCoercibleTo(PDecimal.INSTANCE)) {
        if (formatString == null)
            formatString = context.getNumberFormat();
        formatter = FunctionArgumentType.NUMERIC.getFormatter(formatString);
        type = FunctionArgumentType.NUMERIC;
    } else {
        throw new SQLException(dataType + " type is unsupported for TO_CHAR().  Numeric and temporal types are supported.");
    }
    return new ToCharFunction(children, type, formatString, formatter);
}
Also used : Format(java.text.Format) PDataType(org.apache.phoenix.schema.types.PDataType) SQLException(java.sql.SQLException) FunctionArgumentType(org.apache.phoenix.expression.function.FunctionArgumentType) ToCharFunction(org.apache.phoenix.expression.function.ToCharFunction)

Example 2 with FunctionArgumentType

use of org.apache.phoenix.expression.function.FunctionArgumentType in project phoenix by apache.

the class ToNumberParseNode method create.

@Override
public FunctionExpression create(List<Expression> children, StatementContext context) throws SQLException {
    PDataType dataType = children.get(0).getDataType();
    // either date or number format string
    String formatString = (String) ((LiteralExpression) children.get(1)).getValue();
    Format formatter = null;
    FunctionArgumentType type;
    if (dataType.isCoercibleTo(PTimestamp.INSTANCE)) {
        if (formatString == null) {
            formatString = context.getDateFormat();
            formatter = context.getDateFormatter();
        } else {
            formatter = FunctionArgumentType.TEMPORAL.getFormatter(formatString);
        }
        type = FunctionArgumentType.TEMPORAL;
    } else if (dataType.isCoercibleTo(PChar.INSTANCE)) {
        if (formatString != null) {
            formatter = FunctionArgumentType.CHAR.getFormatter(formatString);
        }
        type = FunctionArgumentType.CHAR;
    } else {
        throw new SQLException(dataType + " type is unsupported for TO_NUMBER().  Numeric and temporal types are supported.");
    }
    return new ToNumberFunction(children, type, formatString, formatter);
}
Also used : Format(java.text.Format) PDataType(org.apache.phoenix.schema.types.PDataType) SQLException(java.sql.SQLException) FunctionArgumentType(org.apache.phoenix.expression.function.FunctionArgumentType) ToNumberFunction(org.apache.phoenix.expression.function.ToNumberFunction)

Aggregations

SQLException (java.sql.SQLException)2 Format (java.text.Format)2 FunctionArgumentType (org.apache.phoenix.expression.function.FunctionArgumentType)2 PDataType (org.apache.phoenix.schema.types.PDataType)2 ToCharFunction (org.apache.phoenix.expression.function.ToCharFunction)1 ToNumberFunction (org.apache.phoenix.expression.function.ToNumberFunction)1