Query Building in ActiveRecord issues

Hello everyone,

I am building an ActiveRecord database adapter for a column store,
open source DBMS.

When running the calculations_test unit test the following query is
generated :

SELECT COUNT(*) AS count_all FROM (SELECT DISTINCT "accounts".id FROM
"accounts" LEFT OUTER JOIN "companies" ON "companies".id =
"accounts".firm_id AND "companies"."type" = 'Firm' WHERE
(companies.id > 1) )

The problem is that I get this error message from my DBMS
!subquery table reference needs alias, use AS xxx in: "select count(*)
as count_all from (select distinct "accounts".id from "accounts" left
outer join "companies" on "companies".id = "accounts".firm_id and
"companies"."type" = 'Firm' where (companies.id > 1) );

which practically means that the query should be like SELECT COUNT(*)
AS count_all FROM (SELECT DISTINCT "accounts".id FROM "accounts" LEFT
OUTER JOIN "companies" ON "companies".id = "accounts".firm_id AND
"companies"."type" = 'Firm' WHERE (companies.id > 1) AS
whatever_string_here)

Moreover for the same unit test another faulty(for the specific DBMS
at least) query is generated :

SELECT count(*) AS count_all, UPPER(companies."type") AS
upper_companies_type FROM "companies" GROUP BY
UPPER(companies."type")

where I get :

!syntax error, unexpected '(', expecting SCOLON in: "select count(*)
as count_all, upper(companies."type") as upper_companies_type from
"companies" group by upper("

This query works if it's formatted like this :
SELECT count(*) AS count_all, UPPER(companies."type") AS
upper_companies_type FROM "companies" GROUP BY "type";

I'm asking if you can point me to where this code is generated in the
ActiveRecord source code so that I try to modify it.

Thanks in advance and hope it's not too much trouble,
Michalis