Skip to content

Commit

Permalink
Updates for blog post
Browse files Browse the repository at this point in the history
  • Loading branch information
eatonphil committed May 1, 2020
1 parent 8d18cee commit 40eadd4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
31 changes: 12 additions & 19 deletions cmd/indextest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"math/rand"
"os"
"runtime"
"strconv"
Expand All @@ -16,15 +15,13 @@ var lastId = 0
var firstId = 0

func doInsert(mb gosql.Backend) {
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
parser := gosql.Parser{}
for i := 0; i < inserts; i++ {
lastId = r.Intn(inserts * 10)
lastId = i
if i == 0 {
firstId = lastId
}
ast, err := parser.Parse(fmt.Sprintf("INSERT INTO users VALUES (%d, %d)", lastId, i))
ast, err := parser.Parse(fmt.Sprintf("INSERT INTO users VALUES (%d)", lastId))
if err != nil {
panic(err)
}
Expand All @@ -38,7 +35,7 @@ func doInsert(mb gosql.Backend) {

func doSelect(mb gosql.Backend) {
parser := gosql.Parser{}
ast, err := parser.Parse(fmt.Sprintf("SELECT id, inc FROM users WHERE id = %d", lastId))
ast, err := parser.Parse(fmt.Sprintf("SELECT id FROM users WHERE id = %d", lastId))
if err != nil {
panic(err)
}
Expand All @@ -52,11 +49,11 @@ func doSelect(mb gosql.Backend) {
panic("Expected 1 row")
}

if int(*r.Rows[0][1].AsInt()) != inserts-1 {
if int(*r.Rows[0][0].AsInt()) != inserts-1 {
panic(fmt.Sprintf("Bad row, got: %d", r.Rows[0][1].AsInt()))
}

ast, err = parser.Parse(fmt.Sprintf("SELECT id, inc FROM users WHERE id = %d", firstId))
ast, err = parser.Parse(fmt.Sprintf("SELECT id FROM users WHERE id = %d", firstId))
if err != nil {
panic(err)
}
Expand All @@ -70,7 +67,7 @@ func doSelect(mb gosql.Backend) {
panic("Expected 1 row")
}

if int(*r.Rows[0][1].AsInt()) != 0 {
if int(*r.Rows[0][0].AsInt()) != 0 {
panic(fmt.Sprintf("Bad row, got: %d", r.Rows[0][1].AsInt()))
}
}
Expand Down Expand Up @@ -100,8 +97,13 @@ func main() {
}
}

primaryKey := ""
if index {
primaryKey = " PRIMARY KEY"
}

parser := gosql.Parser{}
ast, err := parser.Parse("CREATE TABLE users (id INT, inc INT); CREATE INDEX id_idx ON users (id);")
ast, err := parser.Parse(fmt.Sprintf("CREATE TABLE users (id INT%s)", primaryKey))
if err != nil {
panic(err)
}
Expand All @@ -119,14 +121,5 @@ func main() {

perf("INSERT", mb, doInsert)

if index {
perf("CREATE INDEX", mb, func(b gosql.Backend) {
err = mb.CreateIndex(ast.Statements[1].CreateIndexStatement)
if err != nil {
panic(err)
}
})
}

perf("SELECT", mb, doSelect)
}
14 changes: 7 additions & 7 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (i *index) applicableValue(exp expression) *expression {
}

if valueExp.kind != literalKind {
fmt.Println("Only equality checks on literals supported")
fmt.Println("Only index checks on literals supported")
return nil
}

Expand Down Expand Up @@ -554,6 +554,12 @@ func (mb *MemoryBackend) Select(slct *SelectStatement) (*Results, error) {
t.rows = [][]memoryCell{{}}
}

for _, iAndE := range t.getApplicableIndexes(slct.where) {
index := iAndE.i
exp := iAndE.e
t = index.newTableFromSubset(t, exp)
}

// Expand SELECT * at the AST level into a SELECT on all columns
finalItems := []*selectItem{}
for _, item := range *slct.item {
Expand Down Expand Up @@ -581,12 +587,6 @@ func (mb *MemoryBackend) Select(slct *SelectStatement) (*Results, error) {
}
}

for _, iAndE := range t.getApplicableIndexes(slct.where) {
index := iAndE.i
exp := iAndE.e
t = index.newTableFromSubset(t, exp)
}

for i := range t.rows {
result := []Cell{}
isFirstRow := len(results) == 0
Expand Down

0 comments on commit 40eadd4

Please sign in to comment.