Remove offsets from time dimension computations
This commit is contained in:
@@ -124,7 +124,7 @@ function serialNormalize(u, m) {
|
||||
return [u, m];
|
||||
}
|
||||
|
||||
function cyclicNormalize(u, m, c, c_offset) {
|
||||
function cyclicNormalize(u, m, c) {
|
||||
if (u === 'month' && m === 3) {
|
||||
u = 'quarter';
|
||||
m = 1;
|
||||
@@ -138,7 +138,7 @@ function cyclicNormalize(u, m, c, c_offset) {
|
||||
if (m !== 1) {
|
||||
throw new Error(`invalid multiplicity ${m} for cyclic ${u}`);
|
||||
}
|
||||
return [u, m, c, c_offset];
|
||||
return [u, m, c];
|
||||
}
|
||||
|
||||
// timezones can be defined either by an numeric offset in seconds or by
|
||||
@@ -185,95 +185,56 @@ function epochExpression(epoch) {
|
||||
return `TIMESTAMP '${epoch}'`;
|
||||
}
|
||||
|
||||
function cyclicSqlExpr(t, tz, u, c, c_offset = 0, m = 1) {
|
||||
[u, m, c, c_offset] = cyclicNormalize(u, m, c, c_offset);
|
||||
function cyclicSqlExpr(t, tz, u, c, m = 1) {
|
||||
[u, m, c] = cyclicNormalize(u, m, c);
|
||||
const comb = `${u}/${c}`;
|
||||
const column = timeExpression(t, tz);
|
||||
let expr;
|
||||
|
||||
// TODO: drop offset support
|
||||
|
||||
if (m === 1) {
|
||||
switch (comb) {
|
||||
case 'day/week':
|
||||
if (c_offset === 0) {
|
||||
// 1 = monday; 7 = sunday;
|
||||
return `date_part('isodow', ${column})`;
|
||||
}
|
||||
// iso dow 1-6, offset 0 => 1 = monday
|
||||
expr = `date_part('dow, ${column})`;
|
||||
c_offset += 1;
|
||||
return `(${expr} - ${c_offset}) % 7 + 1`;
|
||||
// 1 = monday; 7 = sunday;
|
||||
return `date_part('isodow', ${column})`;
|
||||
|
||||
case 'day/month':
|
||||
// result: 1-31
|
||||
// c_offset not supported
|
||||
return `date_part('day', ${column})`;
|
||||
|
||||
case 'day/year':
|
||||
// result: 1-366
|
||||
// c_offset not supported
|
||||
return `date_part('doy', ${column})`;
|
||||
|
||||
case 'hour/day':
|
||||
// result: 0-23
|
||||
expr = `date_part('hour', ${column})`;
|
||||
if (c_offset !== 0) {
|
||||
expr = `(${expr} - ${c_offset}) % 24`;
|
||||
}
|
||||
return expr;
|
||||
return `date_part('hour', ${column})`;
|
||||
|
||||
case 'month/year':
|
||||
// result 1-12
|
||||
expr = `date_part('month', ${column})`;
|
||||
if (c_offset !== 0) {
|
||||
expr = `((${expr} - ${c_offset} - 1) % 12) + 1`;
|
||||
}
|
||||
return expr;
|
||||
return `date_part('month', ${column})`;
|
||||
|
||||
case 'quarter/year':
|
||||
// result 1-4
|
||||
expr = `date_part('quarter', ${column})`;
|
||||
if (c_offset !== 0) {
|
||||
expr = `((${expr} - ${c_offset} - 1) % 4) + 1`;
|
||||
}
|
||||
return expr;
|
||||
return `date_part('quarter', ${column})`;
|
||||
|
||||
case 'semester/year':
|
||||
// result 1-2
|
||||
expr = `FLOOR((date_part('month', ${column})-1)/6.0) + 1`;
|
||||
if (c_offset !== 0) {
|
||||
expr = `((${expr} - ${c_offset} - 1) % 2) + 1`;
|
||||
}
|
||||
return expr;
|
||||
return `FLOOR((date_part('month', ${column})-1)/6.0) + 1`;
|
||||
|
||||
case 'trimester/year':
|
||||
// result 1-3
|
||||
expr = `FLOOR((date_part('month', ${column})-1)/4.0) + 1`;
|
||||
if (c_offset !== 0) {
|
||||
expr = `((${expr} - ${c_offset} - 1) % 3) + 1`;
|
||||
}
|
||||
return expr;
|
||||
return `FLOOR((date_part('month', ${column})-1)/4.0) + 1`;
|
||||
|
||||
case 'week/year':
|
||||
// result 1-53
|
||||
expr = `date_part('week', ${column})`;
|
||||
if (c_offset !== 0) {
|
||||
expr = `((${expr} - ${c_offset} - 1) % 53) + 1`;
|
||||
}
|
||||
return expr;
|
||||
return `date_part('week', ${column})`;
|
||||
|
||||
case 'minute/hour':
|
||||
// result 0-59
|
||||
expr = `date_part('minute', ${column})`;
|
||||
if (c_offset !== 0) {
|
||||
expr = `((${expr} - ${c_offset}) % 60)`;
|
||||
}
|
||||
return expr;
|
||||
return `date_part('minute', ${column})`;
|
||||
}
|
||||
}
|
||||
// TODO: remove generic expression, reaching here should error
|
||||
return genericCyclicSqlExpr(t, u, c, c_offset, m);
|
||||
return genericCyclicSqlExpr(t, u, c, 0, m);
|
||||
}
|
||||
|
||||
function genericCyclicSqlExpr(t, tz, u, c, c_offset = 0, m = 1) {
|
||||
|
||||
Reference in New Issue
Block a user