MySQL Left join error, Unknown column

Bug #162528 reported by S3ym0ur
6
Affects Status Importance Assigned to Milestone
Storm
Fix Released
Undecided
Gustavo Niemeyer

Bug Description

Currently, the expression builder dont put parenthesis between the FROM and JOIN and this create a bug. In my case, the LEFT JOIN was only on the tests class. With the parenthesis, it work perfectly

Modification I made so it work:
In expr.py in the build_tables function

ORIGINAL VERSION:
def build_tables(compile, tables, default_tables, state):
      ...
        # Remove tables that were seen in join statements.
        table_stmts -= state.join_tables

        state.pop()

        result = ", ".join(sorted(table_stmts)+sorted(join_stmts))

        if half_join_stmts:
            result += " " + " ".join(sorted(half_join_stmts))

        return "".join(result)
   ...

SQL query with original version:
SELECT platforms.name, results.`time` FROM buffer_types, results, tests LEFT JOIN platforms ON results.platform = plat
forms.id WHERE tests.kernel_type = buffer_types.id AND buffer_types.name = %s AND tests.serial_no = results.test ORDER B
Y platforms.name ASC

Exception catched:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
    return self.func(*args)
  File "E:\current\milbench\current\BenchDBTool\source\ExportResultWindow.py", line 97, in search
    for res in results:
  File "C:\Python25\lib\site-packages\storm\store.py", line 1109, in values
    result = self._store._connection.execute(select)
  File "C:\Python25\lib\site-packages\storm\database.py", line 182, in execute
    raw_cursor = self.raw_execute(statement, params)
  File "C:\Python25\lib\site-packages\storm\database.py", line 247, in raw_execute
    raw_cursor.execute(statement, params)
  File "C:\Python25\lib\site-packages\MySQLdb\cursors.py", line 166, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python25\lib\site-packages\MySQLdb\connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (1054, "Unknown column 'results.platform' in 'on clause'")

MODIFIED VERSION:
      ....
        # Remove tables that were seen in join statements.
        table_stmts -= state.join_tables

        state.pop()

        result = "(" + ", ".join(sorted(table_stmts)) + ")"
        result += ", ".join(sorted(join_stmts))
        if half_join_stmts:
            result += " " + " ".join(sorted(half_join_stmts))

        return "".join(result)
      ....
SQL query with modified version:
SELECT platforms.name, results.`time` FROM (buffer_types, results, tests) LEFT JOIN platforms ON results.platform = plat
forms.id WHERE tests.kernel_type = buffer_types.id AND buffer_types.name = %s AND tests.serial_no = results.test ORDER B
Y platforms.name ASC

Modified version work

See following documentation: http://dev.mysql.com/doc/refman/5.0/en/join.html

Related branches

S3ym0ur (malo-lexandre)
description: updated
S3ym0ur (malo-lexandre)
Changed in storm:
assignee: nobody → niemeyer
Revision history for this message
James Henstridge (jamesh) wrote :

The optimal fix would be for the LEFT JOIN's to be paired with the tables we're left joining to. So your statement would become:

  ... FROM ROM buffer_types, results LEFT JOIN platforms ON results.platform = platforms.id, tests ...

I agree that the current SQL looks incorrect.

Revision history for this message
S3ym0ur (malo-lexandre) wrote :

My guess is you are right when you only have a single join to do, but there may be cases where we need to join more then one table.

Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

Is the breaking code using a Proxy? I guess that's the only case where this problem could happen.

Revision history for this message
Raphaël Saint-Pierre (rsaintpi) wrote :

(answering on S3ym0ur's behalf)
Yes, it is indeed.

The class mapped to the results table looks like:
class Result(object):
   __storm_table__ = "results"
   id = Int(primary=True)
   test = Int()
   test_ref = Reference(test,Test.serial_no)
   run = Int()
   run_ref = Reference(run,Run.id)
   run_description_proxy = Proxy(run_ref, Run.description)
   platform = Int()
   platform_ref = Reference(platform,Platform.id)
   platform_name_proxy = Proxy(platform_ref, Platform.name)
   time = Float()

Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

Nice! So the fix for this problem is in the attached branch. We just need some reviews
now. It will be included in the upcoming 0.12 release.

Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

Btw, this isn't specific to MySQL. Postgres will present the same issue if faced with this logic.

Revision history for this message
Jamu Kakar (jkakar) wrote :

+1, nice branch!

Revision history for this message
S3ym0ur (malo-lexandre) wrote : Re: [Bug 162528] Re: MySQL Left join error, Unknown column

Et pis, comment sa avance dans la boite? :) Tout roule, c'est pas trop mort
sans stagiaires? :P

Good pour storm :) !
Alex

2008/1/9, Raphaël Saint-Pierre <email address hidden>:
>
> (answering on S3ym0ur's behalf)
> Yes, it is indeed.
>
> The class mapped to the results table looks like:
> class Result(object):
> __storm_table__ = "results"
> id = Int(primary=True)
> test = Int()
> test_ref = Reference(test,Test.serial_no)
> run = Int()
> run_ref = Reference(run,Run.id)
> run_description_proxy = Proxy(run_ref, Run.description)
> platform = Int()
> platform_ref = Reference(platform,Platform.id)
> platform_name_proxy = Proxy(platform_ref, Platform.name)
> time = Float()
>
> --
> MySQL Left join error, Unknown column
> https://bugs.launchpad.net/bugs/162528
> You received this bug notification because you are a direct subscriber
> of the bug.
>

--
Alexandre Malo
51 promo Génie Informatique
Université de Sherbrooke

Revision history for this message
Christopher Armstrong (radix) wrote :

thanks for the fix, +1

Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

Committed in trunk as revision 204.

Changed in storm:
status: New → Fix Committed
Changed in storm:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.