UNPKG

@qrvey/formula-lang

Version:

QFormula support for qrvey projects

102 lines 4.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IF = void 0; const constants_1 = require("../constants"); const dictionary_1 = require("../errors/dictionary"); const isAggregate_1 = require("../utils/isAggregate"); const isBooleanExpression_1 = require("../utils/isBooleanExpression"); const primitiveFunctions_1 = require("../utils/primitiveFunctions"); /** * `IF` Returns one value if a logical expression is TRUE and another if it is FALSE. */ exports.IF = { identifier: 'IF', operationScope: constants_1.OPERATION_SCOPE.RAW, functionScope: [constants_1.OPERATION_SCOPE.RAW, constants_1.OPERATION_SCOPE.AGGREGATE], parameters: [ { identifier: 'LOGICAL_EXPRESSION', optional: false, expectedPrimitive: constants_1.AST_PRIMITIVES.BOOLEAN, validator: [isBooleanExpression_1.isBooleanExpression], }, { identifier: 'VALUE_IF_TRUE', optional: false, generic: true, validator: [samePrimitiveType], }, { identifier: 'VALUE_IF_FALSE', optional: false, generic: true, validator: [samePrimitiveType], }, ], transpiler: { elasticsearch, snowflake, redshift, postgresql, databricks, }, primitiveResult(args) { var _a, _b, _c, _d; const truePrimitive = (_b = (_a = args[1]) === null || _a === void 0 ? void 0 : _a.primitive) !== null && _b !== void 0 ? _b : constants_1.AST_PRIMITIVES.UNKNOWN; const falsePrimitive = (_d = (_c = args[2]) === null || _c === void 0 ? void 0 : _c.primitive) !== null && _d !== void 0 ? _d : null; if (!falsePrimitive) return truePrimitive; return (0, primitiveFunctions_1.inferPrimitive)(truePrimitive, falsePrimitive); }, }; function samePrimitiveType(_param, _dataType, context) { var _a, _b; const fPrimitive = (_a = context.fnNode.arguments[1]) === null || _a === void 0 ? void 0 : _a.primitive; const firstPrimitive = fPrimitive && (0, primitiveFunctions_1.isSinglePrimitive)(fPrimitive) ? fPrimitive : constants_1.AST_PRIMITIVES.UNKNOWN; const sPrimitive = (_b = context.fnNode.arguments[2]) === null || _b === void 0 ? void 0 : _b.primitive; const secondPrimitive = sPrimitive && (0, primitiveFunctions_1.isSinglePrimitive)(sPrimitive) ? sPrimitive : constants_1.AST_PRIMITIVES.UNKNOWN; const ifPrimitive = calculateIfPrimitive(firstPrimitive, secondPrimitive); const isAggregate = (0, isAggregate_1.isAggregateScope)(context.globalContext); const valid = isAggregate ? constants_1.EXPECTED_AGGREGATE_RESULT.includes(firstPrimitive) : firstPrimitive === secondPrimitive; if (!valid) return Object.assign(Object.assign({ valid }, dictionary_1.ERROR_DICTIONARY[dictionary_1.ERROR_LIST.inferredPrimitive]), { mismatchData: { primitive: isAggregate ? constants_1.EXPECTED_AGGREGATE_RESULT[0] : ifPrimitive, } }); return { valid }; } function calculateIfPrimitive(firstPrimitive, secondPrimitive) { if (firstPrimitive !== constants_1.AST_PRIMITIVES.UNKNOWN) return firstPrimitive; return secondPrimitive; } function elasticsearch(logicalExpression, valueIfTrue, valueIfFalse) { return `(${logicalExpression} ? ${valueIfTrue} : ${valueIfFalse})`; } function snowflake(logicalExpression, valueIfTrue, valueIfFalse) { return `IFF(${logicalExpression}, ${valueIfTrue}, ${valueIfFalse})`; } function sql(logicalExpression, valueIfTrue, valueIfFalse) { return ` (CASE WHEN ${logicalExpression} THEN ${valueIfTrue} ELSE ${valueIfFalse} END)`.trim(); } function redshift(logicalExpression, valueIfTrue, valueIfFalse) { return sql(logicalExpression, valueIfTrue, valueIfFalse); } function postgresql(logicalExpression, valueIfTrue, valueIfFalse) { return sql(logicalExpression, valueIfTrue, valueIfFalse); } function databricks(logicalExpression, valueIfTrue, valueIfFalse) { return sql(logicalExpression, valueIfTrue, valueIfFalse); } //# sourceMappingURL=if.js.map