-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Patrick Trainer
committed
Nov 4, 2022
1 parent
86fe946
commit 361b0eb
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# dbt-no-select-star | ||
![Ah! Ah! Ah!](etc/ah-ah-ah.gif) | ||
|
||
This [dbt](https://github.com/dbt-labs/dbt) package contains a macro that will throw an error if you attempt to use `select *` in your model. This macro effectively forces you to be explicit about the columns you want to select. | ||
|
||
## Installation | ||
Adding this package to your project will enable the macro. To install, add the following to your `packages.yml` file: | ||
|
||
```yml | ||
packages: | ||
- git: "https://github.com/patricktrainer/dbt-no-select-star.git" | ||
revision: 0.1.0 | ||
``` | ||
|
||
## Usage | ||
To use the macro, simply add the following at the end of your model: | ||
|
||
```sql | ||
{{ no_select_star() }} | ||
``` | ||
|
||
For example: | ||
|
||
```sql | ||
select | ||
my_column, | ||
my_other_column, | ||
{{ no_select_star() }} | ||
from {{ ref(my_table) }} | ||
``` | ||
|
||
This will throw an error if you attempt to use `select *` in your model. | ||
|
||
The error message will look like this: | ||
|
||
```bash | ||
'DONT SELECT * FROM THIS TABLE!!!1' | ||
``` | ||
|
||
To avoid the error, be explicit about the columns you want to select: | ||
|
||
```sql | ||
select | ||
my_column, | ||
my_other_column | ||
from {{ ref(my_table) }} | ||
``` | ||
|
||
## How does this work? | ||
This works by leveraging _computed columns_. Computed columns are a feature that allow you to create a column that is the result of a SQL expression. If you inspect [the macro](macros/no_select_star.sql), you'll notice that it creates a `string` typed column. This column is then cast as a `char()` type with a length of 1 byte. Obviously, the string "DONT SELECT * FROM THIS TABLE!!!1" is longer than 1 byte, so the cast will fail. This will cause the query to fail, and the error message will be displayed. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.