Skip to content

Commit

Permalink
sql: Add information_schema.statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
yznming committed Oct 27, 2016
1 parent a6ad154 commit 25627f7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
67 changes: 67 additions & 0 deletions pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var informationSchema = virtualSchema{
informationSchemaKeyColumnUsageTable,
informationSchemaSchemataTable,
informationSchemaSchemataTablePrivileges,
informationSchemaStatisticsTable,
informationSchemaTableConstraintTable,
informationSchemaTablePrivileges,
informationSchemaTablesTable,
Expand Down Expand Up @@ -265,6 +266,72 @@ CREATE TABLE information_schema.schema_privileges (
},
}

var (
indexDirectionNA = "N/A"
)

var informationSchemaStatisticsTable = virtualSchemaTable{
schema: `
CREATE TABLE information_schema.statistics (
TABLE_CATALOG STRING NOT NULL DEFAULT '',
TABLE_SCHEMA STRING NOT NULL DEFAULT '',
TABLE_NAME STRING NOT NULL DEFAULT '',
NON_UNIQUE BOOL NOT NULL DEFAULT FALSE,
INDEX_SCHEMA STRING NOT NULL DEFAULT '',
INDEX_NAME STRING NOT NULL DEFAULT '',
SEQ_IN_INDEX INT NOT NULL DEFAULT 0,
COLUMN_NAME STRING NOT NULL DEFAULT '',
"COLLATION" STRING NOT NULL DEFAULT '',
CARDINALITY INT NOT NULL DEFAULT 0,
DIRECTION STRING NOT NULL DEFAULT '',
STORING BOOL NOT NULL DEFAULT FALSE
);`,
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachTableDesc(p,
func(db *sqlbase.DatabaseDescriptor, table *sqlbase.TableDescriptor) error {
appendRow := func(index *sqlbase.IndexDescriptor, colName string, sequence int,
direction string, isStored bool) error {
return addRow(
defString, // table_catalog
parser.NewDString(db.GetName()), // table_schema
parser.NewDString(table.GetName()), // table_name
parser.MakeDBool(parser.DBool(index.Unique)), // non_unique
parser.NewDString(db.GetName()), // index_schema
parser.NewDString(index.Name), // index_name
parser.NewDInt(parser.DInt(sequence)), // seq_in_index
parser.NewDString(colName), // column_name
parser.DNull, // collation
parser.DNull, // cardinality
parser.NewDString(direction), // direction
parser.MakeDBool(parser.DBool(isStored)), // storing
)
}

return forEachIndexInTable(table, func(index *sqlbase.IndexDescriptor) error {
sequence := 1
for i, col := range index.ColumnNames {
// We add a row for each column of index
if err := appendRow(index, col, sequence,
index.ColumnDirections[i].String(), false); err != nil {
return err
}
sequence++
}
for _, col := range index.StoreColumnNames {
// We add a row for each column of index
if err := appendRow(index, col, sequence,
indexDirectionNA, true); err != nil {
return err
}
sequence++
}
return nil
})
},
)
},
}

var (
constraintTypeCheck = parser.NewDString("CHECK")
constraintTypeForeignKey = parser.NewDString("FOREIGN KEY")
Expand Down
19 changes: 19 additions & 0 deletions pkg/sql/testdata/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ columns
key_column_usage
schema_privileges
schemata
statistics
table_constraints
table_privileges
tables
Expand Down Expand Up @@ -337,6 +338,7 @@ columns
key_column_usage
schema_privileges
schemata
statistics
table_constraints
table_privileges
tables
Expand Down Expand Up @@ -369,6 +371,7 @@ ui
tables
table_privileges
table_constraints
statistics
schemata
schema_privileges
rangelog
Expand All @@ -390,6 +393,7 @@ def information_schema columns SYSTEM VIEW 1
def information_schema key_column_usage SYSTEM VIEW 1
def information_schema schema_privileges SYSTEM VIEW 1
def information_schema schemata SYSTEM VIEW 1
def information_schema statistics SYSTEM VIEW 1
def information_schema table_constraints SYSTEM VIEW 1
def information_schema table_privileges SYSTEM VIEW 1
def information_schema tables SYSTEM VIEW 1
Expand Down Expand Up @@ -431,6 +435,7 @@ def information_schema columns SYSTEM VIEW 1
def information_schema key_column_usage SYSTEM VIEW 1
def information_schema schema_privileges SYSTEM VIEW 1
def information_schema schemata SYSTEM VIEW 1
def information_schema statistics SYSTEM VIEW 1
def information_schema table_constraints SYSTEM VIEW 1
def information_schema table_privileges SYSTEM VIEW 1
def information_schema tables SYSTEM VIEW 1
Expand Down Expand Up @@ -458,6 +463,7 @@ def information_schema columns SYSTEM VIEW 1
def information_schema key_column_usage SYSTEM VIEW 1
def information_schema schema_privileges SYSTEM VIEW 1
def information_schema schemata SYSTEM VIEW 1
def information_schema statistics SYSTEM VIEW 1
def information_schema table_constraints SYSTEM VIEW 1
def information_schema table_privileges SYSTEM VIEW 1
def information_schema tables SYSTEM VIEW 1
Expand Down Expand Up @@ -661,5 +667,18 @@ NULL root def other_db xyz ALL NULL
NULL testuser def other_db xyz SELECT NULL NULL
NULL testuser def other_db xyz UPDATE NULL NULL

## information_schema.statistics
statement ok
CREATE TABLE other_db.teststatis(id INT PRIMARY KEY, c INT, d INT, e STRING, INDEX idx_c(c), UNIQUE INDEX idx_cd(c,d))

query TTTBTTITIITB colnames
SELECT * FROM information_schema.statistics WHERE table_schema='other_db' AND table_name='teststatis' ORDER BY INDEX_SCHEMA,INDEX_NAME,SEQ_IN_INDEX
----
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME NON_UNIQUE INDEX_SCHEMA INDEX_NAME SEQ_IN_INDEX COLUMN_NAME COLLATION CARDINALITY DIRECTION STORING
def other_db teststatis false other_db idx_c 1 c NULL NULL ASC false
def other_db teststatis true other_db idx_cd 1 c NULL NULL ASC false
def other_db teststatis true other_db idx_cd 2 d NULL NULL ASC false
def other_db teststatis true other_db primary 1 id NULL NULL ASC false

statement ok
DROP DATABASE other_db

0 comments on commit 25627f7

Please sign in to comment.