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

Feat/kafka mapping support #420

Merged
merged 10 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected static function checkAndCreateSource(Client $manticoreClient): void {
$sql = /** @lang ManticoreSearch */
'CREATE TABLE ' . Payload::SOURCE_TABLE_NAME .
' (id bigint, type text, name text attribute indexed, '.
'full_name text, buffer_table text, attrs json, original_query text)';
'full_name text, buffer_table text, attrs json, custom_mapping json, original_query text)';

$request = $manticoreClient->sendRequest($sql);
if ($request->hasError()) {
Expand Down
56 changes: 53 additions & 3 deletions src/Plugin/Queue/Handlers/Source/CreateKafka.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use Manticoresearch\Buddy\Base\Plugin\Queue\Handlers\View\CreateViewHandler;
use Manticoresearch\Buddy\Base\Plugin\Queue\Payload;
use Manticoresearch\Buddy\Core\Error\GenericError;
use Manticoresearch\Buddy\Core\Error\ManticoreSearchClientError;
use Manticoresearch\Buddy\Core\Error\QueryValidationError;
use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
use Manticoresearch\Buddy\Core\Task\TaskResult;
use Manticoresearch\Buddy\Core\Tool\Buddy;
Expand Down Expand Up @@ -160,12 +162,13 @@ public static function handle(Payload $payload, Client $manticoreClient): TaskRe
}

$escapedPayload = str_replace("'", "\\'", $payload->originQuery);
$customMapping = str_replace("'", "\\'", $options->customMapping);

$query = /** @lang ManticoreSearch */
'INSERT INTO ' . Payload::SOURCE_TABLE_NAME .
' (id, type, name, full_name, buffer_table, attrs, original_query) VALUES ' .
' (id, type, name, full_name, buffer_table, attrs, custom_mapping, original_query) VALUES ' .
"(0, '" . self::SOURCE_TYPE_KAFKA . "', '$options->name','{$options->name}_$i'," .
"'{$bufferTablePrefix}{$options->name}_$i', '$attrs', '$escapedPayload')";
"'{$bufferTablePrefix}{$options->name}_$i', '$attrs', '$customMapping', '$escapedPayload')";

$request = $manticoreClient->sendRequest($query);
if ($request->hasError()) {
Expand Down Expand Up @@ -317,7 +320,10 @@ public static function parseOptions(Payload $payload): \stdClass {

$parsedPayload = $payload->model->getPayload();
$result->name = strtolower($parsedPayload['SOURCE']['name']);
$result->schema = strtolower($parsedPayload['SOURCE']['create-def']['base_expr']);

$mapping = self::parseMapping($parsedPayload['SOURCE']['create-def']['sub_tree']);
$result->customMapping = $mapping['customMapping'];
$result->schema = $mapping['schema'];

foreach ($parsedPayload['SOURCE']['options'] as $option) {
if (!isset($option['sub_tree'][0]['base_expr'])) {
Expand All @@ -340,4 +346,48 @@ public static function parseOptions(Payload $payload): \stdClass {
}
return $result;
}

/**
* @param array{
* expr_type: string,
* base_expr: string,
* sub_tree: array{
* expr_type: string,
* base_expr: string,
* sub_tree: array{
* expr_type: string,
* base_expr: string
* }[]
* }[]
* }[] $fields
*
* @return array{customMapping: non-empty-string|false, schema:non-falsy-string}
* @throws GenericError
*/
public static function parseMapping(array $fields): array {
$schema = [];
$customMapping = [];
$pattern = '/^`?([a-zA-Z0-9_]+)`?\s*[\'"]+(.*)[\'"]+\s([a-zA-Z]+)$/usi';

foreach ($fields as $field) {
$definition = strtolower($field['base_expr']);

if (preg_match($pattern, $definition, $matches)) {
$schema[] = $matches[1].' '.$matches[3];
$customMapping[$matches[1]] = $matches[2];
continue;
}

$schema[] = $definition ;
}

$encodedMapping = json_encode($customMapping);
if ($encodedMapping === false) {
QueryValidationError::throw('Incorrect custom mapping provided');
}
return [
'customMapping' => $encodedMapping,
'schema' => '('.implode(',', $schema).')',
];
}
}
6 changes: 6 additions & 0 deletions src/Plugin/Queue/Handlers/Source/DropSourceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ protected static function removeSourceRowData(array $sourceRow, Client $client):
foreach ($queries as $query) {
$request = $client->sendRequest($query);
if ($request->hasError()) {
if (str_contains(
(string)$request->getError(),
"unknown table '".Payload::VIEWS_TABLE_NAME."' in update request"
)) {
continue;
}
throw ManticoreSearchClientError::create((string)$request->getError());
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Plugin/Queue/QueueProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function runPool(): void {
* buffer_table:string,
* destination_name:string,
* query:string,
* custom_mapping: string,
djklim87 marked this conversation as resolved.
Show resolved Hide resolved
* attrs:string } $instance
* @param bool $shouldStart
* @throws \Exception
Expand Down
22 changes: 19 additions & 3 deletions src/Plugin/Queue/Workers/Kafka/KafkaWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class KafkaWorker implements WorkerRunnerInterface

private Client $client;
private string $brokerList;
/** @var array|string[] */
/** @var array<string, string> */
private array $customMapping;
djklim87 marked this conversation as resolved.
Show resolved Hide resolved
/** @var array<int, string> */
private array $topicList;
private string $consumerGroup;
private string $bufferTable;
Expand All @@ -45,6 +47,7 @@ class KafkaWorker implements WorkerRunnerInterface
* full_name:string,
* buffer_table:string,
* destination_name:string,
* custom_mapping: string,
* query:string,
* attrs:string } $instance
*
Expand All @@ -61,6 +64,15 @@ public function __construct(
$this->client = $client;
$this->consumerGroup = $attrs['group'];
$this->brokerList = $attrs['broker'];

$decodedMapping = json_decode($instance['custom_mapping'], true);
if ($decodedMapping === false) {
GenericError::throw(
'Custom mapping decoding error: '.json_last_error_msg()
);
}
/** @var array<string,string> $decodedMapping */
$this->customMapping = $decodedMapping;
$this->bufferTable = $instance['buffer_table'];
$this->topicList = array_map(fn($item) => trim($item), explode(',', $attrs['topic']));
$this->batchSize = (int)$attrs['batch'];
Expand Down Expand Up @@ -208,8 +220,12 @@ private function mapMessages(array $batch): array {
private function handleRow(array $message): array {
$row = [];
foreach ($this->fields as $fieldName => $fieldType) {
if (isset($message[$fieldName])) {
$row[$fieldName] = $this->morphValuesByFieldType($message[$fieldName], $fieldType);
$inputKeyName = $fieldName;
if (isset($this->customMapping[$fieldName])) {
$inputKeyName = $this->customMapping[$fieldName];
}
if (isset($message[$inputKeyName])) {
$row[$fieldName] = $this->morphValuesByFieldType($message[$inputKeyName], $fieldType);
} else {
if (in_array(
$fieldType, [Fields::TYPE_INT,
Expand Down