@@ -16,6 +16,7 @@ import (
1616 "quesma/model/typical_queries"
1717 "quesma/queryparser/lucene"
1818 "quesma/quesma/types"
19+ "quesma/schema"
1920 "quesma/util"
2021 "strconv"
2122 "strings"
@@ -232,7 +233,7 @@ func (cw *ClickhouseQueryTranslator) parseMetadata(queryMap QueryMap) QueryMap {
232233}
233234
234235func (cw * ClickhouseQueryTranslator ) ParseAutocomplete (indexFilter * QueryMap , fieldName string , prefix * string , caseIns bool ) model.SimpleQuery {
235- fieldName = cw . ResolveField (cw .Ctx , fieldName )
236+ fieldName = ResolveField (cw .Ctx , fieldName , cw . Schema )
236237 canParse := true
237238 stmts := make ([]model.Expr , 0 )
238239 if indexFilter != nil {
@@ -474,7 +475,7 @@ func (cw *ClickhouseQueryTranslator) parseTerm(queryMap QueryMap) model.SimpleQu
474475 whereClause = model .NewInfixExpr (model .NewLiteral ("0" ), "=" , model .NewLiteral ("0 /* " + k + "=" + sprint (v )+ " */" ))
475476 return model .NewSimpleQuery (whereClause , true )
476477 }
477- fieldName := cw . ResolveField (cw .Ctx , k )
478+ fieldName := ResolveField (cw .Ctx , k , cw . Schema )
478479 whereClause = model .NewInfixExpr (model .NewColumnRef (fieldName ), "=" , model .NewLiteral (sprint (v )))
479480 return model .NewSimpleQuery (whereClause , true )
480481 }
@@ -540,7 +541,7 @@ func (cw *ClickhouseQueryTranslator) parseMatch(queryMap QueryMap, matchPhrase b
540541 }
541542
542543 for fieldName , v := range queryMap {
543- fieldName = cw . ResolveField (cw .Ctx , fieldName )
544+ fieldName = ResolveField (cw .Ctx , fieldName , cw . Schema )
544545 // (fieldName, v) = either e.g. ("message", "this is a test")
545546 // or ("message", map["query": "this is a test", ...]). Here we only care about "query" until we find a case where we need more.
546547 vUnNested := v
@@ -640,7 +641,7 @@ func (cw *ClickhouseQueryTranslator) parsePrefix(queryMap QueryMap) model.Simple
640641 }
641642
642643 for fieldName , v := range queryMap {
643- fieldName = cw . ResolveField (cw .Ctx , fieldName )
644+ fieldName = ResolveField (cw .Ctx , fieldName , cw . Schema )
644645 switch vCasted := v .(type ) {
645646 case string :
646647 simpleStat := model .NewInfixExpr (model .NewColumnRef (fieldName ), "iLIKE" , model .NewLiteral ("'" + vCasted + "%'" ))
@@ -670,7 +671,7 @@ func (cw *ClickhouseQueryTranslator) parseWildcard(queryMap QueryMap) model.Simp
670671 }
671672
672673 for fieldName , v := range queryMap {
673- fieldName = cw . ResolveField (cw .Ctx , fieldName )
674+ fieldName = ResolveField (cw .Ctx , fieldName , cw . Schema )
674675 if vAsMap , ok := v .(QueryMap ); ok {
675676 if value , ok := vAsMap ["value" ]; ok {
676677 if valueAsString , ok := value .(string ); ok {
@@ -762,9 +763,9 @@ func (cw *ClickhouseQueryTranslator) parseRange(queryMap QueryMap) model.SimpleQ
762763 const dateInSchemaExpected = true
763764
764765 for fieldName , v := range queryMap {
765- fieldName = cw . ResolveField (cw .Ctx , fieldName )
766+ fieldName = ResolveField (cw .Ctx , fieldName , cw . Schema )
766767
767- fieldType := cw .Table .GetDateTimeType (cw .Ctx , cw . ResolveField (cw .Ctx , fieldName ), dateInSchemaExpected )
768+ fieldType := cw .Table .GetDateTimeType (cw .Ctx , ResolveField (cw .Ctx , fieldName , cw . Schema ), dateInSchemaExpected )
768769 stmts := make ([]model.Expr , 0 )
769770 if _ , ok := v .(QueryMap ); ! ok {
770771 logger .WarnWithCtx (cw .Ctx ).Msgf ("invalid range type: %T, value: %v" , v , v )
@@ -943,7 +944,7 @@ func (cw *ClickhouseQueryTranslator) extractFields(fields []interface{}) []strin
943944 if fieldStr == "*" {
944945 return []string {model .FullTextFieldNamePlaceHolder }
945946 }
946- fieldStr = cw . ResolveField (cw .Ctx , fieldStr )
947+ fieldStr = ResolveField (cw .Ctx , fieldStr , cw . Schema )
947948 result = append (result , fieldStr )
948949 }
949950 return result
@@ -1044,7 +1045,7 @@ func (cw *ClickhouseQueryTranslator) isItListRequest(queryMap QueryMap) (model.H
10441045 }
10451046 }
10461047
1047- resolvedField := cw . ResolveField (cw .Ctx , fieldName )
1048+ resolvedField := ResolveField (cw .Ctx , fieldName , cw . Schema )
10481049 if resolvedField == "*" {
10491050 return model.HitsCountInfo {Typ : model .ListAllFields , RequestedFields : []string {"*" }, Size : size }, true
10501051 }
@@ -1093,11 +1094,11 @@ func (cw *ClickhouseQueryTranslator) parseSortFields(sortMaps any) (sortColumns
10931094 // sortMap has only 1 key, so we can just iterate over it
10941095 for k , v := range sortMap {
10951096 // TODO replace cw.Table.GetFieldInfo with schema.Field[]
1096- if strings .HasPrefix (k , "_" ) && cw .Table .GetFieldInfo (cw .Ctx , cw . ResolveField (cw .Ctx , k )) == clickhouse .NotExists {
1097+ if strings .HasPrefix (k , "_" ) && cw .Table .GetFieldInfo (cw .Ctx , ResolveField (cw .Ctx , k , cw . Schema )) == clickhouse .NotExists {
10971098 // we're skipping ELK internal fields, like "_doc", "_id", etc.
10981099 continue
10991100 }
1100- fieldName := cw . ResolveField (cw .Ctx , k )
1101+ fieldName := ResolveField (cw .Ctx , k , cw . Schema )
11011102 switch v := v .(type ) {
11021103 case QueryMap :
11031104 if order , ok := v ["order" ]; ok {
@@ -1127,7 +1128,7 @@ func (cw *ClickhouseQueryTranslator) parseSortFields(sortMaps any) (sortColumns
11271128 return sortColumns
11281129 case map [string ]interface {}:
11291130 for fieldName , fieldValue := range sortMaps {
1130- if strings .HasPrefix (fieldName , "_" ) && cw .Table .GetFieldInfo (cw .Ctx , cw . ResolveField (cw .Ctx , fieldName )) == clickhouse .NotExists {
1131+ if strings .HasPrefix (fieldName , "_" ) && cw .Table .GetFieldInfo (cw .Ctx , ResolveField (cw .Ctx , fieldName , cw . Schema )) == clickhouse .NotExists {
11311132 // TODO Elastic internal fields will need to be supported in the future
11321133 continue
11331134 }
@@ -1144,7 +1145,7 @@ func (cw *ClickhouseQueryTranslator) parseSortFields(sortMaps any) (sortColumns
11441145
11451146 case map [string ]string :
11461147 for fieldName , fieldValue := range sortMaps {
1147- if strings .HasPrefix (fieldName , "_" ) && cw .Table .GetFieldInfo (cw .Ctx , cw . ResolveField (cw .Ctx , fieldName )) == clickhouse .NotExists {
1148+ if strings .HasPrefix (fieldName , "_" ) && cw .Table .GetFieldInfo (cw .Ctx , ResolveField (cw .Ctx , fieldName , cw . Schema )) == clickhouse .NotExists {
11481149 // TODO Elastic internal fields will need to be supported in the future
11491150 continue
11501151 }
@@ -1180,11 +1181,9 @@ func createSortColumn(fieldName, ordering string) (model.OrderByExpr, error) {
11801181// What prevents us from moving it to transformation pipeline now, is that
11811182// we need to anotate this field somehow in the AST, to be able
11821183// to distinguish it from other fields
1183- func ( cw * ClickhouseQueryTranslator ) ResolveField (ctx context.Context , fieldName string ) string {
1184+ func ResolveField (ctx context.Context , fieldName string , schemaInstance schema. Schema ) string {
11841185 // Alias resolution should occur *after* the query is parsed, not during the parsing
11851186
1186- schemaInstance := cw .Schema
1187-
11881187 fieldName = strings .TrimSuffix (fieldName , ".keyword" )
11891188 fieldName = strings .TrimSuffix (fieldName , ".text" )
11901189
@@ -1221,7 +1220,7 @@ func (cw *ClickhouseQueryTranslator) parseSize(queryMap QueryMap, defaultSize in
12211220func (cw * ClickhouseQueryTranslator ) GetDateTimeTypeFromSelectClause (ctx context.Context , expr model.Expr ,
12221221 dateInSchemaExpected bool ) clickhouse.DateTimeType {
12231222 if ref , ok := expr .(model.ColumnRef ); ok {
1224- return cw .Table .GetDateTimeType (ctx , cw . ResolveField (ctx , ref .ColumnName ), dateInSchemaExpected )
1223+ return cw .Table .GetDateTimeType (ctx , ResolveField (ctx , ref .ColumnName , cw . Schema ), dateInSchemaExpected )
12251224 }
12261225 return clickhouse .Invalid
12271226}
0 commit comments