@qrvey/formula-lang
Version:
QFormula support for qrvey projects
65 lines • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MILLISECOND = void 0;
const scripts_1 = require("../utils/elasticsearch/scripts");
const utils_1 = require("../utils");
const constants_1 = require("../constants");
/**
* Returns the millisecond of a datetime value. The millisecond is given as an integer ranging from 0 to 999
*/
exports.MILLISECOND = {
identifier: 'MILLISECOND',
operationScope: constants_1.OPERATION_SCOPE.RAW,
functionScope: [constants_1.OPERATION_SCOPE.RAW],
parameters: [
{
identifier: 'DATE',
optional: false,
expectedPrimitive: constants_1.AST_PRIMITIVES.DATE,
validator: [utils_1.isDateParam],
},
],
transpiler: {
elasticsearch,
snowflake,
redshift,
postgresql,
databricks,
},
primitiveResult: constants_1.AST_PRIMITIVES.NUMBER,
};
function elasticsearch(value) {
return (0, scripts_1.millisecondScript)(value);
}
function snowflake(value) {
/**
* Extracts the nanosecond value from a given date and divides it by 1,000,000.
*/
return `(EXTRACT(nanosecond FROM ${value}) / 1000000)`;
}
function redshift(value) {
/*
Operation that calculates the milliseconds of a given date value.
Milliseconds - (seconds * 1000)
ex.
2500 - (2 * 1000) = 500
*/
return `(DATE_PART(milliseconds, ${value}) - (FLOOR(DATE_PART(seconds, ${value})) * 1000))`;
}
function postgresql(value) {
/*
Operation that calculates the milliseconds of a given date value.
Milliseconds - (seconds * 1000)
ex.
2500 - (2 * 1000) = 500
*/
return `(EXTRACT(milliseconds FROM ${value}) - (FLOOR(EXTRACT(seconds FROM ${value})) * 1000))`;
}
function databricks(value) {
/*
In Databricks, the DATE_FORMAT function extracts the milliseconds (SSS) from the timestamp.
For example, '2025-01-03 07:37:56.642' will return 642 as the milliseconds.
*/
return `CAST(DATE_FORMAT(${value}, 'SSS') AS INT)`;
}
//# sourceMappingURL=millisecond.js.map