forked from catherinedevlin/ipython-sql
-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
%sqlcmd improvements #341
Merged
tonykploomber
merged 13 commits into
ploomber:master
from
gtduncan:george-sqlcmb-improvements
Apr 18, 2023
+216
−47
Merged
%sqlcmd improvements #341
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
761b2a6
# This is a combination of 6 commits.
gtduncan 7d7ed83
initial commit
gtduncan c00b050
pretty table
gtduncan 09ec61e
pretty print tables
gtduncan 70fefa2
Merge branch 'ploomber:master' into george-sqlcmb-improvements
gtduncan 503fa8b
lint
gtduncan 0944a21
improvements, integration fix
gtduncan 3914a94
lint
gtduncan 385bdfb
Merge branch 'ploomber:master' into george-sqlcmb-improvements
gtduncan 78fd4bd
edits
gtduncan d4d695d
remove extra
gtduncan d52c0fc
test fix
gtduncan 38cf538
typo
gtduncan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
jupytext: | ||
text_representation: | ||
extension: .md | ||
format_name: myst | ||
format_version: 0.13 | ||
jupytext_version: 1.14.4 | ||
kernelspec: | ||
display_name: Python 3 (ipykernel) | ||
language: python | ||
name: python3 | ||
myst: | ||
html_meta: | ||
description lang=en: Test columns from your database in Jupyter via JupySQL | ||
keywords: jupyter, sql, jupysql | ||
property=og:locale: en_US | ||
--- | ||
|
||
|
||
# Testing with sqlcmd | ||
|
||
```{note} | ||
This example uses `SQLite` but the same commands work for other databases. | ||
``` | ||
|
||
```{code-cell} ipython3 | ||
%load_ext sql | ||
%sql sqlite:// | ||
``` | ||
|
||
Let's create a sample table: | ||
|
||
```{code-cell} ipython3 | ||
:tags: [hide-output] | ||
%%sql sqlite:// | ||
CREATE TABLE writer (first_name, last_name, year_of_death); | ||
INSERT INTO writer VALUES ('William', 'Shakespeare', 1616); | ||
INSERT INTO writer VALUES ('Bertold', 'Brecht', 1956); | ||
``` | ||
|
||
|
||
## Run Tests on Column | ||
|
||
Use `%sqlcmd test` to run quantitative tests on your dataset. | ||
|
||
For example, to see if all the values in the column birth_year are less than 2000, we can use: | ||
|
||
```{code-cell} ipython3 | ||
%sqlcmd test --table writer --column year_of_death --less-than 2000 | ||
``` | ||
|
||
Because both William Shakespeare and Bertold Brecht died before the year 2000, this command will return True. | ||
|
||
However, if we were to run: | ||
|
||
```{code-cell} ipython3 | ||
:tags: [raises-exception] | ||
%sqlcmd test --table writer --column year_of_death --greater 1700 | ||
``` | ||
|
||
We see that a value that failed our test was William Shakespeare, as he died in 1616. | ||
|
||
We can also pass several comparator arguments to test: | ||
|
||
```{code-cell} ipython3 | ||
:tags: [raises-exception] | ||
%sqlcmd test --table writer --column year_of_death --greater-or-equal 1616 --less-than-or-equal 1956 | ||
``` | ||
|
||
Here, because Shakespeare died in 1616 and Brecht in 1956, our test passes. | ||
|
||
However, if we search for a window between 1800 and 1900: | ||
|
||
```{code-cell} ipython3 | ||
:tags: [raises-exception] | ||
%sqlcmd test --table writer --column year_of_death --greater 1800 --less-than 1900 | ||
``` | ||
|
||
The test fails, returning both Shakespeare and Brecht. | ||
|
||
Currently, 5 different comparator arguments are supported: `greater`, `greater-or-equal`, `less-than`, `less-than-or-equal`, and `no-nulls`. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure
UsageError
is the right output here.@edublancas thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we raise any other type of error (e.g.,
ValueError
), Jupyter will display a full traceback, which is pretty bad for UX since the user doesn't care about the internal traceback. If we raiseUsageError
, Jupyter only displays the error message. I agreeUsageError
isn't semantically correct but it's better than having the full traceback.opened: #407