Skip to content

Commit 07bcd5a

Browse files
authored
Merge pull request #438 from lucaslortiz/master
Fix calc_batch_size maximum number of parameters allowed
2 parents 9b1aabd + 42bb50d commit 07bcd5a

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{% macro sqlserver__get_binding_char() %}
2+
{{ return('?') }}
3+
{% endmacro %}
4+
5+
{% macro sqlserver__get_batch_size() %}
6+
{{ return(400) }}
7+
{% endmacro %}
8+
9+
{% macro calc_batch_size(num_columns) %}
10+
{#
11+
SQL Server allows for a max of 2098 parameters in a single statement.
12+
Check if the max_batch_size fits with the number of columns, otherwise
13+
reduce the batch size so it fits.
14+
#}
15+
{% set max_batch_size = get_batch_size() %}
16+
{% set calculated_batch = (2098 / num_columns)|int %}
17+
{% set batch_size = [max_batch_size, calculated_batch] | min %}
18+
19+
{{ return(batch_size) }}
20+
{% endmacro %}
21+
22+
{% macro sqlserver__load_csv_rows(model, agate_table) %}
23+
{% set cols_sql = get_seed_column_quoted_csv(model, agate_table.column_names) %}
24+
{% set batch_size = calc_batch_size(agate_table.column_names|length) %}
25+
{% set bindings = [] %}
26+
{% set statements = [] %}
27+
28+
{{ log("Inserting batches of " ~ batch_size ~ " records") }}
29+
30+
{% for chunk in agate_table.rows | batch(batch_size) %}
31+
{% set bindings = [] %}
32+
33+
{% for row in chunk %}
34+
{% do bindings.extend(row) %}
35+
{% endfor %}
36+
37+
{% set sql %}
38+
insert into {{ this.render() }} ({{ cols_sql }}) values
39+
{% for row in chunk -%}
40+
({%- for column in agate_table.column_names -%}
41+
{{ get_binding_char() }}
42+
{%- if not loop.last%},{%- endif %}
43+
{%- endfor -%})
44+
{%- if not loop.last%},{%- endif %}
45+
{%- endfor %}
46+
{% endset %}
47+
48+
{% do adapter.add_query(sql, bindings=bindings, abridge_sql_log=True) %}
49+
50+
{% if loop.index0 == 0 %}
51+
{% do statements.append(sql) %}
52+
{% endif %}
53+
{% endfor %}
54+
55+
{# Return SQL so we can render it out into the compiled files #}
56+
{{ return(statements[0]) }}
57+
{% endmacro %}

tests/functional/adapter/test_seed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def test_custom_batch_size(self, project, logs_dir):
243243
logs = "".join(fp.readlines())
244244
# this is changed from 350.
245245
# Fabric goes -1 of min batch of (2100/number of columns -1) or 400
246-
assert "Inserting batches of 349.0 records" in logs
246+
assert "Inserting batches of 349 records" in logs
247247

248248

249249
class SeedConfigBase:

0 commit comments

Comments
 (0)