Add experimental tointeger() and todouble() SQL functions.

FossilOrigin-Name: 465fd853d3e3544cb06b15ffa32ce25774d816c7
This commit is contained in:
mistachkin 2013-03-11 01:23:37 +00:00
parent 3da0df9d18
commit 69f3d04810
4 changed files with 440 additions and 8 deletions

View File

@ -1,5 +1,5 @@
C Add\sa\stest\scase\sfor\sthe\sproblem\sfixed\sby\sthe\sprevious\scommit.
D 2013-03-09T14:49:07.597
C Add\sexperimental\stointeger()\sand\stodouble()\sSQL\sfunctions.
D 2013-03-11T01:23:37.658
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 9a804abbd3cae82d196e4d33aba13239e32522a5
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -133,7 +133,7 @@ F src/delete.c aeabdabeeeaa0584127f291baa9617153d334778
F src/expr.c a23b4aac2a455b2e76b55bef5dcfbe62b665375c
F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb
F src/fkey.c e16942bd5c8a868ac53287886464a5ed0e72b179
F src/func.c 48987c025d69399f59a1c2a553cea5da41bf105d
F src/func.c 82dfd6b6744bd583cb52157ee1496af351ec9d49
F src/global.c e59ecd2c553ad0d4bfbc84ca71231336f8993a7a
F src/hash.c ac3470bbf1ca4ae4e306a8ecb0fdf1731810ffe4
F src/hash.h 2894c932d84d9f892d4b4023a75e501f83050970
@ -511,6 +511,7 @@ F test/full.test 6b3c8fb43c6beab6b95438c1675374b95fab245d
F test/func.test b058483c17952eff7797b837bbb61e27e6b05606
F test/func2.test 772d66227e4e6684b86053302e2d74a2500e1e0f
F test/func3.test 001021e5b88bd02a3b365a5c5fd8f6f49d39744a
F test/func4.test f87bfeee439933de8170e96d2a89db7eaf3b42a5
F test/fuzz-oss1.test 4912e528ec9cf2f42134456933659d371c9e0d74
F test/fuzz.test 77fd50afc12847af50fcf1941679d90adebadde6
F test/fuzz2.test 207d0f9d06db3eaf47a6b7bfc835b8e2fc397167
@ -1037,7 +1038,10 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
P ddee56c9b2b591b9386b1072c3b3a699f7c1f853
R 0685395d6c72f7d3eee9637a399e2144
U dan
Z 77cf7ba50ad60c04d236fa95e2bf9a03
P e899b058a703158012c054974bd9a909d75144d8
R b5370fb9874f2982950e0680a1d54689
T *branch * toTypeFuncs
T *sym-toTypeFuncs *
T -sym-trunk *
U mistachkin
Z ce323d9c5c5aa4b2dbf9898a0e2eb8db

View File

@ -1 +1 @@
e899b058a703158012c054974bd9a909d75144d8
465fd853d3e3544cb06b15ffa32ce25774d816c7

View File

@ -962,6 +962,112 @@ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
}
}
/*
** EXPERIMENTAL - This is not an official function. The interface may
** change. This function may disappear. Do not write code that depends
** on this function.
**
** Implementation of the TOINTEGER() function. This function takes a
** single argument. If the argument is an integer or is a double that
** can be losslessly converted to an integer, the return value is the
** same as the argument. If the argument is a double that cannot be
** losslessly represented as an integer, the return value is undefined.
** If the argument is NULL, the return value is NULL. Otherwise, an
** attempt is made to convert the argument to an integer. If the
** conversion is successful, the integer value is returned; otherwise,
** NULL is returned.
*/
static void tointegerFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
assert( argc==1 );
UNUSED_PARAMETER(argc);
switch( sqlite3_value_type(argv[0]) ){
case SQLITE_FLOAT:
case SQLITE_INTEGER: {
sqlite3_result_int64(context, sqlite3_value_int64(argv[0]));
break;
}
case SQLITE_BLOB:
case SQLITE_TEXT: {
const unsigned char *zStr = sqlite3_value_text(argv[0]);
if( zStr ){
int nStr = sqlite3_value_bytes(argv[0]);
if( nStr ){
i64 iVal;
if( !sqlite3Atoi64(zStr, &iVal, nStr, SQLITE_UTF8) ){
sqlite3_result_int64(context, iVal);
return;
}
}
}
sqlite3_result_null(context);
break;
}
default: {
assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
sqlite3_result_null(context);
break;
}
}
}
/*
** EXPERIMENTAL - This is not an official function. The interface may
** change. This function may disappear. Do not write code that depends
** on this function.
**
** Implementation of the TODOUBLE() function. This function takes a
** single argument. If the argument is a double or is an integer that
** can be losslessly converted to a double, the return value is the
** same as the argument. If the argument is an integer that cannot be
** losslessly represented as a double, the return value is undefined.
** If the argument is NULL, the return value is NULL. Otherwise, an
** attempt is made to convert the argument to a double. If the
** conversion is successful, the double value is returned; otherwise,
** NULL is returned.
*/
#ifndef SQLITE_OMIT_FLOATING_POINT
static void todoubleFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
assert( argc==1 );
UNUSED_PARAMETER(argc);
switch( sqlite3_value_type(argv[0]) ){
case SQLITE_FLOAT:
case SQLITE_INTEGER: {
sqlite3_result_double(context, sqlite3_value_double(argv[0]));
break;
}
case SQLITE_BLOB:
case SQLITE_TEXT: {
const unsigned char *zStr = sqlite3_value_text(argv[0]);
if( zStr ){
int nStr = sqlite3_value_bytes(argv[0]);
if( nStr ){
double rVal;
if( sqlite3AtoF(zStr, &rVal, nStr, SQLITE_UTF8) ){
sqlite3_result_double(context, rVal);
return;
}
}
}
sqlite3_result_null(context);
break;
}
default: {
assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
sqlite3_result_null(context);
break;
}
}
}
#endif
/*
** The unicode() function. Return the integer unicode code-point value
** for the first character of the input string.
@ -1670,6 +1776,10 @@ void sqlite3RegisterGlobalFunctions(void){
FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
FUNCTION(quote, 1, 0, 0, quoteFunc ),
FUNCTION(tointeger, 1, 0, 0, tointegerFunc ),
#ifndef SQLITE_OMIT_FLOATING_POINT
FUNCTION(todouble, 1, 0, 0, todoubleFunc ),
#endif
FUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
FUNCTION(changes, 0, 0, 0, changes ),
FUNCTION(total_changes, 0, 0, 0, total_changes ),

318
test/func4.test Normal file
View File

@ -0,0 +1,318 @@
# 2013 March 10
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing the TOINTEGER() and TODOUBLE()
# functions.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set i 0
do_execsql_test func4-1.[incr i] {
SELECT tointeger(NULL);
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger('');
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(' ');
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger('1234');
} {1234}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(' 1234');
} {1234}
do_execsql_test func4-1.[incr i] {
SELECT tointeger('bad');
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger('0xBAD');
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger('123BAD');
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger('0x123BAD');
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger('123NO');
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger('0x123NO');
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger('-0x1');
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger('-0x0');
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger('0x0');
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger('0x1');
} {{}}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(-1);
} {-1}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(-0);
} {0}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(0);
} {0}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(1);
} {1}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(-1.79769313486232e308 - 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(-1.79769313486232e308);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(-1.79769313486232e308 + 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(-9223372036854775808 - 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(-9223372036854775808);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(-9223372036854775808 + 1);
} {-9223372036854775807}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(-2147483648 - 1);
} {-2147483649}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(-2147483648);
} {-2147483648}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(-2147483648 + 1);
} {-2147483647}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(2147483647 - 1);
} {2147483646}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(2147483647);
} {2147483647}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(2147483647 + 1);
} {2147483648}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(9223372036854775807 - 1);
} {9223372036854775806}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(9223372036854775807);
} {9223372036854775807}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(9223372036854775807 + 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(1.79769313486232e308 - 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(1.79769313486232e308);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(1.79769313486232e308 + 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(4503599627370496 - 1);
} {4503599627370495}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(4503599627370496);
} {4503599627370496}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(4503599627370496 + 1);
} {4503599627370497}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(9007199254740992 - 1);
} {9007199254740991}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(9007199254740992);
} {9007199254740992}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(9007199254740992 + 1);
} {9007199254740993}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(9223372036854775808 - 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(9223372036854775808);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(9223372036854775808 + 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(18446744073709551616 - 1);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(18446744073709551616);
} {-9223372036854775808}
do_execsql_test func4-1.[incr i] {
SELECT tointeger(18446744073709551616 + 1);
} {-9223372036854775808}
ifcapable floatingpoint {
set i 0
do_execsql_test func4-2.[incr i] {
SELECT todouble(NULL);
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble('');
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble(' ');
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble('1234');
} {1234.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(' 1234');
} {1234.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble('bad');
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble('0xBAD');
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble('123BAD');
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble('0x123BAD');
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble('123NO');
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble('0x123NO');
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble('-0x1');
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble('-0x0');
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble('0x0');
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble('0x1');
} {{}}
do_execsql_test func4-2.[incr i] {
SELECT todouble(-1);
} {-1.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(-0);
} {0.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(0);
} {0.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(1);
} {1.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(-1.79769313486232e308 - 1);
} {-Inf}
do_execsql_test func4-2.[incr i] {
SELECT todouble(-1.79769313486232e308);
} {-Inf}
do_execsql_test func4-2.[incr i] {
SELECT todouble(-1.79769313486232e308 + 1);
} {-Inf}
do_execsql_test func4-2.[incr i] {
SELECT todouble(-9223372036854775808 - 1);
} {-9.22337203685478e+18}
do_execsql_test func4-2.[incr i] {
SELECT todouble(-9223372036854775808);
} {-9.22337203685478e+18}
do_execsql_test func4-2.[incr i] {
SELECT todouble(-9223372036854775808 + 1);
} {-9.22337203685478e+18}
do_execsql_test func4-2.[incr i] {
SELECT todouble(-2147483648 - 1);
} {-2147483649.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(-2147483648);
} {-2147483648.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(-2147483648 + 1);
} {-2147483647.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(2147483647 - 1);
} {2147483646.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(2147483647);
} {2147483647.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(2147483647 + 1);
} {2147483648.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(9223372036854775807 - 1);
} {9.22337203685478e+18}
do_execsql_test func4-2.[incr i] {
SELECT todouble(9223372036854775807);
} {9.22337203685478e+18}
do_execsql_test func4-2.[incr i] {
SELECT todouble(9223372036854775807 + 1);
} {9.22337203685478e+18}
do_execsql_test func4-2.[incr i] {
SELECT todouble(1.79769313486232e308 - 1);
} {Inf}
do_execsql_test func4-2.[incr i] {
SELECT todouble(1.79769313486232e308);
} {Inf}
do_execsql_test func4-2.[incr i] {
SELECT todouble(1.79769313486232e308 + 1);
} {Inf}
do_execsql_test func4-2.[incr i] {
SELECT todouble(4503599627370496 - 1);
} {4503599627370500.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(4503599627370496);
} {4503599627370500.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(4503599627370496 + 1);
} {4503599627370500.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(9007199254740992 - 1);
} {9007199254740990.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(9007199254740992);
} {9007199254740990.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(9007199254740992 + 1);
} {9007199254740990.0}
do_execsql_test func4-2.[incr i] {
SELECT todouble(9223372036854775808 - 1);
} {9.22337203685478e+18}
do_execsql_test func4-2.[incr i] {
SELECT todouble(9223372036854775808);
} {9.22337203685478e+18}
do_execsql_test func4-2.[incr i] {
SELECT todouble(9223372036854775808 + 1);
} {9.22337203685478e+18}
do_execsql_test func4-2.[incr i] {
SELECT todouble(18446744073709551616 - 1);
} {1.84467440737096e+19}
do_execsql_test func4-2.[incr i] {
SELECT todouble(18446744073709551616);
} {1.84467440737096e+19}
do_execsql_test func4-2.[incr i] {
SELECT todouble(18446744073709551616 + 1);
} {1.84467440737096e+19}
}
finish_test