metabase icon indicating copy to clipboard operation
metabase copied to clipboard

Question Saving Not Working for Particular Queries

Open samiyabroudi opened this issue 8 years ago • 16 comments

For some reason, I cannot save a question once I have pasted the below query inside. Thanks!!

WITH

conditions_by_patient AS
(
SELECT 
	sender_id AS patient_id,
	accounts_account.date_joined AS patient_join_date,
	BOOL_OR(text ILIKE '%hiv%')::int AS HIV,
	BOOL_OR(text ILIKE '%do not sleep%')::int AS sleep_issue,
	BOOL_OR(text ILIKE '%do not get as much exercise%')::int AS exercise_issue,
	BOOL_OR(text ILIKE '%do not eat as healthfully%')::int AS diet_issue,
	BOOL_OR(text ILIKE '%high cholesterol%')::int AS high_colesterol,
	BOOL_OR(text ILIKE '%high blood pressure%')::int AS high_blood_pressure,
	BOOL_OR(text ILIKE '%peyronie%')::int AS peyronie,
	BOOL_OR(text ILIKE '%stomach, intestinal, or bowel ulcers%')::int AS ulcers,
	BOOL_OR(text ILIKE '%heart attack or heart disease at%')::int AS family_heart_issue,
	BOOL_OR(text ILIKE '%liver disease%')::int AS liver_disease,
	BOOL_OR(text ILIKE '%smoke tobacco%')::int AS smoke_tobacco,
	BOOL_OR(text ILIKE '%prior heart attack, heart failure, or narrowing%')::int AS prior_heart_issue,
	BOOL_OR(text ILIKE '%drink more than I should%')::int AS drinking_issue,
	BOOL_OR(text ILIKE '%overweight%')::int AS overweight,
	BOOL_OR(text ILIKE '%angina%')::int AS angina_or_shortness_of_breath,
	BOOL_OR(text ILIKE '%diabetes%')::int AS diabetes,
	BOOL_OR(text ILIKE '%arrhythmia%')::int AS arrhythmia,
	BOOL_OR(text ILIKE '%alpha blocker%')::int AS alpha_blocker,
	BOOL_OR(text ILIKE '%idiopathic hypertrophic subaortic stenosis%')::int AS ihss,
	BOOL_OR(text ILIKE '%fibrous tissue in the penis%')::int AS fibrous_penis,
	BOOL_OR(text ILIKE '%unexplained fainting, lightheadedness, or dizziness%')::int AS unexplained_fainting_etc,
	BOOL_OR(text ILIKE '%Isosorbide mononitrate or Isosorbide dinitrate%')::int AS isosorbide_md,
	BOOL_OR(text ILIKE '%pulmonary hypertension%')::int AS pulm_hypertension,
	BOOL_OR(text ILIKE '%abnormal heart beats%')::int AS abnormal_heartbeats,
	BOOL_OR(text ILIKE '%pain with erections or with ejaculation%')::int AS penis_sex_pain,
	BOOL_OR(text ILIKE '%claudication%')::int AS claudication,
	BOOL_OR(text ILIKE '%foreskin that is too tight%')::int AS foreskin_too_tight,
	BOOL_OR(text ILIKE '%surgery or radiation to the prostate or pelvis%')::int AS prost_pelv_surg_rad,
	BOOL_OR(text ILIKE '%multiple sclerosis (MS) or similar disease, spinal injuries or paralysis, or neurological diseases%')::int AS ms_spine_neural_issue,
	BOOL_OR(text ILIKE '%nitroglycerin%')::int AS nitroglycerin,
	BOOL_OR(text ILIKE '%advised not to have sex%')::int AS advised_no_sex,
	BOOL_OR(text ILIKE '%nitrates%')::int AS nitrates
	
FROM
	chat_api_message
	JOIN chat_api_thread ON (chat_api_message.thread_id = chat_api_thread.id)
	JOIN accounts_account ON (chat_api_message.sender_id = accounts_account.id)
WHERE
	chat_api_thread.type = 'consultation'
	AND accounts_account.role = '00_patient'
	AND meta IS NOT NULL
	AND meta != ''
	AND meta LIKE '{%'
	AND meta LIKE '%positive%'
	AND text NOT ILIKE '%none%'
GROUP BY 1,2
)


SELECT
	DATE_TRUNC('month', convert_timezone('America/New_York', patient_join_date)) AS join_period
	, AVG(1.0*hiv) AS pct_with_hiv
	, AVG(1.0*sleep_issue) AS pct_with_sleep_issue
	, AVG(1.0*exercise_issue) AS pct_with_exercise_issue
	, AVG(1.0*diet_issue) AS pct_with_diet_issue
	, AVG(1.0*high_colesterol) AS pct_with_high_colesterol
	, AVG(1.0*high_blood_pressure) AS pct_with_high_blood_pressure
	, AVG(1.0*peyronie) AS pct_with_peyronie
	, AVG(1.0*ulcers) AS pct_with_ulcers
	, AVG(1.0*family_heart_issue) AS pct_with_family_heart_issue
	, AVG(1.0*liver_disease) AS pct_with_liver_disease
	, AVG(1.0*smoke_tobacco) AS pct_with_smoke_tobacco
	, AVG(1.0*prior_heart_issue) AS pct_with_prior_heart_issue
	, AVG(1.0*drinking_issue) AS pct_with_drinking_issue
	, AVG(1.0*overweight) AS pct_with_overweight
	, AVG(1.0*angina_or_shortness_of_breath) AS pct_with_angina_or_shortness_of_breath
	, AVG(1.0*diabetes) AS pct_with_diabetes
	, AVG(1.0*arrhythmia) AS pct_with_arrhythmia
	, AVG(1.0*alpha_blocker) AS pct_with_alpha_blocker
	, AVG(1.0*ihss) AS pct_with_ihss
	, AVG(1.0*fibrous_penis) AS pct_with_fibrous_penis
	, AVG(1.0*unexplained_fainting_etc) AS pct_with_unexplained_fainting_etc
	, AVG(1.0*isosorbide_md) AS pct_with_isosorbide_md
	, AVG(1.0*pulm_hypertension) AS pct_with_pulm_hypertension
	, AVG(1.0*abnormal_heartbeats) AS pct_with_abnormal_heartbeats
	, AVG(1.0*penis_sex_pain) AS pct_with_penis_sex_pain
	, AVG(1.0*claudication) AS pct_with_claudication
	, AVG(1.0*foreskin_too_tight) AS pct_with_foreskin_too_tight
	, AVG(1.0*prost_pelv_surg_rad) AS pct_with_prost_pelv_surg_rad
	, AVG(1.0*ms_spine_neural_issue) AS pct_with_ms_spine_neural_issue
	, AVG(1.0*nitroglycerin) AS pct_with_nitroglycerin
	, AVG(1.0*advised_no_sex) AS pct_with_advised_no_sex
	, AVG(1.0*nitrates) AS pct_with_nitrates
	, AVG(nitrates) AS pct_with_nitrates
-- 
FROM
	conditions_by_patient
GROUP BY 1

Before filing an issue we'd appreciate it if you could take a moment to ensure there isn't already an open issue or pull-request.

If there's an existing issue, please add a :+1: reaction to the description of the issue. One way we prioritize issues is by the number of :+1: reactions on their descriptions. Please DO NOT add +1 or :+1: comments.

Database support

For requests for new database drivers, please include the name of the database and the version. Additionally, giving us a sense of how and what you use it for, and other high-level details about the database, can help us get a better picture of what would need to done to support it.

Feature requests and proposals

We're excited to hear how we can make Metabase better. Please add as much detail as you can on your use case.

Bugs

If you're filing an issue about a bug please include as much information as you can including the following.

  • Your browser and the version: (e.x. Chrome 52.1, Firefox 48.0, IE 10)

  • Your operating system: (e.x. OS X 10, Windows XP, etc)

  • Your databases: (e.x. MySQL, Postgres, MongoDB, …)

  • Metabase version: (e.x. 0.19.3)

  • Metabase hosting environment: (e.x. Mac app, Elastic Beanstalk, Docker, Heroku, Linux/Ubuntu 12)

  • Metabase internal database: (e.x. H2 (default), MySQL, Postgres)

  • Repeatable steps to reproduce the issue

Thanks for being part of the Metabase project!

samiyabroudi avatar Feb 05 '18 23:02 samiyabroudi

What are you seeing on screen when you try to save the query? Does it return results when you run it? Screenshots (with sensitive bits blurred/blocked out) would be helpful.

mazameli avatar Feb 05 '18 23:02 mazameli

Sorry to have not put that - it basically just spins and spins as if it's trying to save the question but can't. It is possible to exit out of the save dialog by clicking outside of it, but the question is not saved after that when I check.

Thanks!

On Mon, Feb 5, 2018 at 6:47 PM, Maz Ameli [email protected] wrote:

What are you seeing on screen when you try to save the query? Does it return results when you run it? Screenshots (with sensitive bits blurred/blocked out) would be helpful.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/metabase/metabase/issues/6866#issuecomment-363260457, or mute the thread https://github.com/notifications/unsubscribe-auth/AHsei3vcLim9ktWdommJaSOOler7875Nks5tR5MIgaJpZM4R6ReA .

samiyabroudi avatar Feb 05 '18 23:02 samiyabroudi

I am having the same issue. I have narrowed it down to saving questions with joins in them. For example, this wont save:

SELECT DISTINCT cuberesults.*, concdiary.ConcTotalM3, concdiary.MouldID, concdiary.Quantity, concdiary.Time, concdiary.MixRef, concdiary.Litronics, concdiary.WCRatio, concdiary.AirTemp, concdiary.ConcTemp, concdiary.NW
FROM cuberesults
LEFT JOIN concdiary ON cuberesults.Date = concdiary.Date and cuberesults.CubeRef = concdiary.CubeRef

but this will:

SELECT DISTINCT cuberesults.*
FROM cuberesults

I'm running Metabase 0.30.3 on windows server 2016 as a jar. I'm starting the jar with a bat file setup as a service:

set MB_DB_TYPE=mysql
set MB_DB_DBNAME=metabase
set MB_DB_PORT=3306
set MB_DB_USER=metabase
set MB_DB_PASS=password
set MB_DB_HOST=localhost
java -jar metabase.jar

The using MariaDB hosted on the same server as metabase. This contains the database that metabase stores it's info and contains the database i use in queries in metabase.

ghost avatar Sep 26 '18 13:09 ghost

@mazameli This is the first line in the logs after trying to save:

09-26 13:43:04 INFO api.card :: Card results metadata passed in to API is MISSING. Running query to fetch correct metadata.

Is it trying to run the query I am saving, because that will take forever, hence the hang on saving?

ghost avatar Sep 26 '18 13:09 ghost

I have just rolled back the version i have installed until i found a version that worked again. This showed me that the issue was first introduced in version v0.25.0

ghost avatar Sep 26 '18 15:09 ghost

Also seems to by system and deployment agnostic. I have the issue when deployed via docker on Linux and via docker on Windows with Hyper-V as well. With the tests I ran to work out which version the issue was introduced on, i just set it up with the H2 database and connected to a mysql server to run a test query against, so it doesn't appear to be an issue with the metabase backed database. I have not tried running a query against a database other than mysql/mariadb

ghost avatar Sep 26 '18 15:09 ghost

I was having a very similar issue with saving questions using 30.3 and MySQL. I found that my SQL had a mix of both types of 'quotes'. After I converted all of the ' to ` it saved instantly.

reilly3000 avatar Sep 28 '18 23:09 reilly3000

@tlrobinson WRT to the

Card results metadata passed in to API is MISSING. Running query to fetch correct metadata.

I've seen that a few times too, has the frontend stopped passing that in when saving new Cards? If that's not passed in we have to run the query again to get the column metadata so the Card can be used in nested queries

camsaul avatar Sep 29 '18 00:09 camsaul

@reilly3000 Thanks for the tip. I didn't have any of either type of quote mark. but I added ` around everything to see if that changed anything. It did not.

@camsaul @tlrobinson I have narrowed down the issue. To my where clause. This will save:

SELECT `tmp`.`*`, `JobCodes`.`JobCode` FROM(
SELECT `CubeData`.`*`, `Piles`.`StockCode`, `Piles`.`Serials`
FROM `CubeData`
LEFT JOIN `Piles` ON `CubeData`.`ID` = `Piles`.`ID`) tmp
LEFT JOIN `JobCodes` On `tmp`.`Serials` = `JobCodes`.`Serial`
WHERE  `JobCodes`.`JobCode` = {{JobCode}}
[[AND `tmp`.`DateCast` = {{date}}]]

This will not save:

SELECT `tmp`.`*`, `JobCodes`.`JobCode` FROM(
SELECT `CubeData`.`*`, `Piles`.`StockCode`, `Piles`.`Serials`
FROM `CubeData`
LEFT JOIN `Piles` ON `CubeData`.`ID` = `Piles`.`ID`) tmp
LEFT JOIN `JobCodes` On `tmp`.`Serials` = `JobCodes`.`Serial`
WHERE `tmp`.`ID` <> "" 
[[AND `JobCodes`.`JobCode` = {{JobCode}}]]
[[AND `tmp`.`DateCast` = {{date}}]]
[[AND `tmp`.`CubeRef` = {{CubeRef}}]]
[[AND `tmp`.`Serials` = {{Serial}}]]

It seems that the WHERE tmp.ID <> "" is causing the issue. I put that in there so I could have optional variables so I didn't have to write 4 questions and you could have any combination of the 4 vaibales. So I added a where clause that would always be true rather than have a variable that was required. Any suggestions on how I can get around this?

ghost avatar Oct 01 '18 09:10 ghost

Hi @cheesemarathon,

I am not sure double quotes are legal SQL in WHERE tmp.ID <> ""

Best regards,

Updated : indeed valid against a mySQL instance... sorry forget about my post.

fetanchaud avatar Oct 01 '18 09:10 fetanchaud

@fetanchaud I changed it to WHERE tmp.ID IS NOT NULL in case, but of course no change..... :-(

ghost avatar Oct 01 '18 12:10 ghost

Having the same issue on a java instance running on my Debian EC2. Can't save questions sometimes, and then all of a sudden I can save them again. No information on that on logs.

lp-lima avatar Oct 11 '18 15:10 lp-lima

I've been able to consistently reproduce this bug. As far as I can tell, it happens when trying to save a question where:

  1. The query run-time is very high, and
  2. the query hasn't been run, or the query response isn't available in cache.

In these situations, it tries to run the query before saving the question (not sure why), and the save API times out.

Shashwat986 avatar Feb 12 '19 16:02 Shashwat986

I've been able to consistently reproduce this bug. As far as I can tell, it happens when trying to save a question where:

1. The query run-time is very high, and

2. the query hasn't been run, or the query response isn't available in cache.

In these situations, it tries to run the query before saving the question (not sure why), and the save API times out.

I'd agree with this. Removing one of the joins from my query allowed it to save fine. This links with point one. Remove the join and the run-time is shortened.

ghost avatar Feb 12 '19 16:02 ghost

@cheesemarathon one hacky solution to that issue is to save the query immediately after running it and seeing the response. That works. The bug is still there, but the conditions aren't met for it to show up

Shashwat986 avatar Feb 13 '19 17:02 Shashwat986

I believe this was fixed a while ago, but it is definitely fixed in #10947

camsaul avatar Sep 20 '19 22:09 camsaul