Skip to content

Commit

Permalink
made existing SELECT query test cases not includint OR to use optimiz…
Browse files Browse the repository at this point in the history
…er and these passed. (under all column having skip list index condition)
  • Loading branch information
ryogrid committed Oct 5, 2023
1 parent 2b873ca commit a9c8144
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
19 changes: 17 additions & 2 deletions planner/optimizer/selinger_optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,19 @@ func rewiteColNameStrOfBinaryOpExp(tableMap map[string][]*string, exp interface{
}
}

func CheckIncludesORInPredicate(exp interface{}) bool {
switch casted := exp.(type) {
case *parser.BinaryOpExpression:
if casted.LogicalOperationType_ == expression.OR {
return true
} else {
return CheckIncludesORInPredicate(casted.Left_) || CheckIncludesORInPredicate(casted.Right_)
}
default:
return false
}
}

func genTableMapAndColList(c *catalog.Catalog, qi *parser.QueryInfo) (map[string][]*string, []*parser.SelectFieldExpression) {
tableMap := make(map[string][]*string, 0)
colList := make([]*parser.SelectFieldExpression, 0)
Expand Down Expand Up @@ -623,8 +636,10 @@ func RewriteQueryInfo(c *catalog.Catalog, qi *parser.QueryInfo) (*parser.QueryIn
return nil, err
}

// attach predicate of ON clause to one of WHERE clause
qi.WhereExpression_ = qi.WhereExpression_.AppendBinaryOpExpWithAnd(qi.OnExpressions_)
if !(qi.OnExpressions_.Left_ == nil && qi.OnExpressions_.Right_ == nil) {
// attach predicate of ON clause to one of WHERE clause
qi.WhereExpression_ = qi.WhereExpression_.AppendBinaryOpExpWithAnd(qi.OnExpressions_)
}

return qi, nil
}
20 changes: 16 additions & 4 deletions planner/simple_planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ryogrid/SamehadaDB/execution/expression"
"github.com/ryogrid/SamehadaDB/execution/plans"
"github.com/ryogrid/SamehadaDB/parser"
"github.com/ryogrid/SamehadaDB/planner/optimizer"
"github.com/ryogrid/SamehadaDB/storage/access"
"github.com/ryogrid/SamehadaDB/storage/buffer"
"github.com/ryogrid/SamehadaDB/storage/index/index_constants"
Expand Down Expand Up @@ -89,6 +90,11 @@ func (pner *SimplePlanner) MakeSelectPlanWithoutJoin() (error, plans.Plan) {
return nil, plans.NewSeqScanPlanNode(pner.catalog_, outSchema, predicate, tableMetadata.OID())
}

func (pner *SimplePlanner) MakeOptimizedSelectPlanWithJoin() (error, plans.Plan) {
optPlan, err := optimizer.NewSelingerOptimizer(pner.qi, pner.catalog_).Optimize()
return err, optPlan
}

func (pner *SimplePlanner) MakeSelectPlanWithJoin() (error, plans.Plan) {
tblNameL := *pner.qi.JoinTables_[0]
tableMetadataL := pner.catalog_.GetTableByName(tblNameL)
Expand Down Expand Up @@ -218,10 +224,15 @@ func (pner *SimplePlanner) MakeSelectPlanWithJoin() (error, plans.Plan) {
}

func (pner *SimplePlanner) MakeSelectPlan() (error, plans.Plan) {
if len(pner.qi.JoinTables_) == 1 {
return pner.MakeSelectPlanWithoutJoin()
if optimizer.CheckIncludesORInPredicate(pner.qi.WhereExpression_) {
// optimizer does not support OR, so use planning logic without optimization...
if len(pner.qi.JoinTables_) == 1 {
return pner.MakeSelectPlanWithoutJoin()
} else {
return pner.MakeSelectPlanWithJoin()
}
} else {
return pner.MakeSelectPlanWithJoin()
return pner.MakeOptimizedSelectPlanWithJoin()
}
}

Expand Down Expand Up @@ -258,7 +269,8 @@ func (pner *SimplePlanner) MakeCreateTablePlan() (error, plans.Plan) {

columns := make([]*column.Column, 0)
for _, cdefExp := range pner.qi.ColDefExpressions_ {
columns = append(columns, column.NewColumn(*cdefExp.ColName_, *cdefExp.ColType_, false, index_constants.INDEX_KIND_INVALID, types.PageID(-1), nil))
//columns = append(columns, column.NewColumn(*cdefExp.ColName_, *cdefExp.ColType_, false, index_constants.INDEX_KIND_INVALID, types.PageID(-1), nil))
columns = append(columns, column.NewColumn(*cdefExp.ColName_, *cdefExp.ColType_, false, index_constants.INDEX_KIND_SKIP_LIST, types.PageID(-1), nil))
}
schema_ := schema.NewSchema(columns)

Expand Down
2 changes: 2 additions & 0 deletions samehada/samehada.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ryogrid/SamehadaDB/execution/plans"
"github.com/ryogrid/SamehadaDB/parser"
"github.com/ryogrid/SamehadaDB/planner"
"github.com/ryogrid/SamehadaDB/planner/optimizer"
"github.com/ryogrid/SamehadaDB/recovery/log_recovery"
"github.com/ryogrid/SamehadaDB/samehada/samehada_util"
"github.com/ryogrid/SamehadaDB/storage/access"
Expand Down Expand Up @@ -157,6 +158,7 @@ func (sdb *SamehadaDB) ExecuteSQL(sqlStr string) (error, [][]interface{}) {

func (sdb *SamehadaDB) ExecuteSQLRetValues(sqlStr string) (error, [][]*types.Value) {
qi := parser.ProcessSQLStr(&sqlStr)
qi, _ = optimizer.RewriteQueryInfo(sdb.catalog_, qi)
txn := sdb.shi_.transaction_manager.Begin(nil)
err, plan := sdb.planner_.MakePlan(qi, txn)

Expand Down

0 comments on commit a9c8144

Please sign in to comment.