Rafał Kuźmiński
09/30/2024, 9:07 AMclass Abs<T : Number>(
val expr: Expression<T>,
columnType: IColumnType<T>
) : Function<T>(columnType), WindowFunction<T?> {
override fun toQueryBuilder(queryBuilder: QueryBuilder): Unit = queryBuilder { append("ABS(", expr, ")") }
override fun over(): WindowFunctionDefinition<T?> {
return WindowFunctionDefinition(columnType, this)
}
}
fun <T : Number> ExpressionWithColumnType<T>.abs() = Abs(this, this.columnType)
My experiment with nullable type:
class AbsNullable<T : Number?>(
val expr: Expression<T>,
columnType: IColumnType<T>
) : Function<T>(columnType), WindowFunction<T?> {
override fun toQueryBuilder(queryBuilder: QueryBuilder): Unit = queryBuilder { append("ABS(", expr, ")") }
override fun over(): WindowFunctionDefinition<T?> {
return WindowFunctionDefinition(columnType, this)
}
}
fun <T : Number?> ExpressionWithColumnType<T>.abs() = AbsNullable(this, this.columnType)
But compiler throws error for columnType like on screenshot
Do you have any ideas how to configure functions for nullable columns? Or maybe is there built in function for absolute value in exposed?Chantal Loncle
10/01/2024, 12:17 AMAbsFunction
. But these classes are not set up to implement the WindowFunction
interface, which you seem to need.
This single function class works with both nullable and non-nullable columns for me:
class Abs<T : Number?>(
val expr: Expression<T>,
columnType: IColumnType<T & Any>
) : Function<T?>(columnType), WindowFunction<T?> {
override fun toQueryBuilder(
queryBuilder: QueryBuilder
) { queryBuilder { append("ABS(", expr, ")") } }
override fun over(): WindowFunctionDefinition<T?> {
return WindowFunctionDefinition(columnType, this)
}
}
fun <T : Number?> ExpressionWithColumnType<T>.abs(): Abs<T> = Abs(this, this.columnType)
Alternatively you could take a look at how our Min
and Max
classes are implemented.