T-43: M1.1d — Pass 2 observed bar spans: read-only [first_bar_date, last_bar_date] per (source, source_id) + overlap helper
This commit is contained in:
parent
97907f7666
commit
627995c091
2 changed files with 282 additions and 63 deletions
|
|
@ -359,41 +359,115 @@ export function assertReadOnlyBarSpanPipeline(pipeline: readonly Record<string,
|
|||
}
|
||||
}
|
||||
|
||||
// Strict calendar-shape regexes for MongoDB `$regexMatch` (PCRE2) so `$dateFromString`
|
||||
// only parses the exact string shapes accepted by `normalizeBarDate`:
|
||||
// - exact `YYYY-MM-DD` (year 0001..9999)
|
||||
// - ISO-8601 timestamp with an explicit timezone (`Z` or `[+-]HH:MM` / `[+-]HHMM`)
|
||||
// Timezone-less datetimes (e.g. `2024-06-10T12:00:00`) are rejected before `$dateFromString`.
|
||||
const MONGO_VALID_YMD_REGEX =
|
||||
'^(?:000[1-9]|00[1-9]\\d|0[1-9]\\d{2}|[1-9]\\d{3})-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\\d|3[01])$';
|
||||
const MONGO_VALID_ISO_DATETIME_TZ_REGEX =
|
||||
'^(?:000[1-9]|00[1-9]\\d|0[1-9]\\d{2}|[1-9]\\d{3})-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\\d|3[01])[T ](?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d{1,9})?)?(?:Z|[+-](?:[01]\\d|2[0-3]):?[0-5]\\d)$';
|
||||
|
||||
/**
|
||||
* Builds a MongoDB aggregation expression that normalizes a raw date field
|
||||
* (BSON `Date` or ISO-8601 date/timestamp string) to a UTC `YYYY-MM-DD` string
|
||||
* before `$min`/`$max` comparison.
|
||||
* Builds a MongoDB aggregation expression that validates and normalizes a raw date field
|
||||
* (BSON `Date`, exact `YYYY-MM-DD` string, or timezone-qualified ISO-8601 timestamp string)
|
||||
* to a UTC `YYYY-MM-DD` string before `$min`/`$max` comparison.
|
||||
*
|
||||
* Normalizing before `$group` avoids MongoDB's BSON type comparison order
|
||||
* (strings sort before Dates), which would otherwise invert `[firstBarDate, lastBarDate]`
|
||||
* when a provider ID has mixed string and BSON Date bars.
|
||||
* - Normalizing before `$group` avoids MongoDB's BSON type comparison order
|
||||
* (strings sort before Dates), which would otherwise invert `[firstBarDate, lastBarDate]`
|
||||
* when a provider ID has mixed string and BSON Date bars.
|
||||
* - Gating strings with `$regexMatch` before `$dateFromString` prevents MongoDB from
|
||||
* silently accepting timezone-less timestamps (e.g. `2024-06-10T12:00:00`) or unpadded
|
||||
* dates (`2024-6-8`), while `$dateFromString` enforces strict Gregorian calendar rules
|
||||
* (rejecting rollover dates like `2023-02-29`) and converts explicit offsets to UTC.
|
||||
*/
|
||||
function buildNormalizedUtcDateExpr(dateField: string): Record<string, unknown> {
|
||||
const fieldRef = `$${dateField}`;
|
||||
return {
|
||||
$dateToString: {
|
||||
format: '%Y-%m-%d',
|
||||
timezone: 'UTC',
|
||||
onNull: null,
|
||||
date: {
|
||||
$switch: {
|
||||
branches: [
|
||||
{
|
||||
case: { $eq: [{ $type: fieldRef }, 'date'] },
|
||||
then: fieldRef,
|
||||
},
|
||||
{
|
||||
case: { $eq: [{ $type: fieldRef }, 'string'] },
|
||||
then: {
|
||||
$dateFromString: {
|
||||
dateString: { $trim: { input: fieldRef } },
|
||||
onError: null,
|
||||
onNull: null,
|
||||
const parsedDateExpr: Record<string, unknown> = {
|
||||
$switch: {
|
||||
branches: [
|
||||
{
|
||||
case: { $eq: [{ $type: fieldRef }, 'date'] },
|
||||
then: fieldRef,
|
||||
},
|
||||
{
|
||||
case: { $eq: [{ $type: fieldRef }, 'string'] },
|
||||
then: {
|
||||
$let: {
|
||||
vars: {
|
||||
trimmed: { $trim: { input: fieldRef } },
|
||||
},
|
||||
in: {
|
||||
$switch: {
|
||||
branches: [
|
||||
{
|
||||
case: {
|
||||
$regexMatch: {
|
||||
input: '$$trimmed',
|
||||
regex: MONGO_VALID_YMD_REGEX,
|
||||
},
|
||||
},
|
||||
then: {
|
||||
$dateFromString: {
|
||||
dateString: '$$trimmed',
|
||||
format: '%Y-%m-%d',
|
||||
timezone: 'UTC',
|
||||
onError: null,
|
||||
onNull: null,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
case: {
|
||||
$regexMatch: {
|
||||
input: '$$trimmed',
|
||||
regex: MONGO_VALID_ISO_DATETIME_TZ_REGEX,
|
||||
},
|
||||
},
|
||||
then: {
|
||||
$dateFromString: {
|
||||
dateString: '$$trimmed',
|
||||
onError: null,
|
||||
onNull: null,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
default: null,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
default: null,
|
||||
},
|
||||
},
|
||||
],
|
||||
default: null,
|
||||
},
|
||||
};
|
||||
|
||||
return {
|
||||
$let: {
|
||||
vars: {
|
||||
parsedDate: parsedDateExpr,
|
||||
},
|
||||
in: {
|
||||
$cond: {
|
||||
if: {
|
||||
$and: [
|
||||
{ $eq: [{ $type: '$$parsedDate' }, 'date'] },
|
||||
{ $gte: [{ $year: { date: '$$parsedDate', timezone: 'UTC' } }, 1] },
|
||||
{ $lte: [{ $year: { date: '$$parsedDate', timezone: 'UTC' } }, 9999] },
|
||||
],
|
||||
},
|
||||
then: {
|
||||
$dateToString: {
|
||||
format: '%Y-%m-%d',
|
||||
timezone: 'UTC',
|
||||
date: '$$parsedDate',
|
||||
onNull: null,
|
||||
},
|
||||
},
|
||||
else: null,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue