/ Check-in [a8b13002]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Simplifications to the implementation of the sum() SQL function.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:a8b13002378fc4ef0b41c367b44b67bf2b28b6624303c087a3c6d657b5bfe32e
User & Date: drh 2018-07-07 20:55:16
Context
2018-07-08
01:02
Identify specific FuncDef objects for window functions using the pointer to the function name (FuncDef.zName) rather than the pointer to the xStep method. This allows xStep method pointer to be replaced with a single noopStepFunc() procedure, and thus save having lots of different no-op step functions. check-in: 410e13b0 user: drh tags: trunk
2018-07-07
20:55
Simplifications to the implementation of the sum() SQL function. check-in: a8b13002 user: drh tags: trunk
20:26
Fix another problem with reusing LSM cursors for range scans. check-in: ae3fc765 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/func.c.

  1501   1501     type = sqlite3_value_numeric_type(argv[0]);
  1502   1502     if( p && type!=SQLITE_NULL ){
  1503   1503       p->cnt++;
  1504   1504       if( type==SQLITE_INTEGER ){
  1505   1505         i64 v = sqlite3_value_int64(argv[0]);
  1506   1506         p->rSum += v;
  1507   1507         if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
  1508         -        p->overflow = 1;
         1508  +        p->approx = p->overflow = 1;
  1509   1509         }
  1510   1510       }else{
  1511   1511         p->rSum += sqlite3_value_double(argv[0]);
  1512   1512         p->approx = 1;
  1513   1513       }
  1514   1514     }
  1515   1515   }
................................................................................
  1520   1520     assert( argc==1 );
  1521   1521     UNUSED_PARAMETER(argc);
  1522   1522     p = sqlite3_aggregate_context(context, sizeof(*p));
  1523   1523     type = sqlite3_value_numeric_type(argv[0]);
  1524   1524     /* p is always non-NULL because sumStep() will have been called first
  1525   1525     ** to initialize it */
  1526   1526     if( ALWAYS(p) && type!=SQLITE_NULL ){
         1527  +    assert( p->cnt>0 );
  1527   1528       p->cnt--;
  1528         -    if( type==SQLITE_INTEGER ){
         1529  +    assert( type==SQLITE_INTEGER || p->approx );
         1530  +    if( type==SQLITE_INTEGER && p->approx==0 ){
  1529   1531         i64 v = sqlite3_value_int64(argv[0]);
  1530   1532         p->rSum -= v;
  1531         -      if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, -1*v) ){
  1532         -        p->overflow = 1;
  1533         -      }
         1533  +      p->iSum -= v;
  1534   1534       }else{
  1535   1535         p->rSum -= sqlite3_value_double(argv[0]);
  1536         -      p->approx = 1;
  1537   1536       }
  1538   1537     }
  1539   1538   }
  1540   1539   #else
  1541   1540   # define sumInverse 0
  1542   1541   #endif /* SQLITE_OMIT_WINDOWFUNC */
  1543   1542   static void sumFinalize(sqlite3_context *context){