Skip to content

Commit fead612

Browse files
committed
Have Sort, Case, and GroupBy implement Describable
1 parent 0b1008f commit fead612

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

sql/expression/case.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,27 @@ func (c *Case) DebugString() string {
244244
buf.WriteString(" END")
245245
return buf.String()
246246
}
247+
248+
func (c *Case) Describe(options sql.DescribeOptions) string {
249+
var buf bytes.Buffer
250+
251+
buf.WriteString("CASE ")
252+
if c.Expr != nil {
253+
buf.WriteString(sql.Describe(c.Expr, options))
254+
}
255+
256+
for _, b := range c.Branches {
257+
buf.WriteString(" WHEN ")
258+
buf.WriteString(sql.Describe(b.Cond, options))
259+
buf.WriteString(" THEN ")
260+
buf.WriteString(sql.Describe(b.Value, options))
261+
}
262+
263+
if c.Else != nil {
264+
buf.WriteString(" ELSE ")
265+
buf.WriteString(sql.Describe(c.Else, options))
266+
}
267+
268+
buf.WriteString(" END")
269+
return buf.String()
270+
}

sql/plan/group_by.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var _ sql.Expressioner = (*GroupBy)(nil)
3838
var _ sql.Node = (*GroupBy)(nil)
3939
var _ sql.Projector = (*GroupBy)(nil)
4040
var _ sql.CollationCoercible = (*GroupBy)(nil)
41+
var _ sql.Describable = (*GroupBy)(nil)
4142

4243
// NewGroupBy creates a new GroupBy node. Like Project, GroupBy is a top-level node, and contains all the fields that
4344
// will appear in the output of the query. Some of these fields may be aggregate functions, some may be columns or
@@ -169,6 +170,28 @@ func (g *GroupBy) DebugString() string {
169170
return pr.String()
170171
}
171172

173+
func (g *GroupBy) Describe(options sql.DescribeOptions) string {
174+
pr := sql.NewTreePrinter()
175+
_ = pr.WriteNode("GroupBy")
176+
177+
var selectDeps = make([]string, len(g.SelectDeps))
178+
for i, e := range g.SelectDeps {
179+
selectDeps[i] = sql.Describe(e, options)
180+
}
181+
182+
var grouping = make([]string, len(g.GroupByExprs))
183+
for i, g := range g.GroupByExprs {
184+
grouping[i] = sql.Describe(g, options)
185+
}
186+
187+
_ = pr.WriteChildren(
188+
fmt.Sprintf("select: %s", strings.Join(selectDeps, ", ")),
189+
fmt.Sprintf("group: %s", strings.Join(grouping, ", ")),
190+
sql.Describe(g.Child, options),
191+
)
192+
return pr.String()
193+
}
194+
172195
// Expressions implements the Expressioner interface.
173196
func (g *GroupBy) Expressions() []sql.Expression {
174197
var exprs []sql.Expression

sql/plan/hash_lookup.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type HashLookup struct {
6060
var _ sql.Node = (*HashLookup)(nil)
6161
var _ sql.Expressioner = (*HashLookup)(nil)
6262
var _ sql.CollationCoercible = (*HashLookup)(nil)
63+
var _ sql.Describable = (*HashLookup)(nil)
6364

6465
func (n *HashLookup) Expressions() []sql.Expression {
6566
return []sql.Expression{n.RightEntryKey, n.LeftProbeKey}
@@ -102,6 +103,17 @@ func (n *HashLookup) DebugString() string {
102103
return pr.String()
103104
}
104105

106+
func (n *HashLookup) Describe(options sql.DescribeOptions) string {
107+
pr := sql.NewTreePrinter()
108+
_ = pr.WriteNode("HashLookup")
109+
children := make([]string, 3)
110+
children[0] = fmt.Sprintf("left-key: %s", sql.Describe(n.LeftProbeKey, options))
111+
children[1] = fmt.Sprintf("right-key: %s", sql.Describe(n.RightEntryKey, options))
112+
children[2] = sql.Describe(n.Child, options)
113+
_ = pr.WriteChildren(children...)
114+
return pr.String()
115+
}
116+
105117
func (n *HashLookup) WithChildren(children ...sql.Node) (sql.Node, error) {
106118
if len(children) != 1 {
107119
return nil, sql.ErrInvalidChildrenNumber.New(n, len(children), 1)

sql/plan/sort.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var _ sql.Expressioner = (*Sort)(nil)
4444
var _ sql.Node = (*Sort)(nil)
4545
var _ sql.CollationCoercible = (*Sort)(nil)
4646
var _ Sortable = (*Sort)(nil)
47+
var _ sql.Describable = (*Sort)(nil)
4748

4849
// Resolved implements the Resolvable interface.
4950
func (s *Sort) Resolved() bool {
@@ -70,6 +71,18 @@ func (s *Sort) String() string {
7071
return pr.String()
7172
}
7273

74+
// Describe implements the sql.Describable interface
75+
func (s *Sort) Describe(options sql.DescribeOptions) string {
76+
pr := sql.NewTreePrinter()
77+
var fields = make([]string, len(s.SortFields))
78+
for i, f := range s.SortFields {
79+
fields[i] = sql.Describe(f, options)
80+
}
81+
_ = pr.WriteNode("Sort(%s)", strings.Join(fields, ", "))
82+
_ = pr.WriteChildren(sql.Describe(s.Child, options))
83+
return pr.String()
84+
}
85+
7386
func (s *Sort) DebugString() string {
7487
pr := sql.NewTreePrinter()
7588
var fields = make([]string, len(s.SortFields))

sql/plan/subqueryalias.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var _ sql.Node = (*SubqueryAlias)(nil)
4545
var _ sql.CollationCoercible = (*SubqueryAlias)(nil)
4646
var _ sql.RenameableNode = (*SubqueryAlias)(nil)
4747
var _ sql.OpaqueNode = (*SubqueryAlias)(nil)
48+
var _ sql.Describable = (*SubqueryAlias)(nil)
4849

4950
// NewSubqueryAlias creates a new SubqueryAlias node.
5051
func NewSubqueryAlias(name, textDefinition string, node sql.Node) *SubqueryAlias {
@@ -190,6 +191,21 @@ func (sq *SubqueryAlias) DebugString() string {
190191
return pr.String()
191192
}
192193

194+
func (sq *SubqueryAlias) Describe(options sql.DescribeOptions) string {
195+
pr := sql.NewTreePrinter()
196+
_ = pr.WriteNode("SubqueryAlias")
197+
children := make([]string, 7)
198+
children[0] = fmt.Sprintf("name: %s", sq.name)
199+
children[1] = fmt.Sprintf("outerVisibility: %t", sq.OuterScopeVisibility)
200+
children[2] = fmt.Sprintf("isLateral: %t", sq.IsLateral)
201+
children[3] = fmt.Sprintf("cacheable: %t", sq.CanCacheResults())
202+
children[4] = fmt.Sprintf("colSet: %s", sq.Columns())
203+
children[5] = fmt.Sprintf("tableId: %d", sq.Id())
204+
children[6] = sql.Describe(sq.Child, options)
205+
_ = pr.WriteChildren(children...)
206+
return pr.String()
207+
}
208+
193209
func (sq *SubqueryAlias) WithColumnNames(columns []string) *SubqueryAlias {
194210
ret := *sq
195211
ret.ColumnNames = columns

0 commit comments

Comments
 (0)