@qrvey/formula-lang
Version:
QFormula support for qrvey projects
65 lines • 2.05 kB
JavaScript
import { AST_PRIMITIVES, OPERATION_SCOPE } from '../constants';
import { isNumberParam } from '../utils';
import { greaterThanReference } from '../utils/greaterThanReference';
import { fromFloatToNumeric } from '../utils/sql';
/**
* `LOG` Returns a number rounded to the next greatest even integer.
*/
export const LOG = {
identifier: 'LOG',
operationScope: OPERATION_SCOPE.RAW,
functionScope: [OPERATION_SCOPE.RAW, OPERATION_SCOPE.AGGREGATE],
parameters: [
{
identifier: 'NUM_1',
optional: false,
expectedPrimitive: AST_PRIMITIVES.NUMBER,
validator: [isNumberParam, greaterThanReference(0)],
},
{
identifier: 'BASE',
optional: true,
expectedPrimitive: AST_PRIMITIVES.NUMBER,
validator: [isNumberParam, greaterThanReference(1)],
},
],
transpiler: {
elasticsearch,
snowflake,
redshift,
postgresql,
databricks,
},
primitiveResult: 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) {
if (typeof base === 'string')
base = fromFloatToNumeric(base);
return SQL(fromFloatToNumeric(value), base);
}
function databricks(value, base) {
return SQL(value, base);
}
//# sourceMappingURL=log.js.map