Skip to content

Commit d877238

Browse files
authored
Optimized Truncate Function (#2453)
### What problem does this PR solve? _Optimized Truncate Function_ Issue link: (#2033) ### Type of change - [x] Test cases - [x] Other (please describe): Optimized
1 parent 665ac44 commit d877238

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

python/infinity_http.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -921,12 +921,8 @@ def to_result(self):
921921
if k not in df_dict:
922922
df_dict[k] = ()
923923
tup = df_dict[k]
924-
if isinstance(res[k], str) and len(res[k]) > 0 and res[k][0] == " ":
924+
if isinstance(res[k], (int, float)):
925925
new_tup = tup + (res[k],)
926-
elif isinstance(res[k], (int, float)):
927-
new_tup = tup + (res[k],)
928-
elif res[k].isdigit() or is_float(res[k]):
929-
new_tup = tup + (eval(res[k]),)
930926
elif is_list(res[k]):
931927
new_tup = tup + (ast.literal_eval(res[k]),)
932928
elif is_date(res[k]):

python/test_pysdk/test_select.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1038,11 +1038,12 @@ def test_select_truncate(self, suffix):
10381038

10391039
res, extra_res = table_obj.output(["trunc(c1, 14)", "trunc(c2, 2)", "trunc(c3, 2)"]).to_df()
10401040
print(res)
1041-
pd.testing.assert_frame_equal(res, pd.DataFrame({'(c1 trunc 14)': (" 2.12300000000000", " -2.12300000000000", " 2.00000000000000", " 2.10000000000000"),
1042-
'(c2 trunc 2)': (" 2.12", " -2.12", " 2.00", " 2.10"),
1043-
'(c3 trunc 2)': (" 2.12", " -2.12", " 2.00", " 2.10")
1041+
print(res.dtypes)
1042+
pd.testing.assert_frame_equal(res, pd.DataFrame({'(c1 trunc 14)': ("2.12300000000000", "-2.12300000000000", "2.00000000000000", "2.10000000000000"),
1043+
'(c2 trunc 2)': ("2.12", "-2.12", "2.00", "2.10"),
1044+
'(c3 trunc 2)': ("2.12", "-2.12", "2.00", "2.10")
10441045
})
1045-
.astype({'(c1 trunc 14)': dtype('str_'), '(c2 trunc 2)': dtype('str_'), '(c3 trunc 2)': dtype('str_')}))
1046+
.astype({'(c1 trunc 14)': dtype('object'), '(c2 trunc 2)': dtype('object'), '(c3 trunc 2)': dtype('object')}))
10461047

10471048

10481049
res = db_obj.drop_table("test_select_truncate" + suffix)
@@ -1061,6 +1062,7 @@ def test_select_reverse(self, suffix):
10611062

10621063
res, extra_res = table_obj.output(["reverse(c1)", "reverse(c2)"]).to_df()
10631064
print(res)
1065+
print(res.dtypes)
10641066
pd.testing.assert_frame_equal(res, pd.DataFrame({'reverse(c1)': ('cba', '321a', 'c', 'nmlkjihgfedcba'),
10651067
'reverse(c2)': ('CBA', '321a', 'C', 'NMLKJIHGFEDCBA')})
10661068
.astype({'reverse(c1)': dtype('str_'), 'reverse(c2)': dtype('str_')}))

src/function/scalar/truncate.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,16 @@ inline void TruncFunction::Run(DoubleT left, BigIntT right, VarcharT &result, Co
5050
}
5151

5252
char buffer[MinBufferSize];
53-
buffer[0] =' ';
54-
53+
5554
right = (right > MaxRight) ? MaxRight : right;
5655

57-
int len = std::snprintf(buffer + 1, sizeof(buffer) - 2, "%.*f", (int)right, left);
56+
int len = std::snprintf(buffer, sizeof(buffer) - 1, "%.*f", (int)right, left);
5857
if (len < 0) {
5958
Status status = Status::InvalidDataType();
6059
RecoverableError(status);
6160
return;
6261
}
63-
std::string truncated_str(buffer, len + 1);
62+
std::string truncated_str(buffer, len);
6463
result_ptr->AppendVarcharInner(truncated_str, result);
6564

6665
}
@@ -76,15 +75,15 @@ inline void TruncFunction::Run(FloatT left, BigIntT right, VarcharT &result, Col
7675
return;
7776
}
7877
char buffer[MinBufferSize];
79-
buffer[0] =' ';
78+
8079
right = (right > MaxRight) ? MaxRight : right;
81-
int len = std::snprintf(buffer + 1, sizeof(buffer) - 2, "%.*f", (int)right, left);
80+
int len = std::snprintf(buffer, sizeof(buffer) - 1, "%.*f", (int)right, left);
8281
if (len < 0) {
8382
Status status = Status::InvalidDataType();
8483
RecoverableError(status);
8584
return;
8685
}
87-
std::string truncated_str(buffer, len + 1);
86+
std::string truncated_str(buffer, len);
8887
result_ptr->AppendVarcharInner(truncated_str, result);
8988
}
9089

0 commit comments

Comments
 (0)