Skip to content

Commit af88844

Browse files
committed
feat: implement GetSelectivityOfSQLColumns method in PluginImplV1 and PluginImplV2, and add corresponding gRPC server handling
1 parent c3a8148 commit af88844

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

sqle/driver/plugin_adapter_v1.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,7 @@ func (s *PluginImplV1) GetDatabaseObjectDDL(ctx context.Context, objInfos []*dri
353353
func (s *PluginImplV1) GetDatabaseDiffModifySQL(ctx context.Context, calibratedDSN *driverV2.DSN, objInfos []*driverV2.DatabasCompareSchemaInfo) ([]*driverV2.DatabaseDiffModifySQLResult, error) {
354354
return nil, fmt.Errorf("unimplement this method")
355355
}
356+
357+
func (p *PluginImplV1) GetSelectivityOfSQLColumns(ctx context.Context, sql string) (map[string]map[string]float32, error) {
358+
return nil, fmt.Errorf("unimplement this method")
359+
}

sqle/driver/plugin_adapter_v2.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,3 +725,25 @@ func (s *PluginImplV2) GetDatabaseDiffModifySQL(ctx context.Context, calibratedD
725725
}
726726
return dbDiffSQLs, nil
727727
}
728+
729+
func (s *PluginImplV2) GetSelectivityOfSQLColumns(ctx context.Context, sql string) (map[string]map[string]float32, error) {
730+
api := "GetSelectivityOfSQLColumns"
731+
s.preLog(api)
732+
resp, err := s.client.GetSelectivityOfSQLColumns(ctx, &protoV2.GetSelectivityOfSQLColumnsRequest{
733+
Session: s.Session,
734+
Sql: sql,
735+
})
736+
s.afterLog(api, err)
737+
if err != nil {
738+
return nil, err
739+
}
740+
result := make(map[string]map[string]float32, len(resp.Selectivity))
741+
for _, v := range resp.Selectivity {
742+
colMap := make(map[string]float32, len(v.SelectivityOfColumns))
743+
for k, sel := range v.SelectivityOfColumns {
744+
colMap[k] = sel
745+
}
746+
result[v.TableName] = colMap
747+
}
748+
return result, nil
749+
}

sqle/driver/v2/driver_grpc_server.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,3 +674,27 @@ func (d *DriverGrpcServer) GetDatabaseDiffModifySQL(ctx context.Context, req *pr
674674
SchemaDiffModify: scheamDiff,
675675
}, nil
676676
}
677+
678+
func (d *DriverGrpcServer) GetSelectivityOfSQLColumns(ctx context.Context, req *protoV2.GetSelectivityOfSQLColumnsRequest) (*protoV2.GetSelectivityOfSQLColumnsResponse, error) {
679+
driver, err := d.getDriverBySession(req.Session)
680+
if err != nil {
681+
return &protoV2.GetSelectivityOfSQLColumnsResponse{}, err
682+
}
683+
selectivity, err := driver.GetSelectivityOfSQLColumns(ctx, req.Sql)
684+
if err != nil {
685+
return &protoV2.GetSelectivityOfSQLColumnsResponse{}, err
686+
}
687+
protoSelectivity := make([]*protoV2.SelectivityOfSQLColumns, 0, len(selectivity))
688+
for tableName, colMap := range selectivity {
689+
// 直接将 map[string]float32 赋值,无需合并操作
690+
merged := make(map[string]float32, len(colMap))
691+
for col, val := range colMap {
692+
merged[col] = val
693+
}
694+
protoSelectivity = append(protoSelectivity, &protoV2.SelectivityOfSQLColumns{
695+
TableName: tableName,
696+
SelectivityOfColumns: merged,
697+
})
698+
}
699+
return &protoV2.GetSelectivityOfSQLColumnsResponse{Selectivity: protoSelectivity}, nil
700+
}

sqle/pkg/driver/impl.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,7 @@ func (p *DriverImpl) GetDatabaseObjectDDL(ctx context.Context, objInfos []*drive
311311
func (p *DriverImpl) GetDatabaseDiffModifySQL(ctx context.Context, calibratedDSN *driverV2.DSN, objInfos []*driverV2.DatabasCompareSchemaInfo) ([]*driverV2.DatabaseDiffModifySQLResult, error) {
312312
return []*driverV2.DatabaseDiffModifySQLResult{}, nil
313313
}
314+
315+
func (p *DriverImpl) GetSelectivityOfSQLColumns(ctx context.Context, sql string) (map[string]map[string]float32, error) {
316+
return nil, nil
317+
}

sqle/server/sqled_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ func (d *mockDriver) ExplainJSONFormat(ctx context.Context, conf *driverV2.Expla
126126
return nil, nil
127127
}
128128

129+
func (d *mockDriver) GetSelectivityOfSQLColumns(ctx context.Context, sql string) (map[string]map[string]float32, error) {
130+
return make(map[string]map[string]float32), nil
131+
}
132+
129133
func TestAction_validation(t *testing.T) {
130134
actions := map[int]*action{
131135
ActionTypeAudit: {typ: ActionTypeAudit},

0 commit comments

Comments
 (0)