Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit f5f7c5e

Browse files
authored
fbsql: add support for \d meta-command. (#2340)
`\d` will list tables (in the future it will also include things like views) `\d tablename` will show info about tablename
1 parent 10aab58 commit f5f7c5e

3 files changed

Lines changed: 80 additions & 3 deletions

File tree

cli/cli_integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func TestCLIIntegration(t *testing.T) {
6666
"meta_bang",
6767
"meta_cd",
6868
"meta_echo",
69+
"meta_describe",
6970
"meta_file",
7071
"meta_pset_border",
7172
"meta_pset_expanded",

cli/meta.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"bufio"
5+
"fmt"
56
"io"
67
"os"
78
"os/exec"
@@ -39,6 +40,7 @@ var _ metaCommand = (*metaBang)(nil)
3940
var _ metaCommand = (*metaBorder)(nil)
4041
var _ metaCommand = (*metaChangeDirectory)(nil)
4142
var _ metaCommand = (*metaConnect)(nil)
43+
var _ metaCommand = (*metaDescribe)(nil)
4244
var _ metaCommand = (*metaEcho)(nil)
4345
var _ metaCommand = (*metaExpanded)(nil)
4446
var _ metaCommand = (*metaFile)(nil)
@@ -363,6 +365,7 @@ Input/Output
363365
364366
Informational
365367
\d list tables
368+
\d NAME describe table
366369
\dt list tables
367370
\dv list views
368371
\l[ist] list databases
@@ -375,6 +378,7 @@ Formatting
375378
376379
Connection
377380
\c[onnect] [DBNAME] connect to new database
381+
disconnect by sending DBNAME "-"
378382
\org [ORGNAME] set organization id
379383
380384
Operating System
@@ -484,7 +488,44 @@ func (m *metaListDatabases) execute(cmd *Command) (responseAction, error) {
484488
}
485489

486490
// ////////////////////////////////////////////////////////////////////////////
487-
// list tables (d or dt)
491+
// describe (d)
492+
// ////////////////////////////////////////////////////////////////////////////
493+
type metaDescribe struct {
494+
args []string
495+
}
496+
497+
func newMetaDescribe(args []string) *metaDescribe {
498+
return &metaDescribe{
499+
args: args,
500+
}
501+
}
502+
503+
func (m *metaDescribe) execute(cmd *Command) (responseAction, error) {
504+
switch len(m.args) {
505+
case 0:
506+
// Describe with no args should list all relations (tables, views,
507+
// etc.). For now, we're just going to list the tables.
508+
return newMetaListTables().execute(cmd)
509+
510+
case 1:
511+
// Describe with a single arg will assume the arg is a table name, so it
512+
// runs a `SHOW COLUMNS` for that table.
513+
qry := []queryPart{
514+
newPartRaw(fmt.Sprintf(`SHOW COLUMNS FROM "%s"`, m.args[0])),
515+
}
516+
517+
if err := cmd.executeAndWriteQuery(qry); err != nil {
518+
return actionNone, errors.Wrap(err, "executing query")
519+
}
520+
521+
return actionReset, nil
522+
default:
523+
return actionNone, errors.Errorf("meta command 'describe' takes zero or one argument")
524+
}
525+
}
526+
527+
// ////////////////////////////////////////////////////////////////////////////
528+
// describe (dt)
488529
// ////////////////////////////////////////////////////////////////////////////
489530
type metaListTables struct{}
490531

@@ -505,7 +546,7 @@ func (m *metaListTables) execute(cmd *Command) (responseAction, error) {
505546
}
506547

507548
// ////////////////////////////////////////////////////////////////////////////
508-
// list views (dv)
549+
// describe views (dv)
509550
// ////////////////////////////////////////////////////////////////////////////
510551
type metaListViews struct{}
511552

@@ -1064,7 +1105,9 @@ func splitMetaCommand(in string, replacer *replacer) (metaCommand, error) {
10641105
return newMetaChangeDirectory(args), nil
10651106
case "c", "connect":
10661107
return newMetaConnect(args), nil
1067-
case "d", "dt":
1108+
case "d":
1109+
return newMetaDescribe(args), nil
1110+
case "dt":
10681111
return newMetaListTables(), nil
10691112
case "dv":
10701113
return newMetaListViews(), nil

cli/testdata/meta_describe

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// TODO(tlt): we can't run this test until we get the system tables under control (i.e. sorted). Currently, fb_views is in a map with users, so the following can fail 50% of the time.
2+
// Show tables for database by calling describe with no args.
3+
// SEND:\d
4+
// EXPECT:+-------------------------+-------------------------+-------+------------+----------------------+----------------------+-------+------------+------------------------+
5+
// EXPECT:| _id | name | owner | updated_by | created_at | updated_at | keys | space_used | description |
6+
// EXPECT:+-------------------------+-------------------------+-------+------------+----------------------+----------------------+-------+------------+------------------------+
7+
// EXPECTCOMP:WithFormat:| fb_veiws | fb_views | | | {timestamp} | {timestamp} | true | 0 | system table for views |
8+
// EXPECTCOMP:WithFormat:| users | users | | | {timestamp} | {timestamp} | false | 0 | |
9+
// EXPECTCOMP:WithFormat:| fb_____________________ | fb_____________________ | | | {timestamp} | {timestamp} | false | 0 | |
10+
// EXPECTCOMP:WithFormat:| fb_____________________ | fb_____________________ | | | {timestamp} | {timestamp} | false | 0 | |
11+
// EXPECTCOMP:WithFormat:| fb_____________________ | fb_____________________ | | | {timestamp} | {timestamp} | false | 0 | |
12+
// EXPECTCOMP:WithFormat:| fb_____________________ | fb_____________________ | | | {timestamp} | {timestamp} | false | 0 | |
13+
// EXPECTCOMP:WithFormat:| fb_____________________ | fb_____________________ | | | {timestamp} | {timestamp} | false | 0 | |
14+
// EXPECT:+-------------------------+-------------------------+-------+------------+----------------------+----------------------+-------+------------+------------------------+
15+
// EXPECT:
16+
17+
// Show columns for table.
18+
SEND:\d users
19+
EXPECT:+------+------+--------+----------------------+-------+------------+------------+-------+----------------------+---------------------+----------+-------+-------------+-----+
20+
EXPECT:| _id | name | type | created_at | keys | cache_type | cache_size | scale | min | max | timeunit | epoch | timequantum | ttl |
21+
EXPECT:+------+------+--------+----------------------+-------+------------+------------+-------+----------------------+---------------------+----------+-------+-------------+-----+
22+
EXPECTCOMP:WithFormat:| _id | _id | id | {timestamp} | false | | 0 | 0 | 0 | 0 | | 0 | | 0s |
23+
EXPECTCOMP:WithFormat:| name | name | string | {timestamp} | true | ranked | 50000 | 0 | 0 | 0 | | 0 | | 0s |
24+
EXPECTCOMP:WithFormat:| age | age | int | {timestamp} | false | | 0 | 0 | -9223372036854775808 | 9223372036854775807 | | 0 | | 0s |
25+
EXPECT:+------+------+--------+----------------------+-------+------------+------------+-------+----------------------+---------------------+----------+-------+-------------+-----+
26+
EXPECT:
27+
28+
// Show columns for an invalid table.
29+
SEND:\d invalid
30+
EXPECT:Error: compiling plan: [1:19] table 'invalid' not found
31+
32+
SEND:\d users extra
33+
EXPECT:executing meta command: meta command 'describe' takes zero or one argument

0 commit comments

Comments
 (0)