Querying a field inside a nested document, make the query fail

Hello,
I'm trying to query in a MongoDB with Metabase 1.45.2. If I select, like in the image, at least one field inside the nested document.

It fails like this:

[111a1248-72c2-4544-9669-cdf8147fd777] 2023-01-18T14:19:42+01:00 ERROR metabase.query-processor.middleware.catch-exceptions Errore elaborazione query: Errore nell'esecuzione della query
{:database_id 6,
 :started_at #t "2023-01-18T13:19:41.774075Z[UTC]",
 :via
 [{:status :failed,
   :class clojure.lang.ExceptionInfo,
   :error "Error reducing result rows: null",
   :stacktrace
   ["--> query_processor.context.default$default_reducef$fn__39686.invoke(default.clj:59)"
    "query_processor.context.default$default_reducef.invokeStatic(default.clj:56)"
    "query_processor.context.default$default_reducef.invoke(default.clj:48)"
    "query_processor.context$reducef.invokeStatic(context.clj:69)"
    "query_processor.context$reducef.invoke(context.clj:62)"
    "query_processor.context.default$default_runf$respond_STAR___39690.invoke(default.clj:68)"
    "driver.mongo.execute$reduce_results.invokeStatic(execute.clj:138)"
    "driver.mongo.execute$reduce_results.invoke(execute.clj:131)"
    "driver.mongo.execute$execute_reducible_query.invokeStatic(execute.clj:169)"
    "driver.mongo.execute$execute_reducible_query.invoke(execute.clj:157)"
    "driver.mongo$fn__98833$f__97136__auto____98834.invoke(mongo.clj:246)"
    "driver.mongo.util$do_with_mongo_connection$fn__97127.invoke(util.clj:226)"
    "util.ssh$do_with_ssh_tunnel.invokeStatic(ssh.clj:139)"
    "util.ssh$do_with_ssh_tunnel.invoke(ssh.clj:128)"
    "driver.mongo.util$do_with_mongo_connection.invokeStatic(util.clj:220)"
    "driver.mongo.util$do_with_mongo_connection.invoke(util.clj:215)"
    "driver.mongo$fn__98833.invokeStatic(mongo.clj:245)"
    "driver.mongo$fn__98833.invoke(mongo.clj:243)"
    "query_processor.context$executef.invokeStatic(context.clj:59)"
    "query_processor.context$executef.invoke(context.clj:48)"
    "query_processor.context.default$default_runf.invokeStatic(default.clj:67)"
    "query_processor.context.default$default_runf.invoke(default.clj:65)"
    "query_processor.context$runf.invokeStatic(context.clj:45)"
    "query_processor.context$runf.invoke(context.clj:39)"
    "query_processor.reducible$identity_qp.invokeStatic(reducible.clj:12)"
    "query_processor.reducible$identity_qp.invoke(reducible.clj:9)"
    "query_processor.middleware.cache$maybe_return_cached_results$maybe_return_cached_results_STAR___53569.invoke(cache.clj:220)"
    "query_processor.middleware.permissions$check_query_permissions$fn__49259.invoke(permissions.clj:109)"
    "metabase_enterprise.advanced_permissions.query_processor.middleware.permissions$check_download_permissions$fn__89280.invoke(permissions.clj:124)"
    "metabase_enterprise.sandbox.query_processor.middleware.column_level_perms_check$maybe_apply_column_level_perms_check$fn__90357.invoke(column_level_perms_check.clj:30)"
    "query_processor.middleware.mbql_to_native$mbql__GT_native$fn__52514.invoke(mbql_to_native.clj:23)"
    "query_processor$fn__55045$combined_post_process__55050$combined_post_process_STAR___55051.invoke(query_processor.clj:212)"
    "query_processor$fn__55045$combined_pre_process__55046$combined_pre_process_STAR___55047.invoke(query_processor.clj:209)"
    "query_processor.middleware.resolve_database_and_driver$resolve_database_and_driver$fn__53490$fn__53495.invoke(resolve_database_and_driver.clj:35)"
    "driver$do_with_driver.invokeStatic(driver.clj:76)"
    "driver$do_with_driver.invoke(driver.clj:72)"
    "query_processor.middleware.resolve_database_and_driver$resolve_database_and_driver$fn__53490.invoke(resolve_database_and_driver.clj:34)"
    "query_processor.middleware.fetch_source_query$resolve_card_id_source_tables$fn__49525.invoke(fetch_source_query.clj:314)"
    "query_processor.middleware.store$initialize_store$fn__49715$fn__49716.invoke(store.clj:11)"
    "query_processor.store$do_with_store.invokeStatic(store.clj:45)"
    "query_processor.store$do_with_store.invoke(store.clj:39)"
    "query_processor.middleware.store$initialize_store$fn__49715.invoke(store.clj:10)"
    "query_processor.middleware.normalize_query$normalize$fn__53762.invoke(normalize_query.clj:22)"
    "metabase_enterprise.audit_app.query_processor.middleware.handle_audit_queries$handle_internal_queries$fn__60502.invoke(handle_audit_queries.clj:130)"
    "query_processor.middleware.constraints$add_default_userland_constraints$fn__50803.invoke(constraints.clj:53)"
    "query_processor.middleware.process_userland_query$process_userland_query$fn__53701.invoke(process_userland_query.clj:145)"
    "query_processor.middleware.catch_exceptions$catch_exceptions$fn__54073.invoke(catch_exceptions.clj:167)"
    "query_processor.reducible$async_qp$qp_STAR___45514$thunk__45516.invoke(reducible.clj:100)"
    "query_processor.reducible$async_qp$qp_STAR___45514.invoke(reducible.clj:106)"
    "query_processor.reducible$async_qp$qp_STAR___45514.invoke(reducible.clj:91)"
    "query_processor.reducible$sync_qp$qp_STAR___45525.doInvoke(reducible.clj:126)"
    "query_processor$process_userland_query.invokeStatic(query_processor.clj:331)"
    "query_processor$process_userland_query.doInvoke(query_processor.clj:327)"
    "query_processor$fn__55092$process_query_and_save_execution_BANG___55101$fn__55104.invoke(query_processor.clj:342)"
    "query_processor$fn__55092$process_query_and_save_execution_BANG___55101.invoke(query_processor.clj:335)"
    "query_processor$fn__55136$process_query_and_save_with_max_results_constraints_BANG___55145$fn__55148.invoke(query_processor.clj:354)"
    "query_processor$fn__55136$process_query_and_save_with_max_results_constraints_BANG___55145.invoke(query_processor.clj:347)"
    "api.dataset$run_query_async$fn__71225.invoke(dataset.clj:69)"
    "query_processor.streaming$streaming_response_STAR_$fn__40610$fn__40611.invoke(streaming.clj:162)"
    "query_processor.streaming$streaming_response_STAR_$fn__40610.invoke(streaming.clj:161)"
    "async.streaming_response$do_f_STAR_.invokeStatic(streaming_response.clj:65)"
    "async.streaming_response$do_f_STAR_.invoke(streaming_response.clj:63)"
    "async.streaming_response$do_f_async$task__20021.invoke(streaming_response.clj:84)"],
   :error_type :qp,
   :ex-data {:type :qp}}],
 :error_type :qp,
 :json_query
 {:type "query",
  :query {:source-table 30, :fields [["field" 454 nil]]},
  :database 6,
  :parameters [],
  :middleware {:js-int-to-string? true, :add-default-userland-constraints? true}},
 :native
 {:projections ("event_datum.params.message"),
  :query [{"$project" {"_id" false, "event_datum.params.message" "$event_datum.params.message"}} {"$limit" 1048575}],
  :collection "events",
  :mbql? true},
 :status :failed,
 :class java.lang.ClassCastException,
 :stacktrace [nil],
 :card_id nil,
 :context :ad-hoc,
 :error nil,
 :row_count 0,
 :running_time 0,
 :preprocessed
 {:type :query,
  :query
  {:source-table 30,
   :fields [[:field 454 nil]],
   :limit 1048575,
   :metabase.query-processor.middleware.limit/original-limit nil},
  :database 6,
  :middleware {:js-int-to-string? true, :add-default-userland-constraints? true},
  :info {:executed-by 2, :context :ad-hoc}},
 :data {:rows [], :cols []}}

In other topics I found the issue "Error reducing result rows: null" was solved by upgrading, so I did it from 1.44.6 to 1.45.2, but it' still present.
How to solve?

I just tried to reproduce the issue that you are seeing and coudn't

can you try building a dataset that hits this error and let us know?

Thank you Luiggi for the answer.
Thanks to that I got a better insight of the error.
Building the dataset I noticed that normally, like in your example, it would work. The error I get is probably caused by a field in the nested document, that sometimes is populated by an array.
(Should I open a thread with a different title?)

Here's what i built to reproduce, in the database there are the following documents:

{
    _id: ObjectId("63dd0f1d9d478b45649c9604"),
    layer1: { layer2: { layer3: 'value' } }
  },
  {
    _id: ObjectId("63dd0f489d478b45649c9605"),
    layer1: { layer2: 'hello' }
  },
  {
    _id: ObjectId("63e3ab2d3ffd5be1fdb8d756"),
    layer1: { layer2: { layer3: 'value2' } }
  },
  {
    _id: ObjectId("63e3aec63ffd5be1fdb8d758"),
    layer1: { layer2: [ { layer4: 'valueArray' } ] }
  }

image
Now if I try to visualize only the "layer3" I get the same failure.
image
:arrow_down:
image

I can't modify the data, how to solve this situation?