flutterfire
flutterfire copied to clipboard
🐛 [cloud_firestore] Exception with more than 5 aggregate fields
Bug report
When i want to aggregate data on a collection with .aggregate(), it results in following exception when i pass more than five AggregateFields.
Exception
PlatformException (PlatformException(invalid-argument, Client specified an invalid argument. Note that this differs from failed-precondition. invalid-argument indicates arguments that are problematic regardless of the state of the system (e.g., an invalid field name)., {message: Client specified an invalid argument. Note that this differs from failed-precondition. invalid-argument indicates arguments that are problematic regardless of the state of the system (e.g., an invalid field name)., code: invalid-argument}, null))
Steps to reproduce
Steps to reproduce the behavior:
- Run
.aggregate()with sixAggregateFields over different fields - See exception
Expected behavior
Since .aggregate() takes up to 30 AggregateFields, it should not fail when you pass more than five.
Flutter doctor
Run flutter doctor and paste the output below:
Click To Expand
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.7.12, on macOS 14.2.1 23C71 darwin-arm64, locale de-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
[✗] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome)
! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[!] Android Studio (version 2023.1)
✗ Unable to find bundled Java version.
[✓] VS Code (version 1.85.1)
[✓] Connected device (2 available)
[✓] HTTP Host Availability
! Doctor found issues in 2 categories.
Flutter dependencies
Run flutter pub deps -- --style=compact and paste the output below:
Click To Expand
Dart SDK 2.19.6
Flutter SDK 3.7.12
****_dataaccess 0.0.1
dependencies:
- cloud_firestore 4.14.0 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]
- ****_domain 0.0.1 [equatable meta intl enum_to_string rxdart uuid collection clock]
- flutter 0.0.0 [characters collection js material_color_utilities meta vector_math sky_engine]
- get_it 7.2.0 [async collection]
- meta 1.8.0
- rxdart 0.27.7
- synchronized 3.0.1
dev dependencies:
- build_runner 2.3.3 [args async analyzer build build_config build_daemon build_resolvers build_runner_core code_builder collection crypto dart_style frontend_server_client glob graphs http_multi_server io js logging meta mime package_config path pool pub_semver pubspec_parse shelf shelf_web_socket stack_trace stream_transform timing watcher web_socket_channel yaml]
- clock 1.1.1
- dependency_validator 3.2.2 [args build_config checked_yaml glob io json_annotation logging package_config path pub_semver pubspec_parse yaml]
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters collection js matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph]
- mockito 5.3.2 [analyzer build code_builder collection dart_style matcher meta path source_gen test_api]
dependency overrides:
- ****_domain 0.0.1 [equatable meta intl enum_to_string rxdart uuid collection clock]
transitive dependencies:
- _fe_analyzer_shared 49.0.0 [meta]
- _flutterfire_internals 1.3.16 [collection firebase_core firebase_core_platform_interface flutter meta]
- analyzer 5.1.0 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml]
- args 2.4.0
- async 2.10.0 [collection meta]
- boolean_selector 2.1.1 [source_span string_scanner]
- build 2.3.1 [analyzer async convert crypto glob logging meta path]
- build_config 1.1.1 [checked_yaml json_annotation path pubspec_parse yaml]
- build_daemon 3.1.1 [built_collection built_value http_multi_server logging path pool shelf shelf_web_socket stream_transform watcher web_socket_channel]
- build_resolvers 2.0.10 [analyzer async build crypto graphs logging path package_config pool pub_semver stream_transform yaml]
- build_runner_core 7.2.7 [async build build_config build_resolvers collection convert crypto glob graphs json_annotation logging meta path package_config pool timing watcher yaml]
- built_collection 5.1.1
- built_value 8.4.3 [built_collection collection fixnum meta]
- characters 1.2.1
- checked_yaml 2.0.2 [json_annotation source_span yaml]
- cloud_firestore_platform_interface 6.1.0 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- cloud_firestore_web 3.9.0 [_flutterfire_internals cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js]
- code_builder 4.4.0 [built_collection built_value collection matcher meta]
- collection 1.17.0
- convert 3.1.1 [typed_data]
- crypto 3.0.2 [typed_data]
- dart_style 2.2.4 [analyzer args path pub_semver source_span]
- enum_to_string 2.0.1
- equatable 2.0.5 [collection meta]
- fake_async 1.3.1 [clock collection]
- file 6.1.4 [meta path]
- firebase_core 2.24.2 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.10.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- fixnum 1.0.1
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- frontend_server_client 3.2.0 [async path]
- glob 2.1.1 [async collection file path string_scanner]
- graphs 2.3.1 [collection]
- http_multi_server 3.2.1 [async]
- http_parser 4.0.2 [collection source_span string_scanner typed_data]
- intl 0.17.0 [clock path]
- io 1.0.4 [meta path string_scanner]
- js 0.6.5 [meta]
- json_annotation 4.8.0 [meta]
- logging 1.1.1
- matcher 0.12.13 [meta stack_trace]
- material_color_utilities 0.2.0
- mime 1.0.4
- package_config 2.1.0 [path]
- path 1.8.2
- plugin_platform_interface 2.1.3 [meta]
- pool 1.5.1 [async stack_trace]
- pub_semver 2.1.3 [collection meta]
- pubspec_parse 1.2.1 [checked_yaml collection json_annotation pub_semver yaml]
- shelf 1.4.1 [async collection http_parser path stack_trace stream_channel]
- shelf_web_socket 1.0.4 [shelf stream_channel web_socket_channel]
- sky_engine 0.0.99
- source_gen 1.2.6 [analyzer async build dart_style glob meta path source_span yaml]
- source_span 1.9.1 [collection path term_glyph]
- stack_trace 1.11.0 [path]
- stream_channel 2.1.1 [async]
- stream_transform 2.1.0
- string_scanner 1.2.0 [source_span]
- term_glyph 1.2.1
- test_api 0.4.16 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher]
- timing 1.0.1 [json_annotation]
- typed_data 1.3.1 [collection]
- uuid 3.0.7 [crypto]
- vector_math 2.1.4
- watcher 1.0.2 [async path]
- web_socket_channel 2.4.0 [async crypto stream_channel]
- yaml 3.1.1 [collection source_span string_scanner]
Hi @korbipep, what platform(s) are you experiencing this issue on?
Can you also share the code snippet you use for aggregation?
Hi @korbipep, what platform(s) are you experiencing this issue on?
Can you also share the code snippet you use for aggregation?
What do you mean with platform? Flutter and Firebase Firestore
final aggregateSnapshot = collection.aggregate(
sum('someField'),
sum('someOtherField'),
sum('someOtherField'),
average('someOtherField'),
average('someOtherField'),
average('someOtherField'),
).get();
@korbipep - platform - ios, android, web, windows, macos. which one is it?
@korbipep - platform - ios, android, web, windows, macos. which one is it?
Oh yeah. ios. Didn't test it on another platform yet
Hi @korbipep, are you sure the field you're using the aggregate on exists? I only get that error if I use it on a field that does not exist. Please check if you have the correct field names in your aggregate functions.
Hi @korbipep, are you sure the field you're using the aggregate on exists? I only get that error if I use it on a field that does not exist. Please check if you have the correct field names in your aggregate functions.
Yes, because if i separate it into two queries with the exact same aggregate fields it succeeds. I also tried if it maybe is a combination of fields that causes the error, but i came to the conclusion that its the number of fields. Every combination works until there are more than five
Hi @korbipep, can you provide the structure of your data so that we can try to simulate it on our end? Or better still, can you provide a complete minimal reproducible code sample in a repo that we can clone so that we can use that to investigate this?
Thank you
I created a repo with a minimal reproducible code sample https://github.com/korbipep/aggregate-test Thank you and let me know if you need more
I can reproduce using the code sample in the repro provided above https://github.com/korbipep/aggregate-test.
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(invalid-argument, Client specified an invalid argument. Note that this differs from failed-precondition. invalid-argument indicates arguments that are problematic regardless of the state of the system (e.g., an invalid field name)., {message: Client specified an invalid argument. Note that this differs from failed-precondition. invalid-argument indicates arguments that are problematic regardless of the state of the system (e.g., an invalid field name)., code: invalid-argument}, null)
#0 FirebaseFirestoreHostApi.aggregateQuery (package:cloud_firestore_platform_interface/src/pigeon/messages.pigeon.dart:1172:7)
<asynchronous suspension>
#1 MethodChannelAggregateQuery.get (package:cloud_firestore_platform_interface/src/method_channel/method_channel_aggregate_query.dart:31:9)
<asynchronous suspension>
#2 AggregateQuery.get (package:cloud_firestore/src/aggregate_query.dart:24:37)
<asynchronous suspension>
#3 _MyHomePageState._incrementCounter (package:aggregate_test/main.dart:78:23)
<asynchronous suspension>
Running on android, we get a clearer error message
E/flutter ( 5130): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(firebase_firestore, com.google.firebase.firestore.FirebaseFirestoreException: INVALID_ARGUMENT: The maximum number of aggregations allowed in an aggregation query is 5. Received: 6, {code: invalid-argument, message: Client specified an invalid argument. Note that this differs from failed-precondition. invalid-argument indicates arguments that are problematic regardless of the state of the system (e.g., an invalid field name).}, null)
E/flutter ( 5130): #0 FirebaseFirestoreHostApi.aggregateQuery (package:cloud_firestore_platform_interface/src/pigeon/messages.pigeon.dart:1172:7)
E/flutter ( 5130): <asynchronous suspension>
E/flutter ( 5130): #1 MethodChannelAggregateQuery.get (package:cloud_firestore_platform_interface/src/method_channel/method_channel_aggregate_query.dart:31:9)
E/flutter ( 5130): <asynchronous suspension>
E/flutter ( 5130): #2 AggregateQuery.get (package:cloud_firestore/src/aggregate_query.dart:24:37)
E/flutter ( 5130): <asynchronous suspension>
E/flutter ( 5130): #3 _MyHomePageState._incrementCounter (package:aggregate_test/main.dart:78:23)
E/flutter ( 5130): <asynchronous suspension>
E/flutter ( 5130):
cc @russellwheatley, it seems like this might be a limitation of the underlying sdk
I ran in iOS and got the following:
PlatformException(failed-precondition, The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/flutterfire-e2e-tests/firestore/indexes?create_composite=Cltwcm9qZWN0cy9mbHV0dGVyZmlyZS1lMmUtdGVzdHMvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL2ZsdXR0ZXItdGVzdHMvaW5kZXhlcy9fEAEaDQoJc29tZUZpZWxkEAEaEgoOc29tZU90aGVyRmllbGQQARoMCghfX25hbWVfXxAB, {message: The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/flutterfire-e2e-tests/firestore/indexes?create_composite=Cltwcm9qZWN0cy9mbHV0dGVyZmlyZS1lMmUtdGVzdHMvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL2ZsdXR0ZXItdGVzdHMvaW5kZXhlcy9fEAEaDQoJc29tZUZpZWxkEAEaEgoOc29tZU90aGVyRmllbGQQARoMCghfX25hbWVfXxAB, code: failed-precondition}, null)
also the same on android:
PlatformException(firebase_firestore, com.google.firebase.firestore.FirebaseFirestoreException: FAILED_PRECONDITION: The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/flutterfire-e2e-tests/firestore/indexes?create_composite=Cltwcm9qZWN0cy9mbHV0dGVyZmlyZS1lMmUtdGVzdHMvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL2ZsdXR0ZXItdGVzdHMvaW5kZXhlcy9fEAEaDQoJc29tZUZpZWxkEAEaEgoOc29tZU90aGVyRmllbGQQARoMCghfX25hbWVfXxAB, {code: failed-precondition, message: The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/flutterfire-e2e-tests/firestore/indexes?create_composite=Cltwcm9qZWN0cy9mbHV0dGVyZmlyZS1lMmUtdGVzdHMvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL2ZsdXR0ZXItdGVzdHMvaW5kZXhlcy9fEAEaDQoJc29tZUZpZWxkEAEaEgoOc29tZU90aGVyRmllbGQQARoMCghfX25hbWVfXxAB}, null)
Ultimately, if you create an aggregate query with more than 5 fields, you have to create an index on the Firebase console, the exception provides a URL for you to easily create an index.
I guess the underlying SDKs updated the exception message with the URL as the exception experienced by the OP is no longer occurring. Closing out as this has been fixed upstream.
Hi. I'm facing the same issue on Flutter Web. I'm getting the following error log:
FirebaseError: [code=invalid-argument]: The maximum number of aggregations allowed in an aggregation query is 5. Received: 9
As you can see, there is no URL with the exception to help me create an index. What do I do here?
- Where and how do I go about making an index manually, if possible?
- How many fields do I need to include for the index? Should I have all 9 fields included in the index?
So, I just reduced the amount of fields to 5. Now it's showing me a URL with the following exception:
FirebaseError: [code=failed-precondition]: The query requires an index. You can create it here: <long-url-here>
Ultimately, if you create an aggregate query with more than 5 fields, you have to create an index on the Firebase console, the exception provides a URL for you to easily create an index.
I'm having to create an index for 5 fields. And any more than 5 fields is outright denied. Is this expected behavior?