UNPKG

@qrvey/formula-lang

Version:

QFormula support for qrvey projects

68 lines 2.28 kB
"use strict"; 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"); const sql_1 = require("../utils/sql"); /** * `LOG` Returns a number rounded to the next greatest even integer. */ exports.LOG = { identifier: 'LOG', 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, (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, databricks, }, 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) { if (typeof base === 'string') base = (0, sql_1.fromFloatToNumeric)(base); return SQL((0, sql_1.fromFloatToNumeric)(value), base); } function databricks(value, base) { return SQL(value, base); } //# sourceMappingURL=log.js.map