@qrvey/formula-lang
Version:
QFormula support for qrvey projects
70 lines • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ROUNDDOWN = void 0;
const constants_1 = require("../constants");
const utils_1 = require("../utils");
const isBetweenInclusiveRange_1 = require("../utils/isBetweenInclusiveRange");
const isInteger_1 = require("../utils/isInteger");
/**
* `ROUNDDOWN` Returns a number rounded to the next greatest even integer.
*/
exports.ROUNDDOWN = {
identifier: 'ROUNDDOWN',
operationScope: constants_1.OPERATION_SCOPE.RAW,
functionScope: [constants_1.OPERATION_SCOPE.RAW, constants_1.OPERATION_SCOPE.AGGREGATE],
parameters: [
{
identifier: 'NUM_1',
optional: false,
expectedPrimitive: constants_1.AST_PRIMITIVES.NUMBER,
validator: [utils_1.isNumberParam],
},
{
identifier: 'DIGITS',
optional: false,
expectedPrimitive: constants_1.AST_PRIMITIVES.NUMBER,
validator: [
utils_1.isNumberParam,
(0, isBetweenInclusiveRange_1.isBetweenInclusiveRange)(0, 10),
isInteger_1.isInteger,
],
},
],
transpiler: {
elasticsearch,
snowflake,
redshift,
postgresql,
databricks,
},
primitiveResult: constants_1.AST_PRIMITIVES.NUMBER,
};
function elasticsearch(value, digits) {
return `ROUNDDOWN(${value}, ROUNDDOWN(${digits}, 0))`;
}
function SQL(value, digits) {
return `((CASE
WHEN ${value} IS NULL OR ${digits} IS NULL THEN NULL
WHEN ${digits} > 10 THEN FLOOR(ABS(${value}) * POWER(10, 10)) / POWER(10, 10)
WHEN ${digits} < 0 THEN FLOOR(ABS(${value}))
ELSE FLOOR(ABS(${value}) * POWER(10, TRUNC(${digits})::INTEGER)) / POWER(10, TRUNC(${digits})::INTEGER)
END) * (CASE WHEN ${value} < 0 THEN -1 ELSE 1 END))`;
}
function snowflake(value, digits) {
return SQL(value, digits);
}
function redshift(value, digits) {
return SQL(value, digits);
}
function postgresql(value, digits) {
return SQL(value, digits);
}
function databricks(value, digits) {
return `((CASE
WHEN ${value} IS NULL OR ${digits} IS NULL THEN NULL
WHEN ${digits} > 10 THEN FLOOR(ABS(${value}) * POWER(10, 10)) / POWER(10, 10)
WHEN ${digits} < 0 THEN FLOOR(ABS(${value}))
ELSE FLOOR(ABS(${value}) * POWER(10, CAST(${digits} AS INTEGER))) / POWER(10, CAST(${digits} AS INTEGER))
END) * (CASE WHEN ${value} < 0 THEN -1 ELSE 1 END))`;
}
//# sourceMappingURL=rounddown.js.map