Storm accepts order_by clauses that mention columns not present in the output

Bug #675667 reported by Aaron Bentley
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Storm
New
Undecided
Unassigned

Bug Description

Storm accepts order_by clauses that mention columns not present in the output, when this should at least generate a SQL error if not a Storm exception.

For example:
    def getBuilds(self, pending=False):
        """See `ISourcePackageRecipe`."""
        if pending:
            clauses = [BuildFarmJob.date_finished == None]
        else:
            clauses = [BuildFarmJob.date_finished != None]
        # Use subselect as a workaround for Storm bug #674582.
        and_clause = And(
            SourcePackageRecipeBuild.recipe==self,
            SourcePackageRecipeBuild.package_build_id == PackageBuild.id,
            PackageBuild.build_farm_job_id == BuildFarmJob.id, *clauses)
        subselect = Select(SourcePackageRecipeBuild.id, and_clause)
        result = Store.of(self).find(
            SourcePackageRecipeBuild,
            SourcePackageRecipeBuild.id.is_in(subselect))
        result.order_by(Desc(BuildFarmJob.date_finished))
        return result

Note that BuildFarmJob is not mentioned in Store.find, only the subselect. Therefore, sorting by it is not feasible. However, Storm emits this SQL:

SELECT SourcePackageRecipeBuild.distroseries, SourcePackageRecipeBuild.id, SourcePackageRecipeBuild.package_build, SourcePackageRecipeBuild.recipe, SourcePackageRecipeBuild.requester FROM BuildFarmJob, SourcePackageRecipeBuild WHERE SourcePackageRecipeBuild.id IN (SELECT SourcePackageRecipeBuild.id FROM BuildFarmJob, PackageBuild, SourcePackageRecipeBuild WHERE SourcePackageRecipeBuild.recipe = %s AND SourcePackageRecipeBuild.package_build = PackageBuild.id AND PackageBuild.build_farm_job = BuildFarmJob.id AND BuildFarmJob.date_finished IS NULL) ORDER BY BuildFarmJob.date_finished DESC'

The FROM clause includes BuildFarmJob, but it really shouldn't, because BuildFarmJob is only included in the ORDER BY. ORDER BY cannot produce sensible results when the table column is is not mentioned elsewhere, so ORDER BY should not be used to determine which tables to include in the FROM.

Because of this behaviour, ResultSet.count() will give dramatically different behaviour from len(list(ResultSet)). In my testing, ResultSet.count() gives 1, and len(list(ResultSet)) gives 25. (The list is the same SourcePackageRecipeBuild repeated 25 times)

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.