Skip to content

Commit 6eca395

Browse files
authored
Merge pull request #431 from kuba--/update-regression
parse and analyze regression queries
2 parents f68dd7f + ddabf46 commit 6eca395

File tree

4 files changed

+56
-27
lines changed

4 files changed

+56
-27
lines changed

_testdata/regression.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
Name: 'Create pilosa index on language UDF'
3030
Statements:
3131
- CREATE INDEX language_idx ON files USING pilosa (language(file_path, blob_content)) WITH (async = false)
32+
- DROP INDEX language_idx ON files
3233
-
3334
ID: 'query6'
3435
Name: 'Create pilosalib index on language UDF'
3536
Statements:
3637
- CREATE INDEX language_idx ON files USING pilosalib (language(file_path, blob_content)) WITH (async = false)
38+
- DROP INDEX language_idx ON files
3739
-
3840
ID: 'query7'
3941
Name: 'Query by language using the pilosa index'
@@ -52,19 +54,21 @@
5254
ID: 'query9'
5355
Name: 'Query all files from HEAD'
5456
Statements:
55-
- SELECT cf.file_path, f.blob_content FROM ref_commits r NATURAL JOIN commit_files cf NATURAL JOIN files f WHERE r.ref_name = 'HEAD' AND r.index = 0
57+
- SELECT cf.file_path, f.blob_content FROM ref_commits r NATURAL JOIN commit_files cf NATURAL JOIN files f WHERE r.ref_name = 'HEAD' AND r.history_index = 0
5658
-
5759
ID: 'query10'
5860
Name: 'Get all LICENSE blobs using pilosa index'
5961
Statements:
6062
- CREATE INDEX file_path_idx ON files USING pilosa (file_path) WITH (async = false)
6163
- SELECT blob_content FROM files WHERE file_path = 'LICENSE'
64+
- DROP INDEX file_path_idx ON files
6265
-
6366
ID: 'query11'
6467
Name: 'Get all LICENSE blobs using pilosalib index'
6568
Statements:
6669
- CREATE INDEX file_path_idx ON files USING pilosalib (file_path) WITH (async = false)
6770
- SELECT blob_content FROM files WHERE file_path = 'LICENSE'
71+
- DROP INDEX file_path_idx ON files
6872
-
6973
ID: 'query12'
7074
Name: '10 top repos by file count in HEAD'

cmd/gitbase/command/server.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ func (l *jaegerLogrus) Error(s string) {
6666
l.Entry.Error(s)
6767
}
6868

69+
func NewDatabaseEngine(readonly bool, version string) *sqle.Engine {
70+
catalog := sql.NewCatalog()
71+
ab := analyzer.NewBuilder(catalog)
72+
if readonly {
73+
ab = ab.ReadOnly()
74+
}
75+
a := ab.Build()
76+
engine := sqle.New(catalog, a, &sqle.Config{
77+
VersionPostfix: version,
78+
})
79+
80+
return engine
81+
}
82+
6983
// Execute starts a new gitbase server based on provided configuration, it
7084
// honors the go-flags.Commander interface.
7185
func (c *Server) Execute(args []string) error {
@@ -74,7 +88,7 @@ func (c *Server) Execute(args []string) error {
7488
}
7589

7690
if err := c.buildDatabase(); err != nil {
77-
logrus.WithField("error", err).Fatal("unable to start database server")
91+
logrus.WithField("error", err).Fatal("unable to initialize database engine")
7892
return err
7993
}
8094

@@ -136,15 +150,7 @@ func (c *Server) Execute(args []string) error {
136150

137151
func (c *Server) buildDatabase() error {
138152
if c.engine == nil {
139-
catalog := sql.NewCatalog()
140-
ab := analyzer.NewBuilder(catalog)
141-
if c.ReadOnly {
142-
ab = ab.ReadOnly()
143-
}
144-
a := ab.Build()
145-
c.engine = sqle.New(catalog, a, &sqle.Config{
146-
VersionPostfix: c.Version,
147-
})
153+
c.engine = NewDatabaseEngine(c.ReadOnly, c.Version)
148154
}
149155

150156
c.pool = gitbase.NewRepositoryPool()

integration_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"testing"
1111

12+
"github.com/src-d/gitbase/cmd/gitbase/command"
1213
"github.com/src-d/gitbase/internal/rule"
1314

1415
"github.com/src-d/gitbase"
@@ -793,7 +794,6 @@ func deleteIndex(
793794

794795
func setup(t testing.TB) (*sqle.Engine, *gitbase.RepositoryPool, func()) {
795796
t.Helper()
796-
engine := newBaseEngine()
797797
require.NoError(t, fixtures.Init())
798798
cleanup := func() {
799799
require.NoError(t, fixtures.Clean())
@@ -804,24 +804,26 @@ func setup(t testing.TB) (*sqle.Engine, *gitbase.RepositoryPool, func()) {
804804
pool.AddGitWithID("worktree", f.Worktree().Root())
805805
}
806806

807-
return engine, pool, cleanup
807+
return newBaseEngine(), pool, cleanup
808808
}
809809

810810
func newSquashEngine() *sqle.Engine {
811-
catalog := sql.NewCatalog()
812-
analyzer := analyzer.NewBuilder(catalog).
811+
engine := newBaseEngine()
812+
dbname := engine.Analyzer.CurrentDatabase
813+
814+
engine.Catalog.RegisterFunctions(sqlfunction.Defaults)
815+
engine.Analyzer = analyzer.NewBuilder(engine.Catalog).
813816
AddPostAnalyzeRule(rule.SquashJoinsRule, rule.SquashJoins).
814817
Build()
815-
e := sqle.New(catalog, analyzer, &sqle.Config{VersionPostfix: "test"})
816-
e.AddDatabase(gitbase.NewDatabase("foo"))
817-
e.Catalog.RegisterFunctions(sqlfunction.Defaults)
818-
e.Catalog.RegisterFunctions(function.Functions)
819-
return e
818+
engine.Analyzer.CurrentDatabase = dbname
819+
return engine
820820
}
821821

822822
func newBaseEngine() *sqle.Engine {
823-
engine := sqle.NewDefault()
824-
engine.AddDatabase(gitbase.NewDatabase("foo"))
823+
foo := gitbase.NewDatabase("foo")
824+
engine := command.NewDatabaseEngine(false, "test")
825+
826+
engine.AddDatabase(foo)
825827
engine.Catalog.RegisterFunctions(function.Functions)
826828
return engine
827829
}

regression_test.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package gitbase_test
33
import (
44
"context"
55
"io/ioutil"
6+
"os"
67
"testing"
78

89
"github.com/src-d/gitbase"
910
"github.com/stretchr/testify/require"
1011
"gopkg.in/src-d/go-mysql-server.v0/sql"
11-
"gopkg.in/src-d/go-mysql-server.v0/sql/parse"
12+
"gopkg.in/src-d/go-mysql-server.v0/sql/index/pilosa"
13+
"gopkg.in/src-d/go-mysql-server.v0/sql/index/pilosalib"
1214
yaml "gopkg.in/yaml.v2"
1315
)
1416

@@ -18,20 +20,35 @@ type Query struct {
1820
Statements []string `yaml:"Statements"`
1921
}
2022

21-
func TestParseRegressionQueries(t *testing.T) {
23+
func TestRegressionQueries(t *testing.T) {
2224
require := require.New(t)
2325

24-
queries, err := loadQueriesYaml("./_testdata/regression.yml")
26+
engine, pool, cleanup := setup(t)
27+
defer cleanup()
28+
29+
tmpDir, err := ioutil.TempDir(os.TempDir(), "pilosa-idx-gitbase")
2530
require.NoError(err)
31+
defer os.RemoveAll(tmpDir)
32+
engine.Catalog.RegisterIndexDriver(pilosa.NewIndexDriver(tmpDir))
33+
engine.Catalog.RegisterIndexDriver(pilosalib.NewDriver(tmpDir))
2634

2735
ctx := sql.NewContext(
2836
context.TODO(),
29-
sql.WithSession(gitbase.NewSession(gitbase.NewRepositoryPool())),
37+
sql.WithSession(gitbase.NewSession(pool)),
3038
)
3139

40+
queries, err := loadQueriesYaml("./_testdata/regression.yml")
41+
require.NoError(err)
42+
3243
for _, q := range queries {
3344
for _, stmt := range q.Statements {
34-
if _, err := parse.Parse(ctx, stmt); err != nil {
45+
_, iter, err := engine.Query(ctx, stmt)
46+
if err != nil {
47+
require.Failf(err.Error(), "ID: %s, Name: %s, Statement: %s", q.ID, q.Name, stmt)
48+
}
49+
50+
_, err = sql.RowIterToRows(iter)
51+
if err != nil {
3552
require.Failf(err.Error(), "ID: %s, Name: %s, Statement: %s", q.ID, q.Name, stmt)
3653
}
3754
}

0 commit comments

Comments
 (0)