use of org.apache.phoenix.expression.function.ToCharFunction 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);
}
use of org.apache.phoenix.expression.function.ToCharFunction in project phoenix by apache.
the class SortOrderExpressionTest method toChar.
@Test
public void toChar() throws Exception {
List<Expression> args = Lists.newArrayList(getInvertedLiteral(date(12, 11, 2001), PDate.INSTANCE));
evaluateAndAssertResult(new ToCharFunction(args, FunctionArgumentType.TEMPORAL, "", DateUtil.getDateFormatter("MM/dd/yy hh:mm a")), "12/11/01 12:00 AM");
}
Aggregations