Skip to content

Commit

Permalink
Some refactoring (#773)
Browse files Browse the repository at this point in the history
* Refactor scenario name

* Refactor gram

* Refactor interactor.go
  • Loading branch information
EinKrebs authored Sep 17, 2024
1 parent 59f965d commit d5656e7
Show file tree
Hide file tree
Showing 8 changed files with 524 additions and 353 deletions.
80 changes: 58 additions & 22 deletions pkg/clientinteractor/interactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ const TEXTOID = 25
// DOUBLEOID https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.dat#L223
const DOUBLEOID = 701

// INTOID https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.dat#L55
const INTOID = 20

// TODO : unit tests

// TextOidFD generates a pgproto3.FieldDescription object with the provided statement text.
Expand All @@ -106,6 +109,13 @@ func TextOidFD(stmt string) pgproto3.FieldDescription {
}
}

// FloatOidFD generates a pgproto3.FieldDescription object of FLOAT8 type with the provided statement text.
//
// Parameters:
// - stmt (string): The statement text to use in the FieldDescription.
//
// Returns:
// - A pgproto3.FieldDescription object initialized with the provided statement text and default values.
func FloatOidFD(stmt string) pgproto3.FieldDescription {
return pgproto3.FieldDescription{
Name: []byte(stmt),
Expand All @@ -118,6 +128,25 @@ func FloatOidFD(stmt string) pgproto3.FieldDescription {
}
}

// IntOidFD generates a pgproto3.FieldDescription object of INT type with the provided statement text.
//
// Parameters:
// - stmt (string): The statement text to use in the FieldDescription.
//
// Returns:
// - A pgproto3.FieldDescription object initialized with the provided statement text and default values.
func IntOidFD(stmt string) pgproto3.FieldDescription {
return pgproto3.FieldDescription{
Name: []byte(stmt),
TableOID: 0,
TableAttributeNumber: 0,
DataTypeOID: INTOID,
DataTypeSize: 8,
TypeModifier: -1,
Format: 0,
}
}

// TODO : unit tests

// WriteHeader sends the row description message with the specified field descriptions.
Expand Down Expand Up @@ -1298,12 +1327,13 @@ func (pi *PSQLInteractor) KillClient(clientID uint) error {
// BackendConnections writes backend connection information to the PSQL client.
//
// Parameters:
// - ctx (context.Context): The context for the operation.
// - _ (context.Context): The context for the operation.
// - shs ([]shard.Shardinfo): The list of shard information.
// - stmt (*spqrparser.Show): The query itself.
//
// Returns:
// - error: An error if any occurred during the operation.
func (pi *PSQLInteractor) BackendConnections(ctx context.Context, shs []shard.Shardinfo, groupByClause *spqrparser.Group) error {
func (pi *PSQLInteractor) BackendConnections(_ context.Context, shs []shard.Shardinfo, stmt *spqrparser.Show) error {
headers := []string{"backend connection id", "router", "shard key name", "hostname", "pid", "user", "dbname", "sync", "tx_served", "tx status"}
getters := []func(sh shard.Shardinfo) string{
func(sh shard.Shardinfo) string { return fmt.Sprintf("%d", sh.ID()) },
Expand All @@ -1325,27 +1355,30 @@ func (pi *PSQLInteractor) BackendConnections(ctx context.Context, shs []shard.Sh
func(sh shard.Shardinfo) string { return sh.TxStatus().String() },
}

if groupByClause != nil {
return groupBy(headers, shs, getters, groupByClause.Col.ColName, pi)
}

if err := pi.WriteHeader(headers...); err != nil {
spqrlog.Zero.Error().Err(err).Msg("")
return err
}

for _, sh := range shs {
vals := []string{}
for _, getter := range getters {
vals = append(vals, getter(sh))
}
if err := pi.WriteDataRow(vals...); err != nil {
switch gb := stmt.GroupBy.(type) {
case spqrparser.GroupBy:
return groupBy(headers, shs, getters, gb.Col.ColName, pi)
case spqrparser.WhereClauseEmpty:
if err := pi.WriteHeader(headers...); err != nil {
spqrlog.Zero.Error().Err(err).Msg("")
return err
}
}

return pi.CompleteMsg(len(shs))
for _, sh := range shs {
vals := make([]string, 0)
for _, getter := range getters {
vals = append(vals, getter(sh))
}
if err := pi.WriteDataRow(vals...); err != nil {
spqrlog.Zero.Error().Err(err).Msg("")
return err
}
}

return pi.CompleteMsg(len(shs))
default:
return spqrerror.NewByCode(spqrerror.SPQR_INVALID_REQUEST)
}
}

// TODO unit tests
Expand Down Expand Up @@ -1431,11 +1464,14 @@ func (pi *PSQLInteractor) PreparedStatements(ctx context.Context, shs []shard.Pr
return pi.CompleteMsg(len(shs))
}

func groupBy[T any](headers []string, values []T, getters []func(s T) string, groupBy string, pi *PSQLInteractor) error {
func groupBy[T any](headers []string, values []T, getters []func(s T) string, groupByCol string, pi *PSQLInteractor) error {
ind := -1
for i, header := range headers {
if header == groupBy {
if err := pi.WriteHeader(groupBy, "count"); err != nil {
if header == groupByCol {
if err := pi.cl.Send(&pgproto3.RowDescription{
Fields: []pgproto3.FieldDescription{TextOidFD(groupByCol), IntOidFD("connections count")},
}); err != nil {
spqrlog.Zero.Error().Err(err).Msg("Could not write header for backend connections")
return err
}
ind = i
Expand Down
6 changes: 1 addition & 5 deletions pkg/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,7 @@ func ProcessShow(ctx context.Context, stmt *spqrparser.Show, mngr EntityMgr, ci
return err
}

var groupBy *spqrparser.Group
if stmt.Group != nil {
groupBy = stmt.Group.(*spqrparser.Group)
}
return cli.BackendConnections(ctx, resp, groupBy)
return cli.BackendConnections(ctx, resp, stmt)
case spqrparser.ShardsStr:
shards, err := mngr.ListShards(ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion test/feature/features/coordinator_show.feature
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ Feature: Coordinator show clients, pools and backend_connections
]
"""

Scenario: show backend_connections group by
Scenario: 'show backend_connections group by hostname' works
When I run SQL on host "coordinator"
"""
SHOW backend_connections group by hostname
Expand Down
18 changes: 11 additions & 7 deletions yacc/console/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ type Order struct {
Col ColumnRef
}

type GroupClause interface{}
type GroupByClause interface{}

type Group struct {
GroupClause
type GroupByClauseEmpty struct {
GroupByClause
}

type GroupBy struct {
GroupByClause
Col ColumnRef
}

Expand Down Expand Up @@ -54,10 +58,10 @@ type WhereClauseOp struct {
}

type Show struct {
Cmd string
Where WhereClauseNode
Order OrderClause
Group GroupClause
Cmd string
Where WhereClauseNode
Order OrderClause
GroupBy GroupByClause
}

type Set struct {
Expand Down
8 changes: 4 additions & 4 deletions yacc/console/gram.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions yacc/console/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func randomHex(n int) (string, error) {
order_clause OrderClause
opt_asc_desc OptAscDesc

group_clause GroupClause
group_clause GroupByClause
}

// any non-terminal which returns a value needs a type, which is
Expand Down Expand Up @@ -591,15 +591,15 @@ order_clause:
group_clause:
GROUP BY ColRef
{
$$ = &Group{Col: $3}
$$ = GroupBy{Col: $3}
}
| /* empty */ {$$ = GroupClause(nil)}
| /* empty */ {$$ = GroupByClauseEmpty{}}


show_stmt:
SHOW show_statement_type where_clause group_clause order_clause
{
$$ = &Show{Cmd: $2, Where: $3, Group: $4, Order: $5}
$$ = &Show{Cmd: $2, Where: $3, GroupBy: $4, Order: $5}
}
lock_stmt:
LOCK key_range_stmt
Expand Down
Loading

0 comments on commit d5656e7

Please sign in to comment.