-
Notifications
You must be signed in to change notification settings - Fork 2
/
query_generator_test.go
104 lines (88 loc) · 2.41 KB
/
query_generator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package nqo
import (
"context"
"fmt"
"testing"
"path/filepath"
"github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder"
"github.com/cockroachdb/cockroach/pkg/sql/opt/xform"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/testutils/datadriven"
)
func TestGenerateJoins(t *testing.T) {
actual := GenerateJoins(8)
expected := 8462
if len(actual) != expected {
t.Fatalf("expected %d joins but found %d", expected, len(actual))
}
//if !reflect.DeepEqual(actual, expected) {
// t.Fatalf("expected joins: %s\nbut found: %s", expected, actual)
//}
}
func TestGenerateQueries(t *testing.T) {
actual := GenerateQueries(3)
expected := 112
if len(actual) != expected {
t.Fatalf("expected %d queries but found %d", expected, len(actual))
}
//if !reflect.DeepEqual(actual, expected) {
// t.Fatalf("expected queries: %s\nbut found: %s", expected, actual)
//}
}
func TestPrintAllQueries(t *testing.T) {
//printAllQueries(t, 8)
printAllEncoded(t)
}
func printAllQueries(t *testing.T, max int) {
actual := GenerateQueries(max)
for _, query := range actual {
fmt.Println(query)
}
}
func printAllEncoded(t *testing.T) {
actual := GenerateQueries(8)
ctx := context.Background()
catalog := NewTestCatalog()
o := xform.NewOptimizer(catalog, xform.OptimizeNone)
path := "testdata/tpch"
t.Run(filepath.Base(path), func(t *testing.T) {
datadriven.RunTest(t, path, func(d *datadriven.TestData) string {
stmt, err := parser.ParseOne(d.Input)
if err != nil {
d.Fatalf(t, "%v", err)
}
switch d.Cmd {
case "exec-ddl":
if stmt.StatementType() != tree.DDL {
d.Fatalf(t, "statement type is not DDL: %v", stmt.StatementType())
}
switch stmt := stmt.(type) {
case *tree.CreateTable:
tbl := catalog.CreateTable(stmt)
return fmt.Sprintf("%s%s", tbl.String(), "\n")
default:
d.Fatalf(t, "expected CREATE TABLE statement but found: %v", stmt)
return ""
}
default:
d.Fatalf(t, "unsupported command: %s", d.Cmd)
return ""
}
})
})
for _, query := range actual {
stmt, err := parser.ParseOne(query)
if err != nil {
t.Fatalf("%v", err)
}
b := optbuilder.New(ctx, o.Factory(), stmt)
root, props, err := b.Build()
if err != nil {
t.Fatalf("error: %v\n", err)
}
exprView := o.Optimize(root, props)
e := encoder{}
fmt.Println(e.encode(exprView))
}
}