From c096c0fbfc89ae4028b0573736aa1d50555a7e1b Mon Sep 17 00:00:00 2001 From: Jacek Migdal Date: Sun, 19 May 2024 20:04:42 +0200 Subject: [PATCH 1/3] Remove redundant table name --- quesma/clickhouse/quesma_communicator.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/quesma/clickhouse/quesma_communicator.go b/quesma/clickhouse/quesma_communicator.go index a1c393b8a..0c815773f 100644 --- a/quesma/clickhouse/quesma_communicator.go +++ b/quesma/clickhouse/quesma_communicator.go @@ -58,7 +58,7 @@ func (lm *LogManager) ProcessQuery(ctx context.Context, table *Table, query *mod return nil, err } - rows, err := executeQuery(ctx, lm, table.Name, query.StringFromColumns(colNames), columns, rowToScan) + rows, err := executeQuery(ctx, lm, query.StringFromColumns(colNames), columns, rowToScan) if err == nil { for _, row := range rows { row.Index = table.Name @@ -106,7 +106,7 @@ func (lm *LogManager) explainQuery(ctx context.Context, query string, elapsed ti } } -func executeQuery(ctx context.Context, lm *LogManager, tableName string, queryAsString string, fields []string, rowToScan []interface{}) ([]model.QueryResultRow, error) { +func executeQuery(ctx context.Context, lm *LogManager, queryAsString string, fields []string, rowToScan []interface{}) ([]model.QueryResultRow, error) { span := lm.phoneHomeAgent.ClickHouseQueryDuration().Begin() rows, err := lm.Query(ctx, queryAsString) @@ -115,7 +115,7 @@ func executeQuery(ctx context.Context, lm *LogManager, tableName string, queryAs return nil, fmt.Errorf("clickhouse: query failed. err: %v, query: %v", err, queryAsString) } - res, err := read(tableName, rows, fields, rowToScan) + res, err := read(rows, fields, rowToScan) elapsed := span.End(nil) if err == nil { if lm.shouldExplainQuery(elapsed) { @@ -128,7 +128,7 @@ func executeQuery(ctx context.Context, lm *LogManager, tableName string, queryAs // 'selectFields' are all values that we return from the query, both columns and non-schema fields, // like e.g. count(), or toInt8(boolField) -func read(tableName string, rows *sql.Rows, selectFields []string, rowToScan []interface{}) ([]model.QueryResultRow, error) { +func read(rows *sql.Rows, selectFields []string, rowToScan []interface{}) ([]model.QueryResultRow, error) { rowDb := make([]interface{}, 0, len(rowToScan)) for i := range rowToScan { rowDb = append(rowDb, &rowToScan[i]) @@ -139,7 +139,7 @@ func read(tableName string, rows *sql.Rows, selectFields []string, rowToScan []i if err != nil { return nil, fmt.Errorf("clickhouse: scan failed: %v", err) } - resultRow := model.QueryResultRow{Index: tableName, Cols: make([]model.QueryResultCol, len(selectFields))} + resultRow := model.QueryResultRow{Cols: make([]model.QueryResultCol, len(selectFields))} for i, field := range selectFields { resultRow.Cols[i] = model.QueryResultCol{ColName: field, Value: rowToScan[i]} } From c1f408e678342c03f438ad1573289190df31512b Mon Sep 17 00:00:00 2001 From: Jacek Migdal Date: Sun, 19 May 2024 20:31:02 +0200 Subject: [PATCH 2/3] Nicer name --- quesma/clickhouse/table.go | 2 +- quesma/queryparser/query_parser.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/quesma/clickhouse/table.go b/quesma/clickhouse/table.go index 61eb42f6a..2b5f0e777 100644 --- a/quesma/clickhouse/table.go +++ b/quesma/clickhouse/table.go @@ -25,7 +25,7 @@ type Table struct { TimestampColumn *string } -func (t *Table) GetFields() []string { +func (t *Table) GetFulltextFields() []string { var res = make([]string, 0) for _, col := range t.Cols { if col.IsFullTextMatch { diff --git a/quesma/queryparser/query_parser.go b/quesma/queryparser/query_parser.go index e9379df6d..2f40b4859 100644 --- a/quesma/queryparser/query_parser.go +++ b/quesma/queryparser/query_parser.go @@ -528,7 +528,7 @@ func (cw *ClickhouseQueryTranslator) parseMultiMatch(queryMap QueryMap) SimpleQu return newSimpleQuery(NewSimpleStatement("invalid fields type"), false) } } else { - fields = cw.Table.GetFields() + fields = cw.Table.GetFulltextFields() } if len(fields) == 0 { return newSimpleQuery(alwaysFalseStatement, true) @@ -646,7 +646,7 @@ func (cw *ClickhouseQueryTranslator) parseQueryString(queryMap QueryMap) SimpleQ if fieldsRaw, ok := queryMap["fields"]; ok { fields = cw.extractFields(fieldsRaw.([]interface{})) } else { - fields = cw.Table.GetFields() + fields = cw.Table.GetFulltextFields() } query := queryMap["query"].(string) // query: (Required, string) @@ -847,7 +847,7 @@ func (cw *ClickhouseQueryTranslator) extractFields(fields []interface{}) []strin continue } if fieldStr == "*" { - return cw.Table.GetFields() + return cw.Table.GetFulltextFields() } fieldStr = cw.Table.ResolveField(cw.Ctx, fieldStr) result = append(result, fieldStr) From e99416e87090535ce6781ca8f85bf167bfd93e93 Mon Sep 17 00:00:00 2001 From: Jacek Migdal Date: Sun, 19 May 2024 22:14:18 +0200 Subject: [PATCH 3/3] Another fix --- quesma/quesma/search.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/quesma/quesma/search.go b/quesma/quesma/search.go index 12ca3dc50..d1b11668d 100644 --- a/quesma/quesma/search.go +++ b/quesma/quesma/search.go @@ -282,6 +282,7 @@ func (q *QueryRunner) handleSearchCommon(ctx context.Context, indexPattern strin } listQuery := queryTranslator.BuildNRowsQuery(fieldName, simpleQuery, queryInfo.Size) hitsFallback, err = q.logManager.ProcessQuery(ctx, table, listQuery, nil) + translatedQueryBody = append(translatedQueryBody, []byte("\n"+listQuery.String()+"\n")...) if err != nil { logger.ErrorWithCtx(ctx).Msgf("error processing fallback query. Err: %v, query: %+v", err, listQuery) pushSecondaryInfo(q.quesmaManagementConsole, id, path, body, translatedQueryBody, responseBody, startTime) @@ -289,6 +290,7 @@ func (q *QueryRunner) handleSearchCommon(ctx context.Context, indexPattern strin } countQuery := queryTranslator.BuildSimpleCountQuery(simpleQuery.Sql.Stmt) countResult, err := q.logManager.ProcessQuery(ctx, table, countQuery, nil) + translatedQueryBody = append(translatedQueryBody, []byte("\n"+countQuery.String()+"\n")...) if err != nil { logger.ErrorWithCtx(ctx).Msgf("error processing count query. Err: %v, query: %+v", err, countQuery) pushSecondaryInfo(q.quesmaManagementConsole, id, path, body, translatedQueryBody, responseBody, startTime)