github.com/dolthub/go-mysql-server@v0.18.0/sql/describe.go (about)

     1  // Copyright 2023 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package sql
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  )
    21  
    22  type WithDescribeStats interface {
    23  	GetDescribeStatsString(options DescribeOptions) string
    24  	SetDescribeStats(stats DescribeStats)
    25  	getDescribeStats() *DescribeStats
    26  }
    27  
    28  type DescribeStats struct {
    29  	HasStats           bool
    30  	EstimatedRowCount  uint64
    31  	ActualRowCount     uint64
    32  	NumberOfIterations uint64
    33  	Cost               float64
    34  }
    35  
    36  // GetEstimatedRowCount implements WithDescribeStats
    37  func (e DescribeStats) GetEstimatedRowCount() uint64 {
    38  	return e.EstimatedRowCount
    39  }
    40  
    41  // GetEstimatedCost implements WithDescribeStats
    42  func (e DescribeStats) GetEstimatedCost() float64 {
    43  	return e.Cost
    44  }
    45  
    46  // GetDescribeStatsString implements WithDescribeStats
    47  func (e *DescribeStats) GetDescribeStatsString(options DescribeOptions) string {
    48  	if !e.HasStats {
    49  		return "(No stats)"
    50  	}
    51  	estimatedStats := fmt.Sprintf("(estimated cost=%.3f rows=%v)", e.Cost, e.EstimatedRowCount)
    52  	if !options.Analyze || e.NumberOfIterations == 0 {
    53  		return estimatedStats
    54  	}
    55  	averageRowCount := float64(e.ActualRowCount) / float64(e.NumberOfIterations)
    56  	actualStats := fmt.Sprintf("(actual rows=%v loops=%v)", averageRowCount, e.NumberOfIterations)
    57  	return fmt.Sprintf("%s %s", estimatedStats, actualStats)
    58  }
    59  
    60  func (e *DescribeStats) SetDescribeStats(newStats DescribeStats) {
    61  	*e = newStats
    62  	e.HasStats = true
    63  }
    64  
    65  func (e *DescribeStats) getDescribeStats() *DescribeStats {
    66  	return e
    67  }
    68  
    69  type CountingRowIter struct {
    70  	RowIter
    71  	Stats *DescribeStats
    72  }
    73  
    74  func NewCountingRowIter(iter RowIter, describable WithDescribeStats) CountingRowIter {
    75  	stats := describable.getDescribeStats()
    76  	stats.NumberOfIterations++
    77  	return CountingRowIter{
    78  		RowIter: iter,
    79  		Stats:   stats,
    80  	}
    81  }
    82  
    83  func (c CountingRowIter) Next(ctx *Context) (Row, error) {
    84  	res, err := c.RowIter.Next(ctx)
    85  	if err == nil {
    86  		c.Stats.ActualRowCount++
    87  	}
    88  	return res, err
    89  }
    90  
    91  type Describable interface {
    92  	Describe(options DescribeOptions) string
    93  }
    94  
    95  // Describe produces a human-readable string for |n|, based on the values set in |options|.
    96  // For |n| to benefit from |options|, it must implement `sql.Describable`.
    97  func Describe(n fmt.Stringer, options DescribeOptions) string {
    98  	if d, ok := n.(Describable); ok {
    99  		return d.Describe(options)
   100  	}
   101  	if d, ok := n.(DebugStringer); ok && options.Debug {
   102  		return d.DebugString()
   103  	}
   104  	return n.String()
   105  }
   106  
   107  type DescribeOptions struct {
   108  	Analyze   bool
   109  	Estimates bool
   110  	Debug     bool
   111  }
   112  
   113  func (d DescribeOptions) String() string {
   114  	result := ""
   115  	if d.Analyze {
   116  		result = result + "analyze,"
   117  	} else if d.Estimates {
   118  		result = result + "estimates,"
   119  	}
   120  	if d.Debug {
   121  		result = result + "debug,"
   122  	}
   123  	result = strings.TrimSuffix(result, ",")
   124  	if result == "" {
   125  		return "tree"
   126  	}
   127  	return result
   128  }