Skip to content

Commit fb067c8

Browse files
committed
include table comment in table status
1 parent b11b01e commit fb067c8

File tree

5 files changed

+124
-79
lines changed

5 files changed

+124
-79
lines changed

enginetest/enginetests.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4550,8 +4550,8 @@ func TestPreparedStatements(t *testing.T, harness Harness) {
45504550
e := mustNewEngine(t, harness)
45514551
defer e.Close()
45524552

4553-
for _, query := range queries.PreparedScriptTests {
4554-
TestScript(t, harness, query)
4553+
for _, script := range queries.PreparedScriptTests {
4554+
TestScript(t, harness, script)
45554555
}
45564556
}
45574557

@@ -4561,6 +4561,9 @@ func TestShowTableStatus(t *testing.T, harness Harness) {
45614561
for _, tt := range queries.ShowTableStatusQueries {
45624562
TestQuery(t, harness, tt.Query, tt.Expected, nil, nil)
45634563
}
4564+
for _, script := range queries.ShowTableStatusScripts {
4565+
TestScript(t, harness, script)
4566+
}
45644567
}
45654568

45664569
func TestDateParse(t *testing.T, harness Harness) {

enginetest/queries/queries.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10815,48 +10815,6 @@ var VersionedViewTests = []QueryTest{
1081510815
},
1081610816
}
1081710817

10818-
var ShowTableStatusQueries = []QueryTest{
10819-
{
10820-
Query: `SHOW TABLE STATUS FROM mydb`,
10821-
Expected: []sql.Row{
10822-
{"mytable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, nil},
10823-
{"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, nil},
10824-
},
10825-
},
10826-
{
10827-
Query: `SHOW TABLE STATUS LIKE '%table'`,
10828-
Expected: []sql.Row{
10829-
{"mytable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, nil},
10830-
{"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, nil},
10831-
},
10832-
},
10833-
{
10834-
Query: `SHOW TABLE STATUS FROM mydb LIKE 'othertable'`,
10835-
Expected: []sql.Row{
10836-
{"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, nil},
10837-
},
10838-
},
10839-
{
10840-
Query: `SHOW TABLE STATUS WHERE Name = 'mytable'`,
10841-
Expected: []sql.Row{
10842-
{"mytable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, nil},
10843-
},
10844-
},
10845-
{
10846-
Query: `SHOW TABLE STATUS`,
10847-
Expected: []sql.Row{
10848-
{"mytable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, nil},
10849-
{"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, nil},
10850-
},
10851-
},
10852-
{
10853-
Query: `SHOW TABLE STATUS FROM mydb LIKE 'othertable'`,
10854-
Expected: []sql.Row{
10855-
{"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, nil},
10856-
},
10857-
},
10858-
}
10859-
1086010818
func MustParseTime(layout, value string) time.Time {
1086110819
parsed, err := time.Parse(layout, value)
1086210820
if err != nil {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2025 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package queries
16+
17+
import "github.com/dolthub/go-mysql-server/sql"
18+
19+
var ShowTableStatusQueries = []QueryTest{
20+
{
21+
Query: `SHOW TABLE STATUS FROM mydb`,
22+
Expected: []sql.Row{
23+
{"mytable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, ""},
24+
{"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, ""},
25+
},
26+
},
27+
{
28+
Query: `SHOW TABLE STATUS LIKE '%table'`,
29+
Expected: []sql.Row{
30+
{"mytable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, ""},
31+
{"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, ""},
32+
},
33+
},
34+
{
35+
Query: `SHOW TABLE STATUS FROM mydb LIKE 'othertable'`,
36+
Expected: []sql.Row{
37+
{"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, ""},
38+
},
39+
},
40+
{
41+
Query: `SHOW TABLE STATUS WHERE Name = 'mytable'`,
42+
Expected: []sql.Row{
43+
{"mytable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, ""},
44+
},
45+
},
46+
{
47+
Query: `SHOW TABLE STATUS`,
48+
Expected: []sql.Row{
49+
{"mytable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, ""},
50+
{"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, ""},
51+
},
52+
},
53+
{
54+
Query: `SHOW TABLE STATUS FROM mydb LIKE 'othertable'`,
55+
Expected: []sql.Row{
56+
{"othertable", "InnoDB", "10", "Fixed", uint64(3), uint64(88), uint64(264), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, ""},
57+
},
58+
},
59+
}
60+
61+
var ShowTableStatusScripts = []ScriptTest{
62+
{
63+
Name: "filter table status by comment",
64+
SetUpScript: []string{
65+
"create table table_with_comment(pk int, c0 int) comment = 'table comment'",
66+
},
67+
Assertions: []ScriptTestAssertion{
68+
{
69+
Query: `show table status where comment = 'table comment'`,
70+
Expected: []sql.Row{
71+
{"table_with_comment", "InnoDB", "10", "Fixed", uint64(0), uint64(0), uint64(0), uint64(0), int64(0), int64(0), nil, nil, nil, nil, "utf8mb4_0900_bin", nil, nil, "table comment"},
72+
},
73+
},
74+
},
75+
},
76+
}

sql/rowexec/show.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,10 @@ func (b *BaseBuilder) buildShowTableStatus(ctx *sql.Context, n *plan.ShowTableSt
170170
return nil, err
171171
}
172172

173-
var numRows uint64
174-
var dataLength uint64
175-
176-
if st, ok := table.(sql.StatisticsTable); ok {
177-
numRows, _, err = st.RowCount(ctx)
178-
if err != nil {
179-
return nil, err
180-
}
181-
182-
dataLength, err = st.DataLength(ctx)
183-
if err != nil {
184-
return nil, err
185-
}
173+
rows[i], err = tableToStatusRow(ctx, table)
174+
if err != nil {
175+
return nil, err
186176
}
187-
188-
rows[i] = tableToStatusRow(tName, numRows, dataLength, table.Collation())
189177
}
190178

191179
return sql.RowsToRowIter(rows...), nil

sql/rowexec/show_iters.go

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,34 +73,54 @@ func (p process) toRow() sql.Row {
7373
}
7474

7575
// cc here: https://dev.mysql.com/doc/refman/8.0/en/show-table-status.html
76-
func tableToStatusRow(table string, numRows uint64, dataLength uint64, collation sql.CollationID) sql.Row {
76+
func tableToStatusRow(ctx *sql.Context, table sql.Table) (sql.Row, error) {
77+
var numRows uint64
78+
var dataLength uint64
79+
var err error
80+
if st, ok := table.(sql.StatisticsTable); ok {
81+
numRows, _, err = st.RowCount(ctx)
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
dataLength, err = st.DataLength(ctx)
87+
if err != nil {
88+
return nil, err
89+
}
90+
}
7791
var avgLength float64 = 0
7892
if numRows > 0 {
7993
avgLength = float64(dataLength) / float64(numRows)
8094
}
95+
96+
var comment string
97+
if ct, ok := table.(sql.CommentedTable); ok {
98+
comment = ct.Comment()
99+
}
100+
81101
return sql.NewRow(
82-
table, // Name
83-
"InnoDB", // Engine
102+
table.Name(), // Name
103+
"InnoDB", // Engine
84104
// This column is unused. With the removal of .frm files in MySQL 8.0, this
85105
// column now reports a hardcoded value of 10, which is the last .frm file
86106
// version used in MySQL 5.7.
87-
"10", // Version
88-
"Fixed", // Row_format
89-
numRows, // Rows
90-
uint64(avgLength), // Avg_row_length
91-
dataLength, // Data_length
92-
uint64(0), // Max_data_length (Unused for InnoDB)
93-
int64(0), // Index_length
94-
int64(0), // Data_free
95-
nil, // Auto_increment (always null)
96-
nil, // Create_time
97-
nil, // Update_time
98-
nil, // Check_time
99-
collation.String(), // Collation
100-
nil, // Checksum
101-
nil, // Create_options
102-
nil, // Comments
103-
)
107+
"10", // Version
108+
"Fixed", // Row_format
109+
numRows, // Rows
110+
uint64(avgLength), // Avg_row_length
111+
dataLength, // Data_length
112+
uint64(0), // Max_data_length (Unused for InnoDB)
113+
int64(0), // Index_length
114+
int64(0), // Data_free
115+
nil, // Auto_increment (always null)
116+
nil, // Create_time
117+
nil, // Update_time
118+
nil, // Check_time
119+
table.Collation().String(), // Collation
120+
nil, // Checksum
121+
nil, // Create_options
122+
comment, // Comment
123+
), nil
104124
}
105125

106126
// generatePrivStrings creates a formatted GRANT <privilege_list> on <global/database/table> to <user@host> string

0 commit comments

Comments
 (0)