SQL Expressions: Query Service Support (#101955)

---------

Co-authored-by: Adam Simpson <adam@adamsimpson.net>
Co-authored-by: Sarah Zinger <sarah.zinger@grafana.com>
This commit is contained in:
Kyle Brandt
2025-04-03 09:36:02 -04:00
committed by GitHub
co-authored by Adam Simpson Sarah Zinger
parent 370f4c2bcd
commit 4a0ec27e5d
8 changed files with 118 additions and 57 deletions
+2 -37
View File
@@ -341,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)
responseType, result, err := s.converter.Convert(ctx, dn.datasource.Type, dataFrames, dn.isInputToSQLExpr)
if err != nil {
result.Error = makeConversionError(dn.RefID(), err)
}
@@ -409,44 +409,9 @@ func (dn *DSNode) Execute(ctx context.Context, now time.Time, _ mathexp.Vars, s
}
var result mathexp.Results
// 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
}
}
}
responseType, result, err = s.converter.Convert(ctx, dn.datasource.Type, dataFrames, dn.isInputToSQLExpr)
if needsConversion {
convertedFrames, err := ConvertToFullLong(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)
}