From ef0130f486859021ad92530f700621c2aee0720f Mon Sep 17 00:00:00 2001 From: Bobby Iliev Date: Fri, 7 Jun 2024 22:48:35 +0300 Subject: [PATCH] dbt-materialize: Stop adding aliases to subqueries with --empty (#27522) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Motivation As per this bug [here](https://github.com/dbt-labs/dbt-adapters/issues/124), the `--empty` flag doesn't work with relation that are already named and fails. A quick repro: * Create a simple model with: `SELECT * FROM {{ ref('my_upstream_model')}} some_alias` * The generated SQL when using the `--empty` flag, looks like this: ```sql as ( SELECT * FROM ( select * from "my_upstream_model" where false limit 0) _dbt_limit_subq_my_upstream_model alias ); ``` ### Tips for reviewer ### Checklist - [ ] This PR has adequate test coverage / QA involvement has been duly considered. ([trigger-ci for additional test/nightly runs](https://trigger-ci.dev.materialize.com/)) - [ ] This PR has an associated up-to-date [design doc](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/README.md), is a design doc ([template](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/00000000_template.md)), or is sufficiently small to not require a design. - [ ] If this PR evolves [an existing `$T ⇔ Proto$T` mapping](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/command-and-response-binary-encoding.md) (possibly in a backwards-incompatible way), then it is tagged with a `T-proto` label. - [ ] If this PR will require changes to cloud orchestration or tests, there is a companion cloud PR to account for those changes that is tagged with the release-blocker label ([example](https://github.com/MaterializeInc/cloud/pull/5021)). - [ ] This PR includes the following [user-facing behavior changes](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/guide-changes.md#what-changes-require-a-release-note): - --- misc/dbt-materialize/CHANGELOG.md | 1 + .../dbt-materialize/dbt/adapters/materialize/relation.py | 1 + misc/dbt-materialize/tests/adapter/test_empty.py | 9 ++++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/misc/dbt-materialize/CHANGELOG.md b/misc/dbt-materialize/CHANGELOG.md index 1ec4645e4d22..8420cc0ca960 100644 --- a/misc/dbt-materialize/CHANGELOG.md +++ b/misc/dbt-materialize/CHANGELOG.md @@ -4,6 +4,7 @@ * Add support for overriding the `generate_cluster_name` macro to customize the cluster name generation logic in user projects. +* Stop adding aliases to subqueries when calling with `--empty` flag. ## 1.8.0 - 2024-05-23 diff --git a/misc/dbt-materialize/dbt/adapters/materialize/relation.py b/misc/dbt-materialize/dbt/adapters/materialize/relation.py index 6dd57e3bbb9b..2ace22cb914d 100644 --- a/misc/dbt-materialize/dbt/adapters/materialize/relation.py +++ b/misc/dbt-materialize/dbt/adapters/materialize/relation.py @@ -44,6 +44,7 @@ class MaterializeRelationType(StrEnum): @dataclass(frozen=True, eq=False, repr=False) class MaterializeRelation(PostgresRelation): type: Optional[MaterializeRelationType] = None + require_alias: bool = False # Materialize does not have a 63-character limit for relation names, unlike # PostgreSQL (see dbt-core #2727). Instead, we set 255 as the maximum diff --git a/misc/dbt-materialize/tests/adapter/test_empty.py b/misc/dbt-materialize/tests/adapter/test_empty.py index f2a80f0401ca..91dc5b3ed789 100644 --- a/misc/dbt-materialize/tests/adapter/test_empty.py +++ b/misc/dbt-materialize/tests/adapter/test_empty.py @@ -13,8 +13,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -from dbt.tests.adapter.empty.test_empty import BaseTestEmpty +from dbt.tests.adapter.empty.test_empty import ( + BaseTestEmpty, + BaseTestEmptyInlineSourceRef, +) class TestMaterializeEmpty(BaseTestEmpty): pass + + +class TestMaterializeEmptyInlineSourceRef(BaseTestEmptyInlineSourceRef): + pass