Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
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: 3 additions & 0 deletions components/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ set(SOURCE_FILES_unitTest
src/clp/ffi/ir_stream/protocol_constants.hpp
src/clp/ffi/ir_stream/Serializer.cpp
src/clp/ffi/ir_stream/Serializer.hpp
src/clp/ffi/ir_stream/search/utils.cpp
src/clp/ffi/ir_stream/search/utils.hpp
src/clp/ffi/ir_stream/utils.cpp
src/clp/ffi/ir_stream/utils.hpp
src/clp/ffi/KeyValuePairLogEvent.cpp
Expand Down Expand Up @@ -605,6 +607,7 @@ set(SOURCE_FILES_unitTest
tests/test-clp_s-search.cpp
tests/test-EncodedVariableInterpreter.cpp
tests/test-encoding_methods.cpp
tests/test-ffi_ir_stream_search_utils.cpp
tests/test-ffi_IrUnitHandlerInterface.cpp
tests/test-ffi_KeyValuePairLogEvent.cpp
tests/test-ffi_SchemaTree.cpp
Expand Down
381 changes: 381 additions & 0 deletions components/core/src/clp/ffi/ir_stream/search/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,381 @@
#include "utils.hpp"

#include <concepts>
#include <memory>
#include <optional>
#include <string>
#include <string_view>

#include <string_utils/string_utils.hpp>

#include "../../../../clp_s/search/ast/FilterExpr.hpp"
#include "../../../../clp_s/search/ast/FilterOperation.hpp"
#include "../../../../clp_s/search/ast/Literal.hpp"
#include "../../../ir/EncodedTextAst.hpp"
#include "../../SchemaTree.hpp"
#include "../../Value.hpp"

namespace clp::ffi::ir_stream::search {
namespace {
using clp_s::search::ast::FilterOperation;
using clp_s::search::ast::Literal;
using clp_s::search::ast::LiteralType;
using clp_s::search::ast::LiteralTypeBitmask;

/**
* Evaluates a numerical filter operation by applying the specified `FilterOperation` to two
* operands.
* @tparam OperandType The type of the operands, must be either `value_int_t` or `value_float_t`.
* @param op
* @param filter_operand The operand associated with the filter.
* @param value_operand The value operand to evaluate.
* @return true if the filter condition is satisfied; false otherwise.
*/
template <typename OperandType>
requires std::same_as<OperandType, value_int_t> || std::same_as<OperandType, value_float_t>
[[nodiscard]] auto evaluate_numerical_filter_op(
FilterOperation op,
OperandType filter_operand,
OperandType value_operand
) -> bool;

/**
* Evaluates a string filter operation by applying the specified `FilterOperation` to two string
* operands.
* @param op
* @param filter_operand The operand associated with the filter.
* @param value_operand The value operand to evaluate.
* @return true if the filter condition is satisfied; false otherwise.
*/
[[nodiscard]] auto evaluate_string_filter_op(
FilterOperation op,
std::string_view filter_operand,
std::string_view value_operand,
bool case_sensitive_match
) -> bool;

/**
* Evaluates an integer filter operation by applying the specified `FilterOperation` to the given
* filter operand and value.
* @param op
* @param operand
* @param value
* @return true if the filter condition is satisfied; false otherwise.
*/
[[nodiscard]] auto evaluate_int_filter_op(
FilterOperation op,
std::shared_ptr<Literal> const& operand,
Value const& value
) -> bool;

/**
* Evaluates a float filter operation by applying the specified `FilterOperation` to the given
* filter operand and value.
* @param op
* @param operand
* @param value
* @return true if the filter condition is satisfied; false otherwise.
*/
[[nodiscard]] auto evaluate_float_filter_op(
FilterOperation op,
std::shared_ptr<Literal> const& operand,
Value const& value
) -> bool;

/**
* Evaluates a boolean filter operation by applying the specified `FilterOperation` to the given
* operand and value.
* @param op
* @param operand
* @param value
* @return true if the filter condition is satisfied; false otherwise.
*/
[[nodiscard]] auto evaluate_bool_filter_op(
FilterOperation op,
std::shared_ptr<Literal> const& operand,
Value const& value
) -> bool;

/**
* Evaluates a `VarString` filter operation by applying the specified `FilterOperation` to the given
* operand and value.
* @param op
* @param operand
* @param value
* @return true if the filter condition is satisfied; false otherwise.
*/
[[nodiscard]] auto evaluate_var_string_filter_op(
FilterOperation op,
std::shared_ptr<Literal> const& operand,
Value const& value,
bool case_sensitive_match
) -> bool;

/**
* Evaluates a `ClpString` filter operation by applying the specified `FilterOperation` to the given
* operand and value.
* @param op
* @param operand
* @param value
* @return true if the filter condition is satisfied; false otherwise.
*/
[[nodiscard]] auto evaluate_clp_string_filter_op(
FilterOperation op,
std::shared_ptr<Literal> const& operand,
Value const& value,
bool case_sensitive_match
) -> bool;

template <typename OperandType>
requires std::same_as<OperandType, value_int_t> || std::same_as<OperandType, value_float_t>
auto evaluate_numerical_filter_op(
FilterOperation op,
OperandType filter_operand,
OperandType value_operand
) -> bool {
switch (op) {
case FilterOperation::EQ:
return value_operand == filter_operand;
case FilterOperation::NEQ:
return value_operand != filter_operand;
case FilterOperation::LT:
return value_operand < filter_operand;
case FilterOperation::GT:
return value_operand > filter_operand;
case FilterOperation::LTE:
return value_operand <= filter_operand;
case FilterOperation::GTE:
return value_operand >= filter_operand;
default:
return false;
}
}

auto evaluate_string_filter_op(
FilterOperation op,
std::string_view filter_operand,
std::string_view value_operand,
bool case_sensitive_match
) -> bool {
switch (op) {
case FilterOperation::EQ:
return clp::string_utils::wildcard_match_unsafe(
value_operand,
filter_operand,
case_sensitive_match
);
case FilterOperation::NEQ:
return false
== clp::string_utils::wildcard_match_unsafe(
value_operand,
filter_operand,
case_sensitive_match
);
default:
return false;
}
}

auto evaluate_int_filter_op(
FilterOperation op,
std::shared_ptr<Literal> const& operand,
Value const& value
) -> bool {
value_int_t filter_operand{};
if (false == operand->as_int(filter_operand, op)) {
return false;
}
return evaluate_numerical_filter_op(
op,
filter_operand,
value.get_immutable_view<value_int_t>()
);
}

auto evaluate_float_filter_op(
FilterOperation op,
std::shared_ptr<Literal> const& operand,
Value const& value
) -> bool {
value_float_t filter_operand{};
if (false == operand->as_float(filter_operand, op)) {
return false;
}
return evaluate_numerical_filter_op(
op,
filter_operand,
value.get_immutable_view<value_float_t>()
);
}

auto evaluate_bool_filter_op(
FilterOperation op,
std::shared_ptr<Literal> const& operand,
Value const& value
) -> bool {
bool filter_operand{};
if (false == operand->as_bool(filter_operand, op)) {
return false;
}
auto const value_operand{value.get_immutable_view<clp::ffi::value_bool_t>()};
Comment thread
LinZhihao-723 marked this conversation as resolved.
Resolved

switch (op) {
case FilterOperation::EQ:
return value_operand == filter_operand;
case FilterOperation::NEQ:
return value_operand != filter_operand;
default:
return false;
}
}

auto evaluate_var_string_filter_op(
FilterOperation op,
std::shared_ptr<Literal> const& operand,
Value const& value,
bool case_sensitive_match
) -> bool {
std::string filter_operand;
if (false == operand->as_var_string(filter_operand, op)) {
return false;
}
auto const value_operand{value.get_immutable_view<std::string>()};

return evaluate_string_filter_op(op, filter_operand, value_operand, case_sensitive_match);
}

auto evaluate_clp_string_filter_op(
FilterOperation op,
std::shared_ptr<Literal> const& operand,
Value const& value,
bool case_sensitive_match
) -> bool {
std::string filter_operand;
if (false == operand->as_clp_string(filter_operand, op)) {
return false;
}

auto const optional_value_operand{
value.is<clp::ir::EightByteEncodedTextAst>()
? value.get_immutable_view<clp::ir::EightByteEncodedTextAst>()
.decode_and_unparse()
: value.get_immutable_view<clp::ir::FourByteEncodedTextAst>()
.decode_and_unparse()
};
if (false == optional_value_operand.has_value()) {
// TODO: We should return an error.
Comment thread
LinZhihao-723 marked this conversation as resolved.
Resolved
return false;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

return evaluate_string_filter_op(
op,
filter_operand,
*optional_value_operand,
case_sensitive_match
);
}
} // namespace

auto schema_tree_node_type_to_literal_types(SchemaTree::Node::Type node_type)
-> clp_s::search::ast::LiteralTypeBitmask {
switch (node_type) {
case SchemaTree::Node::Type::Int:
return LiteralType::IntegerT;
case SchemaTree::Node::Type::Float:
return LiteralType::FloatT;
case SchemaTree::Node::Type::Bool:
return LiteralType::BooleanT;
case SchemaTree::Node::Type::Str:
return LiteralType::ClpStringT | LiteralType::VarStringT | LiteralType::EpochDateT;
case SchemaTree::Node::Type::UnstructuredArray:
return LiteralType::ArrayT;
case SchemaTree::Node::Type::Obj:
// TODO: Add `LiteralType::ObjectT` once supported.
return LiteralType::NullT;
default:
return LiteralType::UnknownT;
}
}

auto schema_tree_node_type_value_pair_to_literal_type(
SchemaTree::Node::Type node_type,
std::optional<Value> const& value
) -> clp_s::search::ast::LiteralType {
if (false == value.has_value()) {
// TODO: Consider `LiteralType::ObjectT` once supported.
return LiteralType::UnknownT;
}

switch (node_type) {
case clp::ffi::SchemaTree::Node::Type::Int:
Comment thread
LinZhihao-723 marked this conversation as resolved.
Resolved
return LiteralType::IntegerT;
case clp::ffi::SchemaTree::Node::Type::Float:
return LiteralType::FloatT;
case clp::ffi::SchemaTree::Node::Type::Bool:
return LiteralType::BooleanT;
case clp::ffi::SchemaTree::Node::Type::UnstructuredArray:
return LiteralType::ArrayT;
case clp::ffi::SchemaTree::Node::Type::Str:
if (value.value().is<std::string>()) {
return LiteralType::VarStringT;
}
return LiteralType::ClpStringT;
case clp::ffi::SchemaTree::Node::Type::Obj:
if (value.value().is_null()) {
return LiteralType::NullT;
}
// TODO: Return `LiteralType::ObjectT` once supported.
return LiteralType::UnknownT;
default:
return LiteralType::UnknownT;
}
}

auto evaluate_filter_against_literal_type_value_pair(
clp_s::search::ast::FilterExpr const* filter,
LiteralType literal_type,
std::optional<Value> const& value,
bool case_sensitive_match
) -> bool {
auto const op{filter->get_operation()};
if (FilterOperation::EXISTS == op) {
return true;
}
if (FilterOperation::NEXISTS == op) {
return false;
}

Comment thread
LinZhihao-723 marked this conversation as resolved.
Resolved
if (false == value.has_value()) {
return false;
}

switch (literal_type) {
case LiteralType::IntegerT:
return evaluate_int_filter_op(op, filter->get_operand(), *value);
case LiteralType::FloatT:
return evaluate_float_filter_op(op, filter->get_operand(), *value);
case LiteralType::BooleanT:
return evaluate_bool_filter_op(op, filter->get_operand(), *value);
case LiteralType::VarStringT:
return evaluate_var_string_filter_op(
op,
filter->get_operand(),
*value,
case_sensitive_match
);
case LiteralType::ClpStringT:
return evaluate_clp_string_filter_op(
Comment thread
kirkrodrigues marked this conversation as resolved.
Resolved
op,
filter->get_operand(),
*value,
case_sensitive_match
);
case LiteralType::ArrayT:
case LiteralType::NullT:
case LiteralType::EpochDateT:
case LiteralType::UnknownT:
default:
return false;

@gibber9809 gibber9809 Apr 28, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you think its worth adding a comment to differentiate these cases?

E.g. we'll implement search for LiteralType::ArrayT at some point (maybe in this function, maybe somewhere else), but we won't for the other three cases for various reasons.

NullT because this branch should be unreachable by construction (should always be an EXISTS/NEXISTS filter), EpochDateT because schema_tree_node_type_value_pair_to_literal_type only returns (and only has to return) VarStringT and ClpStringT for string types, and UnknownT because it should again be unreachable because column resolution should have failed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

sgtm

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

How about let's merge #863 first, and we can then change this function to return a result instead. For these currently not supported types, we return ErrorCodeEnum::NotImplemented

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Change evaluate_filter_against_literal_type_value_pair to return a result instead. The details are added to the docstring.

  • For internal errors, we return LiteralTypeUnexpected.
  • For currently unsupported types, we return LiteralTypeUnsupported.
    • I understand the concern for returning an error for array type; I think we can add a special handling to ignore this error on the caller end.

}
}
} // namespace clp::ffi::ir_stream::search
Loading