Skip to content
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 0.7.10dev
* [Feature] Modified `TableDescription` to add styling, generate messages and format the calculated outputs (#459)
* [Feature] Support flexible spacing `myvar=<<` operator ([#525](https://github.com/ploomber/jupysql/issues/525))
* [Feature] Moved `%sqlrender` feature to `%sqlcmd snippets` (#647)

* [Doc] Modified integrations content to ensure they're all consistent (#523)
* [Doc] Document --persist-replace in API section (#539)
* [Fix] Fixed CI issue by updating `invalid_connection_string_duckdb` in `test_magic.py` (#631)
Expand Down
16 changes: 10 additions & 6 deletions doc/api/magic-snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.14.5
jupytext_version: 1.14.6
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
myst:
html_meta:
description lang=en: Documentation for %sqlcmd snippets
from JupySQL
description lang=en: Documentation for %sqlcmd snippets from JupySQL
keywords: jupyter, sql, jupysql, snippets
property=og:locale: en_US
---
Expand Down Expand Up @@ -73,14 +72,22 @@ Returns all the snippets saved in the environment

Arguments:

`{snippet_name}` Return a snippet.

`-d`/`--delete` Delete a snippet.

`-D`/`--delete-force` Force delete a snippet. This may be useful if there are other dependent snippets, and you still need to delete this snippet.

`-A`/`--delete-force-all` Force delete a snippet and all dependent snippets.

```{code-cell} ipython3
cte = %sqlcmd snippets gentoo
print(cte)
Copy link

@neelasha23 neelasha23 Jun 26, 2023

Choose a reason for hiding this comment

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

This is currently displaying

WITH
SELECT * FROM penguins.csv where species == 'Gentoo'

Is it supposed to display WITH even when it is not used as subquery as part of another query?

Also, previously we could sqlrender the final query as well, that uses a snippet as subquery. e.g.
SELECT * from gentoo where bill_length < 50... can you add a similar example ?

Copy link
Author

@bbeat2782 bbeat2782 Jun 26, 2023

Choose a reason for hiding this comment

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

Also, previously we could sqlrender the final query as well, that uses a snippet as subquery. e.g.
SELECT * from gentoo where bill_length < 50... can you add a similar example ?

For this one, when you mentioned sqlrender showing the final query, are you referring to something like the following?
Screen Shot 2023-06-26 at 1 20 06 PM

Choose a reason for hiding this comment

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

@neelasha23: yes, this is a bug we discovered recently. I reported it on #657, @bbeat2782 will work on it.

@bbeat2782: please work on this first, and then tackle the other one

```

This returns the stored snippet `gentoo`.

```{code-cell} ipython3
%sqlcmd snippets -d gentoo
```

Expand All @@ -94,7 +101,6 @@ To demonstrate `force-delete` let's create a snippet dependent on `chinstrap` sn
%%sql --save chinstrap_sub
SELECT * FROM chinstrap where island == 'Dream'
```
+++

Trying to delete the `chinstrap` snippet will display an error message:

Expand All @@ -107,7 +113,6 @@ Trying to delete the `chinstrap` snippet will display an error message:
If you still wish to delete this snippet, you can run the below command:

```{code-cell} ipython3

%sqlcmd snippets -D chinstrap
```

Expand All @@ -130,6 +135,5 @@ SELECT * FROM chinstrap where island == 'Dream'
Now, force delete `chinstrap` and its dependent `chinstrap_sub`:

```{code-cell} ipython3

%sqlcmd snippets -A chinstrap
```
7 changes: 7 additions & 0 deletions src/sql/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class RenderMagic(Magics):
@telemetry.log_call("sqlrender")
def sqlrender(self, line):
args = parse_argstring(self.sqlrender, line)
warnings.warn(
"'%sqlrender' will be deprecated soon, "
f"please use '%sqlcmd snippets {args.line[0]}' instead. "
"For documentation, follow this link : "
"https://jupysql.ploomber.io/en/latest/api/magic-snippets.html#id1",
FutureWarning,
)
return str(store[args.line[0]])


Expand Down
3 changes: 3 additions & 0 deletions src/sql/sqlcmd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from sql.magic_cmd import CmdParser
from sql import util
from sql.exceptions import UsageError
from sql.store import store


def _modify_display_msg(key, remaining_keys, dependent_keys=None):
Expand Down Expand Up @@ -58,6 +59,8 @@ def sqlcmd_snippets(others):
help="Force delete all stored snippets",
required=False,
)
if len(others) == 1 and others[0] in util.get_all_keys():
return str(store[others[0]])
args = parser.parse_args(others)
SNIPPET_ARGS = [args.delete, args.delete_force, args.delete_force_all]
if SNIPPET_ARGS.count(None) == len(SNIPPET_ARGS):
Expand Down
8 changes: 3 additions & 5 deletions src/tests/test_magic_cte.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def test_trailing_semicolons_removed_from_cte(ip):
"""
)

cell_final_query = ip.run_cell(
"%sqlrender final --with positive_x --with positive_y"
)
cell_final_query = ip.run_cell("%sqlcmd snippets final")

assert cell_execution.success
assert cell_final_query.result == (
Expand All @@ -51,7 +49,7 @@ def test_infer_dependencies(ip, capsys):
"SELECT last_name FROM author_sub;",
)
out, _ = capsys.readouterr()
result = ip.run_cell("%sqlrender final").result
result = ip.run_cell("%sqlcmd snippets final").result
expected = (
"WITH `author_sub` AS (\nSELECT last_name FROM author "
"WHERE year_of_death > 1900)\nSELECT last_name FROM author_sub;"
Expand Down Expand Up @@ -168,7 +166,7 @@ def test_snippets_delete(ip, capsys):
INNER JOIN customers ON o.customer_id=customers.customer_id;
""",
)
result = ip.run_cell("%sqlrender final").result
result = ip.run_cell("%sqlcmd snippets final").result
expected = (
"WITH\n\n SELECT o.order_id, customers.name, "
"o.order_value\n "
Expand Down
10 changes: 6 additions & 4 deletions src/tests/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ def test_data_frame_telemetry_execution(mock_log_api, ip, simple_file_path_iris)
)


def test_sqlrender_telemetry_execution(mock_log_api, ip, simple_file_path_iris):
# Simulate the sqlrender query
def test_sqlcmd_snippets_query_telemetry_execution(
mock_log_api, ip, simple_file_path_iris
):
# Simulate the sqlcmd snippets query
ip.run_cell("%sql duckdb://")
ip.run_cell(
"%sql --save class_setosa --no-execute "
Expand All @@ -122,10 +124,10 @@ def test_sqlrender_telemetry_execution(mock_log_api, ip, simple_file_path_iris):
+ "')"
+ " WHERE class='Iris-setosa'"
)
ip.run_cell("%sqlrender class_setosa")
ip.run_cell("%sqlcmd snippets class_setosa")

mock_log_api.assert_called_with(
action="jupysql-sqlrender-success", total_runtime=ANY, metadata=ANY
action="jupysql-execute-success", total_runtime=ANY, metadata=ANY
)


Expand Down