Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-release-notes] Expose JSON type and comparison functions for use by Dolt #2597

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sql/expression/function/json/json_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func getMutableJSONVal(ctx *sql.Context, row sql.Row, json sql.Expression) (type
return nil, err
}

return mutableJsonDoc(ctx, doc)
return MutableJsonDoc(ctx, doc)
}

// getSearchableJSONVal returns a SearchableJSONValue from the given row and expression. The underlying value is not copied
Expand Down Expand Up @@ -92,8 +92,8 @@ func getJsonFunctionError(functionName string, argumentPosition int, err error)
return err
}

// mutableJsonDoc returns a copy of |wrapper| that can be safely mutated.
func mutableJsonDoc(ctx context.Context, wrapper sql.JSONWrapper) (types.MutableJSON, error) {
// MutableJsonDoc returns a copy of |wrapper| that can be safely mutated.
func MutableJsonDoc(ctx context.Context, wrapper sql.JSONWrapper) (types.MutableJSON, error) {
// Call Clone() even if |wrapper| isn't mutable. This is because some implementations (like LazyJsonDocument)
// cache and reuse the result of ToInterface(), and mutating this map may cause unintended behavior.
clonedJsonWrapper := wrapper.Clone(ctx)
Expand Down
67 changes: 37 additions & 30 deletions sql/expression/function/json/json_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,56 +88,63 @@ func (j JSONType) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return "NULL", nil
}

if conv, ok := j.JSON.(*expression.Convert); ok {
typ := conv.Child.Type()
if types.IsDatetimeType(typ) {
return "DATETIME", nil
}
if types.IsDateType(typ) {
return "DATE", nil
}
if types.IsTime(typ) {
return "TIME", nil
}
if types.IsUnsigned(typ) || types.IsYear(typ) {
return "UNSIGNED INTEGER", nil
}

}

if comparableDoc, ok := doc.(types.ComparableJSON); ok {
return comparableDoc.Type(ctx)
}

val, err := doc.ToInterface()
if err != nil {
return nil, err
}

return TypeOfJsonValue(val), nil
}

func TypeOfJsonValue(val interface{}) string {
switch v := val.(type) {
case nil:
return "NULL", nil
return "NULL"
case bool:
return "BOOLEAN", nil
return "BOOLEAN"
case int64:
return "INTEGER", nil
return "INTEGER"
case uint64:
return "UNSIGNED INTEGER", nil
return "UNSIGNED INTEGER"
case float64:
if conv, ok := j.JSON.(*expression.Convert); ok {
typ := conv.Child.Type()
if types.IsUnsigned(typ) || types.IsYear(typ) {
return "UNSIGNED INTEGER", nil
}
}
if math.Floor(v) == v {
if v >= (math.MaxInt32+1)*2 {
return "UNSIGNED INTEGER", nil
return "UNSIGNED INTEGER"
}
return "INTEGER", nil
return "INTEGER"
}
return "DOUBLE", nil
return "DOUBLE"
case string:
if conv, ok := j.JSON.(*expression.Convert); ok {
typ := conv.Child.Type()
if types.IsDatetimeType(typ) {
return "DATETIME", nil
}
if types.IsDateType(typ) {
return "DATE", nil
}
if types.IsTime(typ) {
return "TIME", nil
}
}
return "STRING", nil
return "STRING"
case []interface{}:
return "ARRAY", nil
return "ARRAY"
case map[string]interface{}:
return "OBJECT", nil
return "OBJECT"
case decimal.Decimal:
return "DECIMAL", nil
return "DECIMAL"
default:
return "OPAQUE", nil
return "OPAQUE"
}
}

Expand Down
15 changes: 15 additions & 0 deletions sql/types/json_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ type SearchableJSON interface {
Lookup(ctx context.Context, path string) (sql.JSONWrapper, error)
}

type ComparableJSON interface {
sql.JSONWrapper
Compare(other interface{}) (int, error)
Type(ctx context.Context) (string, error)
}

// MutableJSON is a JSON value that can be efficiently modified. These modifications return the new value, but they
// are not required to preserve the state of the original value. If you want to preserve the old value, call |Clone|
// first and modify the clone, which is guaranteed to not affect the original.
Expand Down Expand Up @@ -502,6 +508,15 @@ func CompareJSON(a, b interface{}) (int, error) {
return res, nil
}

if comparableA, ok := a.(ComparableJSON); ok {
return comparableA.Compare(b)
}

if comparableB, ok := b.(ComparableJSON); ok {
result, err := comparableB.Compare(a)
return -result, err
}

switch a := a.(type) {
case bool:
return compareJSONBool(a, b)
Expand Down
Loading