Skip to content

Commit

Permalink
Plot error message
Browse files Browse the repository at this point in the history
testcase

Fix

Fix

Fix

test fix

test fix

test fix

changelog
  • Loading branch information
neelasha23 committed Apr 25, 2023
1 parent de483c8 commit 19afb6b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* [Fix] Fixes `%sqlcmd plot` when `--table` or `--column` have spaces (#409)
* [Feature] Add sticky first column styling to sqlcmd profile command
* [Feature] Improve boxplot performance [#152]
* [Feature] Better error message when `percentile_disc` function missing in DB driver (#441)

## 0.7.1 (2023-04-19)

* [Feature] Upgrades SQLAlchemy version to 2
Expand Down
28 changes: 22 additions & 6 deletions src/sql/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from jinja2 import Template

from sql.util import flatten
from sqlalchemy.exc import ProgrammingError
from ploomber_core.exceptions import COMMUNITY

try:
import matplotlib.pyplot as plt
Expand All @@ -25,7 +27,7 @@
from sql import util


def _summary_stats(conn, table, column, with_=None):
def _summary_stats(conn, table, column, with_=None, driver=None):
"""Compute percentiles and mean for boxplot"""

if not conn:
Expand All @@ -44,7 +46,13 @@ def _summary_stats(conn, table, column, with_=None):

query = template.render(table=table, column=column)

values = conn.execute(query, with_).fetchone()
try:
values = conn.execute(query, with_).fetchone()
except ProgrammingError as e:
print(e)
print(f"\nEnsure that percentile_disc function is available on {driver}.")
print(COMMUNITY)

keys = ["q1", "med", "q3", "mean", "N"]
return {k: float(v) for k, v in zip(keys, flatten(values))}

Expand Down Expand Up @@ -124,7 +132,9 @@ def _between(conn, table, column, whislo, whishi, with_=None):

# https://github.com/matplotlib/matplotlib/blob/b5ac96a8980fdb9e59c9fb649e0714d776e26701/lib/matplotlib/cbook/__init__.py
@modify_exceptions
def _boxplot_stats(conn, table, column, whis=1.5, autorange=False, with_=None):
def _boxplot_stats(
conn, table, column, whis=1.5, autorange=False, with_=None, driver=None
):
"""Compute statistics required to create a boxplot"""
if not conn:
conn = sql.connection.Connection.current
Expand All @@ -141,7 +151,8 @@ def _compute_conf_interval(N, med, iqr):
stats = dict()

# arithmetic mean
s_stats = _summary_stats(conn, table, column, with_=with_)

s_stats = _summary_stats(conn, table, column, with_=with_, driver=driver)

stats["mean"] = s_stats["mean"]
q1, med, q3 = s_stats["q1"], s_stats["med"], s_stats["q3"]
Expand Down Expand Up @@ -250,20 +261,25 @@ def boxplot(payload, table, column, *, orient="v", with_=None, conn=None, ax=Non

payload["connection_info"] = conn._get_curr_sqlalchemy_connection_info()

driver = payload["connection_info"]["driver"]

ax = plt.gca()
vert = orient == "v"

set_ticklabels = ax.set_xticklabels if vert else ax.set_yticklabels
set_label = ax.set_ylabel if vert else ax.set_xlabel

if isinstance(column, str):
stats = [_boxplot_stats(conn, table, column, with_=with_)]
stats = [_boxplot_stats(conn, table, column, with_=with_, driver=driver)]
ax.bxp(stats, vert=vert)
ax.set_title(f"{column!r} from {table!r}")
set_label(column)
set_ticklabels([column])
else:
stats = [_boxplot_stats(conn, table, col, with_=with_) for col in column]
stats = [
_boxplot_stats(conn, table, col, with_=with_, driver=driver)
for col in column
]
ax.bxp(stats, vert=vert)
ax.set_title(f"Boxplot from {table!r}")
set_ticklabels(column)
Expand Down
15 changes: 15 additions & 0 deletions src/tests/integration/test_mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,18 @@ def test_sqlplot_boxplot(ip_with_MSSQL, cell):
out = ip_with_MSSQL.run_cell(cell)

assert type(out.result).__name__ in {"Axes", "AxesSubplot"}


def test_sqlplot_boxplot_error_message(ip_with_MSSQL, test_table_name_dict, capsys):
# clean current Axes
plt.cla()
ip_with_MSSQL.run_cell(
f"%sqlplot boxplot --table " f"{test_table_name_dict['taxi']} --column x"
)

out, _ = capsys.readouterr()
assert "Ensure that percentile_disc function is available" in out
assert (
"If you need help solving this issue, "
"send us a message: https://ploomber.io/community" in out
)

0 comments on commit 19afb6b

Please sign in to comment.