Skip to content

fix: ref() and source() in Python models #666

Merged
yotahk merged 4 commits intoaws-samples:mainfrom
akmalsoliev:fix_table_ref_source_issue
Mar 12, 2026
Merged

fix: ref() and source() in Python models #666
yotahk merged 4 commits intoaws-samples:mainfrom
akmalsoliev:fix_table_ref_source_issue

Conversation

@akmalsoliev
Copy link
Copy Markdown
Contributor

@akmalsoliev akmalsoliev commented Feb 23, 2026

fix: ref() and source() in Python models to use dbt-core's resolved functions

The previous implementation constructed table references manually. When calling source("my_source_aliase", "my_table") it would literally pass those string into spark.table resulting in a call spark.table("my_source_aliase"."my_table").

This fix will resolve this issue with reutilization of

def ref(*args, **kwargs):
    refs = {"my_ref_table": "my_schema.my_ref_table"}
    key = '.'.join(args)
    version = kwargs.get("v") or kwargs.get("version")
    if version:
        key += f".v{version}"
    dbt_load_df_function = kwargs.get("dbt_load_df_function")
    return dbt_load_df_function(refs[key])

def source(*args, dbt_load_df_function):
    sources = {"my_source_aliase.my_source_table": "my_source_schema.my_source_table"}
    key = '.'.join(args)
    return dbt_load_df_function(sources[key])

NOTE: that instead of having my_source_aliase it points to my_source_schema, which would solve the issue of an incorrect pointer with spark.table, outputting spark.table("my_source_schema"."my_source_table")

This took me ages to figure out, was so confused on why there was source declaration.

resolves #

Description

Checklist

  • I have signed the CLA
  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • I have updated the CHANGELOG.md and added information about my change to the "dbt-glue next" section.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…unctions

The previous implementation constructed table references manually. When calling
`source("my_source_aliase", "my_table")` it would literally pass those string into
`spark.table` resulting in a call `spark.table("my_source_aliase"."my_table")`.

This fix will resolve this issue with reutilization of

```py

def ref(*args, **kwargs):
    refs = {"my_ref_table": "my_schema.my_ref_table"}
    key = '.'.join(args)
    version = kwargs.get("v") or kwargs.get("version")
    if version:
        key += f".v{version}"
    dbt_load_df_function = kwargs.get("dbt_load_df_function")
    return dbt_load_df_function(refs[key])

def source(*args, dbt_load_df_function):
    sources = {"my_source_aliase.my_source_table": "my_source_schema.my_source_table"}
    key = '.'.join(args)
    return dbt_load_df_function(sources[key])
```

**NOTE:** that instead of having `my_source_aliase` it instead points to
`my_source_schema`, which would solve the issue of an incorrect pointer with
`spark.table`, outputting `spark.table("my_source_schema"."my_source_table")`

This took me ages to figure out, was so confused on why there was source
declaration.
@akmalsoliev akmalsoliev changed the title fix: ref() and source() in Python models to use dbt-core's resolved f… fix: ref() and source() in Python models Feb 23, 2026
@akmalsoliev
Copy link
Copy Markdown
Contributor Author

I believe this also fixes the issue: #635

@yotahk yotahk added the enable-functional-tests This label enable functional tests label Mar 9, 2026
@yotahk
Copy link
Copy Markdown
Collaborator

yotahk commented Mar 9, 2026

Thanks for the fix. I believe delegating to dbt-core's resolved ref/source functions (as defined in py_script_postfix) is the right approach.

One small ask: could you add an entry to CHANGELOG.md for this change & complete the PR checklist?

@akmalsoliev
Copy link
Copy Markdown
Contributor Author

Thanks for the fix. I believe delegating to dbt-core's resolved ref/source functions (as defined in py_script_postfix) is the right approach.

One small ask: could you add an entry to CHANGELOG.md for this change & complete the PR checklist?

Hey @yotahk,

You'll find the updates, thanks to Claude was able to understand how in the god's name to create test.

@yotahk yotahk added enable-functional-tests This label enable functional tests and removed enable-functional-tests This label enable functional tests labels Mar 12, 2026
@yotahk yotahk added enable-functional-tests This label enable functional tests and removed enable-functional-tests This label enable functional tests labels Mar 12, 2026
@yotahk yotahk merged commit eb716ff into aws-samples:main Mar 12, 2026
26 checks passed
@yotahk
Copy link
Copy Markdown
Collaborator

yotahk commented Mar 12, 2026

Merged, thank you for the contribution!

@akmalsoliev akmalsoliev deleted the fix_table_ref_source_issue branch May 2, 2026 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beginning-contributor enable-functional-tests This label enable functional tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants