# EXPLAIN

Shows generated Intermediate Representation (IR) code, identifying whether it is executed on GPU or CPU. This is primarily used internally by HEAVY.AI to monitor behavior.

```sql
EXPLAIN <STMT>
```

For example, when you use the `EXPLAIN` command on a basic statement, the utility returns 90 lines of IR code that is not meant to be human readable. However, at the top of the listing, a heading indicates whether it is `IR for the CPU` or `IR for the GPU`, which can be useful to know in some situations.

## EXPLAIN CALCITE

Returns a relational algebra tree describing the high-level plan to execute the statement.

```sql
EXPLAIN CALCITE <STMT>
```

The table below lists the relational algebra classes used to describe the execution plan for a SQL statement.

<table data-header-hidden><thead><tr><th width="548">Method</th><th>Description</th></tr></thead><tbody><tr><td>Method</td><td>Description</td></tr><tr><td><code>LogicalAggregate</code></td><td>Operator that eliminates duplicates and computes totals.</td></tr><tr><td><code>LogicalCalc</code></td><td>Expression that computes project expressions and also filters.</td></tr><tr><td><code>LogicalChi</code></td><td>Operator that converts a stream to a relation.</td></tr><tr><td><code>LogicalCorrelate</code></td><td>Operator that performs nested-loop joins.</td></tr><tr><td><code>LogicalDelta</code></td><td>Operator that converts a relation to a stream.</td></tr><tr><td><code>LogicalExchange</code></td><td>Expression that imposes a particular distribution on its input without otherwise changing its content.</td></tr><tr><td><code>LogicalFilter</code></td><td>Expression that iterates over its input and returns elements for which a condition evaluates to true.</td></tr><tr><td><code>LogicalIntersect</code></td><td>Expression that returns the intersection of the rows of its inputs.</td></tr><tr><td><code>LogicalJoin</code></td><td>Expression that combines two relational expressions according to some condition.</td></tr><tr><td><code>LogicalMatch</code></td><td>Expression that represents a MATCH_RECOGNIZE node.</td></tr><tr><td><code>LogicalMinus</code></td><td>Expression that returns the rows of its first input minus any matching rows from its other inputs. Corresponds to the SQL EXCEPT operator.</td></tr><tr><td><code>LogicalProject</code></td><td>Expression that computes a set of ‘select expressions’ from its input relational expression.</td></tr><tr><td><code>LogicalSort</code></td><td>Expression that imposes a particular sort order on its input without otherwise changing its content.</td></tr><tr><td><code>LogicalTableFunctionScan</code></td><td>Expression that calls a table-valued function.</td></tr><tr><td><code>LogicalTableModify</code></td><td>Expression that modifies a table. Similar to TableScan, but represents a request to modify a table instead of read from it.</td></tr><tr><td><code>LogicalTableScan</code></td><td>Reads all the rows from a RelOptTable.</td></tr><tr><td><code>LogicalUnion</code></td><td>Expression that returns the union of the rows of its inputs, optionally eliminating duplicates.</td></tr><tr><td><code>LogicalValues</code></td><td>Expression for which the value is a sequence of zero or more literal row values.</td></tr><tr><td><code>LogicalWindow</code></td><td>Expression representing a set of window aggregates. See <a href="">Window Functions</a></td></tr></tbody></table>

For example, a `SELECT` statement is described as a table scan and projection.

```sql
omnisql> EXPLAIN CALCITE (SELECT * FROM movies);
Explanation
LogicalProject(movieId=[$0], title=[$1], genres=[$2])
   LogicalTableScan(TABLE=[[CATALOG, heavyai, MOVIES]])
```

If you add a sort order, the table projection is folded under a `LogicalSort` procedure.

```sql
omnisql> EXPLAIN calcite (SELECT * FROM movies ORDER BY title);
Explanation
LogicalSort(sort0=[$1], dir0=[ASC])
   LogicalProject(movieId=[$0], title=[$1], genres=[$2])
      LogicalTableScan(TABLE=[[CATALOG, omnisci, MOVIES]])
```

When the SQL statement is simple, the EXPLAIN CALCITE version is actually less “human readable.” EXPLAIN CALCITE is more useful when you work with more complex SQL statements, like the one that follows. This query performs a scan on the BOOK table before scanning the BOOK\_ORDER table.

```sql
omnisql> EXPLAIN calcite SELECT bc.firstname, bc.lastname, b.title, bo.orderdate, s.name
FROM book b, book_customer bc, book_order bo, shipper s
WHERE bo.cust_id = bc.cust_id AND b.book_id = bo.book_id AND bo.shipper_id = s.shipper_id
AND s.name = 'UPS';
Explanation
LogicalProject(firstname=[$5], lastname=[$6], title=[$2], orderdate=[$11], name=[$14])
    LogicalFilter(condition=[AND(=($9, $4), =($0, $8), =($10, $13), =($14, 'UPS'))])
        LogicalJoin(condition=[true], joinType=[INNER])
            LogicalJoin(condition=[true], joinType=[INNER])
                LogicalJoin(condition=[true], joinType=[INNER])
                    LogicalTableScan(TABLE=[[CATALOG, omnisci, BOOK]])
                    LogicalTableScan(TABLE=[[CATALOG, omnisci, BOOK_CUSTOMER]])
                LogicalTableScan(TABLE=[[CATALOG, omnisci, BOOK_ORDER]])
            LogicalTableScan(TABLE=[[CATALOG, omnisci, SHIPPER]])
```

Revising the original SQL command results in a more natural selection order and a more performant query.

```sql
omnisql> EXPLAIN calcite SELECT bc.firstname, bc.lastname, b.title, bo.orderdate, s.name
FROM book_order bo, book_customer bc, book b, shipper s
WHERE bo.cust_id = bc.cust_id AND bo.book_id = b.book_id AND bo.shipper_id = s.shipper_id
AND s.name = 'UPS';
Explanation
LogicalProject(firstname=[$10], lastname=[$11], title=[$7], orderdate=[$3], name=[$14])
    LogicalFilter(condition=[AND(=($1, $9), =($5, $0), =($2, $13), =($14, 'UPS'))])
        LogicalJoin(condition=[true], joinType=[INNER])
            LogicalJoin(condition=[true], joinType=[INNER])
                LogicalJoin(condition=[true], joinType=[INNER])
                  LogicalTableScan(TABLE=[[CATALOG, omnisci, BOOK_ORDER]])
                  LogicalTableScan(TABLE=[[CATALOG, omnisci, BOOK_CUSTOMER]])
                LogicalTableScan(TABLE=[[CATALOG, omnisci, BOOK]])
            LogicalTableScan(TABLE=[[CATALOG, omnisci, SHIPPER]])
```

##
