Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix]: add Jinja code to clean string columns in br_ibge_pnadc.microdados #418

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions models/br_ibge_pnadc/br_ibge_pnadc__microdados.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
cluster_by = "sigla_uf",
labels = {'tema': 'economia'})
}}

{%- set columns = adapter.get_columns_in_relation(this) -%}
with microdados as(
SELECT
SAFE_CAST(ano AS INT64) ano,
SAFE_CAST(trimestre AS INT64) trimestre,
Expand Down Expand Up @@ -439,4 +440,19 @@ SAFE_CAST(V1028199 AS FLOAT64) V1028199,
SAFE_CAST(V1028200 AS FLOAT64) V1028200,
SAFE_CAST(habitual AS FLOAT64) habitual,
SAFE_CAST(efetivo AS FLOAT64) efetivo
FROM basedosdados-staging.br_ibge_pnadc_staging.microdados AS t
FROM basedosdados-staging.br_ibge_pnadc_staging.microdados AS t)
-- verifica se a coluna é do tipo STRING e, caso seja, limpa as observações que começam com 0 (ie. transforma '05' em '5')
select
{% for column in columns %}
{% if column.data_type == 'STRING' and column.name.startswith('V') %}
(CASE
WHEN LENGTH(TRIM(`{{ column.name }}`)) > 1 AND LEFT(TRIM(`{{ column.name }}`), 1) = '0' THEN SUBSTR(TRIM(`{{ column.name }}`), 2)
ELSE TRIM(`{{ column.name }}`)
END) AS `{{ column.name }}`,
{{ log("Column is of type STRING and starts with V: " ~ column.name, info=true) }}
{% else %}
`{{ column.name }}`,
{{ log("Column is of type not STRING and does not start with V: " ~ column.name, info=true) }}
{% endif %}
{% endfor %}
from microdados
Loading