Skip to content

Commit

Permalink
Merge branch 'master' into cted
Browse files Browse the repository at this point in the history
  • Loading branch information
neelasha23 authored May 31, 2023
2 parents acfa369 + 0cf58e2 commit e5f0665
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM mcr.microsoft.com/devcontainers/anaconda:0-3

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>
23 changes: 23 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// For format details, see https://aka.ms/devcontainer.json.
{
"name": "Jupyter",
"build": {
"context": "..",
"dockerfile": "Dockerfile"
},
"features": {
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {}
},
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"ms-toolsai.jupyter",
"GitHub.codespaces"
]
}
},
"postCreateCommand": "./.devcontainer/postCreate.sh"
}
13 changes: 13 additions & 0 deletions .devcontainer/postCreate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash -x

conda init bash

# Perform install instructions from
# https://ploomber-contributing.readthedocs.io/en/latest/contributing/setup.html
conda create --name ploomber-base python=3.10 --yes
conda activate ploomber-base
pip install pkgmt
pkgmt setup --doc

# After the devcontainer comes up, you can just enable the jupysql conda env:
# conda activate jupysql
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@ nosetests.xml
.venv

# Do not include test output
/result_images/
/result_images/

# Ignore Github codespace build artifact
oryx-build-commands.txt
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

* [Feature] Automated dependency inference when creating CTEs. `--with` is now deprecated and will display a warning. (#166)

* [Fix] Fix CTE generation when the snippets have trailing semicolons

* [Doc] Added Howto documentation for enabling JupyterLab cell runtime display ([#448](https://github.com/ploomber/jupysql/issues/448))


## 0.7.6 (2023-05-29)

* [Feature] Add `%sqlcmd explore` to explore tables interactively ([#330](https://github.com/ploomber/jupysql/issues/330))
Expand All @@ -16,6 +21,8 @@

* [Doc] documenting how to manage connections with `Connection` object ([#282](https://github.com/ploomber/jupysql/issues/282))

* [Feature] Github Codespace (Devcontainer) support for development (by [@jorisroovers](https://github.com/jorisroovers)) ([#484](https://github.com/ploomber/jupysql/issues/484))

## 0.7.5 (2023-05-24)

* [Feature] Using native DuckDB `.df()` method when using `autopandas`
Expand Down
1 change: 1 addition & 0 deletions doc/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ parts:
- file: howto/csv
- file: howto/autocompletion
- file: howto/format-sql
- file: howto/benchmarking-time

- caption: Tutorials
chapters:
Expand Down
14 changes: 13 additions & 1 deletion doc/community/developer-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,19 @@ myst:

# Developer guide

Before continuing, ensure you have a working [development environment.](https://ploomber-contributing.readthedocs.io/en/latest/contributing/setup.html)
Before continuing, ensure you have a [working development environment locally](https://ploomber-contributing.readthedocs.io/en/latest/contributing/setup.html) or on [github codespaces](https://github.com/features/codespaces).

## Github Codespace

Github Codespaces allow you to spin up a fully configured dev environment in the cloud in a few minutes. Github provides 60 hours a month of free usage (for a 2-core codespace). While codespaces will automatically pauze after 30 min of idle time, it's a good idea to shut your codespace down entirely via [the management dashboard](https://github.com/codespaces) and to [setup spending limits](https://github.com/settings/billing/spending_limit) to avoid unexpected charges.

![JupySQL github codespace](../static/github-codespace.png)
You can launch a new github codespace from the green "Code" button on [the JupySQL github repository](https://github.com/ploomber/jupysql).

Note that setup will take a few minutes to finish after the codespace becomes available (wait for the **postCreateCommand** step to finish).
![JupySQL github codespace](../static/github-codespace-setup.png)

After the codespace has finished setting up, you can run `conda activate jupysql` to activate the JupySQL Conda environment.

+++

Expand Down
48 changes: 48 additions & 0 deletions doc/howto/benchmarking-time.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
jupytext:
notebook_metadata_filter: myst
text_representation:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.14.5
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
myst:
html_meta:
description lang=en: Display cell runtime in JupyterLab
keywords: jupyter, jupyterlab, sql
property=og:locale: en_US
---

# Benchmarking runtime
To record the time taken to run each cell
in JupyterLab, we suggest using `jupyterlab-execute-time`

## Installation

```sh
pip install jupyterlab_execute_time
```

## Usage
This plugin displays the metadata collected by the
JupyterLab notebook, to ensure that the time is collected
as part of the metadata, enable the record-time feature in
notebook settings
`Settings -> Notebook -> Recording timing`

### Change notebook settings

![syntax](../static/benchmarking-time_1.png)

### Sample notebook

![syntax](../static/benchmarking-time_2.png)

Each executed cell shows the last executed time
and the runtime

![syntax](../static/benchmarking-time_3.png)
Binary file added doc/static/benchmarking-time_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/static/benchmarking-time_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/static/benchmarking-time_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/static/github-codespace-setup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/static/github-codespace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 11 additions & 3 deletions src/sql/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ def __str__(self) -> str:
` (backtick)
"""
with_clause_template = Template(
"""WITH{% for name in with_ %} {{name}} AS ({{saved[name]._query}})\
"""WITH{% for name in with_ %} {{name}} AS ({{rts(saved[name]._query)}})\
{{ "," if not loop.last }}{% endfor %}{{query}}"""
)
with_clause_template_backtick = Template(
"""WITH{% for name in with_ %} `{{name}}` AS ({{saved[name]._query}})\
"""WITH{% for name in with_ %} `{{name}}` AS ({{rts(saved[name]._query)}})\
{{ "," if not loop.last }}{% endfor %}{{query}}"""
)
is_use_backtick = sql.connection.Connection.current.is_use_backtick_template()
Expand All @@ -131,10 +131,18 @@ def __str__(self) -> str:
with_clause_template_backtick if is_use_backtick else with_clause_template
)
return template.render(
query=self._query, saved=self._store._data, with_=with_all
query=self._query,
saved=self._store._data,
with_=with_all,
rts=_remove_trailing_semicolon,
)


def _remove_trailing_semicolon(query):
query_ = query.rstrip()
return query_[:-1] if query_[-1] == ";" else query


def _get_dependencies(store, keys):
"""Get a list of all dependencies to reconstruct the CTEs in keys"""
# get the dependencies for each key
Expand Down
34 changes: 34 additions & 0 deletions src/tests/test_magic_cte.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def test_trailing_semicolons_removed_from_cte(ip):
ip.run_cell(
"""%%sql --save positive_x
SELECT * FROM number_table WHERE x > 0;
"""
)

ip.run_cell(
"""%%sql --save positive_y
SELECT * FROM number_table WHERE y > 0;
"""
)

cell_execution = ip.run_cell(
"""%%sql --save final --with positive_x --with positive_y
SELECT * FROM positive_x
UNION
SELECT * FROM positive_y;
"""
)

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

assert cell_execution.success
assert cell_final_query.result == (
"WITH `positive_x` AS (\nSELECT * "
"FROM number_table WHERE x > 0), `positive_y` AS (\nSELECT * "
"FROM number_table WHERE y > 0)\nSELECT * FROM positive_x\n"
"UNION\nSELECT * FROM positive_y;"
)

0 comments on commit e5f0665

Please sign in to comment.