@qrvey/formula-lang
Version:
QFormula support for qrvey projects
50 lines • 1.46 kB
JavaScript
import { AST_PRIMITIVES } from '../constants';
import { isNumberParam } from '../utils';
import { isAValidPower } from '../utils/isAValidPower';
/**
* `POWER` Returns the value of the first argument raised to the power of the second argument.
*/
export const POWER = {
identifier: 'POWER',
parameters: [
{
identifier: 'NUM_1',
optional: false,
expectedPrimitive: AST_PRIMITIVES.NUMBER,
validator: [isNumberParam],
},
{
identifier: 'NUM_2',
optional: false,
expectedPrimitive: AST_PRIMITIVES.NUMBER,
validator: [isNumberParam, isAValidPower],
},
],
transpiler: {
elasticsearch,
snowflake,
redshift,
postgresql,
},
primitiveResult: AST_PRIMITIVES.NUMBER,
};
function elasticsearch(value, numberToRaise) {
return `POWER(${value}, ${numberToRaise})`;
}
function SQL(value, numberToRaise) {
return `
(CASE
WHEN (${numberToRaise} IS NULL OR ${value} IS NULL) OR (${value} = 0 AND ${numberToRaise} < 0) THEN null
ELSE POWER(${value}, ${numberToRaise})
END)`.trim();
}
function snowflake(value, numberToRaise) {
return SQL(value, numberToRaise);
}
function redshift(value, numberToRaise) {
return SQL(value, numberToRaise);
}
function postgresql(value, numberToRaise) {
return SQL(value, numberToRaise);
}
//# sourceMappingURL=power.js.map