@qrvey/formula-lang
Version:
QFormula support for qrvey projects
59 lines • 1.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LOG = void 0;
const constants_1 = require("../constants");
const utils_1 = require("../utils");
const greaterThanReference_1 = require("../utils/greaterThanReference");
/**
* `LOG` Returns a number rounded to the next greatest even integer.
*/
exports.LOG = {
identifier: 'LOG',
parameters: [
{
identifier: 'NUM_1',
optional: false,
expectedPrimitive: constants_1.AST_PRIMITIVES.NUMBER,
validator: [utils_1.isNumberParam, (0, greaterThanReference_1.greaterThanReference)(0)],
},
{
identifier: 'BASE',
optional: true,
expectedPrimitive: constants_1.AST_PRIMITIVES.NUMBER,
validator: [utils_1.isNumberParam, (0, greaterThanReference_1.greaterThanReference)(1)],
},
],
transpiler: {
elasticsearch,
snowflake,
redshift,
postgresql,
},
primitiveResult: constants_1.AST_PRIMITIVES.NUMBER,
};
function elasticsearch(value, base) {
return `LOG(${base !== null && base !== void 0 ? base : '10'}, ${value})`;
}
function SQL(value, base) {
const _base = base !== null && base !== void 0 ? base : '10';
return `
(CASE
WHEN (${_base} IS NULL OR ${value} IS NULL) OR (${_base} <= 1 OR ${value} <= 0) THEN null
ELSE LOG(${_base}, ${value})
END)`.trim();
}
function snowflake(value, base) {
return SQL(value, base);
}
function redshift(value, base) {
const _base = base !== null && base !== void 0 ? base : '10';
return `
(CASE
WHEN (${_base} IS NULL OR ${value} IS NULL) OR (${_base} <= 1 OR ${value} <= 0) THEN null
ELSE (LN(${value}) / LN(${_base}))
END)`.trim();
}
function postgresql(value, base) {
return SQL(value, base);
}
//# sourceMappingURL=log.js.map