Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questions referencing another question with a GROUP BY on a custom column generate invalid SQL #23862

Closed
GriffinSchneider opened this issue Jul 12, 2022 · 5 comments · Fixed by #24404
Assignees
Labels
Priority:P2 Average run of the mill bug Querying/Nested Queries Questions based on other saved questions Querying/Notebook/Custom Column .Reproduced Issues reproduced in test (usually Cypress) Type:Bug Product defects
Milestone

Comments

@GriffinSchneider
Copy link

Describe the bug
If I create a query builder question with a custom column like case(something, "A", "B") and GROUP BY on that custom column, and then make another query builder question referencing the first one, the second question generates invalid SQL.

Logs

JavaScript console logs
error Column "PUBLIC.ORDERS.TOTAL" must be in the GROUP BY list; SQL statement:
-- Metabase:: userID: 1 queryType: MBQL queryHash: ed3848855aea99cb66388521bd50dc94c8ca7ab6adbf408be64d89d6d7bcd0be
SELECT "source"."size" AS "size", "source"."sum" AS "sum" FROM (SELECT CASE WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END AS "size", sum("PUBLIC"."ORDERS"."TOTAL") AS "sum" FROM "PUBLIC"."ORDERS" GROUP BY CASE WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END ORDER BY CASE WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END ASC) "source" LIMIT 2000 [90016-197]
Server logs
2022-07-12 11:40:13,342 INFO api.dataset :: Source query for this query is Card 1,196
2022-07-12 11:40:13,891 ERROR middleware.catch-exceptions :: Error processing query: Column "PUBLIC.ORDERS.TOTAL" must be in the GROUP BY list; SQL statement:
-- Metabase:: userID: 1 queryType: MBQL queryHash: ed3848855aea99cb66388521bd50dc94c8ca7ab6adbf408be64d89d6d7bcd0be
SELECT "source"."size" AS "size", "source"."sum" AS "sum" FROM (SELECT CASE WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END AS "size", sum("PUBLIC"."ORDERS"."TOTAL") AS "sum" FROM "PUBLIC"."ORDERS" GROUP BY CASE WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END ORDER BY CASE WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END ASC) "source" LIMIT 2000 [90016-197]
{:database_id 4,
 :started_at #t "2022-07-12T11:40:13.379273-04:00[US/Eastern]",
 :via
 [{:status :failed,
   :class clojure.lang.ExceptionInfo,
   :error
   "Error executing query: Column \"PUBLIC.ORDERS.TOTAL\" must be in the GROUP BY list; SQL statement:\n-- Metabase:: userID: 1 queryType: MBQL queryHash: ed3848855aea99cb66388521bd50dc94c8ca7ab6adbf408be64d89d6d7bcd0be\nSELECT \"source\".\"size\" AS \"size\", \"source\".\"sum\" AS \"sum\" FROM (SELECT CASE WHEN \"PUBLIC\".\"ORDERS\".\"TOTAL\" > 10 THEN ? ELSE ? END AS \"size\", sum(\"PUBLIC\".\"ORDERS\".\"TOTAL\") AS \"sum\" FROM \"PUBLIC\".\"ORDERS\" GROUP BY CASE WHEN \"PUBLIC\".\"ORDERS\".\"TOTAL\" > 10 THEN ? ELSE ? END ORDER BY CASE WHEN \"PUBLIC\".\"ORDERS\".\"TOTAL\" > 10 THEN ? ELSE ? END ASC) \"source\" LIMIT 2000 [90016-197]",
   :stacktrace
   ["--> driver.sql_jdbc.execute$execute_reducible_query$fn__54060.invoke(execute.clj:502)"
    "driver.sql_jdbc.execute$execute_reducible_query.invokeStatic(execute.clj:499)"
    "driver.sql_jdbc.execute$execute_reducible_query.invoke(execute.clj:486)"
    "driver.sql_jdbc.execute$execute_reducible_query.invokeStatic(execute.clj:494)"
    "driver.sql_jdbc.execute$execute_reducible_query.invoke(execute.clj:486)"
    "driver.sql_jdbc$fn__83581.invokeStatic(sql_jdbc.clj:54)"
    "driver.sql_jdbc$fn__83581.invoke(sql_jdbc.clj:52)"
    "driver.h2$fn__80664.invokeStatic(h2.clj:90)"
    "driver.h2$fn__80664.invoke(h2.clj:87)"
    "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___51447.invoke(cache.clj:220)"
    "query_processor.middleware.permissions$check_query_permissions$fn__47230.invoke(permissions.clj:109)"
    "query_processor.middleware.mbql_to_native$mbql__GT_native$fn__50388.invoke(mbql_to_native.clj:23)"
    "query_processor$fn__52921$combined_post_process__52926$combined_post_process_STAR___52927.invoke(query_processor.clj:207)"
    "query_processor$fn__52921$combined_pre_process__52922$combined_pre_process_STAR___52923.invoke(query_processor.clj:204)"
    "query_processor.middleware.resolve_database_and_driver$resolve_database_and_driver$fn__51368$fn__51373.invoke(resolve_database_and_driver.clj:35)"
    "driver$do_with_driver.invokeStatic(driver.clj:75)"
    "driver$do_with_driver.invoke(driver.clj:71)"
    "query_processor.middleware.resolve_database_and_driver$resolve_database_and_driver$fn__51368.invoke(resolve_database_and_driver.clj:34)"
    "query_processor.middleware.fetch_source_query$resolve_card_id_source_tables$fn__47473.invoke(fetch_source_query.clj:277)"
    "query_processor.middleware.store$initialize_store$fn__47661$fn__47662.invoke(store.clj:11)"
    "query_processor.store$do_with_store.invokeStatic(store.clj:44)"
    "query_processor.store$do_with_store.invoke(store.clj:38)"
    "query_processor.middleware.store$initialize_store$fn__47661.invoke(store.clj:10)"
    "query_processor.middleware.normalize_query$normalize$fn__51640.invoke(normalize_query.clj:22)"
    "query_processor.middleware.constraints$add_default_userland_constraints$fn__48730.invoke(constraints.clj:53)"
    "query_processor.middleware.process_userland_query$process_userland_query$fn__51579.invoke(process_userland_query.clj:145)"
    "query_processor.middleware.catch_exceptions$catch_exceptions$fn__51951.invoke(catch_exceptions.clj:168)"
    "query_processor.reducible$async_qp$qp_STAR___44200$thunk__44202.invoke(reducible.clj:100)"
    "query_processor.reducible$async_qp$qp_STAR___44200.invoke(reducible.clj:106)"
    "query_processor.reducible$async_qp$qp_STAR___44200.invoke(reducible.clj:91)"
    "query_processor.reducible$sync_qp$qp_STAR___44211.doInvoke(reducible.clj:126)"
    "query_processor$process_userland_query.invokeStatic(query_processor.clj:326)"
    "query_processor$process_userland_query.doInvoke(query_processor.clj:322)"
    "query_processor$fn__52968$process_query_and_save_execution_BANG___52977$fn__52980.invoke(query_processor.clj:337)"
    "query_processor$fn__52968$process_query_and_save_execution_BANG___52977.invoke(query_processor.clj:330)"
    "query_processor$fn__53012$process_query_and_save_with_max_results_constraints_BANG___53021$fn__53024.invoke(query_processor.clj:349)"
    "query_processor$fn__53012$process_query_and_save_with_max_results_constraints_BANG___53021.invoke(query_processor.clj:342)"
    "api.dataset$run_query_async$fn__66704.invoke(dataset.clj:68)"
    "query_processor.streaming$streaming_response_STAR_$fn__39322$fn__39323.invoke(streaming.clj:162)"
    "query_processor.streaming$streaming_response_STAR_$fn__39322.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__18948.invoke(streaming_response.clj:84)"],
   :error_type :invalid-query,
   :ex-data
   {:sql
    "-- Metabase:: userID: 1 queryType: MBQL queryHash: ed3848855aea99cb66388521bd50dc94c8ca7ab6adbf408be64d89d6d7bcd0be\nSELECT \"source\".\"size\" AS \"size\", \"source\".\"sum\" AS \"sum\" FROM (SELECT CASE WHEN \"PUBLIC\".\"ORDERS\".\"TOTAL\" > 10 THEN ? ELSE ? END AS \"size\", sum(\"PUBLIC\".\"ORDERS\".\"TOTAL\") AS \"sum\" FROM \"PUBLIC\".\"ORDERS\" GROUP BY CASE WHEN \"PUBLIC\".\"ORDERS\".\"TOTAL\" > 10 THEN ? ELSE ? END ORDER BY CASE WHEN \"PUBLIC\".\"ORDERS\".\"TOTAL\" > 10 THEN ? ELSE ? END ASC) \"source\" LIMIT 2000",
    :params ("Large" "Small" "Large" "Small" "Large" "Small"),
    :type :invalid-query}}],
 :state "90016",
 :error_type :invalid-query,
 :json_query
 {:database 4,
  :query {:source-table "card__1196"},
  :type "query",
  :parameters [],
  :middleware {:js-int-to-string? true, :add-default-userland-constraints? true}},
 :native
 {:query
  "SELECT \"source\".\"size\" AS \"size\", \"source\".\"sum\" AS \"sum\" FROM (SELECT CASE WHEN \"PUBLIC\".\"ORDERS\".\"TOTAL\" > 10 THEN ? ELSE ? END AS \"size\", sum(\"PUBLIC\".\"ORDERS\".\"TOTAL\") AS \"sum\" FROM \"PUBLIC\".\"ORDERS\" GROUP BY CASE WHEN \"PUBLIC\".\"ORDERS\".\"TOTAL\" > 10 THEN ? ELSE ? END ORDER BY CASE WHEN \"PUBLIC\".\"ORDERS\".\"TOTAL\" > 10 THEN ? ELSE ? END ASC) \"source\" LIMIT 1048575",
  :params ("Large" "Small" "Large" "Small" "Large" "Small")},
 :status :failed,
 :class org.h2.jdbc.JdbcSQLException,
 :stacktrace
 ["org.h2.message.DbException.getJdbcSQLException(DbException.java:357)"
  "org.h2.message.DbException.get(DbException.java:179)"
  "org.h2.message.DbException.get(DbException.java:155)"
  "org.h2.expression.ExpressionColumn.updateAggregate(ExpressionColumn.java:172)"
  "org.h2.expression.Comparison.updateAggregate(Comparison.java:488)"
  "org.h2.expression.Function.updateAggregate(Function.java:2488)"
  "org.h2.expression.Alias.updateAggregate(Alias.java:87)"
  "org.h2.command.dml.Select.queryGroup(Select.java:350)"
  "org.h2.command.dml.Select.queryWithoutCache(Select.java:628)"
  "org.h2.command.dml.Query.queryWithoutCacheLazyCheck(Query.java:114)"
  "org.h2.command.dml.Query.query(Query.java:371)"
  "org.h2.command.dml.Query.query(Query.java:333)"
  "org.h2.index.ViewIndex.find(ViewIndex.java:288)"
  "org.h2.index.ViewIndex.find(ViewIndex.java:160)"
  "org.h2.index.BaseIndex.find(BaseIndex.java:130)"
  "org.h2.index.IndexCursor.find(IndexCursor.java:176)"
  "org.h2.table.TableFilter.next(TableFilter.java:471)"
  "org.h2.command.dml.Select$LazyResultQueryFlat.fetchNextRow(Select.java:1452)"
  "org.h2.result.LazyResult.hasNext(LazyResult.java:79)"
  "org.h2.result.LazyResult.next(LazyResult.java:59)"
  "org.h2.command.dml.Select.queryFlat(Select.java:527)"
  "org.h2.command.dml.Select.queryWithoutCache(Select.java:633)"
  "org.h2.command.dml.Query.queryWithoutCacheLazyCheck(Query.java:114)"
  "org.h2.command.dml.Query.query(Query.java:371)"
  "org.h2.command.dml.Query.query(Query.java:333)"
  "org.h2.command.CommandContainer.query(CommandContainer.java:114)"
  "org.h2.command.Command.executeQuery(Command.java:202)"
  "org.h2.jdbc.JdbcPreparedStatement.executeQuery(JdbcPreparedStatement.java:114)"
  "com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:1471)"
  "--> driver.sql_jdbc.execute$fn__53978.invokeStatic(execute.clj:363)"
  "driver.sql_jdbc.execute$fn__53978.invoke(execute.clj:361)"
  "driver.sql_jdbc.execute$execute_statement_or_prepared_statement_BANG_.invokeStatic(execute.clj:376)"
  "driver.sql_jdbc.execute$execute_statement_or_prepared_statement_BANG_.invoke(execute.clj:372)"
  "driver.sql_jdbc.execute$execute_reducible_query$fn__54060.invoke(execute.clj:500)"
  "driver.sql_jdbc.execute$execute_reducible_query.invokeStatic(execute.clj:499)"
  "driver.sql_jdbc.execute$execute_reducible_query.invoke(execute.clj:486)"
  "driver.sql_jdbc.execute$execute_reducible_query.invokeStatic(execute.clj:494)"
  "driver.sql_jdbc.execute$execute_reducible_query.invoke(execute.clj:486)"
  "driver.sql_jdbc$fn__83581.invokeStatic(sql_jdbc.clj:54)"
  "driver.sql_jdbc$fn__83581.invoke(sql_jdbc.clj:52)"
  "driver.h2$fn__80664.invokeStatic(h2.clj:90)"
  "driver.h2$fn__80664.invoke(h2.clj:87)"
  "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___51447.invoke(cache.clj:220)"
  "query_processor.middleware.permissions$check_query_permissions$fn__47230.invoke(permissions.clj:109)"
  "query_processor.middleware.mbql_to_native$mbql__GT_native$fn__50388.invoke(mbql_to_native.clj:23)"
  "query_processor$fn__52921$combined_post_process__52926$combined_post_process_STAR___52927.invoke(query_processor.clj:207)"
  "query_processor$fn__52921$combined_pre_process__52922$combined_pre_process_STAR___52923.invoke(query_processor.clj:204)"
  "query_processor.middleware.resolve_database_and_driver$resolve_database_and_driver$fn__51368$fn__51373.invoke(resolve_database_and_driver.clj:35)"
  "driver$do_with_driver.invokeStatic(driver.clj:75)"
  "driver$do_with_driver.invoke(driver.clj:71)"
  "query_processor.middleware.resolve_database_and_driver$resolve_database_and_driver$fn__51368.invoke(resolve_database_and_driver.clj:34)"
  "query_processor.middleware.fetch_source_query$resolve_card_id_source_tables$fn__47473.invoke(fetch_source_query.clj:277)"
  "query_processor.middleware.store$initialize_store$fn__47661$fn__47662.invoke(store.clj:11)"
  "query_processor.store$do_with_store.invokeStatic(store.clj:44)"
  "query_processor.store$do_with_store.invoke(store.clj:38)"
  "query_processor.middleware.store$initialize_store$fn__47661.invoke(store.clj:10)"
  "query_processor.middleware.normalize_query$normalize$fn__51640.invoke(normalize_query.clj:22)"
  "query_processor.middleware.constraints$add_default_userland_constraints$fn__48730.invoke(constraints.clj:53)"
  "query_processor.middleware.process_userland_query$process_userland_query$fn__51579.invoke(process_userland_query.clj:145)"
  "query_processor.middleware.catch_exceptions$catch_exceptions$fn__51951.invoke(catch_exceptions.clj:168)"
  "query_processor.reducible$async_qp$qp_STAR___44200$thunk__44202.invoke(reducible.clj:100)"
  "query_processor.reducible$async_qp$qp_STAR___44200.invoke(reducible.clj:106)"
  "query_processor.reducible$async_qp$qp_STAR___44200.invoke(reducible.clj:91)"
  "query_processor.reducible$sync_qp$qp_STAR___44211.doInvoke(reducible.clj:126)"
  "query_processor$process_userland_query.invokeStatic(query_processor.clj:326)"
  "query_processor$process_userland_query.doInvoke(query_processor.clj:322)"
  "query_processor$fn__52968$process_query_and_save_execution_BANG___52977$fn__52980.invoke(query_processor.clj:337)"
  "query_processor$fn__52968$process_query_and_save_execution_BANG___52977.invoke(query_processor.clj:330)"
  "query_processor$fn__53012$process_query_and_save_with_max_results_constraints_BANG___53021$fn__53024.invoke(query_processor.clj:349)"
  "query_processor$fn__53012$process_query_and_save_with_max_results_constraints_BANG___53021.invoke(query_processor.clj:342)"
  "api.dataset$run_query_async$fn__66704.invoke(dataset.clj:68)"
  "query_processor.streaming$streaming_response_STAR_$fn__39322$fn__39323.invoke(streaming.clj:162)"
  "query_processor.streaming$streaming_response_STAR_$fn__39322.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__18948.invoke(streaming_response.clj:84)"],
 :card_id 1196,
 :context :ad-hoc,
 :error
 "Column \"PUBLIC.ORDERS.TOTAL\" must be in the GROUP BY list; SQL statement:\n-- Metabase:: userID: 1 queryType: MBQL queryHash: ed3848855aea99cb66388521bd50dc94c8ca7ab6adbf408be64d89d6d7bcd0be\nSELECT \"source\".\"size\" AS \"size\", \"source\".\"sum\" AS \"sum\" FROM (SELECT CASE WHEN \"PUBLIC\".\"ORDERS\".\"TOTAL\" > 10 THEN ? ELSE ? END AS \"size\", sum(\"PUBLIC\".\"ORDERS\".\"TOTAL\") AS \"sum\" FROM \"PUBLIC\".\"ORDERS\" GROUP BY CASE WHEN \"PUBLIC\".\"ORDERS\".\"TOTAL\" > 10 THEN ? ELSE ? END ORDER BY CASE WHEN \"PUBLIC\".\"ORDERS\".\"TOTAL\" > 10 THEN ? ELSE ? END ASC) \"source\" LIMIT 2000 [90016-197]",
 :row_count 0,
 :running_time 0,
 :preprocessed
 {:database 4,
  :query
  {:source-card-id 1196,
   :source-metadata
   [{:base_type :type/Text,
     :name "size",
     :display_name "size",
     :expression_name "size",
     :field_ref [:expression "size"],
     :source :breakout}
    {:base_type :type/Float,
     :semantic_type nil,
     :settings nil,
     :name "sum",
     :display_name "Sum of TOTAL",
     :source :aggregation,
     :field_ref [:aggregation 0]}],
   :fields [[:field "size" {:base-type :type/Text}] [:field "sum" {:base-type :type/Float}]],
   :source-query
   {:source-table 84463,
    :expressions
    {"size"
     [:case
      [[[:>
         [:field 1518904 nil]
         [:value
          10
          {:base_type :type/Float,
           :effective_type :type/Float,
           :coercion_strategy nil,
           :semantic_type nil,
           :database_type "DOUBLE",
           :name "TOTAL"}]]
        "Large"]]
      {:default "Small"}]},
    :breakout [[:expression "size"]],
    :aggregation [[:aggregation-options [:sum [:field 1518904 nil]] {:name "sum"}]],
    :order-by [[:asc [:expression "size"]]]},
   :limit 1048575,
   :metabase.query-processor.middleware.limit/original-limit nil},
  :type :query,
  :middleware {:js-int-to-string? true, :add-default-userland-constraints? true},
  :info {:executed-by 1, :context :ad-hoc, :card-id 1196}},
 :data {:rows [], :cols []}}

To Reproduce
Steps to reproduce the behavior:

  1. In the sample database, create a question with the query builder like this:

    The custom size column is: case([TOTAL] > 10, "Large", "Small").
    This question works as expected.

  2. Create another question that just references the first one and doesn't do anything else:

    and click "visualize".

  3. See this error:

Expected behavior
The second question should return the same results as the first one, since it didn't do anything besides reference the first question.

Information about your Metabase Installation:

  • Your browser and the version: Latest Firefox
  • Your operating system: MacOS 12.0
  • Your databases: RDS Postgres 11.13
  • Metabase version: v0.43.4
  • Metabase hosting environment: Elastic Beanstalk
  • Metabase internal database: RDS Postgres 11.13

Severity
A number of our existing questions were broken when we upgraded to v0.43.4 and we haven't found a workaround yet besides converting all affected questions to SQL, which we'd like to avoid. We're considering whether we need to downgrade.

Additional context
In my reproduction example, the query generated by the second question looks like this:

SELECT
  "source"."size" AS "size",
  "source"."sum" AS "sum"
FROM (
  SELECT
    CASE WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END AS "size",
    sum("PUBLIC"."ORDERS"."TOTAL") AS "sum"
  FROM "PUBLIC"."ORDERS"
  GROUP BY CASE WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END
  ORDER BY CASE WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END ASC
) "source" LIMIT 2000

If I run this query replacing all the ? parameters with the same value, then the query runs successfully. But if I replace the ? with different values such that the values in the SELECT don't match the values in the GROUP BY, then I get the error I see in my example. Based on my question, it seems like those ?s should all be getting the same value when Metabase runs the query, but maybe they don't? Or maybe Postgres doesn't know that the values are going to match when it's figuring out whether the query is valid?

Also this is a minimal reproduction of the issue, in our actual use case the second referencing question is actually trying to do some data processing, sometimes there's more than one level of question references going on, etc.

Thanks for your help!

@paoliniluis
Copy link
Contributor

thanks, which version did you upgrade from?

@GriffinSchneider
Copy link
Author

thanks, which version did you upgrade from?

We noticed this issue when we upgraded from v0.41.6 to v0.43.4

@paoliniluis paoliniluis added .Regression Bugs that were previously fixed and/or bugs unintentionally shipped with new features. Priority:P2 Average run of the mill bug and removed .Needs Triage .Regression Bugs that were previously fixed and/or bugs unintentionally shipped with new features. labels Jul 12, 2022
@paoliniluis
Copy link
Contributor

Not flagging as a regression since I went all the way back to 41.7 and hit the bug there as well

@nemanjaglumac
Copy link
Member

@GriffinSchneider thank you for concise reproduction steps using the Sample Database. I was able to reproduce the issue and am now creating an E2E test following those steps.

nemanjaglumac added a commit that referenced this issue Jul 13, 2022
nemanjaglumac added a commit that referenced this issue Jul 14, 2022
… (#23957)

Co-authored-by: Nemanja Glumac <31325167+nemanjaglumac@users.noreply.github.com>
@nemanjaglumac nemanjaglumac added the .Reproduced Issues reproduced in test (usually Cypress) label Jul 14, 2022
@flamber flamber added Querying/Nested Queries Questions based on other saved questions Querying/Notebook/Custom Column and removed Querying/Processor labels Jul 21, 2022
@metamben metamben self-assigned this Jul 25, 2022
@camsaul
Copy link
Member

camsaul commented Jul 27, 2022

The reason we push expression definitions into a nested query in the first place is that a lot of DB engines have problems if you try to use expression definitions directly in a GROUP BY amongst other problems. e.g. with that failing query

SELECT
  "source"."size" AS "size",
  "source"."sum" AS "sum"
FROM (
  SELECT
    CASE WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END AS "size",
    sum("PUBLIC"."ORDERS"."TOTAL") AS "sum"
  FROM "PUBLIC"."ORDERS"
  GROUP BY CASE WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END
  ORDER BY CASE WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END ASC
) "source" LIMIT 2000

I'm guessing the parameters are indeed all the same but it's not smart enough to figure out that WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END is the same as WHEN "PUBLIC"."ORDERS"."TOTAL" > 10 THEN ? ELSE ? END even if we supply the exact same values for the ? parameters. If it's not parameterized tho it seems like it can figure it out.

We've probably never ran into this bug in the past because it would only pop up when you use a parameterized expression like case with a String inside of a nested query.

Anyways as @metamben suggested the root problem here is that we should be recursively pushing expressions into subselects but we're currently only doing it at the top level -- seems like a clear bug

This was referenced Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority:P2 Average run of the mill bug Querying/Nested Queries Questions based on other saved questions Querying/Notebook/Custom Column .Reproduced Issues reproduced in test (usually Cypress) Type:Bug Product defects
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants