Skip to content

Added option for reference table creation using reference keyword#46

Closed
jimvekemans wants to merge 6 commits into
memsql:masterfrom
DataSenseNV:define_reference_tables
Closed

Added option for reference table creation using reference keyword#46
jimvekemans wants to merge 6 commits into
memsql:masterfrom
DataSenseNV:define_reference_tables

Conversation

@jimvekemans
Copy link
Copy Markdown
Contributor

@jimvekemans jimvekemans commented Jul 14, 2025

Issue synopsis

There is a need to create reference tables in dbt-singlestore

Solution

Accept a new reference configuration option for tables which will accept true/false (default false) as it's value, altering the CREATE TABLE statement to CREATE REFERENCE TABLE. This should be done without impacting existing functionality.

Breakdown

testmodel.sql

{{
    config(
        database='staging',
        materialized='table',
        reference=true,
        storage_type='rowstore'
    )
}}
select 1

Before change

Will run as:

create  table `staging`.`testmodel` (SHARD KEY ()) as ...

After considering the reference option in singlestore__create_table_as

create reference table `staging`.`testmodel__dbt_tmp` as ...

Without impacting other types of table

{{
    config(
        database='staging',
        materialized='table',
        storage_type='rowstore'
    )
}}
select 1

Will still normally run as

create rowstore table `staging`.`testmodel` (SHARD KEY ()) as
{{ statement }};

And setting a shard key for the reference table will expectedly result in an error:

{{
    config(
        database='staging',
        materialized='table',
        reference=true,
        shard_key=['1'],
    )
}}
select 1

Will throw the error

1706: Feature 'Sharded reference tables' is not supported by SingleStore.
compiled code at target/run/dbt_academy/models/testmodel.sql

@okramarenko
Copy link
Copy Markdown
Collaborator

hi @jimvekemans — thanks so much for the PRs!

we're updating our contribution guidelines and asking that new functionality include tests -- it helps reviews go faster and keeps the codebase reliable. could you please add a few tests for this change? we've already added the jaffle_shop project to the repo if that's easier to use.

thanks again, and let us know if you'd like any pointers!

@jimvekemans
Copy link
Copy Markdown
Contributor Author

hi @jimvekemans — thanks so much for the PRs!

we're updating our contribution guidelines and asking that new functionality include tests -- it helps reviews go faster and keeps the codebase reliable. could you please add a few tests for this change? we've already added the jaffle_shop project to the repo if that's easier to use.

thanks again, and let us know if you'd like any pointers!

Hi @okramarenko I will pick this up next week, and rebase the work on your new version of the adapter!

@jimvekemans
Copy link
Copy Markdown
Contributor Author

@okramarenko added tests for validation of the option.

The reference option now is a boolean (like transient tables in snowflake), so {{ config(materialized="table", reference=true, primary_key=["somekey"]) }} now creates a ref table, and {{ config(materialized="table", reference=true, primary_key=["somekey"], storage_type="rowstore") }} now create a rowstore ref table.

This also solves the new open issue #61

@jimvekemans jimvekemans changed the title Added option for reference table creation using storage_type keyword Added option for reference table creation using reference keyword Dec 23, 2025
Copy link
Copy Markdown
Collaborator

@okramarenko okramarenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just run your PR against our test suite — all tests are green :) before we can approve and merge could you please take a look at the comments I left and address them?

thanks again for contributing to the project!

{% endif -%}

{% if reference -%}
{% set storage_type = storage_type ~ ' reference ' -%}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there’s a chance we end up with smth like temporary reference or rowstore temporary reference, which triggers a db error (temporary reference tables not supported). that can be a bit confusing for users, so it’s better to raise a clearer dbt error (smth like reference=true isn’t compatible with operations that create temporary tables)

{% set create_definition_str = create_definition_list|join(", ") -%}
{% elif not contract_defined_primary and not contract_defined_unique -%}
{% set create_definition_str = 'SHARD KEY ()' -%}
{% if reference -%}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you already omit adding the default shard key when reference=true, but if a user defines shard_key in the config alongside reference=true, they’ll get a db error about an unsupported feature. we should add a check like {%- if reference and shard_key | length -%} (say line 56) and raise a dbt error pointing to the config issue (smth like shard_key cannot be used with reference=true because reference tables are not sharded)

def models(self):
return {
"ref_table.sql": "{{ config(materialized='table', reference=true) }} select 1 as id",
"rowstore_ref_table.sql": "{{ config(materialized='table', reference=true, storage_type='rowstore', primary_key=['id']) }} select 1 as id",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets add one more test here with reference = true and shard_key=[...] and check that it fails with a clear compiler error (the comment above)

okramarenko added a commit that referenced this pull request Jan 30, 2026
Co-authored-by: Jim Vekemans <JimVekemans@hotmail.com>
@okramarenko
Copy link
Copy Markdown
Collaborator

I resolved the comments in #63 to speed up the process a little bit as it would be nice to ship this with 1.10 version of the adapter. Thanks again for your contribution @jimvekemans! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants