Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/ts 4883: Fixed float number truncation in cjson library reading configuration item min or max property #770

Merged
merged 7 commits into from
Jul 4, 2024
3 changes: 3 additions & 0 deletions inc/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,9 @@ typedef struct SField {
StmtData stmtData;
int64_t max;
int64_t min;
double maxInDbl;
double minInDbl;
uint32_t scalingFactor;
tools_cJSON * values;

// fun
Expand Down
22 changes: 22 additions & 0 deletions src/benchData.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,17 @@ float tmpFloatImpl(Field *field, int i, int32_t angle, int32_t k) {
+ floatTmp / 1000000000) / 360);
}
}

if (field->scalingFactor > 0) {
if (field->scalingFactor > 1)
floatTmp = floatTmp / field->scalingFactor;

if (floatTmp > field->maxInDbl)
floatTmp = field->maxInDbl;
else if (floatTmp < field->minInDbl)
floatTmp = field->minInDbl;
}

return floatTmp;
}

Expand All @@ -674,6 +685,17 @@ double tmpDoubleImpl(Field *field, int32_t angle, int32_t k) {
taosRandom() % 1000000 / 1000000.0);
}
}

if (field->scalingFactor > 0) {
if (field->scalingFactor > 1)
doubleTmp = doubleTmp / field->scalingFactor;

if (doubleTmp > field->maxInDbl)
doubleTmp = field->maxInDbl;
else if (doubleTmp < field->minInDbl)
doubleTmp = field->minInDbl;
}

return doubleTmp;
}

Expand Down
64 changes: 62 additions & 2 deletions src/benchJsonOpt.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ static int getColumnAndTagTypeFromInsertJsonFile(
int count = 1;
int64_t max = RAND_MAX >> 1;
int64_t min = 0;
double maxInDbl = max;
double minInDbl = min;
uint32_t scalingFactor = 1;
int32_t length = 4;
// fun type
uint8_t funType = FUNTYPE_NONE;
Expand Down Expand Up @@ -241,17 +244,41 @@ static int getColumnAndTagTypeFromInsertJsonFile(
tools_cJSON *dataMax = tools_cJSON_GetObjectItem(column, "max");
if (tools_cJSON_IsNumber(dataMax)) {
max = dataMax->valueint;
maxInDbl = dataMax->valuedouble;
} else {
max = convertDatatypeToDefaultMax(type);
maxInDbl = max;
}

tools_cJSON *dataMin = tools_cJSON_GetObjectItem(column, "min");
if (tools_cJSON_IsNumber(dataMin)) {
min = dataMin->valueint;
minInDbl = dataMin->valuedouble;
} else {
min = convertDatatypeToDefaultMin(type);
}

minInDbl = min;
}

double valueRange = maxInDbl - minInDbl;
tools_cJSON *dataScalingFactor = tools_cJSON_GetObjectItem(column, "scalingFactor");
if (tools_cJSON_IsNumber(dataScalingFactor)) {
scalingFactor = dataScalingFactor->valueint;
if (scalingFactor > 1) {
max = maxInDbl * scalingFactor;
min = minInDbl * scalingFactor;
} else {
scalingFactor = 1;
}
} else {
if (0 < valueRange && valueRange <= 1) {
scalingFactor = 1000;
max = maxInDbl * scalingFactor;
min = minInDbl * scalingFactor;
} else {
scalingFactor = 1;
}
}

// gen
tools_cJSON *dataGen = tools_cJSON_GetObjectItem(column, "gen");
if (tools_cJSON_IsString(dataGen)) {
Expand Down Expand Up @@ -325,6 +352,9 @@ static int getColumnAndTagTypeFromInsertJsonFile(
col->sma = sma;
col->max = max;
col->min = min;
col->maxInDbl = maxInDbl;
col->minInDbl = minInDbl;
col->scalingFactor = scalingFactor;
col->gen = gen;
col->fillNull = fillNull;
col->values = dataValues;
Expand Down Expand Up @@ -396,6 +426,9 @@ static int getColumnAndTagTypeFromInsertJsonFile(
int count = 1;
int64_t max = RAND_MAX >> 1;
int64_t min = 0;
double maxInDbl = max;
double minInDbl = min;
uint32_t scalingFactor = 1;
int32_t length = 4;
tools_cJSON *tagObj = tools_cJSON_GetArrayItem(tags, k);
if (!tools_cJSON_IsObject(tagObj)) {
Expand Down Expand Up @@ -439,15 +472,39 @@ static int getColumnAndTagTypeFromInsertJsonFile(
tools_cJSON *dataMax = tools_cJSON_GetObjectItem(tagObj, "max");
if (tools_cJSON_IsNumber(dataMax)) {
max = dataMax->valueint;
maxInDbl = dataMax->valuedouble;
} else {
max = convertDatatypeToDefaultMax(type);
maxInDbl = max;
}

tools_cJSON *dataMin = tools_cJSON_GetObjectItem(tagObj, "min");
if (tools_cJSON_IsNumber(dataMin)) {
min = dataMin->valueint;
minInDbl = dataMin->valuedouble;
} else {
min = convertDatatypeToDefaultMin(type);
minInDbl = min;
}

double valueRange = maxInDbl - minInDbl;
tools_cJSON *dataScalingFactor = tools_cJSON_GetObjectItem(tagObj, "scalingFactor");
if (tools_cJSON_IsNumber(dataScalingFactor)) {
scalingFactor = dataScalingFactor->valueint;
if (scalingFactor > 1) {
max = maxInDbl * scalingFactor;
min = minInDbl * scalingFactor;
} else {
scalingFactor = 1;
}
} else {
if (0 < valueRange && valueRange <= 1) {
scalingFactor = 1000;
max = maxInDbl * scalingFactor;
min = minInDbl * scalingFactor;
} else {
scalingFactor = 1;
}
}

tools_cJSON *dataValues = tools_cJSON_GetObjectItem(tagObj, "values");
Expand Down Expand Up @@ -478,6 +535,9 @@ static int getColumnAndTagTypeFromInsertJsonFile(
}
tag->max = max;
tag->min = min;
tag->maxInDbl = maxInDbl;
tag->minInDbl = minInDbl;
tag->scalingFactor = scalingFactor;
tag->values = dataValues;
if (customName) {
if (n >= 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/benchUtil.c
Original file line number Diff line number Diff line change
Expand Up @@ -954,9 +954,9 @@ int64_t convertDatatypeToDefaultMax(uint8_t type) {
ret = 65534;
break;
case TSDB_DATA_TYPE_INT:
case TSDB_DATA_TYPE_DOUBLE:
case TSDB_DATA_TYPE_BIGINT:
case TSDB_DATA_TYPE_FLOAT:
case TSDB_DATA_TYPE_DOUBLE:
ret = RAND_MAX >> 1;
break;
case TSDB_DATA_TYPE_UINT:
Expand Down
Loading