MCPcopy Create free account
hub / github.com/dolthub/doltgresql / Compare

Method Compare

server/types/type.go:199–404  ·  view source on GitHub ↗

Compare implements the types.ExtendedType interface.

(ctx context.Context, v1 interface{}, v2 interface{})

Source from the content-addressed store, hash-verified

197
198// Compare implements the types.ExtendedType interface.
199func (t *DoltgresType) Compare(ctx context.Context, v1 interface{}, v2 interface{}) (int, error) {
200 // TODO: for some large types, we could do this much faster by doing it chunk-by-chunk, rather than eagerly loading
201 // the full value into memory
202 var err error
203 v1, err = sql.UnwrapAny(ctx, v1)
204 if err != nil {
205 return 0, err
206 }
207 v2, err = sql.UnwrapAny(ctx, v2)
208 if err != nil {
209 return 0, err
210 }
211
212 if v1 == nil && v2 == nil {
213 return 0, nil
214 } else if v1 != nil && v2 == nil {
215 return 1, nil
216 } else if v1 == nil && v2 != nil {
217 return -1, nil
218 }
219
220 if t.TypType == TypeType_Enum {
221 // TODO: temporary solution to getting the enum type (which has label info) into the 'enum_cmp' function
222 // ctx is not guaranteed to be a *sql.Context when called from index comparator goroutines.
223 sqlCtx, ok := ctx.(*sql.Context)
224 if !ok {
225 sqlCtx = sql.NewEmptyContext()
226 }
227 qf := globalFunctionRegistry.GetFunction(sqlCtx, t.CompareFunc)
228 resTypes := qf.ResolvedTypes()
229 newFunc := qf.WithResolvedTypes([]*DoltgresType{t, t, resTypes[len(resTypes)-1]})
230 i, err := newFunc.(QuickFunction).CallVariadic(nil, v1, v2)
231 if err != nil {
232 return 0, err
233 }
234 return int(i.(int32)), nil
235 } else if t == Oidvector {
236 // ctx is not guaranteed to be a *sql.Context when called from index comparator goroutines.
237 sqlCtx, ok := ctx.(*sql.Context)
238 if !ok {
239 sqlCtx = sql.NewEmptyContext()
240 }
241 i, err := globalFunctionRegistry.GetFunction(sqlCtx, t.CompareFunc).CallVariadic(nil, v1, v2)
242 if err != nil {
243 return 0, err
244 }
245 return int(i.(int32)), nil
246 }
247
248 switch ab := v1.(type) {
249 case bool:
250 bb := v2.(bool)
251 if ab == bb {
252 return 0, nil
253 } else if !ab {
254 return -1, nil
255 } else {
256 return 1, nil

Callers

nothing calls this directly

Calls 15

IsArrayTypeMethod · 0.95
ArrayBaseTypeMethod · 0.95
IsCompositeTypeMethod · 0.95
CacheFunction · 0.92
MinFunction · 0.92
NumericCompareFunction · 0.85
UnwrapAnyMethod · 0.80
GetBytesMutMethod · 0.80
ToOIDMethod · 0.80
OIDMethod · 0.80
ResolvedTypesMethod · 0.65
WithResolvedTypesMethod · 0.65

Tested by

no test coverage detected