From bd89750504537a72a2122296528941a3d2b43014 Mon Sep 17 00:00:00 2001 From: Przemek Delewski Date: Wed, 8 May 2024 11:12:45 +0200 Subject: [PATCH] Unifying interface of ProcessAutocompleteSuggestionsQuery --- quesma/clickhouse/quesma_communicator.go | 9 +++++++-- quesma/quesma/termsenum/terms_enum.go | 6 +++--- quesma/quesma/termsenum/terms_enum_test.go | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/quesma/clickhouse/quesma_communicator.go b/quesma/clickhouse/quesma_communicator.go index a08a1e2c5..0a6e01e25 100644 --- a/quesma/clickhouse/quesma_communicator.go +++ b/quesma/clickhouse/quesma_communicator.go @@ -114,8 +114,13 @@ func executeQuery(ctx context.Context, lm *LogManager, tableName string, queryAs return res, err } -func (lm *LogManager) ProcessAutocompleteSuggestionsQuery(ctx context.Context, table string, query *model.Query) ([]model.QueryResultRow, error) { - return executeQuery(ctx, lm, table, query.String(), query.Fields, []interface{}{""}) +func (lm *LogManager) ProcessAutocompleteSuggestionsQuery(ctx context.Context, table *Table, query *model.Query) ([]model.QueryResultRow, error) { + colNames, err := table.extractColumns(query, false) + if err != nil { + return nil, err + } + rowToScan := make([]interface{}, len(colNames)+len(query.NonSchemaFields)) + return executeQuery(ctx, lm, table.Name, query.String(), query.Fields, rowToScan) } func (lm *LogManager) ProcessGeneralAggregationQuery(ctx context.Context, table *Table, query *model.Query) ([]model.QueryResultRow, error) { diff --git a/quesma/quesma/termsenum/terms_enum.go b/quesma/quesma/termsenum/terms_enum.go index 55a685364..32753d279 100644 --- a/quesma/quesma/termsenum/terms_enum.go +++ b/quesma/quesma/termsenum/terms_enum.go @@ -20,11 +20,11 @@ func HandleTermsEnum(ctx context.Context, index string, reqBody []byte, lm *clic logger.Error().Msg(errorMsg) return nil, fmt.Errorf(errorMsg) } else { - return handleTermsEnumRequest(ctx, resolvedTableName, reqBody, &queryparser.ClickhouseQueryTranslator{ClickhouseLM: lm, Table: lm.FindTable(resolvedTableName), Ctx: context.Background()}, qmc) + return handleTermsEnumRequest(ctx, reqBody, &queryparser.ClickhouseQueryTranslator{ClickhouseLM: lm, Table: lm.FindTable(resolvedTableName), Ctx: context.Background()}, qmc) } } -func handleTermsEnumRequest(ctx context.Context, table string, reqBody []byte, qt *queryparser.ClickhouseQueryTranslator, qmc *ui.QuesmaManagementConsole) (result []byte, err error) { +func handleTermsEnumRequest(ctx context.Context, reqBody []byte, qt *queryparser.ClickhouseQueryTranslator, qmc *ui.QuesmaManagementConsole) (result []byte, err error) { request := NewRequest() startTime := time.Now() if err := request.UnmarshalJSON(reqBody); err != nil { @@ -37,7 +37,7 @@ func handleTermsEnumRequest(ctx context.Context, table string, reqBody []byte, q dbQueryCtx, cancel := context.WithCancel(ctx) // TODO this will be used to cancel goroutine that is executing the query _ = cancel - if rows, err2 := qt.ClickhouseLM.ProcessAutocompleteSuggestionsQuery(dbQueryCtx, table, selectQuery); err2 != nil { + if rows, err2 := qt.ClickhouseLM.ProcessAutocompleteSuggestionsQuery(dbQueryCtx, qt.Table, selectQuery); err2 != nil { logger.Error().Msgf("terms enum failed - error processing SQL query [%s]", err2) result, err = json.Marshal(emptyTermsEnumResponse()) } else { diff --git a/quesma/quesma/termsenum/terms_enum_test.go b/quesma/quesma/termsenum/terms_enum_test.go index 2b0757f9c..c2837e0f1 100644 --- a/quesma/quesma/termsenum/terms_enum_test.go +++ b/quesma/quesma/termsenum/terms_enum_test.go @@ -88,7 +88,7 @@ func TestHandleTermsEnumRequest(t *testing.T) { mock.ExpectQuery(fmt.Sprintf("%s|%s", regexp.QuoteMeta(expectedQuery1), regexp.QuoteMeta(expectedQuery2))). WillReturnRows(sqlmock.NewRows([]string{"client_name"}).AddRow("client_a").AddRow("client_b")) - resp, err := handleTermsEnumRequest(ctx, table.Name, rawRequestBody, qt, managementConsole) + resp, err := handleTermsEnumRequest(ctx, rawRequestBody, qt, managementConsole) assert.NoError(t, err) var responseModel model.TermsEnumResponse