---
# System prepended metadata

title: 無標題

---

<style>
    .text-center {
        text-align: center
        
    }
    h1{
        border :none!important
    }
</style>
<h1 class="text-center">
    Assignment 3 Report 1
</h1>
<p class="text-center">110062143, 110062207, 110062208, 110062214</p>

## Implementation

### Lexer

- We add `explain` to the keyword list

### Parser

- We modify function `queryCommand()`. Using flag `isExplain` to check if there's an `explain` keyword in fornt of select command.
- Passing the flag "isExplain" as a parameter to the return value `QueryData `constructor.

![image](https://hackmd.io/_uploads/S1mjA-Wl0.png)

### QueryData

- Adding a `isExplain` variable and initialzied with the passed in parameter `isExplain` in the constructor.
- A public function "isExplain()" is implemented, which return the private variable `isExplain`.

### AS3QueryPlanner

- Modify the properties QUERYPLANNER to AS3QueryPlanner
![image](https://hackmd.io/_uploads/SkxbZzbxA.png)
- Creating a `List<ExplainTreeNode>` to store the information of every plan is the plan tree.
- For example, we add a Node with planeType `TablePlan` and its information
![image](https://hackmd.io/_uploads/BkViWzWe0.png)
- We've noticed that group by plan will call a sort plan by itself, so we will have to get the sort plan and add it to the `explainTree` when we have a group by plan.
- In the end, we create a ExplainPlan if the `isExplain` flag is true and pass the explainTree as a parameter to the ExplainPlan.
![image](https://hackmd.io/_uploads/r1EfGfWg0.png)


### ExplainPlan

- Add field "query-plan" to schema and store the `explainTree` in the constructor.
- pass the `explainTree` and the fields into a new `ExplainScan` in the function `open()`

![image](https://hackmd.io/_uploads/rJzp4GblC.png)

### ExplainScan

- Add a variable `ans` to store the output and an `explainTree` variable.
- We call the function `getPlanTree()` to deal with the information in `explainTree`.
- A variable `visited` is created to make sure `next()` function will only be useful in the first call.

#### getPlanTree()

This function iterate through the List `explainTree` and add the infromation of the plan tree into variable `ans`.


#### getVal()

This function has a parameter fldName, is the fldName is in the field, that is, fldName == "query-plan", we will return the ans. Otherwise, we throw an exception.

![image](https://hackmd.io/_uploads/S1ESUMWxC.png)

### Plan

#### getDetails()

We've implement this function in `Plan` in order to get the details such as predicates and table info when storing the ans.



## Results
- A query accessing single table with `WHERE`
![image](https://hackmd.io/_uploads/S1eYO-be0.png)

- A query accessing multiple tables with `WHERE`
![Screenshot 2024-04-08 at 4.13.56 PM](https://hackmd.io/_uploads/SydDD7Zg0.png)


- A query with `ORDER BY`
![image](https://hackmd.io/_uploads/rkmViZWeC.png)

- A query with `GROUP BY` and at least one aggregation function (`MIN`, `MAX`, `COUNT`, `AVG`... etc.)
![Screenshot 2024-04-08 at 3.42.31 PM](https://hackmd.io/_uploads/B1GblQZeR.png)

