Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fetchmany #341

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20230809-162435.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Implemented fetchmany
time: 2023-08-09T16:24:35.116089+02:00
custom:
Author: damian3031
Issue: ""
PR: "341"
11 changes: 11 additions & 0 deletions dbt/adapters/trino/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,17 @@ def fetchone(self):

return None

def fetchmany(self, size):
if self._cursor is None:
return None

if self._fetch_result is not None:
ret = self._fetch_result[:size]
self._fetch_result = None
return ret

return None

def execute(self, sql, bindings=None):
if not self._prepared_statements_enabled and bindings is not None:
# DEPRECATED: by default prepared statements are used.
Expand Down
84 changes: 84 additions & 0 deletions tests/functional/adapter/show/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
models__sample_model = """
select * from {{ ref('sample_seed') }}
"""

models__second_model = """
select
sample_num as col_one,
sample_bool as col_two,
42 as answer
from {{ ref('sample_model') }}
"""

models__sql_header = """
{% call set_sql_header(config) %}
set time zone 'Asia/Kolkata';
{%- endcall %}
select current_timezone() as timezone
"""

private_model_yml = """
groups:
- name: my_cool_group
owner: {name: me}

models:
- name: private_model
access: private
config:
group: my_cool_group
"""


schema_yml = """
models:
- name: sample_model
latest_version: 1

# declare the versions, and fully specify them
versions:
- v: 2
config:
materialized: table
columns:
- name: sample_num
data_type: int
- name: sample_bool
data_type: boolean
- name: answer
data_type: int

- v: 1
config:
materialized: table
contract: {enforced: true}
columns:
- name: sample_num
data_type: int
- name: sample_bool
data_type: boolean
"""

models__ephemeral_model = """
{{ config(materialized = 'ephemeral') }}
select
coalesce(sample_num, 0) + 10 as col_deci
from {{ ref('sample_model') }}
"""

models__second_ephemeral_model = """
{{ config(materialized = 'ephemeral') }}
select
col_deci + 100 as col_hundo
from {{ ref('ephemeral_model') }}
"""

seeds__sample_seed = """sample_num,sample_bool
1,true
2,false
3,true
4,false
5,true
6,false
7,true
"""
160 changes: 160 additions & 0 deletions tests/functional/adapter/show/test_show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import pytest
from dbt.exceptions import DbtRuntimeError
from dbt.exceptions import Exception as DbtException
from dbt.tests.util import run_dbt, run_dbt_and_capture

from tests.functional.adapter.show.fixtures import (
models__ephemeral_model,
models__sample_model,
models__second_ephemeral_model,
models__second_model,
models__sql_header,
private_model_yml,
schema_yml,
seeds__sample_seed,
)


class TestShow:
@pytest.fixture(scope="class")
def models(self):
return {
"sample_model.sql": models__sample_model,
"second_model.sql": models__second_model,
"ephemeral_model.sql": models__ephemeral_model,
"sql_header.sql": models__sql_header,
}

@pytest.fixture(scope="class")
def seeds(self):
return {"sample_seed.csv": seeds__sample_seed}

def test_none(self, project):
with pytest.raises(
DbtRuntimeError, match="Either --select or --inline must be passed to show"
):
run_dbt(["seed"])
run_dbt(["show"])

def test_select_model_text(self, project):
run_dbt(["build"])
(results, log_output) = run_dbt_and_capture(["show", "--select", "second_model"])
assert "Previewing node 'sample_model'" not in log_output
assert "Previewing node 'second_model'" in log_output
assert "col_one" in log_output
assert "col_two" in log_output
assert "answer" in log_output

def test_select_multiple_model_text(self, project):
run_dbt(["build"])
(results, log_output) = run_dbt_and_capture(
["show", "--select", "sample_model second_model"]
)
assert "Previewing node 'sample_model'" in log_output
assert "sample_num" in log_output
assert "sample_bool" in log_output

def test_select_single_model_json(self, project):
run_dbt(["build"])
(results, log_output) = run_dbt_and_capture(
["show", "--select", "sample_model", "--output", "json"]
)
assert "Previewing node 'sample_model'" not in log_output
assert "sample_num" in log_output
assert "sample_bool" in log_output

def test_inline_pass(self, project):
run_dbt(["build"])
(results, log_output) = run_dbt_and_capture(
["show", "--inline", "select * from {{ ref('sample_model') }}"]
)
assert "Previewing inline node" in log_output
assert "sample_num" in log_output
assert "sample_bool" in log_output

def test_inline_fail(self, project):
with pytest.raises(DbtException, match="Error parsing inline query"):
run_dbt(["show", "--inline", "select * from {{ ref('third_model') }}"])

def test_inline_fail_database_error(self, project):
with pytest.raises(DbtRuntimeError, match="Database Error"):
run_dbt(["show", "--inline", "slect asdlkjfsld;j"])

def test_ephemeral_model(self, project):
run_dbt(["build"])
(results, log_output) = run_dbt_and_capture(["show", "--select", "ephemeral_model"])
assert "col_deci" in log_output

def test_second_ephemeral_model(self, project):
run_dbt(["build"])
(results, log_output) = run_dbt_and_capture(
["show", "--inline", models__second_ephemeral_model]
)
assert "col_hundo" in log_output

# test_limit tests ConnectionWrapper.fetchmany()
@pytest.mark.parametrize(
"args,expected",
[
([], 5), # default limit
(["--limit", 3], 3), # fetch 3 rows
(["--limit", -1], 7), # fetch all rows
Comment on lines +99 to +101
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment that this test is used for fetchmany testing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

],
)
def test_limit(self, project, args, expected):
run_dbt(["build"])
dbt_args = ["show", "--inline", models__second_ephemeral_model, *args]
results, log_output = run_dbt_and_capture(dbt_args)
assert len(results.results[0].agate_table) == expected

def test_seed(self, project):
(results, log_output) = run_dbt_and_capture(["show", "--select", "sample_seed"])
assert "Previewing node 'sample_seed'" in log_output

def test_sql_header(self, project):
run_dbt(["build"])
(results, log_output) = run_dbt_and_capture(["show", "--select", "sql_header"])
assert "Asia/Kolkata" in log_output


class TestShowModelVersions:
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": schema_yml,
"sample_model.sql": models__sample_model,
"sample_model_v2.sql": models__second_model,
}

@pytest.fixture(scope="class")
def seeds(self):
return {"sample_seed.csv": seeds__sample_seed}

def test_version_unspecified(self, project):
run_dbt(["build"])
(results, log_output) = run_dbt_and_capture(["show", "--select", "sample_model"])
assert "Previewing node 'sample_model.v1'" in log_output
assert "Previewing node 'sample_model.v2'" in log_output

def test_none(self, project):
run_dbt(["build"])
(results, log_output) = run_dbt_and_capture(["show", "--select", "sample_model.v2"])
assert "Previewing node 'sample_model.v1'" not in log_output
assert "Previewing node 'sample_model.v2'" in log_output


class TestShowPrivateModel:
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": private_model_yml,
"private_model.sql": models__sample_model,
}

@pytest.fixture(scope="class")
def seeds(self):
return {"sample_seed.csv": seeds__sample_seed}

def test_version_unspecified(self, project):
run_dbt(["build"])
run_dbt(["show", "--inline", "select * from {{ ref('private_model') }}"])