> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8c05c8a2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# DataStore execution model

> Understanding lazy evaluation, execution triggers, and caching in DataStore

Understanding DataStore's lazy evaluation model is key to using it effectively and achieving optimal performance.

<h2 id="lazy-evaluation">
  Lazy Evaluation
</h2>

DataStore uses **lazy evaluation** - operations are not executed immediately but are recorded and compiled into optimized SQL queries. Execution happens only when results are actually needed.

<h3 id="lazy-vs-eager">
  Example: Lazy vs Eager
</h3>

```python theme={null}
from pathlib import Path
Path("sales.csv").write_text("""\
region,product,category,amount,quantity,price,date,order_id
East,Widget,Electronics,5200,10,120,2024-01-15,1001
West,Gadget,Electronics,800,5,160,2024-02-20,1002
East,Gizmo,Home,6500,3,100,2024-03-10,1003
North,Widget,Electronics,4500,6,150,2024-06-18,1004
West,Gadget,Electronics,2000,8,250,2024-09-14,1005
""")

from chdb import datastore as pd

ds = pd.read_csv("sales.csv")

# These operations are NOT executed yet
result = (ds
    .filter(ds['amount'] > 1000)    # Recorded, not executed
    .select('region', 'amount')      # Recorded, not executed
    .groupby('region')               # Recorded, not executed
    .agg({'amount': 'sum'})          # Recorded, not executed
    .sort('sum', ascending=False)    # Recorded, not executed
)

# Still no execution - just building the query plan
print(result.to_sql())
# SELECT region, SUM(amount) AS sum
# FROM file('sales.csv', 'CSVWithNames')
# WHERE amount > 1000
# GROUP BY region
# ORDER BY sum DESC

# NOW execution happens
df = result.to_df()  # <-- Triggers execution
```

<h3 id="benefits">
  Benefits of Lazy Evaluation
</h3>

1. **Query Optimization**: Multiple operations compile to a single optimized SQL query
2. **Filter Pushdown**: Filters are applied at the data source level
3. **Column Pruning**: Only needed columns are read
4. **Deferred Decisions**: Execution engine can be chosen at runtime
5. **Plan Inspection**: You can view/debug the query before executing

***

<h2 id="triggers">
  Execution Triggers
</h2>

Execution is triggered automatically when you need actual values:

<h3 id="automatic-triggers">
  Automatic Triggers
</h3>

| Trigger              | Example            | Description        |
| -------------------- | ------------------ | ------------------ |
| `print()` / `repr()` | `print(ds)`        | Display results    |
| `len()`              | `len(ds)`          | Get row count      |
| `.columns`           | `ds.columns`       | Get column names   |
| `.dtypes`            | `ds.dtypes`        | Get column types   |
| `.shape`             | `ds.shape`         | Get dimensions     |
| `.index`             | `ds.index`         | Get row index      |
| `.values`            | `ds.values`        | Get NumPy array    |
| Iteration            | `for row in ds`    | Iterate over rows  |
| `to_df()`            | `ds.to_df()`       | Convert to pandas  |
| `to_pandas()`        | `ds.to_pandas()`   | Alias for to\_df   |
| `to_dict()`          | `ds.to_dict()`     | Convert to dict    |
| `to_numpy()`         | `ds.to_numpy()`    | Convert to array   |
| `.equals()`          | `ds.equals(other)` | Compare DataStores |

**Examples:**

```python theme={null}
# All these trigger execution
print(ds)              # Display
len(ds)                # 1000
ds.columns             # Index(['name', 'age', 'city'])
ds.shape               # (1000, 3)
list(ds)               # List of values
ds.to_df()             # pandas DataFrame
```

<h3 id="stay-lazy">
  Operations That Stay Lazy
</h3>

| Operation              | Returns     | Description           |
| ---------------------- | ----------- | --------------------- |
| `filter()`             | DataStore   | Adds WHERE clause     |
| `select()`             | DataStore   | Adds column selection |
| `sort()`               | DataStore   | Adds ORDER BY         |
| `groupby()`            | LazyGroupBy | Prepares GROUP BY     |
| `join()`               | DataStore   | Adds JOIN             |
| `ds['col']`            | ColumnExpr  | Column reference      |
| `ds[['col1', 'col2']]` | DataStore   | Column selection      |

**Examples:**

```python theme={null}
# These do NOT trigger execution - they stay lazy
result = ds.filter(ds['age'] > 25)      # Returns DataStore
result = ds.select('name', 'age')        # Returns DataStore
result = ds['name']                      # Returns ColumnExpr
result = ds.groupby('city')              # Returns LazyGroupBy
```

***

<h2 id="three-phase">
  Three-Phase Execution
</h2>

DataStore operations follow a three-phase execution model:

<h3 id="phase-1">
  Phase 1: SQL Query Building (Lazy)
</h3>

Operations that can be expressed in SQL are accumulated:

```python theme={null}
result = (ds
    .filter(ds['status'] == 'active')   # WHERE
    .select('user_id', 'amount')         # SELECT
    .groupby('user_id')                  # GROUP BY
    .agg({'amount': 'sum'})              # SUM()
    .sort('sum', ascending=False)        # ORDER BY
    .limit(10)                           # LIMIT
)
# All compiled into one SQL query
```

<h3 id="phase-2">
  Phase 2: Execution Point
</h3>

When a trigger occurs, the accumulated SQL is executed:

```python theme={null}
# Execution triggered here
df = result.to_df()  
# The single optimized SQL query runs now
```

<h3 id="phase-3">
  Phase 3: DataFrame Operations (if any)
</h3>

If you chain pandas-only operations after execution:

```python theme={null}
# Mixed operations
result = (ds
    .filter(ds['amount'] > 100)          # Phase 1: SQL
    .to_df()                             # Phase 2: Execute
    .pivot_table(...)                    # Phase 3: pandas
)
```

***

<h2 id="explain">
  Viewing Execution Plans
</h2>

Use `explain()` to see what will be executed:

```python title="Query" theme={null}
ds = pd.read_csv("sales.csv")

query = (ds
    .filter(ds['amount'] > 1000)
    .groupby('region')
    .agg({'amount': ['sum', 'mean']})
)

# View execution plan
query.explain()
```

```text title="Response" theme={null}
Pipeline:
  1. Source: file('sales.csv', 'CSVWithNames')
  2. Filter: amount > 1000
  3. GroupBy: region
  4. Aggregate: sum(amount), avg(amount)

Generated SQL:
SELECT region, SUM(amount) AS sum, AVG(amount) AS mean
FROM file('sales.csv', 'CSVWithNames')
WHERE amount > 1000
GROUP BY region
```

Use `verbose=True` for more details:

```python theme={null}
query.explain(verbose=True)
```

See [Debugging: explain()](/products/chdb/debugging/explain) for complete documentation.

***

<h2 id="caching">
  Caching
</h2>

DataStore caches execution results to avoid redundant queries.

<h3 id="how-caching">
  How Caching Works
</h3>

```python theme={null}
from pathlib import Path
Path("data.csv").write_text("""\
name,age,city,salary,department
Alice,25,NYC,55000,Engineering
Bob,30,LA,65000,Product
Charlie,35,NYC,80000,Engineering
Diana,28,SF,70000,Design
Eve,42,NYC,95000,Product
""")

ds = pd.read_csv("data.csv")
result = ds.filter(ds['age'] > 25)

# First access - executes query
print(result.shape)  # Executes and caches

# Second access - uses cache
print(result.columns)  # Uses cached result

# Third access - uses cache
df = result.to_df()  # Uses cached result
```

<h3 id="cache-invalidation">
  Cache Invalidation
</h3>

Cache is invalidated when operations modify the DataStore:

```python theme={null}
result = ds.filter(ds['age'] > 25)
print(result.shape)  # Executes, caches

# New operation invalidates cache
result2 = result.filter(result['city'] == 'NYC')
print(result2.shape)  # Re-executes (different query)
```

<h3 id="cache-control">
  Manual Cache Control
</h3>

```python theme={null}
# Clear cache
ds.clear_cache()

# Disable caching
from chdb.datastore.config import config
config.set_cache_enabled(False)
```

***

<h2 id="mixing">
  Mixing SQL and Pandas Operations
</h2>

DataStore intelligently handles operations that mix SQL and pandas:

<h3 id="sql-ops">
  SQL-Compatible Operations
</h3>

These compile to SQL:

* `filter()`, `where()`
* `select()`
* `groupby()`, `agg()`
* `sort()`, `orderby()`
* `limit()`, `offset()`
* `join()`, `union()`
* `distinct()`
* Column operations (math, comparison, string methods)

<h3 id="pandas-ops">
  Pandas-Only Operations
</h3>

These trigger execution and use pandas:

* `apply()` with custom functions
* `pivot_table()` with complex aggregations
* `stack()`, `unstack()`
* Operations on executed DataFrames

<h3 id="hybrid">
  Hybrid Pipelines
</h3>

```python theme={null}
# SQL phase
result = (ds
    .filter(ds['amount'] > 100)      # SQL
    .groupby('category')              # SQL
    .agg({'amount': 'sum'})           # SQL
)

# Execution + pandas phase
result = (result
    .to_df()                          # Execute SQL
    .pivot_table(...)                 # pandas operation
)
```

***

<h2 id="engine-selection">
  Execution Engine Selection
</h2>

DataStore can execute operations using different engines:

<h3 id="auto-mode">
  Auto Mode (Default)
</h3>

```python theme={null}
from chdb.datastore.config import config

config.set_execution_engine('auto')  # Default
# Automatically selects best engine per operation
```

<h3 id="chdb-engine">
  Force chDB Engine
</h3>

```python theme={null}
config.set_execution_engine('chdb')
# All operations use ClickHouse SQL
```

<h3 id="pandas-engine">
  Force pandas Engine
</h3>

```python theme={null}
config.set_execution_engine('pandas')
# All operations use pandas
```

See [Configuration: Execution Engine](/products/chdb/configuration/execution-engine) for details.

***

<h2 id="performance">
  Performance Implications
</h2>

<h3 id="filter-early">
  Good: Filter Early
</h3>

```python theme={null}
# Good: Filter in SQL, then aggregate
result = (ds
    .filter(ds['date'] >= '2024-01-01')  # Reduces data early
    .groupby('category')
    .agg({'amount': 'sum'})
)
```

<h3 id="filter-late">
  Bad: Filter Late
</h3>

```python theme={null}
# Bad: Aggregate all, then filter
result = (ds
    .groupby('category')
    .agg({'amount': 'sum'})
    .to_df()
    .query('sum > 1000')  # Pandas filter after aggregation
)
```

<h3 id="select-early">
  Good: Select Columns Early
</h3>

```python theme={null}
# Good: Select columns in SQL
result = (ds
    .select('user_id', 'amount', 'date')
    .filter(ds['date'] >= '2024-01-01')
    .groupby('user_id')
    .agg({'amount': 'sum'})
)
```

<h3 id="sql-work">
  Good: Let SQL Do the Work
</h3>

```python theme={null}
# Good: Complex aggregation in SQL
result = (ds
    .groupby('category')
    .agg({
        'amount': ['sum', 'mean', 'count'],
        'quantity': 'sum'
    })
    .sort('sum', ascending=False)
    .limit(10)
)
# One SQL query does everything

# Bad: Multiple separate queries
sums = ds.groupby('category')['amount'].sum().to_df()
means = ds.groupby('category')['amount'].mean().to_df()
# Two queries instead of one
```

***

<h2 id="best-practices">
  Best Practices Summary
</h2>

1. **Chain operations before executing** - Build the full query, then trigger once
2. **Filter early** - Reduce data at the source
3. **Select only needed columns** - Column pruning improves performance
4. **Use `explain()` to understand execution** - Debug before running
5. **Let SQL handle aggregations** - ClickHouse is optimized for this
6. **Be aware of execution triggers** - Avoid accidental early execution
7. **Use caching wisely** - Understand when cache is invalidated
