Skip to content

Commit

Permalink
Added the handling of bigints; updated deps (#335)
Browse files Browse the repository at this point in the history
* Added the handling of bigints; updated deps

* Fix tests and adapt code to proper use of TaskResult::raw

* Updated handling of Struct in Show plugin, tests and deps

* Fixed codestyle

* Updated plugins to fix Struct compatibility issues

* Updated deps

---------

Co-authored-by: nick <[email protected]>
Co-authored-by: Don Hardman <[email protected]>
  • Loading branch information
3 people authored Nov 12, 2024
1 parent 9044180 commit 5d89277
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 83 deletions.
68 changes: 34 additions & 34 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/Plugin/Insert/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Manticoresearch\Buddy\Base\Plugin\Insert\Error\AutoSchemaDisabledError;
use Manticoresearch\Buddy\Core\Error\GenericError;
use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
use Manticoresearch\Buddy\Core\Network\Struct;
use Manticoresearch\Buddy\Core\Plugin\BaseHandlerWithClient;
use Manticoresearch\Buddy\Core\Task\Task;
use Manticoresearch\Buddy\Core\Task\TaskResult;
Expand Down Expand Up @@ -68,7 +69,10 @@ public function run(): Task {
throw new Exception('Empty queries to process');
}

return TaskResult::raw((array)json_decode($resp->getBody(), true));
$struct = Struct::fromJson(
$resp->getBody()
);
return TaskResult::raw($struct);
};
return Task::create(
$taskFn, [$this->payload, $this->manticoreClient]
Expand Down
1 change: 1 addition & 0 deletions src/Plugin/Insert/QueryParser/CheckInsertDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static function checkColTypesError(
protected static function checkPredefinedColTypes(array &$types, array $cols): void {
$predefinedTypes = [
'@timestamp' => Datatype::Timestamp,
'id' => Datatype::Bigint,
];
foreach (array_keys($types) as $i) {
if (!isset($cols[$i], $predefinedTypes[$cols[$i]])) {
Expand Down
11 changes: 7 additions & 4 deletions src/Plugin/Insert/QueryParser/JSONParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Manticoresearch\Buddy\Base\Plugin\Insert\QueryParser;

use Manticoresearch\Buddy\Core\Error\QueryParseError;
use Manticoresearch\Buddy\Core\Network\Struct;

abstract class JSONParser extends BaseParser implements JSONParserInterface {

Expand All @@ -26,13 +27,15 @@ abstract class JSONParser extends BaseParser implements JSONParserInterface {
*/
public function parse($query): array {
$this->cols = $this->colTypes = [];
$row = json_decode($query, true);
if (!is_array($row)) {
$struct = Struct::fromJson($query);
$row = $struct->toArray();
if (!$row || !is_array($row)) {
// checking if query has ndjson format
$queries = static::parseNdJSON($query);
foreach ($queries as $query) {
$row = json_decode($query, true);
if (!is_array($row)) {
$struct = Struct::fromJson($query);
$row = $struct->toArray();
if (!$row || !is_array($row)) {
throw new QueryParseError('Invalid JSON in query');
}
$this->isNdJSON = true;
Expand Down
29 changes: 18 additions & 11 deletions src/Plugin/Knn/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Manticoresearch\Buddy\Core\Error\QueryParseError;
use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
use Manticoresearch\Buddy\Core\ManticoreSearch\Endpoint;
use Manticoresearch\Buddy\Core\Network\Struct;
use Manticoresearch\Buddy\Core\Plugin\BaseHandlerWithClient;
use Manticoresearch\Buddy\Core\Task\Task;
use Manticoresearch\Buddy\Core\Task\TaskResult;
Expand Down Expand Up @@ -72,9 +73,14 @@ private static function getQueryVectorValue(Client $client, Payload $payload): s
}
$document = $request->getResult();

if (is_array($document) && !empty($document[0]['data'])) {
if (!empty($document[0]['data'][0][$payload->field])) {
return $document[0]['data'][0][$payload->field];
if (!isset($document[0])) {
return false;
}
/** @var array{data:array<int,array<string,string>>} $documentStruct */
$documentStruct = $document[0];
if (!empty($documentStruct['data'])) {
if (!empty($documentStruct['data'][0][$payload->field])) {
return $documentStruct['data'][0][$payload->field];
}
return false;
}
Expand All @@ -87,10 +93,10 @@ private static function getQueryVectorValue(Client $client, Payload $payload): s
* @param Client $manticoreClient
* @param Payload $payload
* @param string $queryVector
* @return array <string, string>
* @return Struct<int|string, mixed>
* @throws ManticoreSearchClientError|QueryParseError
*/
private static function getKnnResult(Client $manticoreClient, Payload $payload, string $queryVector): array {
private static function getKnnResult(Client $manticoreClient, Payload $payload, string $queryVector): Struct {
if ($payload->endpointBundle === Endpoint::Search) {
return self::knnHttpQuery($manticoreClient, $payload, $queryVector);
}
Expand All @@ -103,10 +109,10 @@ private static function getKnnResult(Client $manticoreClient, Payload $payload,
* @param Payload $payload
* @param string $queryVector
*
* @return array <string, string>
* @return Struct<int|string, mixed>
* @throws ManticoreSearchClientError|GenericError
*/
private static function knnHttpQuery(Client $manticoreClient, Payload $payload, string $queryVector): array {
private static function knnHttpQuery(Client $manticoreClient, Payload $payload, string $queryVector): Struct {
$query = [
'table' => $payload->table,
'knn' => [
Expand Down Expand Up @@ -156,10 +162,10 @@ function ($val) {
* @param Client $manticoreClient
* @param Payload $payload
* @param string $queryVector
* @return array <string, string>
* @return Struct<int|string, mixed>
* @throws QueryParseError|ManticoreSearchClientError|GenericError
*/
private static function knnSqlQuery(Client $manticoreClient, Payload $payload, string $queryVector): array {
private static function knnSqlQuery(Client $manticoreClient, Payload $payload, string $queryVector): Struct {

self::substituteParsedQuery($payload, $queryVector);

Expand All @@ -172,12 +178,13 @@ private static function knnSqlQuery(Client $manticoreClient, Payload $payload, s
$result = $request->getResult();

if (is_array($result[0])) {
foreach ($result[0]['data'] as $k => $v) {
$resultStruct = &$result[0];
foreach ($resultStruct['data'] as $k => $v) {
if (!isset($v['id']) || $v['id'] !== (int)$payload->docId) {
continue;
}

unset($result[0]['data'][$k]);
unset($resultStruct[0]['data'][$k]);
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/Plugin/Queue/Handlers/BaseGetHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ public function run(): Task {
}

$rawResult = $rawResult->getResult();

if (is_array($rawResult[0]) && empty($rawResult[0]['data'])) {
/** @var array{data:array<int,array<string,string>>} $rawResultStruct */
$rawResultStruct = $rawResult[0];
if (is_array($rawResultStruct) && empty($rawResultStruct['data'])) {
return TaskResult::none();
}

$formattedResult = static::formatResult($rawResult[0]['data'][0]['original_query']);
$formattedResult = static::formatResult($rawResultStruct['data'][0]['original_query']);

$resultData = [
$type => $name,
Expand All @@ -92,7 +93,7 @@ public function run(): Task {
if ($field === 'original_query') {
continue;
}
$resultData[$field] = $rawResult[0]['data'][0][$field];
$resultData[(string)$field] = $rawResultStruct['data'][0][$field];
}

return self::prepareTaskResult($resultData, $type, $fields);
Expand Down
Loading

0 comments on commit 5d89277

Please sign in to comment.