From 734477aaee772bba2aec48b41cfdde72864a847f Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Thu, 8 Dec 2022 22:50:41 -0600 Subject: [PATCH] Add Table.Description, Table.CreatedAt, Field.CreatedAt support to SchemaAPI (#2340) * Thread Table.Description through SchemaAPI * Thread Table.CreatedAt through SchemaAPI * Thread Field.CreatedAt through SchemaAPI --- dax/mds/schemar/boltdb/schemar.go | 17 +++++++++++++++++ dax/table.go | 5 +++++ schema.go | 16 ++++++++++++---- sql3/planner/opcreatetable.go | 4 ++-- sql3/planner/opfeaturebasecolumns.go | 3 +-- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/dax/mds/schemar/boltdb/schemar.go b/dax/mds/schemar/boltdb/schemar.go index 1d4ec02b2..6c7e5549a 100644 --- a/dax/mds/schemar/boltdb/schemar.go +++ b/dax/mds/schemar/boltdb/schemar.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "strings" + "time" "github.com/molecula/featurebase/v3/dax" "github.com/molecula/featurebase/v3/dax/boltdb" @@ -61,6 +62,18 @@ func (s *Schemar) CreateTable(ctx context.Context, qtbl *dax.QualifiedTable) err return schemar.NewErrInvalidPrimaryKey() } + // Set the CreateAt value for the table. + // TODO(tlt): We may want to consider erroring here if the value is != 0. + if qtbl.CreatedAt == 0 { + now := timestamp() + qtbl.CreatedAt = now + + // Set CreatedAt for all of the fields as well. + for i := range qtbl.Fields { + qtbl.Fields[i].CreatedAt = now + } + } + //////////// end validation tx, err := s.db.BeginTx(ctx, true) @@ -384,3 +397,7 @@ func (s *Schemar) TableID(ctx context.Context, qual dax.TableQualifier, name dax return s.tableIDByName(tx, qual, name) } + +func timestamp() int64 { + return time.Now().UnixNano() +} diff --git a/dax/table.go b/dax/table.go index 2ef28a739..ab9c566da 100644 --- a/dax/table.go +++ b/dax/table.go @@ -162,6 +162,9 @@ type Table struct { Name TableName `json:"name,omitempty"` Fields []*Field `json:"fields"` PartitionN int `json:"partitionN"` + + Description string `json:"description,omitempty"` + CreatedAt int64 `json:"createdAt,omitempty"` } // CreateID generates a unique identifier for Table. If Table has already been @@ -518,6 +521,8 @@ type Field struct { Name FieldName `json:"name"` Type BaseType `json:"type"` Options FieldOptions `json:"options"` + + CreatedAt int64 `json:"createdAt,omitempty"` } // String returns the field name as a string. diff --git a/schema.go b/schema.go index 35b10d363..399ab9a2b 100644 --- a/schema.go +++ b/schema.go @@ -72,6 +72,7 @@ func (s *onPremSchema) CreateTable(ctx context.Context, tbl *dax.Table) error { Keys: keyed, TrackExistence: true, PartitionN: tbl.PartitionN, + Description: tbl.Description, } // Add the index. @@ -135,6 +136,9 @@ func IndexInfoToTable(ii *IndexInfo) *dax.Table { Name: dax.TableName(ii.Name), Fields: make([]*dax.Field, 0, len(ii.Fields)+1), // +1 to account for the _id field PartitionN: dax.DefaultPartitionN, + + Description: ii.Options.Description, + CreatedAt: ii.CreatedAt, } // // sortedFields will contain the sorted list of fields from IndexInfo. @@ -154,8 +158,9 @@ func IndexInfoToTable(ii *IndexInfo) *dax.Table { idType = dax.BaseTypeString } tbl.Fields = append(tbl.Fields, &dax.Field{ - Name: "_id", - Type: idType, + Name: "_id", + Type: idType, + CreatedAt: ii.CreatedAt, }) // Populate the rest of the fields. @@ -243,6 +248,8 @@ func FieldInfoToField(fi *FieldInfo) *dax.Field { TTL: fo.TTL, ForeignIndex: foreignIndex, }, + + CreatedAt: fi.CreatedAt, } } @@ -271,10 +278,11 @@ func TablesToIndexInfos(tbls []*dax.Table) []*IndexInfo { func TableToIndexInfo(tbl *dax.Table) *IndexInfo { ii := &IndexInfo{ Name: string(tbl.Name), // TODO(tlt): this should be TableKey i think - CreatedAt: 0, + CreatedAt: tbl.CreatedAt, Options: IndexOptions{ Keys: tbl.StringKeys(), TrackExistence: true, + Description: tbl.Description, }, ShardWidth: ShardWidth, } @@ -320,7 +328,7 @@ func FieldToFieldInfo(fld *dax.Field) *FieldInfo { return &FieldInfo{ Name: string(fld.Name), - CreatedAt: 0, // TODO(tlt): we need to handle this on MDS schemar + CreatedAt: fld.CreatedAt, Options: FieldOptions{ Type: fieldToFieldType(fld), Base: base, diff --git a/sql3/planner/opcreatetable.go b/sql3/planner/opcreatetable.go index 7939c2aff..32f28df8e 100644 --- a/sql3/planner/opcreatetable.go +++ b/sql3/planner/opcreatetable.go @@ -128,10 +128,10 @@ func (i *createTableRowIter) Next(ctx context.Context) (types.Row, error) { // TODO(tlt): once we can support different partitionN's per table, // replace dax.DefaultPartitionN with i.keyPartitions. PartitionN: dax.DefaultPartitionN, - // TODO(tlt): add Description to dax.Table; = i.description + + Description: i.description, } - // TODO (pok) add ability to add description here if err := i.planner.schemaAPI.CreateTable(ctx, tbl); err != nil { if _, ok := errors.Cause(err).(pilosa.ConflictError); ok { if i.failIfExists { diff --git a/sql3/planner/opfeaturebasecolumns.go b/sql3/planner/opfeaturebasecolumns.go index 076029bfc..e2c33a92e 100644 --- a/sql3/planner/opfeaturebasecolumns.go +++ b/sql3/planner/opfeaturebasecolumns.go @@ -154,8 +154,7 @@ func (i *showColumnsRowIter) Next(ctx context.Context) (types.Row, error) { if i.rowIndex < len(i.tbl.Fields) { fields := i.tbl.Fields - //tm := time.Unix(0, fields[i.rowIndex].CreatedAt) - tm := time.Unix(0, 0) + tm := time.Unix(0, fields[i.rowIndex].CreatedAt) row := []interface{}{ fields[i.rowIndex].Name,