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

Add truncate function #2432

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8a02a38
add truncate
kche0169 Jan 7, 2025
10a9abb
add registerfunc
kche0169 Jan 7, 2025
1e7a946
add test cases for truncate
kche0169 Jan 7, 2025
fc6c149
add other support functions file
kche0169 Jan 7, 2025
b1e02ae
add reverse func
kche0169 Jan 7, 2025
1c0cae2
remove useless code about test truncate in test_select.py
kche0169 Jan 7, 2025
b4441f7
remove Unrelated documents
kche0169 Jan 7, 2025
03f1841
success connect the backend and frontend, truncate can be used in the…
kche0169 Jan 8, 2025
7f113d2
add more Error Handings
kche0169 Jan 8, 2025
5217a45
Merge branch 'main' into main
kche0169 Jan 8, 2025
e12bacf
Remove the code commented out
kche0169 Jan 9, 2025
ad10425
add more test cases and change the previous test cases about truncate…
kche0169 Jan 9, 2025
19dac45
add changes in py server to make the database supported trunc function
kche0169 Jan 9, 2025
973a4b4
Merge branch 'main' of github.com:kche0169/infinity
kche0169 Jan 9, 2025
020c773
remove commented code
kche0169 Jan 9, 2025
1579efc
remove changes in test file
kche0169 Jan 9, 2025
b14d2a6
change license information and remove std::cout
kche0169 Jan 9, 2025
655ca81
remove include files, add functions supported float types and more fu…
kche0169 Jan 9, 2025
bbfc406
add two unit tests
kche0169 Jan 9, 2025
b4ad5ca
change the order of the import statements in truncate_functions.cpp
kche0169 Jan 9, 2025
956c4e7
remove useless functions
kche0169 Jan 9, 2025
3f35608
remove useless include files
kche0169 Jan 9, 2025
62d206b
remove useless include files
kche0169 Jan 9, 2025
8566bd0
remove useless include files
kche0169 Jan 9, 2025
aef0e6a
Merge branch 'infiniflow:main' into main
kche0169 Jan 10, 2025
2432705
The implementation form has been modified to complete the function mo…
kche0169 Jan 10, 2025
6fc0d4d
-remove useless include file
kche0169 Jan 10, 2025
4dab966
Delete python/test_pysdk/launch.json
kche0169 Jan 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion python/infinity_embedded/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def binary_exp_to_paser_exp(binary_expr_key) -> str:
elif binary_expr_key == "mod":
return "%"
else:
raise InfinityException(ErrorCode.INVALID_EXPRESSION, f"unknown binary expression: {binary_expr_key}")
# raise InfinityException(ErrorCode.INVALID_EXPRESSION, f"unknown binary expression: {binary_expr_key}")
JinHai-CN marked this conversation as resolved.
Show resolved Hide resolved
return binary_expr_key


def deprecated_api(message):
Expand Down
3 changes: 2 additions & 1 deletion python/infinity_sdk/infinity/remote_thrift/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ def output(self, columns: Optional[list]) -> InfinityThriftQueryBuilder:
parsed_expr = ParsedExpr(type=expr_type)
select_list.append(parsed_expr)
case _:
select_list.append(parse_expr(maybe_parse(column)))
expr = maybe_parse(column)
select_list.append(parse_expr(expr))

self._columns = select_list
return self
Expand Down
14 changes: 14 additions & 0 deletions python/infinity_sdk/infinity/remote_thrift/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,20 @@ def traverse_conditions(cons, fn=None) -> ttypes.ParsedExpr:
parsed_expr = ttypes.ParsedExpr(type=expr_type)
return parsed_expr
# in
elif isinstance(cons, exp.Command):
func_name = cons.args['this']
arguments = []
for arg in cons.args['expression']:
if arg:
arguments.append(parse_expr(arg))

func_expr = ttypes.FunctionExpr(
function_name=func_name,
arguments=arguments
)
expr_type = ttypes.ParsedExprType(function_expr=func_expr)
parsed_expr = ttypes.ParsedExpr(type=expr_type)
return parsed_expr
elif isinstance(cons, exp.In):
left_operand = parse_expr(cons.args['this'])
arguments = []
Expand Down
19 changes: 19 additions & 0 deletions python/test_pysdk/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,3 +1023,22 @@ def test_select_round(self, suffix):

res = db_obj.drop_table("test_select_round" + suffix)
assert res.error_code == ErrorCode.OK

def test_select_truncate(self, suffix):
db_obj = self.infinity_obj.get_database("default_db")
db_obj.drop_table("test_select_truncate" + suffix, ConflictType.Ignore)
db_obj.create_table("test_select_truncate" + suffix,
{"c": {"type": "double"}}, ConflictType.Error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to check 'float' type data

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

table_obj = db_obj.get_table("test_select_truncate" + suffix)
table_obj.insert(
[{'c': 2.123}, {'c': -2.123}, {'c': 2}, {'c': 2.1}, {'c': float("nan")}, {'c': float("inf")}, {'c':float("-inf")}])

res, extra_res = table_obj.output(["trunc(c, 2)"]).to_df()
print(res)
pd.testing.assert_frame_equal(res, pd.DataFrame({'(c trunc 2)': ("2.12", "-2.12", "2.00", "2.10", "NaN", "Inf", "Inf")})
.astype({'(c trunc 2)': dtype('str_')}))


res = db_obj.drop_table("test_select_truncate" + suffix)
assert res.error_code == ErrorCode.OK

4 changes: 2 additions & 2 deletions src/function/builtin_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import default_values;
import special_function;
import internal_types;
import data_type;

import trunc;
import logical_type;

namespace infinity {
Expand Down Expand Up @@ -118,7 +118,7 @@ void BuiltinFunctions::RegisterScalarFunction() {
RegisterIsnanFunction(catalog_ptr_);
RegisterIsinfFunction(catalog_ptr_);
RegisterIsfiniteFunction(catalog_ptr_);

RegisterTruncFunction(catalog_ptr_);
// register comparison operator
RegisterEqualsFunction(catalog_ptr_);
RegisterInEqualFunction(catalog_ptr_);
Expand Down
77 changes: 77 additions & 0 deletions src/function/scalar/truncate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apache license information


module;
#include <type_traits>
#include "type/internal_types.h"
#include <ostream>
#include "type/logical_type.h"
#include <cstddef>
#include <cmath>
#include <iostream>
#include <sstream>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why some many headers need to include?

#include <string>
#include <iomanip>
module trunc;
import stl;
import catalog;
import status;
import logical_type;
import infinity_exception;
import scalar_function;
import scalar_function_set;
import third_party;
import internal_types;
import data_type;
import column_vector;

namespace infinity {

struct TruncFunction {
template <typename TA, typename TB, typename TC, typename TD>
static inline void Run(TA left, TB right, TC &result, TD result_ptr) {
Status status = Status::NotSupport("Not implemented");
RecoverableError(status);
}
};

template <>
inline void TruncFunction::Run(DoubleT left, BigIntT right, VarcharT &result, ColumnVector *result_ptr) {
JinHai-CN marked this conversation as resolved.
Show resolved Hide resolved
std::stringstream ss;
ss << std::fixed << std::setprecision(16);
ss << left;
std::string str = ss.str();
std::string truncated_str;
size_t i = str.find_first_of('.');
if (right < static_cast<BigIntT>(0) || std::isnan(right) || std::isinf(right)) {
Status status = Status::InvalidDataType();
RecoverableError(status);
return;
} else if (std::isnan(left)) {
truncated_str = "NaN";
} else if (std::isinf(left)) {
truncated_str = "Inf";
} else if (right > static_cast<BigIntT>(17) || static_cast<BigIntT>(str.size() - i) < right || right == static_cast<BigIntT>(0)) {
truncated_str = str.substr(0, i);
} else {

truncated_str = str.substr(0, i + right + 1);
std::cout << truncated_str << std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why std::cout?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove it

}
result_ptr->AppendVarcharInner(truncated_str, result);
}

void RegisterTruncFunction(const UniquePtr<Catalog> &catalog_ptr) {
String func_name = "trunc";
JinHai-CN marked this conversation as resolved.
Show resolved Hide resolved

SharedPtr<ScalarFunctionSet> function_set_ptr = MakeShared<ScalarFunctionSet>(func_name);

ScalarFunction truncate_double_bigint(func_name,
{DataType(LogicalType::kDouble), DataType(LogicalType::kBigInt)},
DataType(LogicalType::kVarchar),
&ScalarFunction::BinaryFunctionToVarlen<DoubleT, BigIntT, VarcharT, TruncFunction>);
function_set_ptr->AddFunction(truncate_double_bigint);

Catalog::AddFunctionSet(catalog_ptr.get(), function_set_ptr);
}

} // namespace infinity
12 changes: 12 additions & 0 deletions src/function/scalar/truncate.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module;

export module trunc;

import stl;

namespace infinity {

class Catalog;
export void RegisterTruncFunction(const UniquePtr<Catalog> &catalog_ptr);

}
63 changes: 63 additions & 0 deletions src/unit_test/function/scalar/truncate_functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright(C) 2023 InfiniFlow, Inc. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2023->2025

//
// 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
//
// https://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.


#include "gtest/gtest.h"
import base_test;

import infinity_exception;

import global_resource_usage;
import third_party;

import logger;
import stl;
import infinity_context;
import catalog;
import truncate;
import scalar_function;
import scalar_function_set;
import function_set;
import function;
import column_expression;
import value;
import default_values;
import data_block;
import base_expression;
import column_vector;
import logical_type;
import internal_types;
import data_type;
#if 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ignore the unit test?

using namespace infinity;
class TruncateFunctionsTest : public BaseTestParamStr {};

INSTANTIATE_TEST_SUITE_P(TestWithDifferentParams, AbsFunctionsTest, ::testing::Values(BaseTestParamStr::NULL_CONFIG_PATH));

TEST_P(TruncateFunctionsTest, truncate_func) {
using namespace infinity;

UniquePtr<Catalog> catalog_ptr = MakeUnique<Catalog>();

RegisterAbsFunction(catalog_ptr);

String op = "truncate";

SharedPtr<FunctionSet> function_set = Catalog::GetFunctionSetByName(catalog_ptr.get(), op);
EXPECT_EQ(function_set->type_, FunctionType::kScalar);
SharedPtr<ScalarFunctionSet> scalar_function_set = std::static_pointer_cast<ScalarFunctionSet>(function_set);

{}
}
#endif
5 changes: 3 additions & 2 deletions tools/run_pysdk_remote_infinity_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
python_executable = sys.executable


def python_sdk_test(python_test_dir: str, pytest_mark: str):
def python_sdk_test(python_test_dir: str, pytest_mark: str, single_file_name=""):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why update this file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remove this change

# The single file name must be start with '/'
print("python test path is {}".format(python_test_dir))
# run test
print(f"start pysdk test with {pytest_mark}")
Expand All @@ -22,7 +23,7 @@ def python_sdk_test(python_test_dir: str, pytest_mark: str):
"-x",
"-m",
pytest_mark,
f"{python_test_dir}/test_pysdk",
f"{python_test_dir}/test_pysdk" + single_file_name,
]
quoted_args = ['"' + arg + '"' if " " in arg else arg for arg in args]
print(" ".join(quoted_args))
Expand Down