Skip to content

Commit

Permalink
docs: update SQL section of the README (pola-rs#13529)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie authored Jan 8, 2024
1 parent 7ff3bf7 commit 3478ba8
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,18 @@ shape: (5, 8)
## SQL

```python
>>> # create a sql context
>>> context = pl.SQLContext()
>>> # register a table
>>> table = pl.scan_ipc("file.arrow")
>>> context.register("my_table", table)
>>> # the query we want to run
>>> df = pl.scan_ipc("file.arrow")
>>> # create a sql context, registering the frame as a table
>>> sql = pl.SQLContext(my_table=df)
>>> # create a sql query to execute
>>> query = """
... SELECT sum(v1) as sum_v1, min(v2) as min_v2 FROM my_table
... WHERE id1 = 'id016'
... LIMIT 10
... SELECT sum(v1) as sum_v1, min(v2) as min_v2 FROM my_table
... WHERE id1 = 'id016'
... LIMIT 10
... """
>>> ## OPTION 1
>>> # run query to materialization
>>> context.query(query)
>>> # run the query, materializing as a DataFrame
>>> sql.execute(query, eager=True)
shape: (1, 2)
┌────────┬────────┐
│ sum_v1 ┆ min_v2 │
Expand All @@ -125,17 +123,17 @@ shape: (5, 8)
2982681
└────────┴────────┘
>>> ## OPTION 2
>>> # Don't materialize the query, but return as LazyFrame
>>> # and continue in Python
>>> lf = context.execute(query)
>>> # run the query but don't immediately materialize the result.
>>> # this returns a LazyFrame that you can continue to operate on.
>>> lf = sql.execute(query)
>>> (lf.join(other_table)
... .group_by("foo")
... .agg(
... pl.col("sum_v1").count()
... ).collect())
```

SQL commands can also be ran directly from your terminal using the Polars CLI:
SQL commands can also be run directly from your terminal using the Polars CLI:

```bash
# run an inline sql query
Expand Down

0 comments on commit 3478ba8

Please sign in to comment.