Skip to content

ColumnFunction: use column returning expressions in select and by phrases

Larry White edited this page Jul 4, 2016 · 1 revision

Creating new columns using expressions

Sometimes you don't want the same columns or a subset of columns in the result set, but instead want a new column formed by evaluating an expression on one or more columns. For example, you might want to include the month that a sale occurred, rather than the exact date. This is currently possible using several steps:

Table filtered = aTable.selectWhere(column("Status").isEqualTo("Ok"));
Column month = filtered.dateColumn("startDate").month();
filtered.addColumn(month);
filtered.removeColumn("startDate");

However, it would be helpful to express this as part of the original query statement.

Current idea is to use a ColumnFunction. Here's an example using a theoretical built-in ColumnFunction called Month.

Table t = table.select(product, store, Month.of(salesDate)).where(some filter);

To make this work, we could implement an interface "Columnar" that would be implemented by both columns and ColumnFunctions:

public interface Columnar {
    default Column asColumn();
}

ColumnFunctions would override to call an execute function that returns the new derived column.

Clone this wiki locally