Just you know, if the $sql argument passed have more than two tables in the from, something like
Select * from actual as a, brave as b
It will happen a sql error cause the constructed query here
$sql->select($alias.'.'.$primarykey.' AS JD_MAGIC_KEY_'.$jdkey);
will be something as
select a,.id as JD_MAGIC_KEY_1
I donĀ“t know if this bug only happened to me because we are doing something strange in our module, but here is for you to know

By the way, we solved it with a simple
$alias = str_ireplace(',', '', $alias);
before
if ($alias == '') {
$alias = $tablename;
}