SQL Expressions: Add JSON support (#103157)

- Support bi-directional mapping of frame JSON fields and GMS (go-mysql-server) columns 
- Permit GMS json functions

Co-authored-by: Kyle Brandt <kyle@grafana.com>
This commit is contained in:
Sam Jewell
2025-04-01 12:45:01 +01:00
committed by GitHub
parent 6754781d7b
commit af08a9fae2
5 changed files with 136 additions and 2 deletions

View File

@ -3,6 +3,7 @@
package sql
import (
"encoding/json"
"fmt"
"io"
"strings"
@ -90,7 +91,21 @@ func (ri *rowIter) Next(_ *mysql.Context) (mysql.Row, error) {
if field.NilAt(ri.row) {
continue
}
row[colIndex], _ = field.ConcreteAt(ri.row)
val, _ := field.ConcreteAt(ri.row)
// If the field is JSON, convert json.RawMessage to types.JSONDocument
if raw, ok := val.(json.RawMessage); ok {
doc, inRange, err := types.JSON.Convert(raw)
if err != nil {
return nil, fmt.Errorf("failed to convert json.RawMessage to JSONDocument: %w", err)
}
if !inRange {
return nil, fmt.Errorf("invalid JSON value detected at row %d, column %s: value required type coercion", ri.row, ri.ft.Frame.Fields[colIndex].Name)
}
val = doc
}
row[colIndex] = val
}
ri.row++
@ -156,6 +171,8 @@ func convertDataType(fieldType data.FieldType) mysql.Type {
return types.Boolean
case data.FieldTypeTime, data.FieldTypeNullableTime:
return types.Timestamp
case data.FieldTypeJSON, data.FieldTypeNullableJSON:
return types.JSON
default:
fmt.Printf("------- Unsupported field type: %v", fieldType)
return types.JSON