Hello everyone, I’d love to get some help. I reckon, I just have some gaps in understanding how the jinja works under the hood. many thanks in advance!
The problem I’m having
I’m tring to create a macro in order to generate all columns for staging tables by using a source table where $1 is a variant column (parquet file). however when i run the macro within a model it gives the following exception: SQL compilation error: Unknown function INFER_TABLE_SCHEMA
The context of why I’m trying to do this
I’d like to create an automated way to generate all columns with its matching types from a parquet file. I’m using snowflake as a dwh, and the source tables are as following:
- $1: content
- $2: filename
- $3: date
What I’ve already tried
I’m using the infer_schema function to get all the rows i need. Afterwards, i’m trying to get all the rows as one string, so when i call the macro within a model, it spits out all rows as one string. (please see script below)
please don’t mind the log and print, i just wanna know what is the difference between the two
Some example code or error messages
macro (str, str, int):
{% macro infer_table_schema(X__file_stage_path, file_format_path, max_file_count) %}
{{ print("Running infer_table_schema: stage_path: " ~ arg1 ~ ", file_format_path " ~ arg2) }}
{{ log("Running infer_table_schema: stage_path: " ~ arg1 ~ ", file_format_path " ~ arg2, info=True) }}
{% set query = """
select expression || ' AS ' || column_name
from table(infer_schema(
LOCATION => \'""" ~ X__file_stage_path ~ """\'
, FILE_FORMAT => \'""" ~ file_format_path ~ """\'
, IGNORE_CASE => TRUE
, MAX_FILE_COUNT => """ ~ max_file_count ~ """
)
)
"""
%}
{{ print("Query: " ~ query) }}
{% set res_schema = run_query(query) %}
{# Fetching the definition for each column in the parquet file #}
{% set schema = res_schema.columns[0].values() %}
{% set schema_str = schema|join(", ") %}
{{ return(schema_str) }}
{% endmacro %}
model:
select
infer_table_schema('@<X_file_stage_path>', '<file_format_path>', 5)
from {{ source('X', 'TABLE_X') }}
Any help is highly appreciated!