forked from yyun543/minidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshow_tables_test.go
More file actions
154 lines (133 loc) · 4.45 KB
/
show_tables_test.go
File metadata and controls
154 lines (133 loc) · 4.45 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package test
import (
"fmt"
"strings"
"testing"
"github.com/apache/arrow/go/v18/arrow"
"github.com/apache/arrow/go/v18/arrow/array"
"github.com/stretchr/testify/assert"
"github.com/yyun543/minidb/internal/catalog"
"github.com/yyun543/minidb/internal/executor"
"github.com/yyun543/minidb/internal/optimizer"
"github.com/yyun543/minidb/internal/parser"
"github.com/yyun543/minidb/internal/session"
"github.com/yyun543/minidb/internal/storage"
"github.com/yyun543/minidb/internal/types"
)
// Helper functions
func getArrayValue(arr arrow.Array, index int) string {
if arr.IsNull(index) {
return "NULL"
}
switch arr := arr.(type) {
case *array.Int64:
return fmt.Sprintf("%d", arr.Value(index))
case *array.Float64:
return fmt.Sprintf("%.2f", arr.Value(index))
case *array.String:
return arr.Value(index)
case *array.Boolean:
return fmt.Sprintf("%t", arr.Value(index))
default:
return "unknown"
}
}
func formatBatch(batch *types.Batch, headers []string) string {
if batch == nil || batch.NumRows() == 0 {
return "(no data)"
}
var output strings.Builder
output.WriteString("Headers: [")
for i, header := range headers {
if i > 0 {
output.WriteString(", ")
}
output.WriteString(header)
}
output.WriteString("] Rows: ")
record := batch.Record()
for row := int64(0); row < record.NumRows(); row++ {
output.WriteString("[")
for col := int64(0); col < record.NumCols(); col++ {
if col > 0 {
output.WriteString(", ")
}
array := record.Column(int(col))
output.WriteString(getArrayValue(array, int(row)))
}
output.WriteString("] ")
}
return output.String()
}
// TestShowTablesIssue 测试SHOW TABLES命令应该显示已创建的表
func TestShowTablesIssue(t *testing.T) {
engine, err := storage.NewMemTable("show_tables_test.wal")
assert.NoError(t, err)
defer engine.Close()
err = engine.Open()
assert.NoError(t, err)
cat := catalog.CreateTemporaryCatalog(engine)
sessMgr, err := session.NewSessionManager()
assert.NoError(t, err)
sess := sessMgr.CreateSession()
opt := optimizer.NewOptimizer()
exec := executor.NewExecutor(cat)
execSQL := func(sql string) (string, error) {
stmt, err := parser.Parse(sql)
if err != nil {
return "", err
}
plan, err := opt.Optimize(stmt)
if err != nil {
return "", err
}
result, err := exec.Execute(plan, sess)
if err != nil {
return "", err
}
// Convert result to string for inspection
if result != nil && len(result.Batches()) > 0 {
batch := result.Batches()[0]
return formatBatch(batch, result.GetHeaders()), nil
}
return "(no results)", nil
}
// 创建数据库
_, err = execSQL("CREATE DATABASE test_show_tables")
assert.NoError(t, err)
sess.CurrentDB = "test_show_tables"
t.Log("✓ Database created")
// 创建两个表
_, err = execSQL("CREATE TABLE users (id INTEGER, name VARCHAR(255))")
assert.NoError(t, err)
t.Log("✓ Table 'users' created")
_, err = execSQL("CREATE TABLE products (id INTEGER, name VARCHAR(255), price FLOAT)")
assert.NoError(t, err)
t.Log("✓ Table 'products' created")
// 插入一些数据以确保表真的存在
_, err = execSQL("INSERT INTO users (id, name) VALUES (1, 'Alice')")
assert.NoError(t, err)
_, err = execSQL("INSERT INTO products (id, name, price) VALUES (1, 'Widget', 19.99)")
assert.NoError(t, err)
// 验证表中有数据
result, err := execSQL("SELECT * FROM users")
assert.NoError(t, err)
assert.NotEqual(t, "(no results)", result, "users table should have data")
t.Logf("✓ users table has data: %v", result)
result, err = execSQL("SELECT * FROM products")
assert.NoError(t, err)
assert.NotEqual(t, "(no results)", result, "products table should have data")
t.Logf("✓ products table has data: %v", result)
// 这是关键测试 - SHOW TABLES应该列出刚创建的表
t.Log("Testing SHOW TABLES command...")
result, err = execSQL("SHOW TABLES")
assert.NoError(t, err, "SHOW TABLES should not return error")
t.Logf("SHOW TABLES result: %v", result)
// 验证结果不应该是 "(no tables)" 或 "(no results)"
assert.NotEqual(t, "(no results)", result, "SHOW TABLES should return table names")
assert.NotContains(t, strings.ToLower(result), "no tables", "SHOW TABLES should not say 'no tables'")
// 验证结果应该包含我们创建的表名
assert.Contains(t, result, "users", "SHOW TABLES should include 'users' table")
assert.Contains(t, result, "products", "SHOW TABLES should include 'products' table")
t.Log("✅ SHOW TABLES test PASSED - tables are properly listed")
}