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

Using Ref and schema variables inside a SQLX template #1611

Open
vcetinick opened this issue Dec 11, 2023 · 2 comments
Open

Using Ref and schema variables inside a SQLX template #1611

vcetinick opened this issue Dec 11, 2023 · 2 comments

Comments

@vcetinick
Copy link

vcetinick commented Dec 11, 2023

I am trying to make the schema configuration more dynamic by using "vars" in the dataform.json property as a way to control which schema to use based on the value of the variable. Goal is to have to the definition of schema set depending on when environment being used at execution time.

The SQLX template looks like this

config {
  schema: dataform.projectConfig.vars.env_schema,
  type: "view"
}

select *  from ${ref("users_sys_1")} u1
union all 
select *  from ${ref("users_sys_2")} u2

This template is simply trying union two tables together (users_sys_1 and users_sys_2), however, there seems to be something funny happening in the resolution for the ref command because the compiled query becomes

select *  from `wh-proj1.${dataform.projectConfig.vars.env_schema}.users_sys_1` u1
union all 
select *  from `wh-proj1.${dataform.projectConfig.vars.env_schema}.users_sys_2` u2

I would expect the value to be interpolated from the config.schema block, but instead it seems to have bled through. E.g.

select *  from `wh-proj1.ds_staging.users_sys_1` u1
union all 
select *  from `wh-proj1.ds_staging.users_sys_2` u2

Because the interpolation is not happening, I get the error

Invalid project ID 'wh-proj1.${dataform.projectConfig.vars'. Project IDs must contain 6-63 lowercase letters, digits, or dashes. Some project IDs also include domain name separated by a colon. IDs must start with a letter and may not end with a dash..

Is this correct behaviour?

@vcetinick
Copy link
Author

In addition, I have noticed that if I include the schema in the ref function, I get the desired behaviour. e.g.

config {
  schema: dataform.projectConfig.vars.env_schema,
  type: "view"
}

select *  from ${ref(dataform.projectConfig.vars.env_schema, "users_sys_1")} u1
union all 
select *  from ${ref(dataform.projectConfig.vars.env_schema, "users_sys_2")} u2

Now the SQL is interpolated properly:

select *  from `wh-proj1.ds_staging.users_sys_1` u1
union all 
select *  from `wh-proj1.ds_staging.users_sys_2` u2

@BenBirt
Copy link
Collaborator

BenBirt commented Dec 12, 2023

Interesting! This is definitely not correct/expected. We will look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants