Skip to content

Commit ac51863

Browse files
committed
swap itertools for hand-rolled
1 parent 1d92da8 commit ac51863

14 files changed

Lines changed: 501 additions & 795 deletions

File tree

.github/workflows/tests.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ on:
77
branches:
88
- main
99
jobs:
10-
pre-commit:
10+
prek:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v4
13+
- uses: actions/checkout@v5
1414
- uses: actions/setup-python@v4
15-
- uses: pre-commit/action@v3.0.0
15+
- uses: j178/prek-action@v1
1616
integration-tests:
1717
runs-on: ubuntu-latest
1818
strategy:
1919
matrix:
20-
dbt_core: [1.5.*, 1.8.*]
20+
dbt_core: [1.6.*, 1.8.*]
2121
db_target: [duckdb, postgres, clickhouse]
2222
services:
2323
postgres:
@@ -34,9 +34,12 @@ jobs:
3434
--health-timeout 5s
3535
--health-retries 5
3636
steps:
37-
- uses: actions/checkout@v4
37+
- uses: actions/checkout@v5
3838
- name: Install uv
39-
uses: astral-sh/setup-uv@v5
39+
uses: astral-sh/setup-uv@v6
40+
with:
41+
python-version: 3.13
42+
activate-environment: true
4043
- name: Setup
4144
run: |
4245
sudo apt-get update

.python-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.11
1+
3.13.1

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### `0.3.2`
4+
5+
- Address deprecation of `modules.itertools`
6+
37
### `0.3.1`
48

59
- Fix bug in `vars:` implementation of method options.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Add this the `packages:` list your dbt project's `packages.yml`:
3535

3636
```yaml
3737
- package: "dwreeves/dbt_linreg"
38-
version: "0.3.1"
38+
version: "0.3.2"
3939
```
4040
4141
The full file will look something like this:
@@ -46,7 +46,7 @@ packages:
4646
# Other packages here
4747
# ...
4848
- package: "dwreeves/dbt_linreg"
49-
version: "0.3.1"
49+
version: "0.3.2"
5050
```
5151
5252
# Examples

dbt_project.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "dbt_linreg"
2-
version: "0.3.1"
2+
version: "0.3.2"
33

44
# 1.2 is required because of modules.itertools.
55
require-dbt-version: [">=1.2.0", "<2.0.0"]

integration_tests/dbt_project.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "dbt_linreg_tests"
2-
version: "0.3.1"
2+
version: "0.3.2"
33

44
require-dbt-version: [">=1.0.0", "<2.0.0"]
55

macros/linear_regression/ols_impl_chol/_ols_impl_chol.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
8787
{% macro _forward_substitution(li, safe=true, isa=none) %}
8888
{% set d = {} %}
89-
{% for i, j in modules.itertools.combinations_with_replacement(li, 2) %}
89+
{% for i, j in dbt_linreg._combinations_with_replacement(li, 2) %}
9090
{% set ns = namespace() %}
9191
{% if i == j %}
9292
{% set ns.numerator = '1' %}
@@ -165,7 +165,7 @@ _dbt_linreg_base as (
165165
_dbt_linreg_xtx as (
166166
select
167167
{{ dbt_linreg._gb_cols(group_by, trailing_comma=True) | indent(4) }}
168-
{%- for i, j in modules.itertools.combinations_with_replacement(xcols, 2) %}
168+
{%- for i, j in dbt_linreg._combinations_with_replacement(xcols, 2) %}
169169
{%- if alpha and i == j and i > 0 %}
170170
sum(b.x{{ i }} * b.x{{ j }} + {{ alpha[i-1] }}) as x{{ i }}x{{ j }}
171171
{%- else %}
@@ -246,7 +246,7 @@ _dbt_linreg_inverse_chol as (
246246
_dbt_linreg_inverse_xtx as (
247247
select
248248
{{ dbt_linreg._gb_cols(group_by, trailing_comma=True) | indent(4) }}
249-
{%- for i, j in modules.itertools.combinations_with_replacement(xcols, 2) %}
249+
{%- for i, j in dbt_linreg._combinations_with_replacement(xcols, 2) %}
250250
{%- if not add_constant %}
251251
{%- set upto = upto + 1 %}
252252
{%- endif %}

macros/linear_regression/ols_impl_fwl/_ols_impl_fwl.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{%- if i != j %}
77
{%- do remaining.remove(i) %}
88
{%- do remaining.remove(j) %}
9-
{%- set comb = modules.itertools.combinations(remaining, step - 1) %}
9+
{%- set comb = dbt_linreg._combinations(remaining, step - 1) %}
1010
{%- for c in comb %}
1111
{%- do li.append((i, j, c)) %}
1212
{%- endfor %}
@@ -21,14 +21,14 @@
2121
{%- for i in x %}
2222
{%- set remaining = x.copy() %}
2323
{%- do remaining.remove(i) %}
24-
{%- set comb = modules.itertools.combinations(remaining, step) %}
24+
{%- set comb = dbt_linreg._combinations(remaining, step) %}
2525
{%- for c in comb %}
2626
{%- set ortho = [] %}
2727
{%- if c %}
2828
{%- for b in c %}
2929
{%- set _c = (c | list) %}
3030
{%- do _c.remove(b) %}
31-
{%- do ortho.append([b] + (modules.itertools.combinations(_c, step - 1) | list)) %}
31+
{%- do ortho.append([b] + (dbt_linreg._combinations(_c, step - 1) | list)) %}
3232
{%- endfor %}
3333
{%- endif %}
3434
{%- do li.append((i, ortho)) %}

macros/linear_regression/utils/utils.sql

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,77 @@
1+
{###############################################################################
2+
## Replacements for modules.itertools (deprecated in dbt-core >= 1.9)
3+
###############################################################################}
4+
5+
{# itertools.combinations() #}
6+
{% macro _combinations(iterable, r) %}
7+
{% set result = [] %}
8+
{% set n = iterable | length %}
9+
{% if r > n or r < 0 %}
10+
{{ return(result) }}
11+
{% endif %}
12+
{% if r == 0 %}
13+
{% do result.append(()) %}
14+
{{ return(result) }}
15+
{% endif %}
16+
{% if r == 1 %}
17+
{% for item in iterable %}
18+
{% do result.append((item,)) %}
19+
{% endfor %}
20+
{{ return(result) }}
21+
{% endif %}
22+
{% if r == 2 %}
23+
{% for i in range(n) %}
24+
{% for j in range(i + 1, n) %}
25+
{% do result.append((iterable[i], iterable[j])) %}
26+
{% endfor %}
27+
{% endfor %}
28+
{{ return(result) }}
29+
{% endif %}
30+
{# recursive for r >= 3 #}
31+
{% for i in range(n - r + 1) %}
32+
{% set rest = dbt_linreg._combinations(iterable[i + 1:], r - 1) %}
33+
{% for combo in rest %}
34+
{% do result.append((iterable[i],) + combo) %}
35+
{% endfor %}
36+
{% endfor %}
37+
{{ return(result) }}
38+
{% endmacro %}
39+
40+
{# itertools.combinations_with_replacement() #}
41+
{% macro _combinations_with_replacement(iterable, r) %}
42+
{% set result = [] %}
43+
{% set n = iterable | length %}
44+
{% if n == 0 and r > 0 %}
45+
{{ return(result) }}
46+
{% endif %}
47+
{% if r == 0 %}
48+
{% do result.append(()) %}
49+
{{ return(result) }}
50+
{% endif %}
51+
{% if r == 1 %}
52+
{% for item in iterable %}
53+
{% do result.append((item,)) %}
54+
{% endfor %}
55+
{{ return(result) }}
56+
{% endif %}
57+
{% if r == 2 %}
58+
{% for i in range(n) %}
59+
{% for j in range(i, n) %}
60+
{% do result.append((iterable[i], iterable[j])) %}
61+
{% endfor %}
62+
{% endfor %}
63+
{{ return(result) }}
64+
{% endif %}
65+
{# recursive for r >= 3 #}
66+
{% for i in range(n) %}
67+
{% set rest = dbt_linreg._combinations_with_replacement(iterable[i:], r - 1) %}
68+
{% for combo in rest %}
69+
{% do result.append((iterable[i],) + combo) %}
70+
{% endfor %}
71+
{% endfor %}
72+
{{ return(result) }}
73+
{% endmacro %}
74+
175
{###############################################################################
276
## Simple univariate regression.
377
###############################################################################}

macros/schema.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ macros:
107107
- name: _cholesky_decomposition
108108
docs:
109109
show: false
110+
- name: _combinations
111+
docs:
112+
show: false
113+
- name: _combinations_with_replacement
114+
docs:
115+
show: false
110116
- name: _filter_and_center_if_alpha
111117
docs:
112118
show: false

0 commit comments

Comments
 (0)