@qrvey/formula-lang
Version:
QFormula support for qrvey projects
120 lines • 4.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DATEADD = void 0;
const constants_1 = require("../constants");
const utils_1 = require("../utils");
const greaterThanOrEqualToReference_1 = require("../utils/greaterThanOrEqualToReference");
const isAnAllowedValue_1 = require("../utils/isAnAllowedValue");
const isInteger_1 = require("../utils/isInteger");
/**
* `DATEADD` Adds a specific date part value to a given date.
*/
exports.DATEADD = {
identifier: 'DATEADD',
operationScope: constants_1.OPERATION_SCOPE.RAW,
functionScope: [constants_1.OPERATION_SCOPE.RAW],
parameters: [
{
identifier: 'DATE',
optional: false,
expectedPrimitive: constants_1.AST_PRIMITIVES.DATE,
validator: [utils_1.isDateParam],
},
{
identifier: 'UNIT',
optional: false,
expectedPrimitive: constants_1.AST_PRIMITIVES.STRING,
validator: [
utils_1.isStringParam,
(0, isAnAllowedValue_1.isAnAllowedValue)(['Y', 'M', 'D', 'H', 'MI', 'S']),
],
},
{
identifier: 'INTERVAL',
optional: false,
expectedPrimitive: constants_1.AST_PRIMITIVES.NUMBER,
validator: [
utils_1.isNumberParam,
(0, greaterThanOrEqualToReference_1.greaterThanOrEqualToReference)(0),
isInteger_1.isInteger,
],
},
],
transpiler: {
elasticsearch,
snowflake,
redshift,
postgresql,
databricks,
},
primitiveResult: constants_1.AST_PRIMITIVES.DATE,
};
function elasticsearch(date, unit, interval) {
unit = (0, utils_1.removeQuotes)(unit);
switch (unit) {
case 'Y':
return `${date}.plusYears((long) Math.floor(${interval}))`;
case 'M':
return `${date}.plusMonths((long) Math.floor(${interval}))`;
case 'D':
return `${date}.plusDays((long) Math.floor(${interval}))`;
case 'H':
return `${date}.plusHours((long) Math.floor(${interval}))`;
case 'MI':
return `${date}.plusMinutes((long) Math.floor(${interval}))`;
case 'S':
return `${date}.plusSeconds((long) Math.floor(${interval}))`;
default:
return '';
}
}
function SQL(date, unit, interval) {
unit = (0, utils_1.removeQuotes)(unit);
const sqlDateAdd = (_date, _unit, _interval) => `DATEADD(${_unit}, FLOOR(${_interval})::INTEGER, ${_date})`;
switch (unit) {
case 'Y':
return sqlDateAdd(date, `month`, `${interval} * 12`); // Year to month conversion to get the same result for leap years. Redshift behavior is different for leap year example `2016-02-29` https://docs.aws.amazon.com/redshift/latest/dg/r_DATEADD_function.html
case 'M':
return sqlDateAdd(date, `month`, interval);
case 'D':
return sqlDateAdd(date, `day`, interval);
case 'H':
return sqlDateAdd(date, `hour`, interval);
case 'MI':
return sqlDateAdd(date, `minute`, interval);
case 'S':
return sqlDateAdd(date, `second`, interval);
default:
return '';
}
}
function snowflake(date, unit, interval) {
return SQL(date, unit, interval);
}
function redshift(date, unit, interval) {
return SQL(date, unit, interval);
}
function postgresql(date, unit, interval) {
unit = (0, utils_1.removeQuotes)(unit);
const sqlDateAdd = (_date, _unit) => `${_date} + interval ${_unit}`;
switch (unit) {
case 'Y':
return sqlDateAdd(date, `'1 year' * FLOOR(${interval})::INTEGER`);
case 'M':
return sqlDateAdd(date, `'1 month' * FLOOR(${interval})::INTEGER`);
case 'D':
return sqlDateAdd(date, `'1 day' * FLOOR(${interval})::INTEGER`);
case 'H':
return sqlDateAdd(date, `'1 hour' * FLOOR(${interval})::INTEGER`);
case 'MI':
return sqlDateAdd(date, `'1 minute' * FLOOR(${interval})::INTEGER`);
case 'S':
return sqlDateAdd(date, `'1 second' * FLOOR(${interval})::INTEGER`);
default:
return '';
}
}
function databricks(date, unit, interval) {
return SQL(date, unit, interval);
}
//# sourceMappingURL=dateadd.js.map