@qrvey/formula-lang
Version:
QFormula support for qrvey projects
62 lines • 1.96 kB
JavaScript
import { millisecondScript } from '../utils/elasticsearch/scripts';
import { isDateParam } from '../utils';
import { AST_PRIMITIVES, OPERATION_SCOPE } from '../constants';
/**
* Returns the millisecond of a datetime value. The millisecond is given as an integer ranging from 0 to 999
*/
export const MILLISECOND = {
identifier: 'MILLISECOND',
operationScope: OPERATION_SCOPE.RAW,
functionScope: [OPERATION_SCOPE.RAW],
parameters: [
{
identifier: 'DATE',
optional: false,
expectedPrimitive: AST_PRIMITIVES.DATE,
validator: [isDateParam],
},
],
transpiler: {
elasticsearch,
snowflake,
redshift,
postgresql,
databricks,
},
primitiveResult: AST_PRIMITIVES.NUMBER,
};
function elasticsearch(value) {
return 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