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 07:45:01 -04:00
committed by GitHub
co-authored by Kyle Brandt
parent 6754781d7b
commit af08a9fae2
5 changed files with 136 additions and 2 deletions
+17 -1
View File
@@ -3,6 +3,7 @@
package sql
import (
"encoding/json"
"errors"
"fmt"
"io"
@@ -92,6 +93,8 @@ func MySQLColToFieldType(col *mysql.Column) (data.FieldType, error) {
fT = data.FieldTypeTime
case types.Boolean:
fT = data.FieldTypeBool
case types.JSON:
fT = data.FieldTypeJSON
default:
switch {
case types.IsDecimal(col.Type):
@@ -159,8 +162,21 @@ func fieldValFromRowVal(fieldType data.FieldType, val interface{}) (interface{},
case data.FieldTypeBool, data.FieldTypeNullableBool:
return parseBoolFromInt8(val, nullable)
case data.FieldTypeJSON, data.FieldTypeNullableJSON:
switch v := val.(type) {
case types.JSONDocument:
raw := json.RawMessage(v.String())
if nullable {
return &raw, nil
}
return raw, nil
default:
return nil, fmt.Errorf("JSON field does not support val %v of type %T", val, val)
}
default:
return nil, fmt.Errorf("unsupported field type %s for val %v", fieldType, val)
return nil, fmt.Errorf("unsupported field type %s for val %v of type %T", fieldType, val, val)
}
}