Skip to content

Commit cd0516b

Browse files
committed
fix(graphql): use placeholders when grouping on leaf-backed object fields
Follow-up to #1453. A grouping on an object field backed by a leaf index field used `nil` as its missing-value placeholder, so sub-aggregations on it added a separate missing-value aggregation instead of using the `terms` `missing` option. - Record `grouping_missing_value_placeholder` on each leaf `IndexField` in runtime metadata. - Add `Schema::Type#grouping_missing_value_placeholders_by_index_leaf_path`, memoized per type, so the query adapter does one hash lookup instead of scanning every index definition per field. Generated with Claude Code
1 parent 11db435 commit cd0516b

21 files changed

Lines changed: 851 additions & 106 deletions

File tree

‎config/schema/artifacts/runtime_metadata.yaml‎

Lines changed: 317 additions & 0 deletions
Large diffs are not rendered by default.

‎config/schema/artifacts_with_apollo/runtime_metadata.yaml‎

Lines changed: 317 additions & 0 deletions
Large diffs are not rendered by default.

‎elasticgraph-datastore_core/spec/unit/elastic_graph/datastore_core/index_definition/implementation_shared_examples.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,9 @@ def accessible_cluster_names_to_index_into_for_config(clusters:, index_definitio
294294

295295
expect(index.fields_by_path).to include({
296296
"created_at" => index_field_with,
297-
"id" => index_field_with,
298-
"name" => index_field_with,
299-
"nested_fields.nested_id" => index_field_with
297+
"id" => index_field_with(grouping_missing_value_placeholder: MISSING_STRING_PLACEHOLDER),
298+
"name" => index_field_with(grouping_missing_value_placeholder: MISSING_STRING_PLACEHOLDER),
299+
"nested_fields.nested_id" => index_field_with(grouping_missing_value_placeholder: MISSING_STRING_PLACEHOLDER)
300300
})
301301
end
302302
end

‎elasticgraph-graphql/lib/elastic_graph/graphql/aggregation/query_adapter.rb‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def call(query:, lookahead:, args:, field:, context:)
6767
aggregations_node,
6868
field: field,
6969
context: context,
70-
index_definitions: query.initial_search_index_definitions,
70+
grouping_missing_value_placeholders_by_index_leaf_path: field.type.unwrap_fully.grouping_missing_value_placeholders_by_index_leaf_path,
7171
grouping_adapter: CompositeGroupingAdapter,
7272
# Filters on root aggregations applied to the search query body itself instead of
7373
# using a filter aggregation, like sub-aggregations do, so we don't want a filter
@@ -100,7 +100,7 @@ def extract_aggregation_node(lookahead, field, graphql_query)
100100
)
101101
end
102102

103-
def build_aggregation_query_for(aggregations_node, field:, context:, index_definitions:, grouping_adapter:, nested_path: [], unfiltered: false)
103+
def build_aggregation_query_for(aggregations_node, field:, context:, grouping_missing_value_placeholders_by_index_leaf_path:, grouping_adapter:, nested_path: [], unfiltered: false)
104104
aggregation_name = name_of(_ = aggregations_node.ast_nodes.first)
105105

106106
# Get the AST node for the `nodes` subfield (e.g. from `fooAggregations { nodes { ... } }`)
@@ -134,9 +134,9 @@ def build_aggregation_query_for(aggregations_node, field:, context:, index_defin
134134

135135
Query.new(
136136
name: aggregation_name,
137-
groupings: build_groupings_from(node_node, aggregation_name, index_definitions: index_definitions, from_field_path: nested_path),
137+
groupings: build_groupings_from(node_node, aggregation_name, grouping_missing_value_placeholders_by_index_leaf_path: grouping_missing_value_placeholders_by_index_leaf_path, from_field_path: nested_path),
138138
computations: build_computations_from(node_node, context: context, from_field_path: nested_path),
139-
sub_aggregations: build_sub_aggregations_from(node_node, context: context, index_definitions: index_definitions, parent_nested_path: nested_path),
139+
sub_aggregations: build_sub_aggregations_from(node_node, context: context, grouping_missing_value_placeholders_by_index_leaf_path: grouping_missing_value_placeholders_by_index_leaf_path, parent_nested_path: nested_path),
140140
needs_doc_count: count_detail_node.selected? || node_node.selects?(element_names.count),
141141
needs_doc_count_error: needs_doc_count_error,
142142
paginator: build_paginator_for(aggregations_node),
@@ -215,7 +215,7 @@ def computation_for(fn_node, field_path, context:)
215215
)
216216
end
217217

218-
def build_groupings_from(node_node, aggregation_name, index_definitions:, from_field_path: [])
218+
def build_groupings_from(node_node, aggregation_name, grouping_missing_value_placeholders_by_index_leaf_path:, from_field_path: [])
219219
grouped_by_node = selection_above_grouping_fields(node_node, element_names.grouped_by, aggregation_name)
220220

221221
build_clauses_from(grouped_by_node) do |node, field, field_path|
@@ -230,19 +230,15 @@ def build_groupings_from(node_node, aggregation_name, index_definitions:, from_f
230230
# Non-date/time grouping
231231
missing_value_placeholder = field.type.unwrap_fully.grouping_missing_value_placeholder
232232
[FieldTermGrouping.new(field_path: field_path, missing_value_placeholder: missing_value_placeholder)]
233-
elsif indexed_as_leaf_field?(field_path, index_definitions)
233+
elsif grouping_missing_value_placeholders_by_index_leaf_path.key?(index_path = FieldPathEncoder.join(field_path.filter_map(&:name_in_index)))
234234
# A custom resolver can expose a single indexed value as an object. We group on that value
235235
# rather than on the selected subfields, which have no values of their own in the index.
236-
[FieldTermGrouping.new(field_path: field_path, missing_value_placeholder: nil)]
236+
missing_value_placeholder = grouping_missing_value_placeholders_by_index_leaf_path[index_path]
237+
[FieldTermGrouping.new(field_path: field_path, missing_value_placeholder: missing_value_placeholder)]
237238
end
238239
end
239240
end
240241

241-
def indexed_as_leaf_field?(field_path, index_definitions)
242-
path = FieldPathEncoder.join(field_path.filter_map(&:name_in_index))
243-
index_definitions.any? { |index_def| index_def.fields_by_path.key?(path) }
244-
end
245-
246242
# Builds a grouping for each selected `as_geo_tile` operation, validating its zoom level
247243
# without treating the selected tile properties as additional grouping dimensions.
248244
def geo_tile_groupings_from(field_path:, node:)
@@ -342,7 +338,7 @@ def name_of(ast_node)
342338
ast_node.alias || ast_node.name
343339
end
344340

345-
def build_sub_aggregations_from(node_node, context:, index_definitions:, parent_nested_path: [])
341+
def build_sub_aggregations_from(node_node, context:, grouping_missing_value_placeholders_by_index_leaf_path:, parent_nested_path: [])
346342
key_sub_agg_pairs =
347343
build_clauses_from(node_node.selection(element_names.sub_aggregations)) do |node, field, field_path|
348344
if field.type.elasticgraph_category == :nested_sub_aggregation_connection
@@ -353,7 +349,7 @@ def build_sub_aggregations_from(node_node, context:, index_definitions:, parent_
353349
node,
354350
field: field,
355351
context: context,
356-
index_definitions: index_definitions,
352+
grouping_missing_value_placeholders_by_index_leaf_path: grouping_missing_value_placeholders_by_index_leaf_path,
357353
grouping_adapter: sub_aggregation_grouping_adapter,
358354
nested_path: nested_path
359355
)

‎elasticgraph-graphql/lib/elastic_graph/graphql/schema/type.rb‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,14 @@ def relay_edge?
256256
unwrapped_has_category?(:relay_edge)
257257
end
258258

259+
# Maps the path of each leaf field in this type's search indices to the placeholder that groupings on
260+
# that field use for missing values.
261+
def grouping_missing_value_placeholders_by_index_leaf_path
262+
@grouping_missing_value_placeholders_by_index_leaf_path ||= search_index_definitions
263+
.flat_map { |index_def| index_def.fields_by_path.to_a }
264+
.to_h { |path, index_field| [path, resolve_missing_string_placeholder(index_field.grouping_missing_value_placeholder)] }
265+
end
266+
259267
# Indicates this type should be hidden in the GraphQL schema so as to not be queryable.
260268
# We only hide a type if both of the following are true:
261269
#
@@ -271,18 +279,20 @@ def hidden_from_queries?
271279
def determine_grouping_missing_value_placeholder(scalar_placeholder)
272280
if enum?
273281
MISSING_ENUM_PLACEHOLDER
274-
elsif scalar_placeholder == MISSING_STRING_PLACEHOLDER
275-
# We replace the MISSING_STRING_PLACEHOLDER ($SECURE_RANDOM) with the secure random value
276-
# generated and stored at MISSING_STRING_PLACEHOLDER_VALUE.
277-
MISSING_STRING_PLACEHOLDER_VALUE
278282
else
279283
# Note that if the type is not a scalar then scalar_placeholder will be nil.
280284
# We don't use missing value placeholders (or group aggregations) on fields
281285
# that are not scalars or enums.
282-
scalar_placeholder
286+
resolve_missing_string_placeholder(scalar_placeholder)
283287
end
284288
end
285289

290+
def resolve_missing_string_placeholder(placeholder)
291+
# We replace the MISSING_STRING_PLACEHOLDER ($SECURE_RANDOM) with the secure random value
292+
# generated and stored at MISSING_STRING_PLACEHOLDER_VALUE.
293+
(placeholder == MISSING_STRING_PLACEHOLDER) ? MISSING_STRING_PLACEHOLDER_VALUE : placeholder
294+
end
295+
286296
def lookup_enum_value_by_name(enum_value_name)
287297
graphql_enum_value = @graphql_type.values.fetch(enum_value_name)
288298

‎elasticgraph-graphql/sig/elastic_graph/graphql/aggregation/query_adapter.rbs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module ElasticGraph
5555
::GraphQL::Execution::Lookahead,
5656
field: Schema::Field,
5757
context: QueryContext,
58-
index_definitions: ::Array[DatastoreCore::_IndexDefinition],
58+
grouping_missing_value_placeholders_by_index_leaf_path: ::Hash[::String, (::String | ::Numeric)?],
5959
grouping_adapter: groupingAdapter,
6060
?nested_path: ::Array[PathSegment],
6161
?unfiltered: bool
@@ -103,12 +103,10 @@ module ElasticGraph
103103
def build_groupings_from: (
104104
::GraphQL::Execution::Lookahead,
105105
::String,
106-
index_definitions: ::Array[DatastoreCore::_IndexDefinition],
106+
grouping_missing_value_placeholders_by_index_leaf_path: ::Hash[::String, (::String | ::Numeric)?],
107107
?from_field_path: ::Array[PathSegment]
108108
) -> ::Set[grouping]
109109

110-
def indexed_as_leaf_field?: (::Array[PathSegment], ::Array[DatastoreCore::_IndexDefinition]) -> bool
111-
112110
def field_from_node: (
113111
::GraphQL::Execution::Lookahead
114112
) -> Schema::Field
@@ -148,7 +146,7 @@ module ElasticGraph
148146
def build_sub_aggregations_from: (
149147
::GraphQL::Execution::Lookahead,
150148
context: QueryContext,
151-
index_definitions: ::Array[DatastoreCore::_IndexDefinition],
149+
grouping_missing_value_placeholders_by_index_leaf_path: ::Hash[::String, (::String | ::Numeric)?],
152150
?parent_nested_path: ::Array[PathSegment]
153151
) -> ::Hash[::String, NestedSubAggregation]
154152

‎elasticgraph-graphql/sig/elastic_graph/graphql/schema/type.rbs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module ElasticGraph
99
attr_reader graphql_type: ::GraphQL::Schema::_Type
1010
attr_reader grouping_missing_value_placeholder: ::String? | ::Numeric?
1111
def search_index_definitions: () -> ::Array[DatastoreCore::_IndexDefinition]
12+
def grouping_missing_value_placeholders_by_index_leaf_path: () -> ::Hash[::String, (::String | ::Numeric)?]
1213
def source_type: () -> Type?
1314
def subtypes: () -> ::Set[Type]
1415
def shares_index_with_non_subtypes?: () -> bool

‎elasticgraph-graphql/spec/unit/elastic_graph/graphql/aggregation/query_adapter_spec.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ def build_sub_aggregation_query(args, default_page_size:, max_page_size:)
775775
name: "widget_aggregations",
776776
computations: [],
777777
groupings: [
778-
field_term_grouping_of("amount_cents", field_names_in_graphql_query: ["price"], missing_value_placeholder: nil)
778+
field_term_grouping_of("amount_cents", field_names_in_graphql_query: ["price"], missing_value_placeholder: MISSING_NUMERIC_PLACEHOLDER)
779779
]
780780
)])
781781
end
@@ -797,7 +797,7 @@ def build_sub_aggregation_query(args, default_page_size:, max_page_size:)
797797
name: "widget_aggregations",
798798
computations: [],
799799
groupings: [
800-
field_term_grouping_of("part", "part_amount_cents", field_names_in_graphql_query: ["part", "price"], missing_value_placeholder: nil)
800+
field_term_grouping_of("part", "part_amount_cents", field_names_in_graphql_query: ["part", "price"], missing_value_placeholder: MISSING_NUMERIC_PLACEHOLDER)
801801
]
802802
)])
803803
end
@@ -823,7 +823,7 @@ def build_sub_aggregation_query(args, default_page_size:, max_page_size:)
823823
nested_sub_aggregation_of(path_in_index: ["parts_nested"], query: sub_aggregation_query_of(
824824
name: "parts_nested",
825825
groupings: [
826-
field_term_grouping_of("parts_nested", "part_amount_cents", field_names_in_graphql_query: ["parts_nested", "price"], missing_value_placeholder: nil)
826+
field_term_grouping_of("parts_nested", "part_amount_cents", field_names_in_graphql_query: ["parts_nested", "price"], missing_value_placeholder: MISSING_NUMERIC_PLACEHOLDER)
827827
]
828828
))
829829
])])

‎elasticgraph-graphql/spec/unit/elastic_graph/graphql/schema/field_spec.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class Schema
244244
def field_of_type(type_name)
245245
define_schema do |schema|
246246
schema.object_type "Photo" do |t|
247-
t.field "some_field", type_name, filterable: false, aggregatable: false, groupable: false
247+
t.field "some_field", type_name, filterable: false, aggregatable: false, groupable: false, graphql_only: true
248248
end
249249
end.field_named("Photo", "some_field")
250250
end

‎elasticgraph-graphql/spec/unit/elastic_graph/graphql/schema/type_spec.rb‎

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ class Schema
123123
t.field "list_of_non_null_int", "[Int!]"
124124
t.field "non_null_list_of_int", "[Int]!"
125125
t.field "non_null_list_of_non_null_int", "[Int!]!"
126-
t.field "relay_connection", "PersonConnection", filterable: false, groupable: false
127-
t.field "non_null_relay_connection", "PersonConnection!", filterable: false, groupable: false
128-
t.field "relay_edge", "PersonEdge", filterable: false, groupable: false
129-
t.field "non_null_relay_edge", "PersonEdge!", filterable: false, groupable: false
126+
t.field "relay_connection", "PersonConnection", filterable: false, groupable: false, graphql_only: true
127+
t.field "non_null_relay_connection", "PersonConnection!", filterable: false, groupable: false, graphql_only: true
128+
t.field "relay_edge", "PersonEdge", filterable: false, groupable: false, graphql_only: true
129+
t.field "non_null_relay_edge", "PersonEdge!", filterable: false, groupable: false, graphql_only: true
130130
t.field "color", "Color"
131131
t.field "person", "Person"
132132
t.field "size", "Size"
@@ -144,12 +144,12 @@ class Schema
144144
t.field "non_null_attribute", "Attribute!"
145145
t.field "entity", "Entity"
146146
t.field "non_null_entity", "Entity!"
147-
t.field "indexed_aggregation", "PersonAggregation", filterable: false, groupable: false
148-
t.field "non_null_indexed_aggregation", "PersonAggregation!", filterable: false, groupable: false
149-
t.field "list_of_indexed_aggregation", "[PersonAggregation]", filterable: false, groupable: false do |f|
147+
t.field "indexed_aggregation", "PersonAggregation", filterable: false, groupable: false, graphql_only: true
148+
t.field "non_null_indexed_aggregation", "PersonAggregation!", filterable: false, groupable: false, graphql_only: true
149+
t.field "list_of_indexed_aggregation", "[PersonAggregation]", filterable: false, groupable: false, graphql_only: true do |f|
150150
f.mapping type: "object"
151151
end
152-
t.field "non_null_list_of_indexed_aggregation", "[PersonAggregation]!", filterable: false, groupable: false do |f|
152+
t.field "non_null_list_of_indexed_aggregation", "[PersonAggregation]!", filterable: false, groupable: false, graphql_only: true do |f|
153153
f.mapping type: "object"
154154
end
155155

@@ -1027,6 +1027,59 @@ def search_index_definitions_from(type_name: "TheType")
10271027
end
10281028
end
10291029

1030+
describe "#grouping_missing_value_placeholders_by_index_leaf_path" do
1031+
it "maps the path of each leaf field in the type's index to the placeholder for that field's type" do
1032+
schema = define_schema do |s|
1033+
s.object_type "ThingOptions" do |t|
1034+
t.field "size", "Int"
1035+
end
1036+
1037+
s.object_type "Thing" do |t|
1038+
t.field "id", "ID!"
1039+
t.field "is_draft", "Boolean"
1040+
t.field "options", "ThingOptions"
1041+
t.index "things"
1042+
end
1043+
end
1044+
1045+
placeholders = schema.type_named("Thing").grouping_missing_value_placeholders_by_index_leaf_path
1046+
1047+
expect(placeholders).to eq({
1048+
"id" => MISSING_STRING_PLACEHOLDER_VALUE,
1049+
"is_draft" => nil,
1050+
"options.size" => MISSING_NUMERIC_PLACEHOLDER
1051+
})
1052+
end
1053+
1054+
it "includes the leaf fields of every index of an abstract type" do
1055+
schema = define_schema do |s|
1056+
s.object_type "T1" do |t|
1057+
t.field "id", "ID!"
1058+
t.field "size", "Int"
1059+
t.index "t1"
1060+
end
1061+
1062+
s.object_type "T2" do |t|
1063+
t.field "id", "ID!"
1064+
t.field "is_draft", "Boolean"
1065+
t.index "t2"
1066+
end
1067+
1068+
s.union_type "Thing" do |t|
1069+
t.subtypes "T1", "T2"
1070+
end
1071+
end
1072+
1073+
placeholders = schema.type_named("Thing").grouping_missing_value_placeholders_by_index_leaf_path
1074+
1075+
expect(placeholders).to eq({
1076+
"id" => MISSING_STRING_PLACEHOLDER_VALUE,
1077+
"size" => MISSING_NUMERIC_PLACEHOLDER,
1078+
"is_draft" => nil
1079+
})
1080+
end
1081+
end
1082+
10301083
def define_schema(index_definitions: nil, **overrides, &schema_def)
10311084
build_graphql(schema_definition: schema_def, index_definitions: index_definitions, **overrides).schema
10321085
end

0 commit comments

Comments
 (0)