vtgate sql: consolidate common code

Some boiler-plate behaviors were accumulating in each builder.
They've now been consolidated under builderCommon.

Signed-off-by: Sugu Sougoumarane <ssougou@gmail.com>
This commit is contained in:
Sugu Sougoumarane 2019-06-15 20:29:38 -07:00
Родитель 8e2f781e1e
Коммит 611b4d436b
5 изменённых файлов: 54 добавлений и 173 удалений

Просмотреть файл

@ -107,6 +107,50 @@ type builder interface {
Primitive() engine.Primitive
}
// builderCommon implements some common functionality of builders.
// Make sure to override in case behavior needs to be changed.
type builderCommon struct {
order int
input builder
}
func (bc *builderCommon) Order() int {
return bc.order
}
func (bc *builderCommon) Reorder(order int) {
bc.input.Reorder(order)
bc.order = bc.input.Order() + 1
}
func (bc *builderCommon) First() builder {
return bc.input.First()
}
func (bc *builderCommon) ResultColumns() []*resultColumn {
return bc.input.ResultColumns()
}
func (bc *builderCommon) SetUpperLimit(count *sqlparser.SQLVal) {
bc.input.SetUpperLimit(count)
}
func (bc *builderCommon) PushMisc(sel *sqlparser.Select) {
bc.input.PushMisc(sel)
}
func (bc *builderCommon) Wireup(bldr builder, jt *jointab) error {
return bc.input.Wireup(bldr, jt)
}
func (bc *builderCommon) SupplyVar(from, to int, col *sqlparser.ColName, varname string) {
bc.input.SupplyVar(from, to, col, varname)
}
func (bc *builderCommon) SupplyCol(col *sqlparser.ColName) (rc *resultColumn, colnum int) {
return bc.input.SupplyCol(col)
}
// ContextVSchema defines the interface for this package to fetch
// info about tables.
type ContextVSchema interface {

Просмотреть файл

@ -32,48 +32,24 @@ var _ builder = (*limit)(nil)
// operation. Since a limit is the final operation
// of a SELECT, most pushes are not applicable.
type limit struct {
order int
resultColumns []*resultColumn
input builder
elimit *engine.Limit
builderCommon
elimit *engine.Limit
}
// newLimit builds a new limit.
func newLimit(bldr builder) *limit {
return &limit{
resultColumns: bldr.ResultColumns(),
input: bldr,
builderCommon: builderCommon{input: bldr},
elimit: &engine.Limit{},
}
}
// Order satisfies the builder interface.
func (l *limit) Order() int {
return l.order
}
// Reorder satisfies the builder interface.
func (l *limit) Reorder(order int) {
l.input.Reorder(order)
l.order = l.input.Order() + 1
}
// Primitive satisfies the builder interface.
func (l *limit) Primitive() engine.Primitive {
l.elimit.Input = l.input.Primitive()
return l.elimit
}
// First satisfies the builder interface.
func (l *limit) First() builder {
return l.input.First()
}
// ResultColumns satisfies the builder interface.
func (l *limit) ResultColumns() []*resultColumn {
return l.resultColumns
}
// PushFilter satisfies the builder interface.
func (l *limit) PushFilter(_ *primitiveBuilder, _ sqlparser.Expr, whereType string, _ builder) error {
return errors.New("limit.PushFilter: unreachable")
@ -136,23 +112,3 @@ func (l *limit) SetLimit(limit *sqlparser.Limit) error {
// In the future, we may have to honor this call for subqueries.
func (l *limit) SetUpperLimit(count *sqlparser.SQLVal) {
}
// PushMisc satisfies the builder interface.
func (l *limit) PushMisc(sel *sqlparser.Select) {
l.input.PushMisc(sel)
}
// Wireup satisfies the builder interface.
func (l *limit) Wireup(bldr builder, jt *jointab) error {
return l.input.Wireup(bldr, jt)
}
// SupplyVar satisfies the builder interface.
func (l *limit) SupplyVar(from, to int, col *sqlparser.ColName, varname string) {
l.input.SupplyVar(from, to, col, varname)
}
// SupplyCol satisfies the builder interface.
func (l *limit) SupplyCol(col *sqlparser.ColName) (rc *resultColumn, colnum int) {
panic("BUG: nothing should depend on LIMIT")
}

Просмотреть файл

@ -32,17 +32,16 @@ var _ builder = (*memorySort)(nil)
// operation. Since a limit is the final operation
// of a SELECT, most pushes are not applicable.
type memorySort struct {
order int
builderCommon
resultColumns []*resultColumn
input builder
eMemorySort *engine.MemorySort
}
// newMemorySort builds a new memorySort.
func newMemorySort(bldr builder, orderBy sqlparser.OrderBy) (*memorySort, error) {
ms := &memorySort{
builderCommon: builderCommon{input: bldr},
resultColumns: bldr.ResultColumns(),
input: bldr,
eMemorySort: &engine.MemorySort{},
}
for _, order := range orderBy {
@ -78,28 +77,12 @@ func newMemorySort(bldr builder, orderBy sqlparser.OrderBy) (*memorySort, error)
return ms, nil
}
// Order satisfies the builder interface.
func (ms *memorySort) Order() int {
return ms.order
}
// Reorder satisfies the builder interface.
func (ms *memorySort) Reorder(order int) {
ms.input.Reorder(order)
ms.order = ms.input.Order() + 1
}
// Primitive satisfies the builder interface.
func (ms *memorySort) Primitive() engine.Primitive {
ms.eMemorySort.Input = ms.input.Primitive()
return ms.eMemorySort
}
// First satisfies the builder interface.
func (ms *memorySort) First() builder {
return ms.input.First()
}
// ResultColumns satisfies the builder interface.
func (ms *memorySort) ResultColumns() []*resultColumn {
return ms.resultColumns
@ -141,23 +124,3 @@ func (ms *memorySort) SetLimit(limit *sqlparser.Limit) error {
func (ms *memorySort) SetUpperLimit(count *sqlparser.SQLVal) {
ms.eMemorySort.UpperLimit, _ = sqlparser.NewPlanValue(count)
}
// PushMisc satisfies the builder interface.
func (ms *memorySort) PushMisc(sel *sqlparser.Select) {
ms.input.PushMisc(sel)
}
// Wireup satisfies the builder interface.
func (ms *memorySort) Wireup(bldr builder, jt *jointab) error {
return ms.input.Wireup(bldr, jt)
}
// SupplyVar satisfies the builder interface.
func (ms *memorySort) SupplyVar(from, to int, col *sqlparser.ColName, varname string) {
ms.input.SupplyVar(from, to, col, varname)
}
// SupplyCol satisfies the builder interface.
func (ms *memorySort) SupplyCol(col *sqlparser.ColName) (rc *resultColumn, colnum int) {
panic("BUG: nothing should depend on ORDER BY")
}

Просмотреть файл

@ -33,43 +33,21 @@ var _ builder = (*mergeSort)(nil)
// Since ORDER BY happens near the end of the SQL processing,
// most functions of this primitive are unreachable.
type mergeSort struct {
order int
input *route
builderCommon
}
// newMergeSort builds a new mergeSort.
func newMergeSort(rb *route) *mergeSort {
return &mergeSort{
input: rb,
builderCommon: builderCommon{input: rb},
}
}
// Order satisfies the builder interface.
func (ms *mergeSort) Order() int {
return ms.order
}
// Reorder satisfies the builder interface.
func (ms *mergeSort) Reorder(order int) {
ms.input.Reorder(order)
ms.order = ms.input.Order() + 1
}
// Primitive satisfies the builder interface.
func (ms *mergeSort) Primitive() engine.Primitive {
return ms.input.Primitive()
}
// First satisfies the builder interface.
func (ms *mergeSort) First() builder {
return ms.input.First()
}
// ResultColumns satisfies the builder interface.
func (ms *mergeSort) ResultColumns() []*resultColumn {
return ms.input.ResultColumns()
}
// PushFilter satisfies the builder interface.
func (ms *mergeSort) PushFilter(pb *primitiveBuilder, expr sqlparser.Expr, whereType string, origin builder) error {
return ms.input.PushFilter(pb, expr, whereType, origin)
@ -96,28 +74,3 @@ func (ms *mergeSort) PushGroupBy(groupBy sqlparser.GroupBy) error {
func (ms *mergeSort) PushOrderBy(orderBy sqlparser.OrderBy) (builder, error) {
return nil, errors.New("mergeSort.PushOrderBy: unreachable")
}
// SetUpperLimit satisfies the builder interface.
func (ms *mergeSort) SetUpperLimit(count *sqlparser.SQLVal) {
ms.input.SetUpperLimit(count)
}
// PushMisc satisfies the builder interface.
func (ms *mergeSort) PushMisc(sel *sqlparser.Select) {
ms.input.PushMisc(sel)
}
// Wireup satisfies the builder interface.
func (ms *mergeSort) Wireup(bldr builder, jt *jointab) error {
return ms.input.Wireup(bldr, jt)
}
// SupplyVar satisfies the builder interface.
func (ms *mergeSort) SupplyVar(from, to int, col *sqlparser.ColName, varname string) {
ms.input.SupplyVar(from, to, col, varname)
}
// SupplyCol satisfies the builder interface.
func (ms *mergeSort) SupplyCol(col *sqlparser.ColName) (rc *resultColumn, colnum int) {
return ms.input.SupplyCol(col)
}

Просмотреть файл

@ -36,17 +36,16 @@ var _ builder = (*subquery)(nil)
// clause, because a route is more versatile than
// a subquery.
type subquery struct {
order int
builderCommon
resultColumns []*resultColumn
input builder
esubquery *engine.Subquery
}
// newSubquery builds a new subquery.
func newSubquery(alias sqlparser.TableIdent, bldr builder) (*subquery, *symtab, error) {
sq := &subquery{
input: bldr,
esubquery: &engine.Subquery{},
builderCommon: builderCommon{input: bldr},
esubquery: &engine.Subquery{},
}
// Create a 'table' that represents the subquery.
@ -69,17 +68,6 @@ func newSubquery(alias sqlparser.TableIdent, bldr builder) (*subquery, *symtab,
return sq, st, nil
}
// Order satisfies the builder interface.
func (sq *subquery) Order() int {
return sq.order
}
// Reorder satisfies the builder interface.
func (sq *subquery) Reorder(order int) {
sq.input.Reorder(order)
sq.order = sq.input.Order() + 1
}
// Primitive satisfies the builder interface.
func (sq *subquery) Primitive() engine.Primitive {
sq.esubquery.Subquery = sq.input.Primitive()
@ -140,29 +128,6 @@ func (sq *subquery) PushOrderBy(orderBy sqlparser.OrderBy) (builder, error) {
return nil, errors.New("unsupported: order by on cross-shard subquery")
}
// SetUpperLimit satisfies the builder interface.
// For now, the call is ignored because the
// repercussions of pushing this limit down
// into a subquery have not been studied yet.
// We can consider doing it in the future.
// TODO(sougou): this could be improved.
func (sq *subquery) SetUpperLimit(_ *sqlparser.SQLVal) {
}
// PushMisc satisfies the builder interface.
func (sq *subquery) PushMisc(sel *sqlparser.Select) {
}
// Wireup satisfies the builder interface.
func (sq *subquery) Wireup(bldr builder, jt *jointab) error {
return sq.input.Wireup(bldr, jt)
}
// SupplyVar satisfies the builder interface.
func (sq *subquery) SupplyVar(from, to int, col *sqlparser.ColName, varname string) {
sq.input.SupplyVar(from, to, col, varname)
}
// SupplyCol satisfies the builder interface.
func (sq *subquery) SupplyCol(col *sqlparser.ColName) (rc *resultColumn, colnum int) {
c := col.Metadata.(*column)