SQL Expressions: Exclude CTEs from required Tables (#106479)
Fixes #105030 --------- Co-authored-by: Sam Jewell <2903904+samjewell@users.noreply.github.com>
This commit is contained in:
co-authored by
Sam Jewell
parent
c0e7701ad4
commit
1be9e60667
@@ -167,14 +167,8 @@ func (p *queryParser) parseRequest(ctx context.Context, input *query.QueryDataRe
|
||||
q, ok := queryRefIDs[refId]
|
||||
|
||||
if !ok {
|
||||
_, isSQLCMD := target.Command.(*expr.SQLCommand)
|
||||
if isSQLCMD {
|
||||
continue
|
||||
} else {
|
||||
target, ok = expressions[refId]
|
||||
if !ok {
|
||||
return rsp, makeDependencyError(exp.RefID, refId)
|
||||
}
|
||||
if target, ok = expressions[refId]; !ok {
|
||||
return rsp, makeDependencyError(exp.RefID, refId)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -201,6 +201,46 @@ func TestSqlInputs(t *testing.T) {
|
||||
require.Equal(t, parsedRequestInfo.SqlInputs["B"], struct{}{})
|
||||
}
|
||||
|
||||
func TestSqlCTE(t *testing.T) {
|
||||
parser := newQueryParser(
|
||||
expr.NewExpressionQueryReader(featuremgmt.WithFeatures(featuremgmt.FlagSqlExpressions)),
|
||||
nil,
|
||||
tracing.InitializeTracerForTest(),
|
||||
log.NewNopLogger(),
|
||||
)
|
||||
|
||||
parsedRequestInfo, err := parser.parseRequest(context.Background(), &query.QueryDataRequest{
|
||||
QueryDataRequest: data.QueryDataRequest{
|
||||
Queries: []data.DataQuery{
|
||||
data.NewDataQuery(map[string]any{
|
||||
"refId": "A",
|
||||
"datasource": &data.DataSourceRef{
|
||||
Type: "prometheus",
|
||||
UID: "local-prom",
|
||||
},
|
||||
}),
|
||||
data.NewDataQuery(map[string]any{
|
||||
"refId": "B",
|
||||
"datasource": &data.DataSourceRef{
|
||||
Type: "__expr__",
|
||||
UID: "__expr__",
|
||||
},
|
||||
"type": "sql",
|
||||
"expression": `WITH CTE AS (
|
||||
SELECT
|
||||
Month
|
||||
FROM A
|
||||
)
|
||||
|
||||
SELECT * FROM CTE`,
|
||||
}),
|
||||
},
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, parsedRequestInfo.SqlInputs["B"], struct{}{})
|
||||
}
|
||||
|
||||
func TestGrafanaDS(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
parser := newQueryParser(expr.NewExpressionQueryReader(featuremgmt.WithFeatures()),
|
||||
|
||||
Reference in New Issue
Block a user