Skip to content

Commit 7e02910

Browse files
disable %sql/%%sql in databricks (#1048)
* don't use `%[%]sql` within Databricks * Update CHANGELOG.md * adjust line comment * pkgmt format * upgrade action * doc fix * doc updates, changelog, version * add test * skip test * lint --------- Co-authored-by: Eduardo Blancas <github@blancas.io>
1 parent 6be0d4b commit 7e02910

File tree

9 files changed

+47
-10
lines changed

9 files changed

+47
-10
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ jobs:
9898
- name: Lint
9999
run: |
100100
python -m pip install --upgrade pip
101-
python -m pip install --upgrade pkgmt codespell nox
101+
python -m pip install --upgrade pkgmt nox
102102
pkgmt lint
103-
codespell
104103
105104
- name: Install dependencies
106105
run: |
@@ -113,7 +112,7 @@ jobs:
113112
nox --session test_unit --no-install --reuse-existing-virtualenvs
114113
115114
- name: Upload failed images artifacts
116-
uses: actions/upload-artifact@v3
115+
uses: actions/upload-artifact@v4
117116
if: failure()
118117
with:
119118
name: failed-image-artifacts ${{ matrix.os }} ${{ matrix.python-version }}
@@ -158,7 +157,7 @@ jobs:
158157
nox --session test_unit_sqlalchemy_one --no-install --reuse-existing-virtualenvs
159158
160159
- name: Upload failed images artifacts sqlalchemyv1
161-
uses: actions/upload-artifact@v3
160+
uses: actions/upload-artifact@v4
162161
if: failure()
163162
with:
164163
name: failed-image-artifacts-sqlalchemy ${{ matrix.os }} ${{ matrix.python-version }}

.readthedocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ conda:
2121
sphinx:
2222
builder: html
2323
fail_on_warning: true
24+
configuration: doc/_config.yml

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# CHANGELOG
22

3-
## 0.10.18dev
3+
## 0.11.0dev
4+
5+
* [API Change] Disabled `%sql` and `%%sql` on Databricks ([#1047](https://github.com/ploomber/jupysql/issues/1047))
46

57
## 0.10.17 (2025-01-08)
68

doc/howto.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ some_engine = create_engine("sqlite:///some.db")
361361

362362
## Use `%sql`/`%%sql` in Databricks
363363

364-
Databricks uses the same name (`%sql`/`%%sql`) for its SQL magics; however, JupySQL exposes a `%jupysql`/`%%jupysql` alias so you can use both:
364+
Databricks uses the same name (`%sql`/`%%sql`) for its SQL magics; however, since this
365+
clashes with Databricks' `%%sql` command, JupySQL will enable an `%jupysql`/`%%jupysql`
366+
alias if a `DATABRICKS_RUNTIME_VERSION` is declared:
365367

366368
```{code-cell} ipython3
367369
%jupysql duckdb://

scripts/large-table-gen.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""Renter large-table-template.sql
2-
"""
1+
"""Renter large-table-template.sql"""
32

43
from pathlib import Path
54
from jinja2 import Template

src/sql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from sql.magic import load_ipython_extension
22

33

4-
__version__ = "0.10.18dev"
4+
__version__ = "0.11.0dev"
55

66

77
__all__ = ["load_ipython_extension"]

src/sql/column_guesser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class Column(list):
99
"Store a column of tabular data; record its name and whether it is numeric"
10+
1011
is_quantity = True
1112
name = ""
1213

src/sql/magic.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import re
34
from pathlib import Path
45

@@ -704,7 +705,7 @@ def get_query_type(command: str):
704705

705706
def set_configs(ip, file_path, alternate_path):
706707
"""Set user defined SqlMagic configuration settings"""
707-
sql = ip.find_cell_magic("sql").__self__
708+
sql = ip.find_cell_magic("jupysql").__self__
708709
user_configs, loaded_from = util.get_user_configs(file_path, alternate_path)
709710
default_configs = util.get_default_configs(sql)
710711
table_rows = []
@@ -769,6 +770,12 @@ def load_SqlMagic_configs(ip):
769770

770771
def load_ipython_extension(ip):
771772
"""Load the magics, this function is executed when the user runs: %load_ext sql"""
773+
774+
# If running within Databricks, do not use the sql magics
775+
if "DATABRICKS_RUNTIME_VERSION" in os.environ:
776+
del SqlMagic.magics["cell"]["sql"]
777+
del SqlMagic.magics["line"]["sql"]
778+
772779
sql_magic = SqlMagic(ip)
773780
_set_sql_magic(sql_magic)
774781
ip.register_magics(sql_magic)

src/tests/test_magic.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
from sql.run.resultset import ResultSet
2424
from sql import magic
2525
from sql.warnings import JupySQLQuotedNamedParametersWarning
26+
from sql._testing import TestingShell
27+
from sql.magic import load_ipython_extension
2628

2729

2830
from conftest import runsql
@@ -2767,3 +2769,27 @@ def test_disabled_named_parameters_shows_disabled_warning(ip):
27672769
)
27682770

27692771
assert expected_warning in str(excinfo.value)
2772+
2773+
2774+
@pytest.mark.skip(
2775+
reason=(
2776+
"Running this breaks all subsequent tests because "
2777+
"TestingShell is a singleton. We need to find a way to isolate this test"
2778+
)
2779+
)
2780+
def test_databricks_sql_magic_disabled(monkeypatch):
2781+
monkeypatch.setenv("DATABRICKS_RUNTIME_VERSION", "10.0.0")
2782+
ip = TestingShell.preconfigured_shell()
2783+
load_ipython_extension(ip)
2784+
2785+
ip.run_cell("%jupysql duckdb://")
2786+
ip.run_cell("%jupysql select 1")
2787+
2788+
with pytest.raises(UsageError) as excinfo_line:
2789+
ip.run_cell("%sql select 1")
2790+
2791+
with pytest.raises(UsageError) as excinfo_cell:
2792+
ip.run_cell("%%sql\nselect 1")
2793+
2794+
assert "Line magic function `%sql` not found" in str(excinfo_line.value)
2795+
assert "Cell magic `%%sql` not found" in str(excinfo_cell.value)

0 commit comments

Comments
 (0)