php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #29266 sqlite_create_aggregate() can't return any php type
Submitted: 2004-07-19 22:27 UTC Modified: 2004-09-18 22:02 UTC
From: ozy at ozy dot student dot utwente dot nl Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.0.0 OS: Mac OS X
Private report: No CVE-ID: None
 [2004-07-19 22:27 UTC] ozy at ozy dot student dot utwente dot nl
Description:
------------
[code]
function array_finalize(&$context) {
  if (isset($context)) {
    return $context;
  }
  return array();
}
[/code]
see reproduce code for full snip. This is part of an 
sqlite_create_aggregate().

seems perfectly legal, but the array_finalize(&$context) 
can't return an array. A returned array from 
sqlite_fetch_array() will explicitly hold a NULL value 
instead of our array from our custom function.

If you can't return an array, please tell so in the 
manual, if you are supposed to be able to return an 
array, please fix this.

changing the finalize code into:
[code]
function array_finalize(&$context) {
  if (isset($context)) {
    return serialize($context);
  }
  return serialize(array());
}
[/code]
does work perfectly (if you use unserialize on the other 
end of-course...)

Reproduce code:
---------------
function array_step(&$context, $element) {
  if (!is_array($context)) $context = array();
  if (isset($element)) {
    $e = sqlite_udf_decode_binary($element);
    if (!in_array($e, $context)) { // UNIQUE
      $context[] = $e;
    }
  }
}
  
function array_finalize(&$context) {
  if (isset($context)) {
    return $context;
  }
  return array();
}
  
sqlite_create_aggregate($db, 'ARRAY', 'array_step', 'array_finalize');

$q = "SELECT name, ARRAY(friends) as friends FROM friends GROUP BY name"; 
// just an example of a friends table (name,friendsname)
$r = sqlite_query($db, $q);
while ($a = sqlite_fetch_array($r, SQLITE_ASSOC)) {
    foreach($a['friends'] as $friend) tellFriend($friend); // whatever
}

Expected result:
----------------
tellFriends gets executed per friend of one person. Then 
we while up to the next result and tellFriends gets 
executed per friend of the next person ... etc etc

Actual result:
--------------
$a['friends']=NULL
(explicitly set to NULL)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-19 23:14 UTC] wez@php.net
There is no array type in SQL, so, of course this will not work.
Your finalize function, along with any other UDF, needs to return a scalar type.
 [2004-09-18 22:02 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"Callback functions should return a type understood by SQLite (not array for example)."
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 05:01:34 2025 UTC