Commit 848a1a61 authored by Olivier Bertrand's avatar Olivier Bertrand

Fix decimal problems in bson udf's

parent 7edd4294
...@@ -1501,31 +1501,27 @@ double BJSON::GetDouble(PBVAL vp) ...@@ -1501,31 +1501,27 @@ double BJSON::GetDouble(PBVAL vp)
PBVAL vlp = (vp->Type == TYPE_JVAL) ? MVP(vp->To_Val) : vp; PBVAL vlp = (vp->Type == TYPE_JVAL) ? MVP(vp->To_Val) : vp;
switch (vlp->Type) { switch (vlp->Type) {
case TYPE_DBL: case TYPE_DBL:
d = *(double*)MP(vlp->To_Val); d = *(double*)MP(vlp->To_Val);
break; break;
case TYPE_BINT: case TYPE_BINT:
d = (double)*(longlong*)MP(vlp->To_Val); d = (double)*(longlong*)MP(vlp->To_Val);
break; break;
case TYPE_INTG: case TYPE_INTG:
d = (double)vlp->N; d = (double)vlp->N;
break; break;
case TYPE_FLOAT: case TYPE_FLOAT:
{ char buf[32]; d = (double)vlp->F;
int n = (vlp->Nd) ? vlp->Nd : 5; break;
case TYPE_DTM:
sprintf(buf, "%.*f", n, vlp->F); case TYPE_STRG:
d = atof(buf); d = atof(MZP(vlp->To_Val));
} break; break;
case TYPE_DTM: case TYPE_BOOL:
case TYPE_STRG: d = (vlp->B) ? 1.0 : 0.0;
d = atof(MZP(vlp->To_Val)); break;
break; default:
case TYPE_BOOL: d = 0.0;
d = (vlp->B) ? 1.0 : 0.0;
break;
default:
d = 0.0;
} // endswitch Type } // endswitch Type
return d; return d;
......
...@@ -116,7 +116,7 @@ BJNX::BJNX(PGLOBAL g) : BDOC(g) ...@@ -116,7 +116,7 @@ BJNX::BJNX(PGLOBAL g) : BDOC(g)
Value = NULL; Value = NULL;
MulVal = NULL; MulVal = NULL;
Jpath = NULL; Jpath = NULL;
Buf_Type = TYPE_NULL; Buf_Type = TYPE_STRING;
Long = len; Long = len;
Prec = 0; Prec = 0;
Nod = 0; Nod = 0;
...@@ -171,10 +171,9 @@ BJNX::BJNX(PGLOBAL g, PBVAL row, int type, int len, int prec, my_bool wr) : BDOC ...@@ -171,10 +171,9 @@ BJNX::BJNX(PGLOBAL g, PBVAL row, int type, int len, int prec, my_bool wr) : BDOC
my_bool BJNX::SetJpath(PGLOBAL g, char* path, my_bool jb) my_bool BJNX::SetJpath(PGLOBAL g, char* path, my_bool jb)
{ {
// Check Value was allocated // Check Value was allocated
if (!Value) if (Value)
return true; Value->SetNullable(true);
Value->SetNullable(true);
Jpath = path; Jpath = path;
// Parse the json path // Parse the json path
...@@ -697,8 +696,10 @@ PVAL BJNX::GetCalcValue(PGLOBAL g, PBVAL bap, int n) ...@@ -697,8 +696,10 @@ PVAL BJNX::GetCalcValue(PGLOBAL g, PBVAL bap, int n)
if (IsTypeChar(Buf_Type)) { if (IsTypeChar(Buf_Type)) {
type = TYPE_DOUBLE; type = TYPE_DOUBLE;
prec = 2; prec = 2;
} else } else {
type = Buf_Type; type = Buf_Type;
prec = GetPrecision();
} // endif Buf_Type
break; break;
case OP_MIN: case OP_MIN:
...@@ -711,7 +712,7 @@ PVAL BJNX::GetCalcValue(PGLOBAL g, PBVAL bap, int n) ...@@ -711,7 +712,7 @@ PVAL BJNX::GetCalcValue(PGLOBAL g, PBVAL bap, int n)
type = TYPE_STRING; type = TYPE_STRING;
if (IsTypeChar(Buf_Type)) { if (IsTypeChar(Buf_Type)) {
lng = Long; lng = (Long) ? Long : 512;
prec = GetPrecision(); prec = GetPrecision();
} else } else
lng = 512; lng = 512;
...@@ -740,79 +741,87 @@ PVAL BJNX::CalculateArray(PGLOBAL g, PBVAL bap, int n) ...@@ -740,79 +741,87 @@ PVAL BJNX::CalculateArray(PGLOBAL g, PBVAL bap, int n)
vp->Reset(); vp->Reset();
xtrc(1, "CalculateArray size=%d op=%d\n", ars, op); xtrc(1, "CalculateArray size=%d op=%d\n", ars, op);
for (i = 0; i < ars; i++) { try {
bvrp = GetArrayValue(bap, i); for (i = 0; i < ars; i++) {
xtrc(1, "i=%d nv=%d\n", i, nv); bvrp = GetArrayValue(bap, i);
xtrc(1, "i=%d nv=%d\n", i, nv);
if (!IsValueNull(bvrp) || (op == OP_CNC && GetJsonNull())) {
if (IsValueNull(bvrp)) { if (!IsValueNull(bvrp) || (op == OP_CNC && GetJsonNull())) {
SetString(bvrp, NewStr(GetJsonNull()), 0); if (IsValueNull(bvrp)) {
bvp = bvrp; SetString(bvrp, NewStr(GetJsonNull()), 0);
} else if (n < Nod - 1 && IsJson(bvrp)) { bvp = bvrp;
SetValue(&bval, GetColumnValue(g, bvrp, n + 1)); } else if (n < Nod - 1 && IsJson(bvrp)) {
bvp = &bval; SetValue(&bval, GetColumnValue(g, bvrp, n + 1));
} else bvp = &bval;
bvp = bvrp; } else
bvp = bvrp;
if (trace(1)) if (trace(1))
htrc("bvp=%s null=%d\n", htrc("bvp=%s null=%d\n",
GetString(bvp), IsValueNull(bvp) ? 1 : 0); GetString(bvp), IsValueNull(bvp) ? 1 : 0);
if (!nv++) { if (!nv++) {
SetJsonValue(g, vp, bvp); SetJsonValue(g, vp, bvp);
continue; continue;
} else } else
SetJsonValue(g, mulval, bvp); SetJsonValue(g, mulval, bvp);
if (!mulval->IsNull()) {
switch (op) {
case OP_CNC:
if (Nodes[n].CncVal) {
val[0] = Nodes[n].CncVal;
err = vp->Compute(g, val, 1, op);
} // endif CncVal
if (!mulval->IsNull()) { val[0] = mulval;
switch (op) {
case OP_CNC:
if (Nodes[n].CncVal) {
val[0] = Nodes[n].CncVal;
err = vp->Compute(g, val, 1, op); err = vp->Compute(g, val, 1, op);
} // endif CncVal break;
// case OP_NUM:
case OP_SEP:
val[0] = vp;
val[1] = mulval;
err = vp->Compute(g, val, 2, OP_ADD);
break;
default:
val[0] = vp;
val[1] = mulval;
err = vp->Compute(g, val, 2, op);
} // endswitch Op
val[0] = mulval; if (err)
err = vp->Compute(g, val, 1, op); vp->Reset();
break;
// case OP_NUM:
case OP_SEP:
val[0] = vp;
val[1] = mulval;
err = vp->Compute(g, val, 2, OP_ADD);
break;
default:
val[0] = vp;
val[1] = mulval;
err = vp->Compute(g, val, 2, op);
} // endswitch Op
if (err) if (trace(1)) {
vp->Reset(); char buf(32);
if (trace(1)) { htrc("vp='%s' err=%d\n",
char buf(32); vp->GetCharString(&buf), err ? 1 : 0);
} // endif trace
htrc("vp='%s' err=%d\n", } // endif Zero
vp->GetCharString(&buf), err ? 1 : 0);
} // endif trace
} // endif Zero } // endif jvrp
} // endif jvrp } // endfor i
} // endfor i if (op == OP_SEP) {
// Calculate average
mulval->SetValue(nv);
val[0] = vp;
val[1] = mulval;
if (op == OP_SEP) { if (vp->Compute(g, val, 2, OP_DIV))
// Calculate average vp->Reset();
mulval->SetValue(nv);
val[0] = vp;
val[1] = mulval;
if (vp->Compute(g, val, 2, OP_DIV)) } // endif Op
vp->Reset();
} // endif Op } catch (int n) {
xtrc(1, "Exception %d: %s\n", n, g->Message);
PUSH_WARNING(g->Message);
} catch (const char* msg) {
strcpy(g->Message, msg);
} // end catch
return vp; return vp;
} // end of CalculateArray } // end of CalculateArray
...@@ -4024,8 +4033,8 @@ double bsonget_real(UDF_INIT *initid, UDF_ARGS *args, ...@@ -4024,8 +4033,8 @@ double bsonget_real(UDF_INIT *initid, UDF_ARGS *args,
char *p, *path; char *p, *path;
double d; double d;
PBVAL jsp, jvp; PBVAL jsp, jvp;
PBJNX bxp = NULL;
PGLOBAL g = (PGLOBAL)initid->ptr; PGLOBAL g = (PGLOBAL)initid->ptr;
BJNX bnx(g);
if (g->N) { if (g->N) {
if (!g->Activityp) { if (!g->Activityp) {
...@@ -4044,8 +4053,6 @@ double bsonget_real(UDF_INIT *initid, UDF_ARGS *args, ...@@ -4044,8 +4053,6 @@ double bsonget_real(UDF_INIT *initid, UDF_ARGS *args,
*is_null = 1; *is_null = 1;
return 0.0; return 0.0;
} else { } else {
BJNX bnx(g);
jvp = bnx.MakeValue(args, 0); jvp = bnx.MakeValue(args, 0);
if ((p = bnx.GetString(jvp))) { if ((p = bnx.GetString(jvp))) {
...@@ -4068,21 +4075,22 @@ double bsonget_real(UDF_INIT *initid, UDF_ARGS *args, ...@@ -4068,21 +4075,22 @@ double bsonget_real(UDF_INIT *initid, UDF_ARGS *args,
jsp = (PBVAL)g->Xchk; jsp = (PBVAL)g->Xchk;
path = MakePSZ(g, args, 1); path = MakePSZ(g, args, 1);
bxp = new(g) BJNX(g, jsp, TYPE_DOUBLE); //bxp = new(g) BJNX(g, jsp, TYPE_DOUBLE, 32, jsp->Nd);
if (bxp->SetJpath(g, path)) { if (bnx.SetJpath(g, path)) {
PUSH_WARNING(g->Message); PUSH_WARNING(g->Message);
*is_null = 1; *is_null = 1;
return 0.0; return 0.0;
} else } else
bxp->ReadValue(g); jvp = bnx.GetRowValue(g, jsp, 0);
if (bxp->GetValue()->IsNull()) { if (!jvp || bnx.IsValueNull(jvp)) {
*is_null = 1; *is_null = 1;
return 0.0; return 0.0;
} // endif IsNull } else if (args->arg_count == 2) {
d = atof(bnx.GetString(jvp));
d = bxp->GetValue()->GetFloatValue(); } else
d = bnx.GetDouble(jvp);
if (initid->const_item) { if (initid->const_item) {
// Keep result of constant function // Keep result of constant function
......
...@@ -279,7 +279,7 @@ SELECT * FROM t1; ...@@ -279,7 +279,7 @@ SELECT * FROM t1;
WHO WEEKS SUMS SUM AVGS SUMAVG AVGSUM AVGAVG AVERAGE WHO WEEKS SUMS SUM AVGS SUMAVG AVGSUM AVGAVG AVERAGE
Joe 3, 4, 5 69.00+83.00+26.00 178.00 17.25+16.60+13.00 46.85 59.33 15.62 16.18 Joe 3, 4, 5 69.00+83.00+26.00 178.00 17.25+16.60+13.00 46.85 59.33 15.62 16.18
Beth 3, 4, 5 16.00+32.00+32.00 80.00 16.00+16.00+16.00 48.00 26.67 16.00 16.00 Beth 3, 4, 5 16.00+32.00+32.00 80.00 16.00+16.00+16.00 48.00 26.67 16.00 16.00
Janet 3, 4, 5 55.00+17.00+57.00 129.00 18.33+17.00+14.25 49.58 43.00 16.53 16.13 Janet 3, 4, 5 55.00+17.00+57.00 129.00 18.33+17.00+14.25 49.58 43.00 16.53 16.12
DROP TABLE t1; DROP TABLE t1;
# #
# Expand expense in 3 one week tables # Expand expense in 3 one week tables
......
...@@ -606,14 +606,12 @@ Bson_File('test/fx.json', 0) ...@@ -606,14 +606,12 @@ Bson_File('test/fx.json', 0)
[{"_id":5,"type":"food","item":"beer","taste":"light","price":5.65,"ratings":[5,8,9]},{"_id":6,"type":"car","item":"roadster","mileage":56000,"ratings":[6,9]},{"_id":7,"type":"food","item":"meat","origin":"argentina","ratings":[2,4]},{"_id":8,"type":"furniture","item":"table","size":{"W":60,"L":80,"H":40},"ratings":[5,8,7]}] [{"_id":5,"type":"food","item":"beer","taste":"light","price":5.65,"ratings":[5,8,9]},{"_id":6,"type":"car","item":"roadster","mileage":56000,"ratings":[6,9]},{"_id":7,"type":"food","item":"meat","origin":"argentina","ratings":[2,4]},{"_id":8,"type":"furniture","item":"table","size":{"W":60,"L":80,"H":40},"ratings":[5,8,7]}]
SELECT Bson_File('test/fx.json', '0'); SELECT Bson_File('test/fx.json', '0');
Bson_File('test/fx.json', '0') Bson_File('test/fx.json', '0')
NULL {"_id":5,"type":"food","item":"beer","taste":"light","price":5.65,"ratings":[5,8,9]}
Warnings:
Warning 1105
SELECT Bson_File('test/fx.json', '[?]'); SELECT Bson_File('test/fx.json', '[?]');
Bson_File('test/fx.json', '[?]') Bson_File('test/fx.json', '[?]')
NULL NULL
Warnings: Warnings:
Warning 1105 Warning 1105 Invalid function specification ?
SELECT BsonGet_String(Bson_File('test/fx.json'), '1.*'); SELECT BsonGet_String(Bson_File('test/fx.json'), '1.*');
BsonGet_String(Bson_File('test/fx.json'), '1.*') BsonGet_String(Bson_File('test/fx.json'), '1.*')
{"_id":6,"type":"car","item":"roadster","mileage":56000,"ratings":[6,9]} {"_id":6,"type":"car","item":"roadster","mileage":56000,"ratings":[6,9]}
...@@ -628,57 +626,33 @@ Price ...@@ -628,57 +626,33 @@ Price
5.65 5.65
SELECT Bson_Array_Add(Bson_File('test/fx.json', '2'), 6, 'ratings'); SELECT Bson_Array_Add(Bson_File('test/fx.json', '2'), 6, 'ratings');
Bson_Array_Add(Bson_File('test/fx.json', '2'), 6, 'ratings') Bson_Array_Add(Bson_File('test/fx.json', '2'), 6, 'ratings')
NULL {"_id":7,"type":"food","item":"meat","origin":"argentina","ratings":[2,4,6]}
Warnings:
Warning 1105
Warning 1105 No sub-item at 'ratings'
SELECT Bson_Array_Add(Bson_File('test/fx.json', '2'), 6, 1, 'ratings'); SELECT Bson_Array_Add(Bson_File('test/fx.json', '2'), 6, 1, 'ratings');
Bson_Array_Add(Bson_File('test/fx.json', '2'), 6, 1, 'ratings') Bson_Array_Add(Bson_File('test/fx.json', '2'), 6, 1, 'ratings')
NULL {"_id":7,"type":"food","item":"meat","origin":"argentina","ratings":[2,6,4]}
Warnings:
Warning 1105
Warning 1105 No sub-item at 'ratings'
SELECT Bson_Array_Add(Bson_File('test/fx.json', '2'), 6, 'ratings', 1); SELECT Bson_Array_Add(Bson_File('test/fx.json', '2'), 6, 'ratings', 1);
Bson_Array_Add(Bson_File('test/fx.json', '2'), 6, 'ratings', 1) Bson_Array_Add(Bson_File('test/fx.json', '2'), 6, 'ratings', 1)
NULL {"_id":7,"type":"food","item":"meat","origin":"argentina","ratings":[2,6,4]}
Warnings:
Warning 1105
Warning 1105 No sub-item at 'ratings'
SELECT Bson_Array_Add(Bson_File('test/fx.json', '2.ratings'), 6, 0); SELECT Bson_Array_Add(Bson_File('test/fx.json', '2.ratings'), 6, 0);
Bson_Array_Add(Bson_File('test/fx.json', '2.ratings'), 6, 0) Bson_Array_Add(Bson_File('test/fx.json', '2.ratings'), 6, 0)
[6,null] [6,2,4]
Warnings:
Warning 1105
SELECT Bson_Array_Delete(Bson_File('test/fx.json', '2'), 'ratings', 1); SELECT Bson_Array_Delete(Bson_File('test/fx.json', '2'), 'ratings', 1);
Bson_Array_Delete(Bson_File('test/fx.json', '2'), 'ratings', 1) Bson_Array_Delete(Bson_File('test/fx.json', '2'), 'ratings', 1)
NULL {"_id":7,"type":"food","item":"meat","origin":"argentina","ratings":[2]}
Warnings:
Warning 1105
Warning 1105 No sub-item at 'ratings'
SELECT Bson_Object_Add(Bson_File('test/fx.json', '2'), 'france' origin); SELECT Bson_Object_Add(Bson_File('test/fx.json', '2'), 'france' origin);
Bson_Object_Add(Bson_File('test/fx.json', '2'), 'france' origin) Bson_Object_Add(Bson_File('test/fx.json', '2'), 'france' origin)
NULL {"_id":7,"type":"food","item":"meat","origin":"france","ratings":[2,4]}
Warnings:
Warning 1105
Warning 1105 First argument target is not an object
SELECT Bson_Object_Add(Bson_File('test/fx.json', '2'), 70 H, 'size'); SELECT Bson_Object_Add(Bson_File('test/fx.json', '2'), 70 H, 'size');
Bson_Object_Add(Bson_File('test/fx.json', '2'), 70 H, 'size') Bson_Object_Add(Bson_File('test/fx.json', '2'), 70 H, 'size')
NULL {"_id":7,"type":"food","item":"meat","origin":"argentina","ratings":[2,4]}
Warnings: Warnings:
Warning 1105
Warning 1105 No sub-item at 'size' Warning 1105 No sub-item at 'size'
SELECT Bson_Object_Add(Bson_File('test/fx.json', '3'), 70 H, 'size'); SELECT Bson_Object_Add(Bson_File('test/fx.json', '3'), 70 H, 'size');
Bson_Object_Add(Bson_File('test/fx.json', '3'), 70 H, 'size') Bson_Object_Add(Bson_File('test/fx.json', '3'), 70 H, 'size')
NULL {"_id":8,"type":"furniture","item":"table","size":{"W":60,"L":80,"H":70},"ratings":[5,8,7]}
Warnings:
Warning 1105
Warning 1105 No sub-item at 'size'
SELECT Bson_Object_List(Bson_File('test/fx.json', '3.size')); SELECT Bson_Object_List(Bson_File('test/fx.json', '3.size'));
Bson_Object_List(Bson_File('test/fx.json', '3.size')) Bson_Object_List(Bson_File('test/fx.json', '3.size'))
NULL ["W","L","H"]
Warnings:
Warning 1105
Warning 1105 First argument is not an object
# #
# Testing new functions # Testing new functions
# #
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment