You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* add macro to get columns
* star macro should use get_columns
* add adapter.
* swap adapter for dbt_utils
Co-authored-by: Joel Labes <joel.labes@dbtlabs.com>
* update documentation
* add output_lower arg
* update name to get_filtered_columns_in_relation from get_columns
* add tests
* forgot args
* too much whitespace removal
-----------
Actual:
-----------
--->"field_3"as "test_field_3"<---
-----------
Expected:
-----------
--->"field_3" as "test_field_3"<---
* didnt mean to move a file that i did not create. moving things back.
* remove lowercase logic
* limit_zero
Co-authored-by: Joel Labes <joel.labes@dbtlabs.com>
This macro returns the unique values for a column in a given [relation](https://docs.getdbt.com/docs/writing-code-in-dbt/class-reference/#relation) as an array.
546
547
547
-
Arguments:
548
+
**Args:**
548
549
- `table` (required): a [Relation](https://docs.getdbt.com/reference/dbt-classes#relation) (a `ref` or `source`) that contains the list of columns you wish to select from
549
550
- `column` (required): The name of the column you wish to find the column values of
550
551
- `order_by` (optional, default=`'count(*) desc'`): How the results should be ordered. The default is to order by `count(*) desc`, i.e. decreasing frequency. Setting this as `'my_column'` will sort alphabetically, while `'min(created_at)'` will sort by when thevalue was first observed.
This macro returns an iterable Jinja list of columns for a given [relation](https://docs.getdbt.com/docs/writing-code-in-dbt/class-reference/#relation), (i.e. not from a CTE)
591
+
- optionally exclude columns
592
+
- the input values are not case-sensitive (input uppercase or lowercase and it will work!)
593
+
> Note: The native [`adapter.get_columns_in_relation` macro](https://docs.getdbt.com/reference/dbt-jinja-functions/adapter#get_columns_in_relation) allows you
594
+
to pull column names in a non-filtered fashion, also bringing along with it other (potentially unwanted) information, such as dtype, char_size, numeric_precision, etc.
595
+
596
+
**Args:**
597
+
- `from` (required): a [Relation](https://docs.getdbt.com/reference/dbt-classes#relation) (a `ref` or `source`) that contains the list of columns you wish to select from
598
+
- `except` (optional, default=`[]`): The name of the columns you wish to exclude. (case-insensitive)
599
+
600
+
**Usage:**
601
+
```sql
602
+
-- Returns a list of the columns from a relation, so you can then iterate in a for loop
603
+
{% set column_names = dbt_utils.get_filtered_columns_in_relation(from=ref('your_model'), except=["field_1", "field_2"]) %}
604
+
...
605
+
{% for column_name in column_names %}
606
+
max({{ column_name }}) ... as max_'{{ column_name }}',
Returns a list of [Relations](https://docs.getdbt.com/docs/writing-code-in-dbt/class-reference/#relation)
590
613
that match a given schema- or table-name pattern.
@@ -748,9 +771,19 @@ group by 1,2,3
748
771
```
749
772
750
773
#### star ([source](macros/sql/star.sql))
751
-
This macro generates a comma-separated list of all fields that exist in the `from` relation, excluding any fields listed in the `except` argument. The construction is identical to `select * from {{ref('my_model')}}`, replacing star (`*`) with the star macro. This macro also has an optional `relation_alias` argument that will prefix all generated fields with an alias (`relation_alias`.`field_name`).
774
+
This macro generates a comma-separated list of all fields that exist in the `from` relation, excluding any fields
775
+
listed in the `except` argument. The construction is identical to `select * from {{ref('my_model')}}`, replacing star (`*`) with
776
+
the star macro.
777
+
This macro also has an optional `relation_alias` argument that will prefix all generated fields with an alias (`relation_alias`.`field_name`).
778
+
The macro also has optional `prefix` and `suffix` arguments. When one or both are provided, they will be concatenated onto each field's alias
779
+
in the output (`prefix` ~ `field_name` ~ `suffix`). NB: This prevents the output from being used in any context other than a select statement.
752
780
753
-
The macro also has optional `prefix` and `suffix` arguments. When one or both are provided, they will be concatenated onto each field's alias in the output (`prefix` ~ `field_name` ~ `suffix`). NB: This prevents the output from being used in any context other than a select statement.
781
+
**Args:**
782
+
-`from` (required): a [Relation](https://docs.getdbt.com/reference/dbt-classes#relation) (a `ref` or `source`) that contains the list of columns you wish to select from
783
+
-`except` (optional, default=`[]`): The name of the columns you wish to exclude. (case-insensitive)
784
+
-`relation_alias` (optional, default=`''`): will prefix all generated fields with an alias (`relation_alias`.`field_name`).
785
+
-`prefix` (optional, default=`''`): will prefix the output `field_name` (`field_name as prefix_field_name`).
786
+
-`suffix` (optional, default=`''`): will suffix the output `field_name` (`field_name as field_name_suffix`).
0 commit comments