|
| 1 | +--- |
| 2 | +title: "SQL Query" |
| 3 | +weight: 8 |
| 4 | +type: docs |
| 5 | +aliases: |
| 6 | + - /pypaimon/sql.html |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- |
| 10 | +Licensed to the Apache Software Foundation (ASF) under one |
| 11 | +or more contributor license agreements. See the NOTICE file |
| 12 | +distributed with this work for additional information |
| 13 | +regarding copyright ownership. The ASF licenses this file |
| 14 | +to you under the Apache License, Version 2.0 (the |
| 15 | +"License"); you may not use this file except in compliance |
| 16 | +with the License. You may obtain a copy of the License at |
| 17 | +
|
| 18 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | +
|
| 20 | +Unless required by applicable law or agreed to in writing, |
| 21 | +software distributed under the License is distributed on an |
| 22 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 23 | +KIND, either express or implied. See the License for the |
| 24 | +specific language governing permissions and limitations |
| 25 | +under the License. |
| 26 | +--> |
| 27 | + |
| 28 | +# SQL Query |
| 29 | + |
| 30 | +PyPaimon supports executing SQL queries on Paimon tables, powered by [pypaimon-rust](https://github.com/apache/paimon-rust/tree/main/bindings/python) and [DataFusion](https://datafusion.apache.org/python/). |
| 31 | + |
| 32 | +## Installation |
| 33 | + |
| 34 | +SQL query support requires additional dependencies. Install them with: |
| 35 | + |
| 36 | +```shell |
| 37 | +pip install pypaimon[sql] |
| 38 | +``` |
| 39 | + |
| 40 | +This will install `pypaimon-rust` and `datafusion`. |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +Create a `SQLContext`, register one or more catalogs, and run SQL queries. |
| 45 | + |
| 46 | +### Basic Query |
| 47 | + |
| 48 | +```python |
| 49 | +from pypaimon import CatalogFactory, SQLContext |
| 50 | + |
| 51 | +catalog = CatalogFactory.create({"warehouse": "/path/to/warehouse"}) |
| 52 | + |
| 53 | +ctx = SQLContext() |
| 54 | +ctx.register_catalog("paimon", catalog) |
| 55 | +ctx.set_current_catalog("paimon") |
| 56 | +ctx.set_current_database("default") |
| 57 | + |
| 58 | +# Execute SQL and get PyArrow Table |
| 59 | +table = ctx.sql("SELECT * FROM my_table") |
| 60 | +print(table) |
| 61 | + |
| 62 | +# Convert to Pandas DataFrame |
| 63 | +df = table.to_pandas() |
| 64 | +print(df) |
| 65 | +``` |
| 66 | + |
| 67 | +### Table Reference Format |
| 68 | + |
| 69 | +The default catalog and default database can be configured via `set_current_catalog()` and `set_current_database()`, so you can reference tables in two ways: |
| 70 | + |
| 71 | +```python |
| 72 | +# Direct table name (uses default database) |
| 73 | +ctx.sql("SELECT * FROM my_table") |
| 74 | + |
| 75 | +# Two-part: database.table |
| 76 | +ctx.sql("SELECT * FROM mydb.my_table") |
| 77 | +``` |
| 78 | + |
| 79 | +### Filtering |
| 80 | + |
| 81 | +```python |
| 82 | +table = ctx.sql(""" |
| 83 | + SELECT id, name, age |
| 84 | + FROM users |
| 85 | + WHERE age > 18 AND city = 'Beijing' |
| 86 | +""") |
| 87 | +``` |
| 88 | + |
| 89 | +### Aggregation |
| 90 | + |
| 91 | +```python |
| 92 | +table = ctx.sql(""" |
| 93 | + SELECT city, COUNT(*) AS cnt, AVG(age) AS avg_age |
| 94 | + FROM users |
| 95 | + GROUP BY city |
| 96 | + ORDER BY cnt DESC |
| 97 | +""") |
| 98 | +``` |
| 99 | + |
| 100 | +### Join |
| 101 | + |
| 102 | +```python |
| 103 | +table = ctx.sql(""" |
| 104 | + SELECT u.name, o.order_id, o.amount |
| 105 | + FROM users u |
| 106 | + JOIN orders o ON u.id = o.user_id |
| 107 | + WHERE o.amount > 100 |
| 108 | +""") |
| 109 | +``` |
| 110 | + |
| 111 | +### Subquery |
| 112 | + |
| 113 | +```python |
| 114 | +table = ctx.sql(""" |
| 115 | + SELECT * FROM users |
| 116 | + WHERE id IN ( |
| 117 | + SELECT user_id FROM orders |
| 118 | + WHERE amount > 1000 |
| 119 | + ) |
| 120 | +""") |
| 121 | +``` |
| 122 | + |
| 123 | +### Cross-Database Query |
| 124 | + |
| 125 | +```python |
| 126 | +# Query a table in another database using two-part syntax |
| 127 | +table = ctx.sql(""" |
| 128 | + SELECT u.name, o.amount |
| 129 | + FROM default.users u |
| 130 | + JOIN analytics.orders o ON u.id = o.user_id |
| 131 | +""") |
| 132 | +``` |
| 133 | + |
| 134 | +### Multi-Catalog Query |
| 135 | + |
| 136 | +`SQLContext` supports registering multiple catalogs for cross-catalog queries: |
| 137 | + |
| 138 | +```python |
| 139 | +from pypaimon import CatalogFactory, SQLContext |
| 140 | + |
| 141 | +catalog_a = CatalogFactory.create({"warehouse": "/path/to/warehouse_a"}) |
| 142 | +catalog_b = CatalogFactory.create({ |
| 143 | + "metastore": "rest", |
| 144 | + "uri": "http://localhost:8080", |
| 145 | + "warehouse": "warehouse_b", |
| 146 | +}) |
| 147 | + |
| 148 | +ctx = SQLContext() |
| 149 | +ctx.register_catalog("a", catalog_a) |
| 150 | +ctx.register_catalog("b", catalog_b) |
| 151 | +ctx.set_current_catalog("a") |
| 152 | +ctx.set_current_database("default") |
| 153 | + |
| 154 | +# Cross-catalog join |
| 155 | +table = ctx.sql(""" |
| 156 | + SELECT a_users.name, b_orders.amount |
| 157 | + FROM a.default.users AS a_users |
| 158 | + JOIN b.default.orders AS b_orders ON a_users.id = b_orders.user_id |
| 159 | +""") |
| 160 | +``` |
| 161 | + |
| 162 | +## Supported SQL Syntax |
| 163 | + |
| 164 | +The SQL engine is powered by Apache DataFusion, which supports a rich set of SQL syntax including: |
| 165 | + |
| 166 | +- `SELECT`, `WHERE`, `GROUP BY`, `HAVING`, `ORDER BY`, `LIMIT` |
| 167 | +- `JOIN` (INNER, LEFT, RIGHT, FULL, CROSS) |
| 168 | +- Subqueries and CTEs (`WITH`) |
| 169 | +- Aggregate functions (`COUNT`, `SUM`, `AVG`, `MIN`, `MAX`, etc.) |
| 170 | +- Window functions (`ROW_NUMBER`, `RANK`, `LAG`, `LEAD`, etc.) |
| 171 | +- `UNION`, `INTERSECT`, `EXCEPT` |
| 172 | + |
| 173 | +For the full SQL reference, see the [DataFusion SQL documentation](https://datafusion.apache.org/user-guide/sql/index.html). |
0 commit comments