Skip to content

Commit 3b26419

Browse files
committed
[GLUTEN_8475][VL] Fix C-style casts to C++-style
1 parent 1615e84 commit 3b26419

13 files changed

+47
-36
lines changed

cpp/core/benchmarks/CompressionBenchmark.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class BenchmarkCompression {
109109
setCpu(state.range(2) + state.thread_index());
110110
auto ipcWriteOptions = arrow::ipc::IpcWriteOptions::Defaults();
111111
ipcWriteOptions.use_threads = false;
112-
auto compressBufferSize = (uint32_t)state.range(1);
112+
auto compressBufferSize = static_cast<uint32_t>(state.range(1));
113113
auto compressionType = state.range(0);
114114
switch (compressionType) {
115115
case gluten::kLZ4: {
@@ -248,7 +248,7 @@ class BenchmarkCompression {
248248
GLUTEN_ASSIGN_OR_THROW(
249249
auto len,
250250
codec->Decompress(buffers[j]->size() - 8, buffers[j]->data() + 8, outputSize, out->mutable_data()));
251-
(void)len;
251+
static_cast<void>(len);
252252
}
253253
}
254254
}

cpp/core/memory/MemoryAllocator.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ bool StdMemoryAllocator::reallocateAligned(void* p, uint64_t alignment, int64_t
161161
return false;
162162
}
163163
if (newSize <= size) {
164-
auto aligned = ROUND_TO_LINE(newSize, alignment);
164+
auto aligned = ROUND_TO_LINE(static_cast<uint64_t>(newSize), alignment);
165165
if (aligned <= size) {
166166
// shrink-to-fit
167167
return reallocate(p, size, aligned, out);

cpp/core/shuffle/Spill.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Spill::Spill(Spill::SpillType type) : type_(type) {}
2525

2626
Spill::~Spill() {
2727
if (is_) {
28-
(void)is_->Close();
28+
static_cast<void>(is_->Close());
2929
}
3030
}
3131

cpp/core/utils/qat/QatCodec.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class QatZipCodec : public arrow::util::Codec {
3838
explicit QatZipCodec(int compressionLevel) : compressionLevel_(compressionLevel) {}
3939

4040
~QatZipCodec() {
41-
(void)qzTeardownSession(&qzSession_);
42-
(void)qzClose(&qzSession_);
41+
static_cast<void>(qzTeardownSession(&qzSession_));
42+
static_cast<void>(qzClose(&qzSession_));
4343
}
4444

4545
arrow::Result<int64_t> Decompress(int64_t inputLen, const uint8_t* input, int64_t outputLen, uint8_t* output)

cpp/core/utils/qpl/QplCodec.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class QplGzipCodec final : public arrow::util::Codec {
203203
int64_t MaxCompressedLen(int64_t input_len, const uint8_t* ARROW_ARG_UNUSED(input)) override {
204204
ARROW_DCHECK_GE(input_len, 0);
205205
/// Aligned with ZLIB
206-
return ((input_len) + ((input_len) >> 12) + ((input_len) >> 14) + ((input_len) >> 25) + 13);
206+
return ((input_len) + ((input_len) >> 12) + ((input_len) >> 14) + ((input_len) >> 25) + static_cast<int64_t>(13));
207207
}
208208

209209
arrow::Result<std::shared_ptr<arrow::util::Compressor>> MakeCompressor() override {

cpp/velox/jni/JniFileSystem.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,12 @@ class JniFileSystem : public facebook::velox::filesystems::FileSystem {
338338
JNIEnv* env = nullptr;
339339
attachCurrentThreadAsDaemonOrThrow(vm, &env);
340340
std::vector<std::string> out;
341-
jobjectArray jarray = (jobjectArray)env->CallObjectMethod(obj_, jniFileSystemList, createJString(env, path));
341+
jobjectArray jarray =
342+
static_cast<jobjectArray>(env->CallObjectMethod(obj_, jniFileSystemList, createJString(env, path)));
342343
checkException(env);
343344
jsize length = env->GetArrayLength(jarray);
344345
for (jsize i = 0; i < length; ++i) {
345-
jstring element = (jstring)env->GetObjectArrayElement(jarray, i);
346+
jstring element = static_cast<jstring>(env->GetObjectArrayElement(jarray, i));
346347
std::string cElement = jStringToCString(env, element);
347348
out.push_back(cElement);
348349
}

cpp/velox/operators/serializer/VeloxColumnarToRowConverter.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void VeloxColumnarToRowConverter::convert(std::shared_ptr<ColumnarBatch> cb, int
8080

8181
size_t offset = 0;
8282
for (auto i = 0; i < numRows_; ++i) {
83-
auto rowSize = fast_->serialize(startRow + i, (char*)(bufferAddress_ + offset));
83+
auto rowSize = fast_->serialize(startRow + i, reinterpret_cast<char*>(bufferAddress_ + offset));
8484
lengths_[i] = rowSize;
8585
if (i > 0) {
8686
offsets_[i] = offsets_[i - 1] + lengths_[i - 1];

cpp/velox/operators/serializer/VeloxRowToColumnarConverter.cc

+14-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ inline int64_t getFieldOffset(int64_t nullBitsetWidthInBytes, int32_t index) {
3535
inline bool isNull(uint8_t* buffer_address, int32_t index) {
3636
int64_t mask = 1L << (index & 0x3f); // mod 64 and shift
3737
int64_t wordOffset = (index >> 6) * 8;
38-
int64_t value = *((int64_t*)(buffer_address + wordOffset));
38+
int64_t* valuePtr = buffer_address + wordOffset;
39+
int64_t value = *static_cast<int64_t*>(valuePtr);
3940
return (value & mask) != 0;
4041
}
4142

@@ -51,7 +52,8 @@ int32_t getTotalStringSize(
5152
continue;
5253
}
5354

54-
int64_t offsetAndSize = *(int64_t*)(memoryAddress + offsets[pos] + fieldOffset);
55+
int64_t* offsetAndSizePtr = reinterpret_cast<int64_t*>(memoryAddress + offsets[pos] + fieldOffset);
56+
int64_t offsetAndSize = *(offsetAndSizePtr);
5557
int32_t length = static_cast<int32_t>(offsetAndSize);
5658
if (!StringView::isInline(length)) {
5759
size += length;
@@ -98,11 +100,12 @@ VectorPtr createFlatVector<TypeKind::HUGEINT>(
98100
auto column = BaseVector::create<FlatVector<int128_t>>(type, numRows, pool);
99101
auto rawValues = column->mutableRawValues<uint8_t>();
100102
auto typeWidth = sizeof(int128_t);
101-
auto shift = __builtin_ctz((uint32_t)typeWidth);
103+
auto shift = __builtin_ctz(static_cast<uint32_t>(typeWidth));
102104
for (auto pos = 0; pos < numRows; pos++) {
103105
if (!isNull(memoryAddress + offsets[pos], columnIdx)) {
104106
uint8_t* destptr = rawValues + (pos << shift);
105-
int64_t offsetAndSize = *(int64_t*)(memoryAddress + offsets[pos] + fieldOffset);
107+
int64_t* offsetAndSizePtr = reinterpret_cast<int64_t*>(memoryAddress + offsets[pos] + fieldOffset);
108+
int64_t offsetAndSize = *offsetAndSizePtr;
106109
int32_t length = static_cast<int32_t>(offsetAndSize);
107110
int32_t wordoffset = static_cast<int32_t>(offsetAndSize >> 32);
108111
uint8_t bytesValue[length];
@@ -111,7 +114,7 @@ VectorPtr createFlatVector<TypeKind::HUGEINT>(
111114
for (int k = length - 1; k >= 0; k--) {
112115
bytesValue2[length - 1 - k] = bytesValue[k];
113116
}
114-
if (int8_t(bytesValue[0]) < 0) {
117+
if (static_cast<int8_t>(bytesValue[0]) < 0) {
115118
memset(bytesValue2 + length, 255, 16 - length);
116119
}
117120
memcpy(destptr, bytesValue2, typeWidth);
@@ -135,7 +138,8 @@ VectorPtr createFlatVector<TypeKind::BOOLEAN>(
135138
auto rawValues = column->mutableRawValues<uint64_t>();
136139
for (auto pos = 0; pos < numRows; pos++) {
137140
if (!isNull(memoryAddress + offsets[pos], columnIdx)) {
138-
bool value = *(bool*)(memoryAddress + offsets[pos] + fieldOffset);
141+
bool* valuePtr = reinterpret_cast<bool*>(memoryAddress + offsets[pos] + fieldOffset);
142+
bool value = *valuePtr;
139143
bits::setBit(rawValues, pos, value);
140144
} else {
141145
column->setNull(pos, true);
@@ -156,7 +160,8 @@ VectorPtr createFlatVector<TypeKind::TIMESTAMP>(
156160
auto column = BaseVector::create<FlatVector<Timestamp>>(type, numRows, pool);
157161
for (auto pos = 0; pos < numRows; pos++) {
158162
if (!isNull(memoryAddress + offsets[pos], columnIdx)) {
159-
int64_t value = *(int64_t*)(memoryAddress + offsets[pos] + fieldOffset);
163+
int64_t* valuePtr = static_cast<int64_t*>(memoryAddress + offsets[pos] + fieldOffset);
164+
int64_t value = *(valuePtr);
160165
column->set(pos, Timestamp::fromMicros(value));
161166
} else {
162167
column->setNull(pos, true);
@@ -178,7 +183,8 @@ VectorPtr createFlatVectorStringView(
178183
char* rawBuffer = column->getRawStringBufferWithSpace(size, true);
179184
for (auto pos = 0; pos < numRows; pos++) {
180185
if (!isNull(memoryAddress + offsets[pos], columnIdx)) {
181-
int64_t offsetAndSize = *(int64_t*)(memoryAddress + offsets[pos] + fieldOffset);
186+
int64_t* offsetAndSizePtr = reinterpret_cast<int64_t*>(memoryAddress + offsets[pos] + fieldOffset);
187+
int64_t offsetAndSize = *offsetAndSizePtr;
182188
int32_t length = static_cast<int32_t>(offsetAndSize);
183189
int32_t wordoffset = static_cast<int32_t>(offsetAndSize >> 32);
184190
auto valueSrcPtr = memoryAddress + offsets[pos] + wordoffset;

cpp/velox/shuffle/VeloxShuffleReader.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ std::shared_ptr<ColumnarBatch> VeloxSortShuffleReaderDeserializer::deserializeTo
440440
auto buffer = cur->second;
441441
const auto* rawBuffer = buffer->as<char>();
442442
while (rowOffset_ < cur->first && readRows < batchSize_) {
443-
auto rowSize = *(RowSizeType*)(rawBuffer + byteOffset_) - sizeof(RowSizeType);
443+
auto rowSize = *(static_cast<RowSizeType*>(rawBuffer + byteOffset_)) - sizeof(RowSizeType);
444444
byteOffset_ += sizeof(RowSizeType);
445445
data.push_back(std::string_view(rawBuffer + byteOffset_, rowSize));
446446
byteOffset_ += rowSize;

cpp/velox/shuffle/VeloxSortShuffleWriter.cc

+8-6
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ constexpr uint32_t kPartitionIdEndByteIndex = 7;
3434

3535
uint64_t toCompactRowId(uint32_t partitionId, uint32_t pageNumber, uint32_t offsetInPage) {
3636
// |63 partitionId(24) |39 inputIndex(13) |26 rowIndex(27) |
37-
return (uint64_t)partitionId << 40 | (uint64_t)pageNumber << 27 | offsetInPage;
37+
return static_cast<uint64_t>(partitionId) << 40 | static_cast<uint64_t>(pageNumber) << 27 | offsetInPage;
3838
}
3939

4040
uint32_t extractPartitionId(uint64_t compactRowId) {
41-
return (uint32_t)(compactRowId >> 40);
41+
return static_cast<uint32_t>(compactRowId >> 40);
4242
}
4343

4444
std::pair<uint32_t, uint32_t> extractPageNumberAndOffset(uint64_t compactRowId) {
@@ -187,7 +187,7 @@ arrow::Status VeloxSortShuffleWriter::insert(const facebook::velox::RowVectorPtr
187187
auto rows = maxRowsToInsert(rowOffset, remainingRows);
188188
if (rows == 0) {
189189
auto minSizeRequired = fixedRowSize_ ? fixedRowSize_.value() : rowSize_[rowOffset];
190-
acquireNewBuffer((uint64_t)memLimit, minSizeRequired);
190+
acquireNewBuffer(static_cast<uint64_t>(memLimit), minSizeRequired);
191191
rows = maxRowsToInsert(rowOffset, remainingRows);
192192
ARROW_RETURN_IF(
193193
rows == 0, arrow::Status::Invalid("Failed to insert rows. Remaining rows: " + std::to_string(remainingRows)));
@@ -294,15 +294,16 @@ arrow::Status VeloxSortShuffleWriter::evictPartition(uint32_t partitionId, size_
294294
while (index < end) {
295295
auto pageIndex = extractPageNumberAndOffset(arrayPtr_[index]);
296296
addr = pageAddresses_[pageIndex.first] + pageIndex.second;
297-
size = *(RowSizeType*)addr;
297+
RowSizeType* sizePtr = reinterpret_cast<RowSizeType*>(addr);
298+
size = *sizePtr;
298299
if (offset + size > options_.sortEvictBufferSize && offset > 0) {
299300
sortTime.stop();
300301
RETURN_NOT_OK(evictPartitionInternal(partitionId, index - begin, rawBuffer_, offset));
301302
sortTime.start();
302303
begin = index;
303304
offset = 0;
304305
}
305-
if (size > options_.sortEvictBufferSize) {
306+
if (size > static_cast<uint32_t>(options_.sortEvictBufferSize)) {
306307
// Split large rows.
307308
sortTime.stop();
308309
RowSizeType bytes = 0;
@@ -355,7 +356,8 @@ facebook::velox::vector_size_t VeloxSortShuffleWriter::maxRowsToInsert(
355356
}
356357
auto remainingBytes = pages_.back()->size() - pageCursor_;
357358
if (fixedRowSize_) {
358-
return std::min((facebook::velox::vector_size_t)(remainingBytes / (fixedRowSize_.value())), remainingRows);
359+
return std::min(
360+
static_cast<facebook::velox::vector_size_t>(remainingBytes / (fixedRowSize_.value())), remainingRows);
359361
}
360362
auto beginIter = rowSizePrefixSum_.begin() + 1 + offset;
361363
auto bytesWritten = rowSizePrefixSum_[offset];

cpp/velox/substrait/SubstraitParser.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ int16_t SubstraitParser::getLiteralValue(const ::substrait::Expression::Literal&
327327
template <>
328328
int32_t SubstraitParser::getLiteralValue(const ::substrait::Expression::Literal& literal) {
329329
if (literal.has_date()) {
330-
return int32_t(literal.date());
330+
return static_cast<int32_t>(literal.date());
331331
}
332332
return literal.i32();
333333
}

cpp/velox/tests/FunctionTest.cc

+6-4
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,13 @@ TEST_F(FunctionTest, setVectorFromVariants) {
153153

154154
// Floats are harder to compare because of low-precision. Just making sure
155155
// they don't throw.
156-
EXPECT_NO_THROW(setVectorFromVariants(REAL(), {variant(float(0.99L)), variant(float(-1.99L))}, pool_.get()));
156+
EXPECT_NO_THROW(setVectorFromVariants(
157+
REAL(), {variant(static_cast<float>(0.99L)), variant(static_cast<float>(-1.99L))}, pool_.get()));
157158

158-
resultVec = setVectorFromVariants(DOUBLE(), {variant(double(0.99L)), variant(double(-1.99L))}, pool_.get());
159-
ASSERT_EQ(double(0.99L), resultVec->asFlatVector<double>()->valueAt(0));
160-
ASSERT_EQ(double(-1.99L), resultVec->asFlatVector<double>()->valueAt(1));
159+
resultVec = setVectorFromVariants(
160+
DOUBLE(), {variant(static_cast<double>(0.99L)), variant(static_cast<double>(-1.99L))}, pool_.get());
161+
ASSERT_EQ(static_cast<double>(0.99L), resultVec->asFlatVector<double>()->valueAt(0));
162+
ASSERT_EQ(static_cast<double>(-1.99L), resultVec->asFlatVector<double>()->valueAt(1));
161163

162164
resultVec = setVectorFromVariants(VARCHAR(), {variant(""), variant("asdf")}, pool_.get());
163165
ASSERT_EQ("", resultVec->asFlatVector<StringView>()->valueAt(0).str());

cpp/velox/tests/VeloxSubstraitRoundTripTest.cc

+6-6
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,12 @@ TEST_F(VeloxSubstraitRoundTripTest, notNullLiteral) {
368368
.addNode([&](std::string id, core::PlanNodePtr input) {
369369
std::vector<std::string> projectNames = {"a", "b", "c", "d", "e", "f", "g", "h"};
370370
std::vector<core::TypedExprPtr> projectExpressions = {
371-
makeConstantExpr(BOOLEAN(), static_cast<bool>(1)),
372-
makeConstantExpr(TINYINT(), static_cast<int8_t>(23)),
373-
makeConstantExpr(SMALLINT(), static_cast<int16_t>(45)),
374-
makeConstantExpr(INTEGER(), static_cast<int32_t>(678)),
375-
makeConstantExpr(BIGINT(), static_cast<int64_t>(910)),
376-
makeConstantExpr(REAL(), static_cast<float>(1.23)),
371+
makeConstantExpr(BOOLEAN(), 1),
372+
makeConstantExpr(TINYINT(), 23),
373+
makeConstantExpr(SMALLINT(), 45),
374+
makeConstantExpr(INTEGER(), 678),
375+
makeConstantExpr(BIGINT(), 910),
376+
makeConstantExpr(REAL(), 1.23),
377377
makeConstantExpr(DOUBLE(), static_cast<double>(4.56)),
378378
makeConstantExpr(VARCHAR(), "789")};
379379
return std::make_shared<core::ProjectNode>(

0 commit comments

Comments
 (0)