Skip to content

Commit

Permalink
refactor: Change C style casts to C++ style (Part 5) (facebookincubat…
Browse files Browse the repository at this point in the history
…or#11688)

Summary:
As per the security guideline in
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es49-if-you-must-use-a-cast-use-a-named-cast

Covers the findings in velox/tpch

Pull Request resolved: facebookincubator#11688

Reviewed By: xiaoxmeng

Differential Revision: D67060328

Pulled By: bikramSingh91

fbshipit-source-id: bc4657233abf982fbc4426c44b6038b46dd5e15e
  • Loading branch information
aditi-pandit authored and facebook-github-bot committed Dec 16, 2024
1 parent ba43948 commit 4c6ab14
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 118 deletions.
5 changes: 3 additions & 2 deletions velox/connectors/tpch/TpchConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ void TpchDataSource::addSplit(std::shared_ptr<ConnectorSplit> split) {
currentSplit_ = std::dynamic_pointer_cast<TpchConnectorSplit>(split);
VELOX_CHECK(currentSplit_, "Wrong type of split for TpchDataSource.");

size_t partSize =
std::ceil((double)tpchTableRowCount_ / (double)currentSplit_->totalParts);
size_t partSize = std::ceil(
static_cast<double>(tpchTableRowCount_) /
static_cast<double>(currentSplit_->totalParts));

splitOffset_ = partSize * currentSplit_->partNumber;
splitEnd_ = splitOffset_ + partSize;
Expand Down
2 changes: 1 addition & 1 deletion velox/tpch/gen/TpchGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ std::vector<VectorPtr> allocateVectors(
}

double decimalToDouble(int64_t value) {
return (double)value * 0.01;
return static_cast<double>(value) * 0.01;
}

int32_t toDate(std::string_view stringDate) {
Expand Down
17 changes: 10 additions & 7 deletions velox/tpch/gen/dbgen/bm_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ void e_str(distribution* d, int min, int max, seed_t* seed, char* dest) {

tpch_a_rnd(min, max, seed, dest);
pick_str(d, seed, strtmp);
len = (int)strlen(strtmp);
RANDOM(loc, 0, ((int)strlen(dest) - 1 - len), seed);
len = static_cast<int>(strlen(strtmp));
RANDOM(loc, 0, (static_cast<int>(strlen(dest)) - 1 - len), seed);
memcpy(dest + loc, strtmp, sizeof(char) * len);

return;
Expand Down Expand Up @@ -277,12 +277,14 @@ void read_dist(const char* path, const char* name, distribution* target) {

if (!dsscasecmp(token, "count")) {
target->count = weight;
target->list = (set_member*)malloc((size_t)(weight * sizeof(set_member)));
target->list = reinterpret_cast<set_member*>(
malloc(static_cast<size_t>(weight * sizeof(set_member))));
MALLOC_CHECK(target->list);
target->max = 0;
continue;
}
target->list[count].text = (char*)malloc((size_t)((int)strlen(token) + 1));
target->list[count].text = reinterpret_cast<char*>(
malloc(static_cast<size_t>((static_cast<int>(strlen(token)) + 1))));
MALLOC_CHECK(target->list[count].text);
strcpy(target->list[count].text, token);
target->max += weight;
Expand All @@ -295,7 +297,7 @@ void read_dist(const char* path, const char* name, distribution* target) {
fprintf(stderr, "Read error on dist '%s'\n", name);
exit(1);
}
target->permute = (long*)NULL;
target->permute = reinterpret_cast<long*>(NULL);
return;
}

Expand All @@ -315,7 +317,7 @@ void agg_str(distribution* set, long count, seed_t* seed, char* dest) {
strcat(dest, DIST_MEMBER(set, DIST_PERMUTE(d, i)));
strcat(dest, " ");
}
*(dest + (int)strlen(dest) - 1) = '\0';
*(dest + static_cast<int>(strlen(dest)) - 1) = '\0';

return;
}
Expand Down Expand Up @@ -398,7 +400,8 @@ char** mk_ascdate(void) {
dss_time_t t;
DSS_HUGE i;

m = (char**)malloc((size_t)(TOTDATE * sizeof(char*)));
m = reinterpret_cast<char**>(
malloc(static_cast<size_t>(TOTDATE * sizeof(char*))));
MALLOC_CHECK(m);
for (i = 0; i < TOTDATE; i++) {
mk_time(i + 1, &t);
Expand Down
88 changes: 58 additions & 30 deletions velox/tpch/gen/dbgen/build.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright owned by the Transaction Processing Performance Council.
*
Expand Down Expand Up @@ -28,17 +43,28 @@ namespace facebook::velox::tpch::dbgen {
#define JDAY_BASE 8035 /* start from 1/1/70 a la unix */
#define JMNTH_BASE (-70 * 12) /* start from 1/1/70 a la unix */
#define JDAY(date) ((date) - STARTDATE + JDAY_BASE + 1)
#define PART_SUPP_BRIDGE(tgt, p, s) \
{ \
DSS_HUGE tot_scnt = ctx->tdefs[SUPP].base * ctx->scale_factor; \
tgt = (p + s * (tot_scnt / SUPP_PER_PART + (long)((p - 1) / tot_scnt))) % \
tot_scnt + \
1; \
#define PART_SUPP_BRIDGE(tgt, p, s) \
{ \
DSS_HUGE tot_scnt = ctx->tdefs[SUPP].base * ctx->scale_factor; \
tgt = (p + \
s * \
(tot_scnt / SUPP_PER_PART + \
static_cast<long>((p - 1) / tot_scnt))) % \
tot_scnt + \
1; \
}
#define V_STR(avg, seed, tgt) \
tpch_a_rnd((int)(avg * V_STR_LOW), (int)(avg * V_STR_HGH), seed, tgt)
#define TEXT(avg, seed, tgt) \
dbg_text(tgt, (int)(avg * V_STR_LOW), (int)(avg * V_STR_HGH), seed)
#define V_STR(avg, seed, tgt) \
tpch_a_rnd( \
static_cast<int>(avg * V_STR_LOW), \
static_cast<int>(avg * V_STR_HGH), \
seed, \
tgt)
#define TEXT(avg, seed, tgt) \
dbg_text( \
tgt, \
static_cast<int>(avg * V_STR_LOW), \
static_cast<int>(avg * V_STR_HGH), \
seed)
static void gen_phone PROTO((DSS_HUGE ind, char* target, seed_t* seed));

DSS_HUGE
Expand All @@ -59,10 +85,10 @@ static void gen_phone(DSS_HUGE ind, char* target, seed_t* seed) {
RANDOM(exchg, 100, 999, seed);
RANDOM(number, 1000, 9999, seed);

sprintf(target, "%02d", (int)(10 + (ind % NATIONS_MAX)));
sprintf(target + 3, "%03d", (int)acode);
sprintf(target + 7, "%03d", (int)exchg);
sprintf(target + 11, "%04d", (int)number);
sprintf(target, "%02d", static_cast<int>(10 + (ind % NATIONS_MAX)));
sprintf(target + 3, "%03d", static_cast<int>(acode));
sprintf(target + 7, "%03d", static_cast<int>(exchg));
sprintf(target + 11, "%04d", static_cast<int>(number));
target[2] = target[6] = target[10] = '-';

return;
Expand All @@ -80,14 +106,14 @@ long mk_cust(DSS_HUGE n_cust, customer_t* c, DBGenContext* ctx) {
c->custkey = n_cust;
sprintf(c->name, szFormat, C_NAME_TAG, n_cust);
V_STR(C_ADDR_LEN, &ctx->Seed[C_ADDR_SD], c->address);
c->alen = (int)strlen(c->address);
c->alen = static_cast<int>(strlen(c->address));
RANDOM(i, 0, (nations.count - 1), &ctx->Seed[C_NTRG_SD]);
c->nation_code = i;
gen_phone(i, c->phone, &ctx->Seed[C_PHNE_SD]);
RANDOM(c->acctbal, C_ABAL_MIN, C_ABAL_MAX, &ctx->Seed[C_ABAL_SD]);
pick_str(&c_mseg_set, &ctx->Seed[C_MSEG_SD], c->mktsegment);
TEXT(C_CMNT_LEN, &ctx->Seed[C_CMNT_SD], c->comment);
c->clen = (int)strlen(c->comment);
c->clen = static_cast<int>(strlen(c->comment));

return (0);
}
Expand All @@ -99,7 +125,7 @@ void mk_sparse(DSS_HUGE i, DSS_HUGE* ok, long seq) {
long low_bits;

*ok = i;
low_bits = (long)(i & ((1 << SPARSE_KEEP) - 1));
low_bits = static_cast<long>(i & ((1 << SPARSE_KEEP) - 1));
*ok = *ok >> SPARSE_KEEP;
*ok = *ok << SPARSE_BITS;
*ok += seq;
Expand Down Expand Up @@ -155,7 +181,7 @@ long mk_order(DSS_HUGE index, order_t* o, DBGenContext* ctx, long upd_num) {
&ctx->Seed[O_CLRK_SD]);
sprintf(o->clerk, szFormat, O_CLRK_TAG, clk_num);
TEXT(O_CMNT_LEN, &ctx->Seed[O_CMNT_SD], o->comment);
o->clen = (int)strlen(o->comment);
o->clen = static_cast<int>(strlen(o->comment));
#ifdef DEBUG
if (o->clen > O_CMNT_MAX)
fprintf(stderr, "comment error: O%d\n", index);
Expand All @@ -177,7 +203,7 @@ long mk_order(DSS_HUGE index, order_t* o, DBGenContext* ctx, long upd_num) {
pick_str(&l_instruct_set, &ctx->Seed[L_SHIP_SD], o->l[lcnt].shipinstruct);
pick_str(&l_smode_set, &ctx->Seed[L_SMODE_SD], o->l[lcnt].shipmode);
TEXT(L_CMNT_LEN, &ctx->Seed[L_CMNT_SD], o->l[lcnt].comment);
o->l[lcnt].clen = (int)strlen(o->l[lcnt].comment);
o->l[lcnt].clen = static_cast<int>(strlen(o->l[lcnt].comment));
if (ctx->scale_factor >= 30000)
RANDOM64(
o->l[lcnt].partkey, L_PKEY_MIN, L_PKEY_MAX, &ctx->Seed[L_PKEY_SD]);
Expand All @@ -188,9 +214,10 @@ long mk_order(DSS_HUGE index, order_t* o, DBGenContext* ctx, long upd_num) {
PART_SUPP_BRIDGE(o->l[lcnt].suppkey, o->l[lcnt].partkey, supp_num);
o->l[lcnt].eprice = rprice * o->l[lcnt].quantity;

o->totalprice += ((o->l[lcnt].eprice * ((long)100 - o->l[lcnt].discount)) /
(long)PENNIES) *
((long)100 + o->l[lcnt].tax) / (long)PENNIES;
o->totalprice +=
(o->l[lcnt].eprice * (static_cast<long>(100) - o->l[lcnt].discount)) /
static_cast<long>(PENNIES) * (static_cast<long>(100) + o->l[lcnt].tax) /
static_cast<long>(PENNIES);

RANDOM(s_date, L_SDTE_MIN, L_SDTE_MAX, &ctx->Seed[L_SDTE_SD]);
s_date += tmp_date;
Expand Down Expand Up @@ -238,26 +265,27 @@ long mk_part(DSS_HUGE index, part_t* p, DBGenContext* ctx) {
bInit = 1;
}
p->partkey = index;
agg_str(&colors, (long)P_NAME_SCL, &ctx->Seed[P_NAME_SD], p->name);
agg_str(
&colors, static_cast<long>(P_NAME_SCL), &ctx->Seed[P_NAME_SD], p->name);
RANDOM(temp, P_MFG_MIN, P_MFG_MAX, &ctx->Seed[P_MFG_SD]);
sprintf(p->mfgr, szFormat, P_MFG_TAG, temp);
RANDOM(brnd, P_BRND_MIN, P_BRND_MAX, &ctx->Seed[P_BRND_SD]);
sprintf(p->brand, szBrandFormat, P_BRND_TAG, (temp * 10 + brnd));
p->tlen = pick_str(&p_types_set, &ctx->Seed[P_TYPE_SD], p->type);
p->tlen = (int)strlen(p_types_set.list[p->tlen].text);
p->tlen = static_cast<int>(strlen(p_types_set.list[p->tlen].text));
RANDOM(p->size, P_SIZE_MIN, P_SIZE_MAX, &ctx->Seed[P_SIZE_SD]);
pick_str(&p_cntr_set, &ctx->Seed[P_CNTR_SD], p->container);
p->retailprice = rpb_routine(index);
TEXT(P_CMNT_LEN, &ctx->Seed[P_CMNT_SD], p->comment);
p->clen = (int)strlen(p->comment);
p->clen = static_cast<int>(strlen(p->comment));

for (snum = 0; snum < SUPP_PER_PART; snum++) {
p->s[snum].partkey = p->partkey;
PART_SUPP_BRIDGE(p->s[snum].suppkey, index, snum);
RANDOM(p->s[snum].qty, PS_QTY_MIN, PS_QTY_MAX, &ctx->Seed[PS_QTY_SD]);
RANDOM(p->s[snum].scost, PS_SCST_MIN, PS_SCST_MAX, &ctx->Seed[PS_SCST_SD]);
TEXT(PS_CMNT_LEN, &ctx->Seed[PS_CMNT_SD], p->s[snum].comment);
p->s[snum].clen = (int)strlen(p->s[snum].comment);
p->s[snum].clen = static_cast<int>(strlen(p->s[snum].comment));
}
return (0);
}
Expand All @@ -274,14 +302,14 @@ long mk_supp(DSS_HUGE index, supplier_t* s, DBGenContext* ctx) {
s->suppkey = index;
sprintf(s->name, szFormat, S_NAME_TAG, index);
V_STR(S_ADDR_LEN, &ctx->Seed[S_ADDR_SD], s->address);
s->alen = (int)strlen(s->address);
s->alen = static_cast<int>(strlen(s->address));
RANDOM(i, 0, nations.count - 1, &ctx->Seed[S_NTRG_SD]);
s->nation_code = i;
gen_phone(i, s->phone, &ctx->Seed[S_PHNE_SD]);
RANDOM(s->acctbal, S_ABAL_MIN, S_ABAL_MAX, &ctx->Seed[S_ABAL_SD]);

TEXT(S_CMNT_LEN, &ctx->Seed[S_CMNT_SD], s->comment);
s->clen = (int)strlen(s->comment);
s->clen = static_cast<int>(strlen(s->comment));
/*
* these calls should really move inside the if stmt below, but this
* will simplify seedless parallel load
Expand Down Expand Up @@ -353,7 +381,7 @@ int mk_nation(DSS_HUGE index, code_t* c, DBGenContext* ctx) {
c->text = nations.list[index - 1].text;
c->join = nations.list[index - 1].weight;
TEXT(N_CMNT_LEN, &ctx->Seed[N_CMNT_SD], c->comment);
c->clen = (int)strlen(c->comment);
c->clen = static_cast<int>(strlen(c->comment));
return (0);
}

Expand All @@ -362,7 +390,7 @@ int mk_region(DSS_HUGE index, code_t* c, DBGenContext* ctx) {
c->text = regions.list[index - 1].text;
c->join = 0; /* for completeness */
TEXT(R_CMNT_LEN, &ctx->Seed[R_CMNT_SD], c->comment);
c->clen = (int)strlen(c->comment);
c->clen = static_cast<int>(strlen(c->comment));
return (0);
}

Expand Down
2 changes: 1 addition & 1 deletion velox/tpch/gen/dbgen/include/dbgen/dss.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ typedef struct {
* some handy access functions
*/
#define DIST_SIZE(d) d->count
#define DIST_MEMBER(d, i) ((set_member*)((d)->list + i))->text
#define DIST_MEMBER(d, i) (reinterpret_cast<set_member*>((d)->list + i))->text
#define DIST_PERMUTE(d, i) (d->permute[i])

typedef struct {
Expand Down
Loading

0 comments on commit 4c6ab14

Please sign in to comment.