|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
import dayjs from 'dayjs'; |
|
|
|
|
import { MapFnArgs } from '../mapFunctionName'; |
|
|
|
|
import commonFns from './commonFns'; |
|
|
|
|
import { convertUnits } from '../helpers/convertUnits'; |
|
|
|
|
import { getWeekdayByText } from '../helpers/formulaFnHelper'; |
|
|
|
|
|
|
|
|
|
const pg = { |
|
|
|
@ -50,6 +51,53 @@ const pg = {
|
|
|
|
|
)}')::interval${colAlias}` |
|
|
|
|
); |
|
|
|
|
}, |
|
|
|
|
DATETIME_DIFF: ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
|
const datetime_expr1 = fn(pt.arguments[0]); |
|
|
|
|
const datetime_expr2 = fn(pt.arguments[1]); |
|
|
|
|
const rawUnit = pt.arguments[2] |
|
|
|
|
? fn(pt.arguments[2]).bindings[0] |
|
|
|
|
: 'seconds'; |
|
|
|
|
let sql; |
|
|
|
|
const unit = convertUnits(rawUnit, 'pg'); |
|
|
|
|
switch (unit) { |
|
|
|
|
case 'second': |
|
|
|
|
sql = `EXTRACT(EPOCH from (${datetime_expr1}::TIMESTAMP - ${datetime_expr2}::TIMESTAMP))::INTEGER`; |
|
|
|
|
break; |
|
|
|
|
case 'minute': |
|
|
|
|
sql = `EXTRACT(EPOCH from (${datetime_expr1}::TIMESTAMP - ${datetime_expr2}::TIMESTAMP))::INTEGER / 60`; |
|
|
|
|
break; |
|
|
|
|
case 'milliseconds': |
|
|
|
|
sql = `EXTRACT(EPOCH from (${datetime_expr1}::TIMESTAMP - ${datetime_expr2}::TIMESTAMP))::INTEGER / 3600`; |
|
|
|
|
break; |
|
|
|
|
case 'week': |
|
|
|
|
sql = `TRUNC(DATE_PART('day', ${datetime_expr1}::TIMESTAMP - ${datetime_expr2}::TIMESTAMP) / 7)`; |
|
|
|
|
break; |
|
|
|
|
case 'month': |
|
|
|
|
sql = `(
|
|
|
|
|
DATE_PART('year', ${datetime_expr1}::TIMESTAMP) -
|
|
|
|
|
DATE_PART('year', ${datetime_expr2}::TIMESTAMP) |
|
|
|
|
) * 12 + ( |
|
|
|
|
DATE_PART('month', ${datetime_expr1}::TIMESTAMP) -
|
|
|
|
|
DATE_PART('month', ${datetime_expr2}::TIMESTAMP) |
|
|
|
|
)`;
|
|
|
|
|
break; |
|
|
|
|
case 'quarter': |
|
|
|
|
sql = `((EXTRACT(QUARTER FROM ${datetime_expr1}::TIMESTAMP) +
|
|
|
|
|
DATE_PART('year', AGE(${datetime_expr1},'1900/01/01')) * 4) - 1) -
|
|
|
|
|
((EXTRACT(QUARTER FROM ${datetime_expr2}::TIMESTAMP) +
|
|
|
|
|
DATE_PART('year', AGE(${datetime_expr2},'1900/01/01')) * 4) - 1)`;
|
|
|
|
|
break; |
|
|
|
|
case 'year': |
|
|
|
|
sql = `DATE_PART('year', ${datetime_expr1}::TIMESTAMP) - DATE_PART('year', ${datetime_expr2}::TIMESTAMP)`; |
|
|
|
|
break; |
|
|
|
|
case 'day': |
|
|
|
|
sql = `DATE_PART('day', ${datetime_expr1}::TIMESTAMP - ${datetime_expr2}::TIMESTAMP)`; |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
sql = ''; |
|
|
|
|
} |
|
|
|
|
return knex.raw(`${sql} ${colAlias}`); |
|
|
|
|
}, |
|
|
|
|
WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => { |
|
|
|
|
// isodow: the day of the week as Monday (1) to Sunday (7)
|
|
|
|
|
// WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
|
|
|
|
|