File size: 9,029 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package aggregate

import (
	"fmt"

	"github.com/tailor-inc/graphql"
	"github.com/weaviate/weaviate/adapters/handlers/graphql/descriptions"
	"github.com/weaviate/weaviate/adapters/handlers/graphql/local/common_filters"
	"github.com/weaviate/weaviate/adapters/handlers/graphql/utils"
	"github.com/weaviate/weaviate/entities/aggregation"
	"github.com/weaviate/weaviate/entities/models"
	"github.com/weaviate/weaviate/entities/schema"
	"github.com/weaviate/weaviate/usecases/config"
)

type ModulesProvider interface {
	AggregateArguments(class *models.Class) map[string]*graphql.ArgumentConfig
	ExtractSearchParams(arguments map[string]interface{}, className string) map[string]interface{}
}

// Build the Aggregate Kinds schema
func Build(dbSchema *schema.Schema, config config.Config,

	modulesProvider ModulesProvider,

) (*graphql.Field, error) {
	if len(dbSchema.Objects.Classes) == 0 {
		return nil, utils.ErrEmptySchema
	}

	var err error
	var localAggregateObjects *graphql.Object
	if len(dbSchema.Objects.Classes) > 0 {
		localAggregateObjects, err = classFields(dbSchema.Objects.Classes, config, modulesProvider)
		if err != nil {
			return nil, err
		}
	}

	field := graphql.Field{
		Name:        "Aggregate",
		Description: descriptions.AggregateWhere,
		Type:        localAggregateObjects,
		Resolve:     passThroughResolver,
	}

	return &field, nil
}

func classFields(databaseSchema []*models.Class,

	config config.Config, modulesProvider ModulesProvider,

) (*graphql.Object, error) {
	fields := graphql.Fields{}

	for _, class := range databaseSchema {
		field, err := classField(class, class.Description, config, modulesProvider)
		if err != nil {
			return nil, err
		}

		fields[class.Class] = field
	}

	return graphql.NewObject(graphql.ObjectConfig{
		Name:        "AggregateObjectsObj",
		Fields:      fields,
		Description: descriptions.AggregateObjectsObj,
	}), nil
}

func classField(class *models.Class, description string,

	config config.Config, modulesProvider ModulesProvider,

) (*graphql.Field, error) {
	metaClassName := fmt.Sprintf("Aggregate%s", class.Class)

	fields := graphql.ObjectConfig{
		Name: metaClassName,
		Fields: (graphql.FieldsThunk)(func() graphql.Fields {
			fields, err := classPropertyFields(class)
			if err != nil {
				// we cannot return an error in this FieldsThunk and have to panic unfortunately
				panic(fmt.Sprintf("Failed to assemble single Local Aggregate Class field: %s", err))
			}

			return fields
		}),
		Description: description,
	}

	fieldsObject := graphql.NewObject(fields)
	fieldsField := &graphql.Field{
		Type:        graphql.NewList(fieldsObject),
		Description: description,
		Args: graphql.FieldConfigArgument{
			"limit": &graphql.ArgumentConfig{
				Description: descriptions.First,
				Type:        graphql.Int,
			},
			"where": &graphql.ArgumentConfig{
				Description: descriptions.GetWhere,
				Type: graphql.NewInputObject(
					graphql.InputObjectConfig{
						Name:        fmt.Sprintf("AggregateObjects%sWhereInpObj", class.Class),
						Fields:      common_filters.BuildNew(fmt.Sprintf("AggregateObjects%s", class.Class)),
						Description: descriptions.GetWhereInpObj,
					},
				),
			},
			"groupBy": &graphql.ArgumentConfig{
				Description: descriptions.GroupBy,
				Type:        graphql.NewList(graphql.String),
			},
			"nearVector": nearVectorArgument(class.Class),
			"nearObject": nearObjectArgument(class.Class),
			"objectLimit": &graphql.ArgumentConfig{
				Description: descriptions.First,
				Type:        graphql.Int,
			},
			"hybrid": hybridArgument(fieldsObject, class, modulesProvider),
		},
		Resolve: makeResolveClass(modulesProvider, class),
	}

	if modulesProvider != nil {
		for name, argument := range modulesProvider.AggregateArguments(class) {
			fieldsField.Args[name] = argument
		}
	}

	if schema.MultiTenancyEnabled(class) {
		fieldsField.Args["tenant"] = tenantArgument()
	}

	return fieldsField, nil
}

func classPropertyFields(class *models.Class) (graphql.Fields, error) {
	fields := graphql.Fields{}
	for _, property := range class.Properties {
		propertyType, err := schema.GetPropertyDataType(class, property.Name)
		if err != nil {
			return nil, fmt.Errorf("%s.%s: %s", class.Class, property.Name, err)
		}

		convertedDataType, err := classPropertyField(*propertyType, class, property)
		if err != nil {
			return nil, err
		}

		fields[property.Name] = convertedDataType
	}

	// Special case: meta { count } appended to all regular props
	fields["meta"] = &graphql.Field{
		Description: descriptions.LocalMetaObj,
		Type:        metaObject(fmt.Sprintf("Aggregate%s", class.Class)),
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			// pass-through
			return p.Source, nil
		},
	}

	// Always append Grouped By field
	fields["groupedBy"] = &graphql.Field{
		Description: descriptions.AggregateGroupedBy,
		Type:        groupedByProperty(class),
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			switch typed := p.Source.(type) {
			case aggregation.Group:
				return typed.GroupedBy, nil
			case map[string]interface{}:
				return typed["groupedBy"], nil
			default:
				return nil, fmt.Errorf("groupedBy: unsupported type %T", p.Source)
			}
		},
	}

	return fields, nil
}

func metaObject(prefix string) *graphql.Object {
	return graphql.NewObject(graphql.ObjectConfig{
		Name: fmt.Sprintf("%sMetaObject", prefix),
		Fields: graphql.Fields{
			"count": &graphql.Field{
				Type: graphql.Int,
				Resolve: func(p graphql.ResolveParams) (interface{}, error) {
					group, ok := p.Source.(aggregation.Group)
					if !ok {
						return nil, fmt.Errorf("meta count: expected aggregation.Group, got %T", p.Source)
					}

					return group.Count, nil
				},
			},
		},
	})
}

func classPropertyField(dataType schema.DataType, class *models.Class, property *models.Property) (*graphql.Field, error) {
	switch dataType {
	case schema.DataTypeText:
		return makePropertyField(class, property, stringPropertyFields)
	case schema.DataTypeInt:
		return makePropertyField(class, property, numericPropertyFields)
	case schema.DataTypeNumber:
		return makePropertyField(class, property, numericPropertyFields)
	case schema.DataTypeBoolean:
		return makePropertyField(class, property, booleanPropertyFields)
	case schema.DataTypeDate:
		return makePropertyField(class, property, datePropertyFields)
	case schema.DataTypeCRef:
		return makePropertyField(class, property, referencePropertyFields)
	case schema.DataTypeGeoCoordinates:
		// simply skip for now, see gh-729
		return nil, nil
	case schema.DataTypePhoneNumber:
		// skipping for now, see gh-1088 where it was outscoped
		return nil, nil
	case schema.DataTypeBlob:
		return makePropertyField(class, property, stringPropertyFields)
	case schema.DataTypeTextArray:
		return makePropertyField(class, property, stringPropertyFields)
	case schema.DataTypeIntArray, schema.DataTypeNumberArray:
		return makePropertyField(class, property, numericPropertyFields)
	case schema.DataTypeBooleanArray:
		return makePropertyField(class, property, booleanPropertyFields)
	case schema.DataTypeDateArray:
		return makePropertyField(class, property, datePropertyFields)
	case schema.DataTypeUUID, schema.DataTypeUUIDArray:
		// not aggregatable
		return nil, nil
	case schema.DataTypeObject, schema.DataTypeObjectArray:
		// TODO: check if it's aggregable, skip for now
		return nil, nil
	default:
		return nil, fmt.Errorf(schema.ErrorNoSuchDatatype+": %s", dataType)
	}
}

type propertyFieldMaker func(class *models.Class,

	property *models.Property, prefix string) *graphql.Object

func makePropertyField(class *models.Class, property *models.Property,

	fieldMaker propertyFieldMaker,

) (*graphql.Field, error) {
	prefix := "Aggregate"
	return &graphql.Field{
		Description: fmt.Sprintf(`%s"%s"`, descriptions.AggregateProperty, property.Name),
		Type:        fieldMaker(class, property, prefix),
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			switch typed := p.Source.(type) {
			case aggregation.Group:
				res, ok := typed.Properties[property.Name]
				if !ok {
					return nil, fmt.Errorf("missing property '%s'", property.Name)
				}

				return res, nil

			default:
				return nil, fmt.Errorf("property %s, unsupported type %T", property.Name, p.Source)
			}
		},
	}, nil
}

func passThroughResolver(p graphql.ResolveParams) (interface{}, error) {
	// bubble up root resolver
	return p.Source, nil
}