Connecting Tech Pros Worldwide Forums | Help | Site Map

A query that is on a mission to turn my hair gray

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#1: Oct 1 '09
All I want to ask is

What can you think of in your WILDEST imagination that explains a query that returns results 1 out of several times?

The same exact query (from a mysql prompt) I execute (by pressing the UP key) repeatedly sometimes returns results other times it's an empty result set.

The data is not being rebuild and updated in the background. In fact there is a cron job that updates it every half hour, I've taken that out. There is no other process changing the data.

What in the Universe is going on?

The query is simple. SELECT some fields FROM a few different tables WHERE cartesian joins. (tried LEFT JOINS too).

I use a Stored Function that converts an ID between two format to join a couple of those tables (I had a thread about this function a while back if you remember)

Shoot whatever ideas you have at me. At this point what I know of simple logic is going out the window and I'm ready to smash the computer with a rock.

I envy cavemen


Thanks to anyone who can make me sane again,




Dan

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#2: Oct 2 '09

re: A query that is on a mission to turn my hair gray


I think I found a MySQL bug.

I'm going to create a sample DB with sample Tables and create a Stored Function.

I think the stored function caches the last value returned and returns it even if given a different input but only when used in a LEFT JOIN. Function works fine if you just do a SELECT with it.

Will keep you updated...



Dan
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#3: Oct 5 '09

re: A query that is on a mission to turn my hair gray


Hey Guys,

If anybody has a test MySQL server handy, could you look over this bug report. I'll submit it to MySQL bug if you guys confirm it on your end too.

Thanks alot.

Expand|Select|Wrap|Line Numbers
  1.  
  2. /**
  3.  
  4. Desc:             Example Schema, Data and Query that illustrate a possible MySQL bug involving use Stored Function in a LEFT JOIN. 
  5.  
  6. Created:         10/05/2009
  7. Author:         Dan M (dlite922)
  8. Version:         mysql  Ver 14.12 Distrib 5.0.45, for redhat-linux-gnu (x86_64) using readline 5.0
  9. Repeatability:     Always 
  10.  
  11. /*
  12.  
  13. /** SCHEMA **/
  14. drop table IF EXISTS image;
  15. drop table IF EXISTS event_image;
  16. drop table IF EXISTS event;
  17.  
  18. Create table event (
  19.     event_id Int NOT NULL AUTO_INCREMENT,
  20.     preconverted_id Varchar(5) NOT NULL,
  21.     name Varchar(20) NOT NULL,
  22.     UNIQUE (event_id),
  23.  Primary Key (event_id)) ENGINE = MyISAM;
  24.  
  25. Create table event_image (
  26.     postconverted_id Int NOT NULL,
  27.     image_id Int,
  28.     UNIQUE (postconverted_id),
  29.  Primary Key (postconverted_id)) ENGINE = MyISAM;
  30.  
  31. Create table image (
  32.     image_id Int NOT NULL AUTO_INCREMENT,
  33.     image_name Varchar(20) NOT NULL,
  34.     image_type Varchar(4) NOT NULL,
  35.     UNIQUE (image_id),
  36.  Primary Key (image_id)) ENGINE = MyISAM;
  37.  
  38.  
  39.  
  40.  
  41. /** STORED FUNCTION TO CONVERT IDs **/ 
  42. DELIMITER $$
  43. DROP FUNCTION IF EXISTS ConvertID$$
  44. CREATE FUNCTION ConvertID(preconverted_id VARCHAR(5)) RETURNS INT
  45. DETERMINISTIC
  46. BEGIN
  47.  DECLARE catText CHAR(1); 
  48.  DECLARE catNum INT; 
  49.  DECLARE series CHAR(4); 
  50.  DECLARE FinalID INT;  
  51.  
  52.  SET catText = SUBSTRING(preconverted_id FROM 1 FOR 1); 
  53.  SET series = SUBSTRING(preconverted_id FROM 2 FOR 5); 
  54.  
  55.  IF(catText = 'A') THEN
  56.   SET catNum = 1;
  57.  ELSEIF(catText = 'B') THEN
  58.   SET catNum = 2;
  59.  ELSEIF(catText = 'C') THEN
  60.   SET catNum = 3;
  61.  END IF;
  62.  
  63.  SET FinalID = (catNum << 26 | series); 
  64.  RETURN FinalID;
  65. END $$
  66. DELIMITER ;
  67.  
  68.  
  69.  
  70.  
  71. /** SAMPLE DATA **/
  72. INSERT INTO event VALUES (1,'A1234','Bar'),(2,'B5678','Foo'),(3,'C9011','Baz'); 
  73. INSERT INTO event_image VALUES (67110098,55),(134223406,97),(201335603,42); 
  74. INSERT INTO image VALUES (55,'Bar_and_friends','jpg'),(97,'FoorAreYou','gif'),(42,'BazFishing','png'); 
  75.  
  76.  
  77.  
  78.  
  79.  
  80. /** QUERY THAT EXPOSES BUG **/
  81. /** PLEASE OBSERVE: covertedColumn. All fields have the converted of the last ID (that of 'Baz'). **/
  82. SELECT 
  83.  e.event_id,
  84.  e.preconverted_id,
  85.  e.name, 
  86.  ConvertID(e.preconverted_id) AS convertedColumn,
  87.  ei.postconverted_id,
  88.  ei.image_id, 
  89.  i.image_id,
  90.  i.image_name,
  91.  i.image_type
  92. FROM 
  93.  event AS e LEFT JOIN event_image AS ei ON ConvertID(e.preconverted_id) = ei.postconverted_id 
  94.  LEFT JOIN image AS i ON ei.image_id = i.image_id; 
  95.  
  96.  
  97. /** If you don't believe my function is working properly checkout these results **/
  98. SELECT e.event_id, e.name, ConvertID(e.preconverted_id) FROM event AS e;
  99.  
  100. // OR SIMPLY: 
  101. SELECT ConvertID('A1234'); 
  102. SELECT ConvertID('B5678'); 
  103. SELECT ConvertID('C9011'); 
  104.  
  105.  
  106. // END OF FILE 
  107.  
  108.  
I've tried with and without the DETERMINISTIC flag on the function.

Please let me know if you see something odd. I often make silly mistakes.

Thanks again,


Dan
Reply

Tags
losing your mind