Quantcast
Channel: Lucee Dev - Latest topics
Viewing all articles
Browse latest Browse all 423

Possible Bug: QoQ aggregate issue when using WHERE and ORDER BY

$
0
0

Tested on

  • TryCF Lucee 5.LATEST and 6 BETA (unexpected behavior)
  • Local Lucee 6.0.3.1 (unexpected behavior)
  • TryCF ACF 2023 (working)

I ran into an issue today where I added an ORDER BY to a QoQ I was using to calculating a SUM. It was my mistake as I didn’t need the order at all, but it still produced some pretty unexpected results which got me to test further.

It would appear that when using an ORDER BY with any WHERE clause in the query the aggregate functions (sum) work as if the ORDER BY is a GROUP BY.

I tested it on ACF and I am getting the expected result, but Lucee 5 and 6 gives something unexpected.

<cfscript>
  /*leaving this here to test against a RDBMS as I thought it was related, but it produces the same*/
  /*
    testQuery = queryExecute("
      select 1 as amount, 'a' as relatedData, 1 as unrelatedData
      union select 2 as amount, 'a' as relatedData, 2 as unrelatedData
      union select 3 as amount, 'a' as relatedData, 3 as unrelatedData
    ", {}, {datasource=DSN});
  */

  testQuery = queryNew("amount, relatedData, unrelateddata","integer, varchar, integer");
  testQuery.addRow({amount: 1, relatedData: "a", unrelateddata: 1});
  testQuery.addRow({amount: 2, relatedData: "a", unrelateddata: 2});
  testQuery.addRow({amount: 3, relatedData: "a", unrelateddata: 3});

  writedump(testQuery);

/*Simple Sum Aggregate*/
  sumSimple = queryExecute("select sum(amount) from testQuery", {}, {dbtype="query"});
  writedump(sumSimple); //as expected

/*Sum with a where*/
  SumWithWhere = queryExecute("select sum(amount) from testQuery where 1=1", {}, {dbtype="query"});
  writedump(SumWithWhere); //as expected

/*Sum with an order by*/
  SumWithOrder = queryExecute("select sum(amount) from testQuery order by unrelatedData", {}, {dbtype="query"});
  writedump(SumWithOrder); //as expected

/*Sum with a where AND an order by */
  SumWithOrderAndWhere = queryExecute("select sum(amount) from testQuery where 1=1 order by unrelatedData", {}, {dbtype="query"});
  writedump(SumWithOrderAndWhere); //breaking, behaves as a group by?

/*Updating the ordered value to cause a grouping */
  testQuery.setCell("unrelatedData", 2, 3);
  SumWithOrderAndWhere = queryExecute("select sum(amount) from testQuery where 1=1 order by unrelatedData", {}, {dbtype="query"});
  writedump(SumWithOrderAndWhere); //breaking, behaves as a group by?
</cfscript>

5 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 423

Trending Articles