Skip to content

Commit

Permalink
✨ (grapher) pull in data types from metadata (#3375)
Browse files Browse the repository at this point in the history
* ✨ (grapher) pull in data types from metadata

* ✨ (grapher) use Integer column for int types

* 🐛 (grapher) update column type when conversion factor is applied

* ✨ (grapher) only convert column type if necessary

* 🐛 fix typo

Co-authored-by: Marcel Gerber <[email protected]>

---------

Co-authored-by: Marcel Gerber <[email protected]>
  • Loading branch information
sophiamersmann and marcelgerber authored Apr 3, 2024
1 parent 33c9a3d commit d6dfeda
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/@ourworldindata/grapher/src/core/LegacyToOwidTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getYearFromISOStringAndDayOffset,
intersection,
isNumber,
isInteger,
makeAnnotationsSlug,
trimObject,
uniqBy,
Expand Down Expand Up @@ -145,6 +146,14 @@ export const legacyToOwidTableAndDimensions = (
values = values.map((value) =>
isNumber(value) ? value * conversionFactor : value
)

// If a non-int conversion factor is applied to an integer column,
// we end up with a numeric column.
if (
valueColumnDef.type === ColumnTypeNames.Integer &&
!isInteger(conversionFactor)
)
valueColumnDef.type = ColumnTypeNames.Numeric
}

const columnStore: { [key: string]: any[] } = {
Expand Down Expand Up @@ -551,13 +560,13 @@ const variableTypeToColumnType = (type: OwidVariableType): ColumnTypeNames => {
switch (type) {
case "ordinal":
return ColumnTypeNames.Ordinal
// TODO
// case "string":
// return ColumnTypeNames.String
// case "float":
// case "int":
// return ColumnTypeNames.Numeric
// case "mixed":
case "string":
return ColumnTypeNames.String
case "int":
return ColumnTypeNames.Integer
case "float":
return ColumnTypeNames.Numeric
case "mixed":
default:
return ColumnTypeNames.NumberOrString
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@ourworldindata/utils/src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
isBoolean,
isEmpty,
isEqual,
isInteger,
isNil,
isNull,
isNumber,
Expand Down Expand Up @@ -94,6 +95,7 @@ export {
isBoolean,
isEmpty,
isEqual,
isInteger,
isNil,
isNull,
isNumber,
Expand Down
1 change: 1 addition & 0 deletions packages/@ourworldindata/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export {
isBoolean,
isEmpty,
isEqual,
isInteger,
isNil,
isNull,
isNumber,
Expand Down

0 comments on commit d6dfeda

Please sign in to comment.