func decodeTableKey(valType parser.Datum, key []byte) (parser.Datum, []byte, error) { var isNull bool if key, isNull = encoding.DecodeIfNull(key); isNull { return parser.DNull, key, nil } switch valType.(type) { case parser.DBool: rkey, i, err := encoding.DecodeVarint(key) return parser.DBool(i != 0), rkey, err case parser.DInt: rkey, i, err := encoding.DecodeVarint(key) return parser.DInt(i), rkey, err case parser.DFloat: rkey, f, err := encoding.DecodeFloat(key, nil) return parser.DFloat(f), rkey, err case parser.DString: rkey, r, err := encoding.DecodeString(key, nil) return parser.DString(r), rkey, err case parser.DBytes: rkey, r, err := encoding.DecodeString(key, nil) return parser.DBytes(r), rkey, err case parser.DDate: rkey, t, err := encoding.DecodeTime(key) return parser.DDate{Time: t}, rkey, err case parser.DTimestamp: rkey, t, err := encoding.DecodeTime(key) return parser.DTimestamp{Time: t}, rkey, err case parser.DInterval: rkey, d, err := encoding.DecodeVarint(key) return parser.DInterval{Duration: time.Duration(d)}, rkey, err default: return nil, nil, util.Errorf("TODO(pmattis): decoded index key: %s", valType.Type()) } }
// decodeKeyVals decodes the values that are part of the key. ValTypes is a // slice returned from makeKeyVals. The decoded values are stored in the vals // parameter while the valTypes parameter is unmodified. Note that len(vals) >= // len(valTypes). The types of the decoded values will match the corresponding // entry in the valTypes parameter with the exception that a value might also // be parser.DNull. The remaining bytes in the key after decoding the values // are returned. func decodeKeyVals(valTypes, vals []parser.Datum, key []byte) ([]byte, error) { for j := range valTypes { var isNull bool if key, isNull = encoding.DecodeIfNull(key); isNull { vals[j] = parser.DNull continue } switch valTypes[j].(type) { case parser.DInt: var i int64 key, i = encoding.DecodeVarint(key) vals[j] = parser.DInt(i) case parser.DFloat: var f float64 key, f = encoding.DecodeFloat(key, nil) vals[j] = parser.DFloat(f) case parser.DString: var r string key, r = encoding.DecodeString(key, nil) vals[j] = parser.DString(r) default: return nil, util.Errorf("TODO(pmattis): decoded index key: %s", valTypes[j].Type()) } } return key, nil }
func decodeTableKey(valType parser.Datum, key []byte) (parser.Datum, []byte, error) { var isNull bool if key, isNull = encoding.DecodeIfNull(key); isNull { return parser.DNull, key, nil } switch valType.(type) { case parser.DBool: var i int64 key, i = encoding.DecodeVarint(key) return parser.DBool(i != 0), key, nil case parser.DInt: var i int64 key, i = encoding.DecodeVarint(key) return parser.DInt(i), key, nil case parser.DFloat: var f float64 key, f = encoding.DecodeFloat(key, nil) return parser.DFloat(f), key, nil case parser.DString: var r string key, r = encoding.DecodeString(key, nil) return parser.DString(r), key, nil default: return nil, nil, util.Errorf("TODO(pmattis): decoded index key: %s", valType.Type()) } }
// prettyKey pretty-prints the specified key, skipping over the first skip // fields. func prettyKey(key roachpb.Key, skip int) string { if !bytes.HasPrefix(key, keys.TableDataPrefix) { return fmt.Sprintf("index key missing table data prefix: %q vs %q", key, keys.TableDataPrefix) } key = key[len(keys.TableDataPrefix):] var buf bytes.Buffer for k := 0; len(key) > 0; k++ { var d interface{} var err error switch encoding.PeekType(key) { case encoding.Null: key, _ = encoding.DecodeIfNull(key) d = parser.DNull case encoding.NotNull: key, _ = encoding.DecodeIfNotNull(key) d = "#" case encoding.Int: var i int64 key, i, err = encoding.DecodeVarint(key) d = parser.DInt(i) case encoding.Float: var f float64 key, f, err = encoding.DecodeFloat(key, nil) d = parser.DFloat(f) case encoding.Bytes: var s string key, s, err = encoding.DecodeString(key, nil) d = parser.DString(s) case encoding.Time: var t time.Time key, t, err = encoding.DecodeTime(key) d = parser.DTimestamp{Time: t} default: // This shouldn't ever happen, but if it does let the loop exit. key = nil d = "unknown" } if skip > 0 { skip-- continue } if err != nil { fmt.Fprintf(&buf, "/<%v>", err) continue } fmt.Fprintf(&buf, "/%s", d) } return buf.String() }
func decodeKeyPrint(key roachpb.Key) string { var buf bytes.Buffer for k := 0; len(key) > 0; k++ { var err error switch encoding.PeekType(key) { case encoding.Null: key, _ = encoding.DecodeIfNull(key) fmt.Fprintf(&buf, "/NULL") case encoding.NotNull: key, _ = encoding.DecodeIfNotNull(key) fmt.Fprintf(&buf, "/#") case encoding.Int: var i int64 key, i, err = encoding.DecodeVarint(key) if err == nil { fmt.Fprintf(&buf, "/%d", i) } case encoding.Float: var f float64 key, f, err = encoding.DecodeFloat(key, nil) if err == nil { fmt.Fprintf(&buf, "/%f", f) } case encoding.Bytes: var s string key, s, err = encoding.DecodeString(key, nil) if err == nil { fmt.Fprintf(&buf, "/%q", s) } case encoding.Time: var t time.Time key, t, err = encoding.DecodeTime(key) if err == nil { fmt.Fprintf(&buf, "/%s", t.UTC().Format(time.UnixDate)) } default: // This shouldn't ever happen, but if it does let the loop exit. fmt.Fprintf(&buf, "/%q", []byte(key)) key = nil } if err != nil { fmt.Fprintf(&buf, "/<%v>", err) continue } } return buf.String() }
// ShowDatabases returns all the databases. // Privileges: None. // Notes: postgres does not have a "show databases" // mysql has a "SHOW DATABASES" permission, but we have no system-level permissions. func (p *planner) ShowDatabases(n *parser.ShowDatabases) (planNode, error) { // TODO(pmattis): This could be implemented as: // // SELECT id FROM system.namespace WHERE parentID = 0 prefix := MakeNameMetadataKey(keys.RootNamespaceID, "") sr, err := p.txn.Scan(prefix, prefix.PrefixEnd(), 0) if err != nil { return nil, err } v := &valuesNode{columns: []string{"Database"}} for _, row := range sr { _, name := encoding.DecodeString(bytes.TrimPrefix(row.Key, prefix), nil) v.rows = append(v.rows, []parser.Datum{parser.DString(name)}) } return v, nil }
func (p *planner) getTableNames(dbDesc *DatabaseDescriptor) (parser.QualifiedNames, error) { prefix := MakeNameMetadataKey(dbDesc.ID, "") sr, err := p.txn.Scan(prefix, prefix.PrefixEnd(), 0) if err != nil { return nil, err } var qualifiedNames parser.QualifiedNames for _, row := range sr { _, tableName := encoding.DecodeString(bytes.TrimPrefix(row.Key, prefix), nil) qname := &parser.QualifiedName{ Base: parser.Name(dbDesc.Name), Indirect: parser.Indirection{parser.NameIndirection(tableName)}, } if err := qname.NormalizeTableName(""); err != nil { return nil, err } qualifiedNames = append(qualifiedNames, qname) } return qualifiedNames, nil }