Unify parameter names

This commit is contained in:
Javier Goizueta 2018-10-03 19:07:47 +02:00
parent 96ba075698
commit aff55351ad
2 changed files with 60 additions and 60 deletions

View File

@ -122,9 +122,9 @@ const timeDimensionParameters = definition => {
// definition.column should correspond to a wrapped date column
return {
time: `to_timestamp("${definition.column}")`,
timezone: definition.timezone || 'utc',
group_by: definition.group_by,
group_by_count: definition.group_by_count || 1,
timeZone: definition.timezone || 'utc',
groupBy: definition.group_by,
groupByCount: definition.group_by_count || 1,
starting: definition.starting
};
};

View File

@ -39,18 +39,18 @@ const serialParts = {
}
};
function serialSqlExpr(t, tz, u, m = 1, starting = undefined) {
[u, m] = serialNormalize(u, m);
let { sql, zeroBased } = serialParts[u];
const column = timeExpression(t, tz);
function serialSqlExpr(time, timeZone, groupBy, count = 1, starting = undefined) {
[groupBy, count] = serialNormalize(groupBy, count);
let { sql, zeroBased } = serialParts[groupBy];
const column = timeExpression(time, timeZone);
const epoch = epochExpression(starting);
const serial = sql.replace(/\$t/g, column).replace(/\$epoch/g, epoch);
let expr = serial;
if (m !== 1) {
if (count !== 1) {
if (zeroBased) {
expr = `FLOOR((${expr})/(${m}::double precision))::int`;
expr = `FLOOR((${expr})/(${count}::double precision))::int`;
} else {
expr = `CEIL((${expr})/(${m}::double precision))::int`;
expr = `CEIL((${expr})/(${count}::double precision))::int`;
}
} else {
expr = `(${expr})::int`;
@ -74,50 +74,50 @@ const isoParts = {
millennium: `to_char(date_part('millenium', $t), '"M"999')`
};
function isoSqlExpr(t, tz, u, m = 1) {
const column = timeExpression(t, tz);
if (m > 1) {
function isoSqlExpr(time, timeZone, groupBy, count = 1) {
const column = timeExpression(time, timeZone);
if (count > 1) {
// TODO: it would be sensible to return the ISO of the firt unit in the period
throw new Error('Multiple time units not supported for ISO format');
}
return isoParts[u].replace(/\$t/g, column);
return isoParts[groupBy].replace(/\$t/g, column);
}
function serialNormalize(u, m) {
if (u === 'semester') {
u = 'month';
m *= 6;
} else if (u === 'trimester') {
u = 'month';
m *= 4;
} else if (u === 'decade') {
u = 'year';
m *= 10;
} else if (u === 'century') {
u = 'year';
m *= 100;
} else if (u === 'millenium') {
u = 'year';
m *= 1000;
function serialNormalize(groupBy, count) {
if (groupBy === 'semester') {
groupBy = 'month';
count *= 6;
} else if (groupBy === 'trimester') {
groupBy = 'month';
count *= 4;
} else if (groupBy === 'decade') {
groupBy = 'year';
count *= 10;
} else if (groupBy === 'century') {
groupBy = 'year';
count *= 100;
} else if (groupBy === 'millenium') {
groupBy = 'year';
count *= 1000;
}
return [u, m];
return [groupBy, count];
}
function cyclicNormalize(u, m, c) {
if (u === 'monthOfYear' && m === 3) {
u = 'quarterOfYear';
m = 1;
} else if (u === 'monthOfYear' && m === 6) {
u = 'semesterOfYear';
m = 1;
} else if (u === 'monthOfYear' && m === 4) {
u = 'trimesterOfYear';
m = 1;
function cyclicNormalize(groupBy, count) {
if (groupBy === 'monthOfYear' && count === 3) {
groupBy = 'quarterOfYear';
count = 1;
} else if (groupBy === 'monthOfYear' && count === 6) {
groupBy = 'semesterOfYear';
count = 1;
} else if (groupBy === 'monthOfYear' && count === 4) {
groupBy = 'trimesterOfYear';
count = 1;
}
if (m !== 1) {
throw new Error(`invalid multiplicity ${m} for cyclic ${u}`);
if (count !== 1) {
throw new Error(`invalid multiplicity ${count} for cyclic ${groupBy}`);
}
return [u, m, c];
return [groupBy, count];
}
// timezones can be defined either by an numeric offset in seconds or by
@ -159,17 +159,17 @@ function epochExpression(epoch) {
const day = match[3] || '01';
const hour = match[4] || '00';
const minute = match[5] || '00';
const second = match[6] || '00';
const second = match[6]t || '00';
epoch = `${year}-${month}-${day}T${hour}:${minute}:${second}`;
return `TIMESTAMP '${epoch}'`;
}
function cyclicSqlExpr(t, tz, u, m = 1) {
[u, m, c] = cyclicNormalize(u, m, c);
const column = timeExpression(t, tz);
function cyclicSqlExpr(time, timeZone, groupBy, count = 1) {
[groupBy, count] = cyclicNormalize(groupBy, count);
const column = timeExpression(time, timeZone);
if (m === 1) {
switch (u) {
if (count === 1) {
switch (groupBy) {
case 'dayOfWeek':
// 1 = monday; 7 = sunday;
return `date_part('isodow', ${column})`;
@ -211,7 +211,7 @@ function cyclicSqlExpr(t, tz, u, m = 1) {
return `date_part('minute', ${column})`;
}
}
throw new Error(`Invalid cyclic time grouping ${u}`)
throw new Error(`Invalid cyclic time grouping ${groupBy} with count ${count}`)
}
function validateParameters(_params) {
@ -228,24 +228,24 @@ function classificationSql(params) {
// TODO: validate group_by_count === 1, No epoch
return cyclicSqlExpr(
params.time,
params.timezone,
params.group_by,
params.group_by_count
params.timeZone,
params.groupBy,
params.groupByCount
);
} else if (params.format === 'iso') {
// TODO: validate group_by_count === 1, No epoch
return isoSqlExpr(
params.time,
params.timezone,
params.group_by,
params.group_by_count
params.timeZone,
params.groupBy,
params.groupByCount
);
} else {
return serialSqlExpr(
params.time,
params.timezone,
params.group_by,
params.group_by_count,
params.timeZone,
params.groupBy,
params.groupByCount,
params.starting
);