diff --git a/examples/integrations/sqlalchemy example.ipynb b/examples/integrations/sqlalchemy example.ipynb new file mode 100644 index 00000000..07f0eea1 --- /dev/null +++ b/examples/integrations/sqlalchemy example.ipynb @@ -0,0 +1,862 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "86850a2b-3753-4de5-99bd-eccc834c3b26", + "metadata": {}, + "source": [ + "### Install dependencies" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "baab9af8-190d-49c6-b159-4905bae6a830", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Processing /Users/ovcharuk/work/ydb-sqlalchemy/dist/ydb_sqlalchemy-0.0.1b23-py2.py3-none-any.whl\n", + "Collecting sqlalchemy<3.0.0,>=2.0.7 (from ydb-sqlalchemy==0.0.1b23)\n", + " Using cached SQLAlchemy-2.0.36-cp39-cp39-macosx_11_0_arm64.whl.metadata (9.7 kB)\n", + "Collecting ydb>=3.18.8 (from ydb-sqlalchemy==0.0.1b23)\n", + " Using cached ydb-3.18.8-py2.py3-none-any.whl.metadata (2.1 kB)\n", + "Collecting ydb-dbapi>=0.0.1b8 (from ydb-sqlalchemy==0.0.1b23)\n", + " Using cached ydb_dbapi-0.0.1b8-py3-none-any.whl.metadata (1.7 kB)\n", + "Collecting typing-extensions>=4.6.0 (from sqlalchemy<3.0.0,>=2.0.7->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached typing_extensions-4.12.2-py3-none-any.whl.metadata (3.0 kB)\n", + "Collecting grpcio>=1.42.0 (from ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached grpcio-1.67.1-cp39-cp39-macosx_10_9_universal2.whl.metadata (3.9 kB)\n", + "Collecting packaging (from ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached packaging-24.1-py3-none-any.whl.metadata (3.2 kB)\n", + "Collecting protobuf<5.0.0,>=3.13.0 (from ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached protobuf-4.25.5-cp37-abi3-macosx_10_9_universal2.whl.metadata (541 bytes)\n", + "Collecting aiohttp<4 (from ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached aiohttp-3.10.10-cp39-cp39-macosx_11_0_arm64.whl.metadata (7.6 kB)\n", + "Collecting aiohappyeyeballs>=2.3.0 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached aiohappyeyeballs-2.4.3-py3-none-any.whl.metadata (6.1 kB)\n", + "Collecting aiosignal>=1.1.2 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached aiosignal-1.3.1-py3-none-any.whl.metadata (4.0 kB)\n", + "Collecting attrs>=17.3.0 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached attrs-24.2.0-py3-none-any.whl.metadata (11 kB)\n", + "Collecting frozenlist>=1.1.1 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached frozenlist-1.5.0-cp39-cp39-macosx_11_0_arm64.whl.metadata (13 kB)\n", + "Collecting multidict<7.0,>=4.5 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached multidict-6.1.0-cp39-cp39-macosx_11_0_arm64.whl.metadata (5.0 kB)\n", + "Collecting yarl<2.0,>=1.12.0 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached yarl-1.17.1-cp39-cp39-macosx_11_0_arm64.whl.metadata (64 kB)\n", + "Collecting async-timeout<5.0,>=4.0 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached async_timeout-4.0.3-py3-none-any.whl.metadata (4.2 kB)\n", + "Collecting idna>=2.0 (from yarl<2.0,>=1.12.0->aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached idna-3.10-py3-none-any.whl.metadata (10 kB)\n", + "Collecting propcache>=0.2.0 (from yarl<2.0,>=1.12.0->aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached propcache-0.2.0-cp39-cp39-macosx_11_0_arm64.whl.metadata (7.7 kB)\n", + "Using cached SQLAlchemy-2.0.36-cp39-cp39-macosx_11_0_arm64.whl (2.1 MB)\n", + "Using cached ydb-3.18.8-py2.py3-none-any.whl (793 kB)\n", + "Using cached ydb_dbapi-0.0.1b8-py3-none-any.whl (14 kB)\n", + "Using cached aiohttp-3.10.10-cp39-cp39-macosx_11_0_arm64.whl (391 kB)\n", + "Using cached grpcio-1.67.1-cp39-cp39-macosx_10_9_universal2.whl (11.0 MB)\n", + "Using cached protobuf-4.25.5-cp37-abi3-macosx_10_9_universal2.whl (394 kB)\n", + "Using cached typing_extensions-4.12.2-py3-none-any.whl (37 kB)\n", + "Using cached packaging-24.1-py3-none-any.whl (53 kB)\n", + "Using cached aiohappyeyeballs-2.4.3-py3-none-any.whl (14 kB)\n", + "Using cached aiosignal-1.3.1-py3-none-any.whl (7.6 kB)\n", + "Using cached async_timeout-4.0.3-py3-none-any.whl (5.7 kB)\n", + "Using cached attrs-24.2.0-py3-none-any.whl (63 kB)\n", + "Using cached frozenlist-1.5.0-cp39-cp39-macosx_11_0_arm64.whl (52 kB)\n", + "Using cached multidict-6.1.0-cp39-cp39-macosx_11_0_arm64.whl (29 kB)\n", + "Using cached yarl-1.17.1-cp39-cp39-macosx_11_0_arm64.whl (91 kB)\n", + "Using cached idna-3.10-py3-none-any.whl (70 kB)\n", + "Using cached propcache-0.2.0-cp39-cp39-macosx_11_0_arm64.whl (46 kB)\n", + "Installing collected packages: typing-extensions, protobuf, propcache, packaging, idna, grpcio, frozenlist, attrs, async-timeout, aiohappyeyeballs, sqlalchemy, multidict, aiosignal, yarl, aiohttp, ydb, ydb-dbapi, ydb-sqlalchemy\n", + " Attempting uninstall: typing-extensions\n", + " Found existing installation: typing_extensions 4.12.2\n", + " Uninstalling typing_extensions-4.12.2:\n", + " Successfully uninstalled typing_extensions-4.12.2\n", + " Attempting uninstall: protobuf\n", + " Found existing installation: protobuf 4.25.5\n", + " Uninstalling protobuf-4.25.5:\n", + " Successfully uninstalled protobuf-4.25.5\n", + " Attempting uninstall: propcache\n", + " Found existing installation: propcache 0.2.0\n", + " Uninstalling propcache-0.2.0:\n", + " Successfully uninstalled propcache-0.2.0\n", + " Attempting uninstall: packaging\n", + " Found existing installation: packaging 24.1\n", + " Uninstalling packaging-24.1:\n", + " Successfully uninstalled packaging-24.1\n", + " Attempting uninstall: idna\n", + " Found existing installation: idna 3.10\n", + " Uninstalling idna-3.10:\n", + " Successfully uninstalled idna-3.10\n", + " Attempting uninstall: grpcio\n", + " Found existing installation: grpcio 1.67.1\n", + " Uninstalling grpcio-1.67.1:\n", + " Successfully uninstalled grpcio-1.67.1\n", + " Attempting uninstall: frozenlist\n", + " Found existing installation: frozenlist 1.5.0\n", + " Uninstalling frozenlist-1.5.0:\n", + " Successfully uninstalled frozenlist-1.5.0\n", + " Attempting uninstall: attrs\n", + " Found existing installation: attrs 24.2.0\n", + " Uninstalling attrs-24.2.0:\n", + " Successfully uninstalled attrs-24.2.0\n", + " Attempting uninstall: async-timeout\n", + " Found existing installation: async-timeout 4.0.3\n", + " Uninstalling async-timeout-4.0.3:\n", + " Successfully uninstalled async-timeout-4.0.3\n", + " Attempting uninstall: aiohappyeyeballs\n", + " Found existing installation: aiohappyeyeballs 2.4.3\n", + " Uninstalling aiohappyeyeballs-2.4.3:\n", + " Successfully uninstalled aiohappyeyeballs-2.4.3\n", + " Attempting uninstall: sqlalchemy\n", + " Found existing installation: SQLAlchemy 2.0.36\n", + " Uninstalling SQLAlchemy-2.0.36:\n", + " Successfully uninstalled SQLAlchemy-2.0.36\n", + " Attempting uninstall: multidict\n", + " Found existing installation: multidict 6.1.0\n", + " Uninstalling multidict-6.1.0:\n", + " Successfully uninstalled multidict-6.1.0\n", + " Attempting uninstall: aiosignal\n", + " Found existing installation: aiosignal 1.3.1\n", + " Uninstalling aiosignal-1.3.1:\n", + " Successfully uninstalled aiosignal-1.3.1\n", + " Attempting uninstall: yarl\n", + " Found existing installation: yarl 1.17.1\n", + " Uninstalling yarl-1.17.1:\n", + " Successfully uninstalled yarl-1.17.1\n", + " Attempting uninstall: aiohttp\n", + " Found existing installation: aiohttp 3.10.10\n", + " Uninstalling aiohttp-3.10.10:\n", + " Successfully uninstalled aiohttp-3.10.10\n", + " Attempting uninstall: ydb\n", + " Found existing installation: ydb 3.18.8\n", + " Uninstalling ydb-3.18.8:\n", + " Successfully uninstalled ydb-3.18.8\n", + " Attempting uninstall: ydb-dbapi\n", + " Found existing installation: ydb-dbapi 0.0.1b8\n", + " Uninstalling ydb-dbapi-0.0.1b8:\n", + " Successfully uninstalled ydb-dbapi-0.0.1b8\n", + " Attempting uninstall: ydb-sqlalchemy\n", + " Found existing installation: ydb-sqlalchemy 0.0.1b23\n", + " Uninstalling ydb-sqlalchemy-0.0.1b23:\n", + " Successfully uninstalled ydb-sqlalchemy-0.0.1b23\n", + "Successfully installed aiohappyeyeballs-2.4.3 aiohttp-3.10.10 aiosignal-1.3.1 async-timeout-4.0.3 attrs-24.2.0 frozenlist-1.5.0 grpcio-1.67.1 idna-3.10 multidict-6.1.0 packaging-24.1 propcache-0.2.0 protobuf-4.25.5 sqlalchemy-2.0.36 typing-extensions-4.12.2 yarl-1.17.1 ydb-3.18.8 ydb-dbapi-0.0.1b8 ydb-sqlalchemy-0.0.1b23\n", + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.1.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "# Change into ydb-sqlalchemy from pypi after release\n", + "%pip install --force-reinstall /Users/ovcharuk/work/ydb-sqlalchemy/dist/ydb_sqlalchemy-0.0.1b23-py2.py3-none-any.whl" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "1bac5cad-31e1-4dcb-a8c9-5cac24899220", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: pandas in ./.venv/lib/python3.9/site-packages (2.2.3)\n", + "Requirement already satisfied: jupysql in ./.venv/lib/python3.9/site-packages (0.10.14)\n", + "Requirement already satisfied: matplotlib in ./.venv/lib/python3.9/site-packages (3.9.2)\n", + "Requirement already satisfied: numpy>=1.22.4 in ./.venv/lib/python3.9/site-packages (from pandas) (2.0.2)\n", + "Requirement already satisfied: python-dateutil>=2.8.2 in ./.venv/lib/python3.9/site-packages (from pandas) (2.9.0.post0)\n", + "Requirement already satisfied: pytz>=2020.1 in ./.venv/lib/python3.9/site-packages (from pandas) (2024.2)\n", + "Requirement already satisfied: tzdata>=2022.7 in ./.venv/lib/python3.9/site-packages (from pandas) (2024.2)\n", + "Requirement already satisfied: prettytable in ./.venv/lib/python3.9/site-packages (from jupysql) (3.12.0)\n", + "Requirement already satisfied: sqlalchemy in ./.venv/lib/python3.9/site-packages (from jupysql) (2.0.36)\n", + "Requirement already satisfied: sqlparse in ./.venv/lib/python3.9/site-packages (from jupysql) (0.5.1)\n", + "Requirement already satisfied: ipython-genutils>=0.1.0 in ./.venv/lib/python3.9/site-packages (from jupysql) (0.2.0)\n", + "Requirement already satisfied: jinja2 in ./.venv/lib/python3.9/site-packages (from jupysql) (3.1.4)\n", + "Requirement already satisfied: sqlglot>=11.3.7 in ./.venv/lib/python3.9/site-packages (from jupysql) (25.28.0)\n", + "Requirement already satisfied: jupysql-plugin>=0.4.2 in ./.venv/lib/python3.9/site-packages (from jupysql) (0.4.5)\n", + "Requirement already satisfied: ploomber-core>=0.2.7 in ./.venv/lib/python3.9/site-packages (from jupysql) (0.2.25)\n", + "Requirement already satisfied: contourpy>=1.0.1 in ./.venv/lib/python3.9/site-packages (from matplotlib) (1.3.0)\n", + "Requirement already satisfied: cycler>=0.10 in ./.venv/lib/python3.9/site-packages (from matplotlib) (0.12.1)\n", + "Requirement already satisfied: fonttools>=4.22.0 in ./.venv/lib/python3.9/site-packages (from matplotlib) (4.54.1)\n", + "Requirement already satisfied: kiwisolver>=1.3.1 in ./.venv/lib/python3.9/site-packages (from matplotlib) (1.4.7)\n", + "Requirement already satisfied: packaging>=20.0 in ./.venv/lib/python3.9/site-packages (from matplotlib) (24.1)\n", + "Requirement already satisfied: pillow>=8 in ./.venv/lib/python3.9/site-packages (from matplotlib) (11.0.0)\n", + "Requirement already satisfied: pyparsing>=2.3.1 in ./.venv/lib/python3.9/site-packages (from matplotlib) (3.2.0)\n", + "Requirement already satisfied: importlib-resources>=3.2.0 in ./.venv/lib/python3.9/site-packages (from matplotlib) (6.4.5)\n", + "Requirement already satisfied: zipp>=3.1.0 in ./.venv/lib/python3.9/site-packages (from importlib-resources>=3.2.0->matplotlib) (3.20.2)\n", + "Requirement already satisfied: pyyaml in ./.venv/lib/python3.9/site-packages (from ploomber-core>=0.2.7->jupysql) (6.0.2)\n", + "Requirement already satisfied: posthog in ./.venv/lib/python3.9/site-packages (from ploomber-core>=0.2.7->jupysql) (3.7.0)\n", + "Requirement already satisfied: six>=1.5 in ./.venv/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n", + "Requirement already satisfied: MarkupSafe>=2.0 in ./.venv/lib/python3.9/site-packages (from jinja2->jupysql) (3.0.2)\n", + "Requirement already satisfied: wcwidth in ./.venv/lib/python3.9/site-packages (from prettytable->jupysql) (0.2.13)\n", + "Requirement already satisfied: typing-extensions>=4.6.0 in ./.venv/lib/python3.9/site-packages (from sqlalchemy->jupysql) (4.12.2)\n", + "Requirement already satisfied: requests<3.0,>=2.7 in ./.venv/lib/python3.9/site-packages (from posthog->ploomber-core>=0.2.7->jupysql) (2.32.3)\n", + "Requirement already satisfied: monotonic>=1.5 in ./.venv/lib/python3.9/site-packages (from posthog->ploomber-core>=0.2.7->jupysql) (1.6)\n", + "Requirement already satisfied: backoff>=1.10.0 in ./.venv/lib/python3.9/site-packages (from posthog->ploomber-core>=0.2.7->jupysql) (2.2.1)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in ./.venv/lib/python3.9/site-packages (from requests<3.0,>=2.7->posthog->ploomber-core>=0.2.7->jupysql) (3.4.0)\n", + "Requirement already satisfied: idna<4,>=2.5 in ./.venv/lib/python3.9/site-packages (from requests<3.0,>=2.7->posthog->ploomber-core>=0.2.7->jupysql) (3.10)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in ./.venv/lib/python3.9/site-packages (from requests<3.0,>=2.7->posthog->ploomber-core>=0.2.7->jupysql) (2.2.3)\n", + "Requirement already satisfied: certifi>=2017.4.17 in ./.venv/lib/python3.9/site-packages (from requests<3.0,>=2.7->posthog->ploomber-core>=0.2.7->jupysql) (2024.8.30)\n", + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.1.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "%pip install pandas jupysql matplotlib" + ] + }, + { + "cell_type": "markdown", + "id": "b9b9db69-80a9-4ebb-b80c-18041ea01e37", + "metadata": {}, + "source": [ + "## Actions with YDB" + ] + }, + { + "cell_type": "markdown", + "id": "c025c448-f6f9-48a1-8926-3a67cf29f81e", + "metadata": {}, + "source": [ + "### Load DataFrame to work with" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "009110c9-10b1-4643-9609-a7d3dcc90529", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + " | id | \n", + "sex | \n", + "age | \n", + "income | \n", + "spending_score | \n", + "
---|---|---|---|---|---|
0 | \n", + "1 | \n", + "Male | \n", + "19 | \n", + "15 | \n", + "39 | \n", + "
1 | \n", + "2 | \n", + "Male | \n", + "21 | \n", + "15 | \n", + "81 | \n", + "
2 | \n", + "3 | \n", + "Female | \n", + "20 | \n", + "16 | \n", + "6 | \n", + "
3 | \n", + "4 | \n", + "Female | \n", + "23 | \n", + "16 | \n", + "77 | \n", + "
4 | \n", + "5 | \n", + "Female | \n", + "31 | \n", + "17 | \n", + "40 | \n", + "
... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "
195 | \n", + "196 | \n", + "Female | \n", + "35 | \n", + "120 | \n", + "79 | \n", + "
196 | \n", + "197 | \n", + "Female | \n", + "45 | \n", + "126 | \n", + "28 | \n", + "
197 | \n", + "198 | \n", + "Male | \n", + "32 | \n", + "126 | \n", + "74 | \n", + "
198 | \n", + "199 | \n", + "Male | \n", + "32 | \n", + "137 | \n", + "18 | \n", + "
199 | \n", + "200 | \n", + "Male | \n", + "30 | \n", + "137 | \n", + "83 | \n", + "
200 rows × 5 columns
\n", + "\n", + " | age | \n", + "id | \n", + "income | \n", + "sex | \n", + "spending_score | \n", + "
---|---|---|---|---|---|
0 | \n", + "19 | \n", + "1 | \n", + "15 | \n", + "Male | \n", + "39 | \n", + "
1 | \n", + "21 | \n", + "2 | \n", + "15 | \n", + "Male | \n", + "81 | \n", + "
2 | \n", + "20 | \n", + "3 | \n", + "16 | \n", + "Female | \n", + "6 | \n", + "
3 | \n", + "23 | \n", + "4 | \n", + "16 | \n", + "Female | \n", + "77 | \n", + "
4 | \n", + "31 | \n", + "5 | \n", + "17 | \n", + "Female | \n", + "40 | \n", + "
... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "
195 | \n", + "35 | \n", + "196 | \n", + "120 | \n", + "Female | \n", + "79 | \n", + "
196 | \n", + "45 | \n", + "197 | \n", + "126 | \n", + "Female | \n", + "28 | \n", + "
197 | \n", + "32 | \n", + "198 | \n", + "126 | \n", + "Male | \n", + "74 | \n", + "
198 | \n", + "32 | \n", + "199 | \n", + "137 | \n", + "Male | \n", + "18 | \n", + "
199 | \n", + "30 | \n", + "200 | \n", + "137 | \n", + "Male | \n", + "83 | \n", + "
200 rows × 5 columns
\n", + "sex | \n", + "spending_score | \n", + "
---|---|
Female | \n", + "40 | \n", + "
Female | \n", + "6 | \n", + "
Male | \n", + "3 | \n", + "
Male | \n", + "14 | \n", + "
Female | \n", + "99 | \n", + "
Female | \n", + "15 | \n", + "
Male | \n", + "13 | \n", + "
Female | \n", + "35 | \n", + "
Male | \n", + "29 | \n", + "
Female | \n", + "98 | \n", + "