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 Query support #18

Merged
merged 131 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
131 commits
Select commit Hold shift + click to select a range
6bbe288
[fix] Rework code according to Valgrind's recommendations
Oct 8, 2024
d27da93
Clear code
Oct 9, 2024
81ca870
Avoid PEP's warnings
Oct 9, 2024
2f9edd5
Update code
Oct 9, 2024
0ded690
Support sync transactions
Oct 14, 2024
de64dac
Rewrite query results wrapper
Oct 14, 2024
0392983
Properly use NewItem method. Reorganize code
Oct 15, 2024
1cbd051
Implement transaction py API
Oct 15, 2024
1a77b8e
Add transaction py-tests
Oct 15, 2024
3ac3485
Update Readme.md
Oct 16, 2024
0d44728
[fix] Fix build pyreindexer with reindexer v.4.15
Oct 16, 2024
2f01596
Try fix MacOS tests
Oct 17, 2024
ce25efa
Merge branch 'fix/rework_code_according_to_valgrinds_recommendations'…
Oct 17, 2024
da03def
Correct style
Oct 17, 2024
e016c33
Update tests
Oct 17, 2024
a7b03c2
Reorganize README
Oct 17, 2024
d0d732d
Correct test_item
Oct 18, 2024
afa4770
Merge branch 'fix/build_pyreindexer_with_v4.15.0_GetAggregationResult…
Oct 18, 2024
99099fa
Update transaction test
Oct 23, 2024
ae9dcd4
Simplify code
Oct 23, 2024
5454cf9
Style changes
Oct 23, 2024
247b231
Start of implementation of query builder
Oct 23, 2024
b3fad40
Part I: start of implementation of query builder
Oct 24, 2024
1f53241
Part II: start of implementation of query builder
Oct 25, 2024
122c00c
Part III: start of implementation of query builder
Oct 25, 2024
25c868a
Part IV: start of implementation of query builder
Oct 25, 2024
f403305
Merge branch 'master' into feature/add_transaction_support
Oct 25, 2024
a68c5b7
Merge branch 'feature/add_transaction_support' into feature/add_query…
Oct 25, 2024
5b10ceb
Part V: start of implementation of query builder
Oct 25, 2024
cbc34aa
Part VI: start of implementation of query builder
Oct 28, 2024
cd35c30
Add commit_with_count()
Oct 28, 2024
8f7542d
Add commit_with_count(). Fix code and check in test
Oct 29, 2024
0972bc7
Merge branch 'feature/add_transaction_support' into feature/add_query…
Oct 29, 2024
c9070b4
Rewrite query_wrapper
Oct 29, 2024
30b8806
Rewrite query_wrapper. Part II
Oct 30, 2024
bd55939
Rewrite query_wrapper. Part III: add where for Variant
Oct 30, 2024
2d64757
Rewrite query_wrapper. Part IV: add where for Variant. Clean code
Oct 30, 2024
2d6423f
Part VII: start of implementation of query builder. Add where
Oct 30, 2024
5280a2b
Part VIII: start of implementation of query builder. Add Point and dw…
Oct 30, 2024
dd7aceb
Part IX: start of implementation of query builder. Add simple aggregate
Oct 30, 2024
35dd3ed
Part X: start of implementation of query builder. Add sort
Oct 31, 2024
eee1335
Part XI: start of implementation of query builder. Add aggregation
Oct 31, 2024
87cf4a6
Part XI: start of implementation of query builder. Add aggregation. Fix
Nov 1, 2024
3e04ec2
Part XII: start of implementation of query builder. Add SetObject
Nov 1, 2024
ea30af2
Part XIII: start of implementation of query builder. Add Join
Nov 1, 2024
066efca
Part XIV: start of implementation of query builder. Add Delete. Part I
Nov 1, 2024
c14a9ae
Part XV: start of implementation of query builder. Add Insert\Delete\…
Nov 2, 2024
5cd9e40
Part XV: start of implementation of query builder. Add Insert\Delete\…
Nov 2, 2024
d051dba
Part XV: start of implementation of query builder. Add Insert\Delete\…
Nov 5, 2024
cc74f67
[ref] tests
Nov 5, 2024
e6fee62
Part XVI: start of implementation of query builder. Add Get(firstItem…
Nov 5, 2024
4286c1b
Part XVII: clear code. Hide all context unimplemented functions
Nov 5, 2024
f412d96
Prevent warning on build
Nov 5, 2024
e57ed58
Update comments
Nov 6, 2024
4ab751e
[ref] more test refactoring
Nov 6, 2024
5f242fe
[fix] bump setup version
Nov 6, 2024
7fca19e
[fix] fix imports for ci
Nov 6, 2024
35c2fd0
[fix] bump version
Nov 6, 2024
e57fa7a
[fix] update package_data
Nov 6, 2024
20b70d9
[fix] fix imports and package_data
Nov 6, 2024
5351f97
Part XVIII: add using fetch count
Nov 6, 2024
c328388
Merge branch 'ref_tests' into feature/add_query_support
Nov 6, 2024
c629d18
[ref] small code ref
Nov 6, 2024
4b40a64
[ref] ref tests with tx
Nov 6, 2024
319e615
Merge remote-tracking branch 'origin/feature/add_query_support' into …
Nov 6, 2024
c255552
Clear code after review
Nov 6, 2024
dc5a7d3
Part XVIII: rollback using fetch count. Fix execute method
Nov 6, 2024
d5bb868
Part XIX: remove Query aliases from connector (select\delete\update)
Nov 6, 2024
02908c5
Simplify example code
Nov 6, 2024
08941cf
Remove `-Wold-style-cast` cmake flag
Nov 6, 2024
d60c9f6
Merge remote-tracking branch 'origin/feature/add_query_support' into …
Nov 7, 2024
e2a8ea8
[fix] fix tests with tx
Nov 7, 2024
0c72d21
add initial query tests
Nov 7, 2024
19e881f
Part XX: remove support of FetchCount from Query object
Nov 7, 2024
17009b8
Simplify code
Nov 7, 2024
b3303be
add new query tests
Nov 7, 2024
c4b8f08
Part XXI: bug fix. Segmentation fault
Nov 8, 2024
3cf7a9a
add new query select tests
Nov 8, 2024
bb98bca
Merge branch 'master' into feature/add_query_support
Nov 8, 2024
edf2f41
Part XXII: update readme generation (pydoc_markdown)
Nov 8, 2024
599410c
Part XXIII: enum for LogLevel
Nov 8, 2024
e4fc259
Small updates
Nov 8, 2024
f38e721
Part XXIV: Add support GetExplainResults
Nov 11, 2024
192e168
add query tests: update, delete
Nov 11, 2024
603ac96
Update markdown
Nov 11, 2024
2553303
Part XXV: Bug fixes
Nov 11, 2024
fae7270
Part XXV: Bug fixes. Part II
Nov 12, 2024
c9e74da
[fix] err for select and update, querytests
Nov 12, 2024
835b7dc
[fix] more query test fixes
Nov 12, 2024
f4946b5
[fix] fix forced_sort test
Nov 13, 2024
49eb82e
Part XXV: Bug fixes. Part III: set & set_object
Nov 13, 2024
60d071a
add more query tests
Nov 13, 2024
b091390
Simplify code
Nov 13, 2024
46afcd5
Part XXV: Bug fixes. Part IV: expected: <[{'id': -1}]> but: was <[{'i…
Nov 13, 2024
7bc415e
add more tests with negative
Nov 13, 2024
2164fcf
add null to create items test
Nov 13, 2024
89a7465
Part XXVI: add return TotalCount
Nov 15, 2024
26d70c2
Part XXVII: disable WAL-queries
Nov 18, 2024
7c0bc72
Part XXVII: disable WAL-queries. Catch unhandled exception
Nov 18, 2024
4480dec
Part XXVIII: Return JOIN result. Part
Nov 19, 2024
9a4abfb
Part XXIX: where_query check
Nov 20, 2024
c57663f
Part XXX: where_composite fixes. Part
Nov 21, 2024
fdcfe30
Part XXX: where_composite fixes. Part
Nov 21, 2024
49ddd64
Part XXX: where_composite fixes. Part. Fix
Nov 21, 2024
cfce014
Part XXX: where_composite done
Nov 21, 2024
4378562
Part XXXI: join support/ Part
Nov 25, 2024
bbf4b41
Part XXXI: join support. Part
Nov 25, 2024
9ec593f
Part XXXI: join support. Simplify code
Nov 26, 2024
6910083
add join tests
Nov 27, 2024
0d09c11
Part XXXII: fix v.4 build
Nov 27, 2024
ca7916e
Corrections and fixes
Nov 29, 2024
c23fb33
Simplify code, correct style
Nov 29, 2024
ceea55b
Merge branch 'master' into feature/add_query_support
Dec 2, 2024
1f726be
Fix. Add default value
Dec 2, 2024
d2c0a66
Update minimal Reindexer version
Dec 2, 2024
1bccaae
add exceptions
Dec 3, 2024
fcba1b5
fix raises in docstrings
Dec 3, 2024
aaeb1fa
small ref, fix expected exception
Dec 4, 2024
b4360b3
Update readme file
Dec 4, 2024
28ff2dc
Fix build
Dec 4, 2024
4748384
Fix build
Dec 4, 2024
794eb5f
Fix build
Dec 5, 2024
ab66015
Review changes
Dec 5, 2024
a39b966
Review changes
Dec 5, 2024
f280c66
Review changes
Dec 9, 2024
1325842
Merge branch 'master' into feature/add_query_support
Dec 9, 2024
1e4a49f
Review changes. Fix build
Dec 9, 2024
efd1436
Add query in transaction
Dec 10, 2024
b15395b
Update version
Dec 10, 2024
2fc87d9
Delete changes for transaction support query modify
Dec 11, 2024
7984ea1
Review changes
Dec 12, 2024
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
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ Adds where condition to DB query with interface args for composite indexes
### Query.where\_uuid

```python
def where_uuid(index: str, condition: CondType, *keys: str) -> Query
def where_uuid(index: str, condition: CondType, *uuids: UUID) -> Query
```

Adds where condition to DB query with UUID as string args.
Expand All @@ -848,7 +848,7 @@ Adds where condition to DB query with UUID as string args.
#### Arguments:
index (string): Field name used in condition clause
condition (:enum:`CondType`): Type of condition
keys (*string): Value of index to be compared with. For composite indexes keys must be list,
uuids (*:obj:`UUID`): Value of index to be compared with. For composite indexes uuids must be list,
with value of each sub-index

#### Returns:
Expand Down Expand Up @@ -1051,19 +1051,21 @@ Gets fields facet value. Applicable to multiple data fields and the result of th

```python
def sort(
index: str,
desc: bool = False,
keys: Union[simple_types, tuple[list[simple_types],
...]] = None) -> Query
index: str,
desc: bool = False,
forced_sort_values: Union[simple_types, tuple[list[simple_types],
...]] = None
) -> Query
```

Applies sort order to return from query items. If values argument specified, then items equal to values,
if found will be placed in the top positions. Forced sort is support for the first sorting field only
Applies sort order to return from query items. If forced_sort_values argument specified, then items equal to
values, if found will be placed in the top positions. Forced sort is support for the first sorting field
only

#### Arguments:
index (string): The index name
desc (bool): Sort in descending order
keys (union[simple_types, (list[simple_types], ...)]):
forced_sort_values (union[simple_types, (list[simple_types], ...)]):
Value of index to match. For composite indexes keys must be list, with value of each sub-index

#### Returns:
Expand Down
28 changes: 17 additions & 11 deletions pyreindexer/example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,20 @@ def select_item_query_example(db, namespace):

return db.select("SELECT * FROM " + namespace + " WHERE name='" + item_name_for_lookup + "'")

def select_all_item_query_example(db, namespace):
return db.select("SELECT * FROM " + namespace)

def print_all_records_from_namespace(db, namespace, message):
selected_items_tr = select_all_item_query_example(db, namespace)

res_count = selected_items_tr.count()
print(message, res_count)

for item in selected_items_tr:
print('Item: ', item)

def transaction_example(db, namespace, items_in_base):
# start transaction
transaction = db.new_transaction(namespace)

items_count = len(items_in_base)
Expand All @@ -75,18 +87,10 @@ def transaction_example(db, namespace, items_in_base):
# stop transaction and commit changes to namespace
transaction.commit()

# print records from namespace
selected_items_tr = select_item_query_example(db, namespace)

res_count = selected_items_tr.count()
print('Transaction results count: ', res_count)

# disposable QueryResults iterator
for item in selected_items_tr:
print('Item: ', item)

print_all_records_from_namespace(db, namespace, 'Transaction results count: ')

def query_example(db, namespace):
# query all items
any_items = (db.new_query(namespace)
.where('value', CondType.CondAny)
.sort('id')
Expand All @@ -95,6 +99,7 @@ def query_example(db, namespace):
for item in any_items:
print('Item: ', item)

# query some items
selected_items = (db.new_query(namespace)
.where('value', CondType.CondEq, 'check')
.sort('id')
Expand All @@ -104,19 +109,20 @@ def query_example(db, namespace):
for item in selected_items:
print('Item: ', item)

# delete some items
del_count = (db.new_query(namespace)
.where('name', CondType.CondEq, 'item_1')
.delete())
print(f'Deleted count: {del_count}')

# query all actual items
any_items = (db.new_query(namespace)
.where('value', CondType.CondAny)
.must_execute())
print(f'Query results count (Any after delete): {any_items.count()}')
for item in any_items:
print('Item: ', item)


def rx_example():
db = RxConnector('builtin:///tmp/pyrx')
# db = RxConnector('cproto://127.0.0.1:6534/pyrx')
Expand Down
12 changes: 6 additions & 6 deletions pyreindexer/lib/include/pyobjtools.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ void PyObjectToJson(PyObject** obj, reindexer::WrSerializer& wrSer) {
}
}

std::vector<std::string> PyObjectToJson(PyObject** obj) {
std::vector<std::string> values;
reindexer::h_vector<std::string, 2> PyObjectToJson(PyObject** obj) {
reindexer::h_vector<std::string, 2> values;

reindexer::WrSerializer wrSer;
if (PyDict_Check(*obj)) {
Expand Down Expand Up @@ -132,8 +132,8 @@ std::vector<std::string> PyObjectToJson(PyObject** obj) {
return values;
}

std::vector<std::string> ParseStrListToStrVec(PyObject** list) {
std::vector<std::string> result;
reindexer::h_vector<std::string, 2> ParseStrListToStrVec(PyObject** list) {
reindexer::h_vector<std::string, 2> result;

Py_ssize_t sz = PyList_Size(*list);
result.reserve(sz);
Expand Down Expand Up @@ -191,8 +191,8 @@ reindexer::Variant convert(PyObject** value) {
return {};
}

std::vector<reindexer::Variant> ParseListToVec(PyObject** list) {
std::vector<reindexer::Variant> result;
reindexer::VariantArray ParseListToVec(PyObject** list) {
reindexer::VariantArray result;

Py_ssize_t sz = PyList_Size(*list);
result.reserve(sz);
Expand Down
7 changes: 4 additions & 3 deletions pyreindexer/lib/include/pyobjtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
#include <Python.h>
#include <vector>
#include "core/keyvalue/variant.h"
#include "estl/h_vector.h"

namespace pyreindexer {

std::vector<std::string> ParseStrListToStrVec(PyObject** list);
std::vector<reindexer::Variant> ParseListToVec(PyObject** list);
reindexer::h_vector<std::string, 2> ParseStrListToStrVec(PyObject** list);
reindexer::VariantArray ParseListToVec(PyObject** list);

void PyObjectToJson(PyObject** obj, reindexer::WrSerializer& wrSer);
std::vector<std::string> PyObjectToJson(PyObject** obj);
reindexer::h_vector<std::string, 2> PyObjectToJson(PyObject** obj);
PyObject* PyObjectFromJson(reindexer::span<char> json);

} // namespace pyreindexer
58 changes: 31 additions & 27 deletions pyreindexer/lib/include/query_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

#include "core/query/query.h"
#include "core/keyvalue/uuid.h"
#include "queryresults_wrapper.h"

namespace pyreindexer {
QueryWrapper::QueryWrapper(DBInterface* db, std::string_view ns) : db_{db} {
assert(db_);
ser_.PutVString(ns);
}

void QueryWrapper::Where(std::string_view index, CondType condition, const std::vector<reindexer::Variant>& keys) {
void QueryWrapper::Where(std::string_view index, CondType condition, const reindexer::VariantArray& keys) {
ser_.PutVarUint(QueryItemType::QueryCondition);
ser_.PutVString(index);
ser_.PutVarUint(nextOperation_);
Expand All @@ -22,7 +23,7 @@ void QueryWrapper::Where(std::string_view index, CondType condition, const std::
++queriesCount_;
}

void QueryWrapper::WhereSubQuery(QueryWrapper& query, CondType condition, const std::vector<reindexer::Variant>& keys) {
void QueryWrapper::WhereSubQuery(QueryWrapper& query, CondType condition, const reindexer::VariantArray& keys) {
ser_.PutVarUint(QueryItemType::QuerySubQueryCondition);
ser_.PutVarUint(nextOperation_);
ser_.PutVString(query.ser_.Slice());
Expand All @@ -44,7 +45,8 @@ void QueryWrapper::WhereFieldSubQuery(std::string_view index, CondType condition
++queriesCount_;
}

void QueryWrapper::WhereUUID(std::string_view index, CondType condition, const std::vector<std::string>& keys) {
void QueryWrapper::WhereUUID(std::string_view index, CondType condition,
const reindexer::h_vector<std::string, 2>& keys) {
ser_.PutVarUint(QueryItemType::QueryCondition);
ser_.PutVString(index);
ser_.PutVarUint(nextOperation_);
Expand Down Expand Up @@ -129,7 +131,7 @@ void QueryWrapper::Aggregate(std::string_view index, AggType type) {
ser_.PutVString(index);
}

void QueryWrapper::Aggregation(const std::vector<std::string>& fields) {
void QueryWrapper::Aggregation(const reindexer::h_vector<std::string, 2>& fields) {
ser_.PutVarUint(QueryItemType::QueryAggregation);
ser_.PutVarUint(AggType::AggFacet);
ser_.PutVarUint(fields.size());
Expand All @@ -138,11 +140,11 @@ void QueryWrapper::Aggregation(const std::vector<std::string>& fields) {
}
}

void QueryWrapper::Sort(std::string_view index, bool desc, const std::vector<reindexer::Variant>& keys) {
void QueryWrapper::Sort(std::string_view index, bool desc, const reindexer::VariantArray& sortValues) {
ser_.PutVarUint(QueryItemType::QuerySortIndex);
ser_.PutVString(index);
ser_.PutVarUint(desc? 1 : 0);
putKeys(keys);
putKeys(sortValues);
}

void QueryWrapper::LogOp(OpType op) {
Expand Down Expand Up @@ -183,13 +185,14 @@ void serializeJoinQuery(JoinType type, reindexer::WrSerializer& data, reindexer:
}
} // namespace

void QueryWrapper::addJoinQueries(const std::vector<QueryWrapper*>& queries, reindexer::WrSerializer& buffer) const {
void QueryWrapper::addJoinQueries(const reindexer::h_vector<QueryWrapper*, 1>& queries, reindexer::WrSerializer& buffer)
const {
for (auto query : queries) {
serializeJoinQuery(query->joinType_, query->ser_, buffer);
}
}

reindexer::Error QueryWrapper::prepareQuery(reindexer::Query& query) {
reindexer::Error QueryWrapper::buildQuery(reindexer::Query& query) {
reindexer::Error error = errOK;
try {
// current query (root)
Expand Down Expand Up @@ -217,9 +220,9 @@ reindexer::Error QueryWrapper::prepareQuery(reindexer::Query& query) {
return error;
}

reindexer::Error QueryWrapper::ExecuteQuery(ExecuteType type, QueryResultsWrapper& qr) {
reindexer::Error QueryWrapper::SelectQuery(std::unique_ptr<QueryResultsWrapper>& qr) {
reindexer::Query query;
auto err = prepareQuery(query);
auto err = buildQuery(query);
if (!err.ok()) {
return err;
}
Expand All @@ -228,29 +231,30 @@ reindexer::Error QueryWrapper::ExecuteQuery(ExecuteType type, QueryResultsWrappe
return reindexer::Error(ErrorCode::errQueryExec, "WAL queries are not supported");
}

switch (type) {
case ExecuteType::Select:
err = db_->SelectQuery(query, qr);
break;
case ExecuteType::Update:
err = db_->UpdateQuery(query, qr);
break;
default:
assert(false);
qr = std::make_unique<QueryResultsWrapper>(db_);
return db_->SelectQuery(query, *qr);
}

reindexer::Error QueryWrapper::UpdateQuery(std::unique_ptr<QueryResultsWrapper>& qr) {
reindexer::Query query;
auto err = buildQuery(query);
if (!err.ok()) {
return err;
}
return err;
qr = std::make_unique<QueryResultsWrapper>(db_);
return db_->UpdateQuery(query, *qr);
}

reindexer::Error QueryWrapper::DeleteQuery(size_t& count) {
reindexer::Query query;
auto err = prepareQuery(query);
auto err = buildQuery(query);
if (!err.ok()) {
return err;
}
return db_->DeleteQuery(query, count);
}

void QueryWrapper::Set(std::string_view field, const std::vector<reindexer::Variant>& values,
void QueryWrapper::Set(std::string_view field, const reindexer::VariantArray& values,
IsExpression isExpression) {
ser_.PutVarUint(QueryItemType::QueryUpdateFieldV2);
ser_.PutVString(field);
Expand All @@ -262,7 +266,7 @@ void QueryWrapper::Set(std::string_view field, const std::vector<reindexer::Vari
}
}

void QueryWrapper::SetObject(std::string_view field, const std::vector<std::string>& values) {
void QueryWrapper::SetObject(std::string_view field, const reindexer::h_vector<std::string, 2>& values) {
ser_.PutVarUint(QueryItemType::QueryUpdateObject);
ser_.PutVString(field);
ser_.PutVarUint(values.size()); // values count
Expand Down Expand Up @@ -315,21 +319,21 @@ reindexer::Error QueryWrapper::On(std::string_view joinField, CondType condition
return errOK;
}

void QueryWrapper::SelectFilter(const std::vector<std::string>& fields) {
void QueryWrapper::SelectFilter(const reindexer::h_vector<std::string, 2>& fields) {
for (const auto& field : fields) {
ser_.PutVarUint(QueryItemType::QuerySelectFilter);
ser_.PutVString(field);
}
}

void QueryWrapper::AddFunctions(const std::vector<std::string>& functions) {
void QueryWrapper::AddFunctions(const reindexer::h_vector<std::string, 2>& functions) {
for (const auto& function : functions) {
ser_.PutVarUint(QueryItemType::QuerySelectFunction);
ser_.PutVString(function);
}
}

void QueryWrapper::AddEqualPosition(const std::vector<std::string>& equalPositions) {
void QueryWrapper::AddEqualPosition(const reindexer::h_vector<std::string, 2>& equalPositions) {
ser_.PutVarUint(QueryItemType::QueryEqualPosition);
ser_.PutVarUint(openedBrackets_.empty()? 0 : int(openedBrackets_.back() + 1));
ser_.PutVarUint(equalPositions.size());
Expand All @@ -338,7 +342,7 @@ void QueryWrapper::AddEqualPosition(const std::vector<std::string>& equalPositio
}
}

void QueryWrapper::putKeys(const std::vector<reindexer::Variant>& keys) {
void QueryWrapper::putKeys(const reindexer::VariantArray& keys) {
ser_.PutVarUint(keys.size());
for (const auto& key : keys) {
ser_.PutVariant(key);
Expand Down
Loading