SQL Expressions: Re-implement feature using go-mysql-server (#99521)

* Under feature flag `sqlExpressions` and is experimental
* Excluded from arm32
* Will not work with the Query Service yet
* Does not have limits in place yet
* Does not working with alerting yet
* Currently requires "prepare time series" Transform for time series viz
 
---------

Co-authored-by: Sam Jewell <sam.jewell@grafana.com>
This commit is contained in:
Kyle Brandt
2025-02-06 07:27:28 -05:00
committed by GitHub
co-authored by Sam Jewell
parent 4e6bdce41c
commit d64f41afdc
33 changed files with 1969 additions and 405 deletions
+47 -2
View File
@@ -112,6 +112,12 @@ func buildCMDNode(rn *rawNode, toggles featuremgmt.FeatureToggles) (*CMDNode, er
return nil, fmt.Errorf("invalid command type in expression '%v': %w", rn.RefID, err)
}
if commandType == TypeSQL {
if !toggles.IsEnabledGlobally(featuremgmt.FlagSqlExpressions) {
return nil, fmt.Errorf("sql expressions are disabled")
}
}
node := &CMDNode{
baseNode: baseNode{
id: rn.idx,
@@ -185,6 +191,8 @@ type DSNode struct {
intervalMS int64
maxDP int64
request Request
isInputToSQLExpr bool
}
func (dn *DSNode) String() string {
@@ -333,7 +341,7 @@ func executeDSNodesGrouped(ctx context.Context, now time.Time, vars mathexp.Vars
}
var result mathexp.Results
responseType, result, err := s.converter.Convert(ctx, dn.datasource.Type, dataFrames, s.allowLongFrames)
responseType, result, err := s.converter.Convert(ctx, dn.datasource.Type, dataFrames)
if err != nil {
result.Error = makeConversionError(dn.RefID(), err)
}
@@ -401,7 +409,44 @@ func (dn *DSNode) Execute(ctx context.Context, now time.Time, _ mathexp.Vars, s
}
var result mathexp.Results
responseType, result, err = s.converter.Convert(ctx, dn.datasource.Type, dataFrames, s.allowLongFrames)
// If the datasource node is an input to a SQL expression,
// the data must be in the Long format
if dn.isInputToSQLExpr {
var needsConversion bool
// Convert it if Multi:
if len(dataFrames) > 1 {
needsConversion = true
}
// Convert it if Wide (has labels):
if len(dataFrames) == 1 {
for _, field := range dataFrames[0].Fields {
if len(field.Labels) > 0 {
needsConversion = true
break
}
}
}
if needsConversion {
convertedFrames, err := ConvertToLong(dataFrames)
if err != nil {
return result, fmt.Errorf("failed to convert data frames to long format for sql: %w", err)
}
result.Values = mathexp.Values{
mathexp.TableData{Frame: convertedFrames[0]},
}
return result, nil
}
// Otherwise it is already Long format; return as is
result.Values = mathexp.Values{
mathexp.TableData{Frame: dataFrames[0]},
}
return result, nil
}
responseType, result, err = s.converter.Convert(ctx, dn.datasource.Type, dataFrames)
if err != nil {
err = makeConversionError(dn.refID, err)
}