mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 21:42:37 +08:00
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:
@ -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
|
||||
|
Reference in New Issue
Block a user