@@ -343,7 +343,7 @@ func (cw *ClickhouseQueryTranslator) parseIds(queryMap QueryMap) model.SimpleQue
343343 return model .NewSimpleQueryInvalid ()
344344 } else {
345345 tsWithoutTZ := strings .TrimSuffix (string (idAsStr ), " +0000 UTC" )
346- ids [i ] = fmt . Sprintf ( "'%s'" , tsWithoutTZ )
346+ ids [i ] = util . SingleQuote ( tsWithoutTZ )
347347 }
348348 }
349349
@@ -494,7 +494,7 @@ func (cw *ClickhouseQueryTranslator) parseTerm(queryMap QueryMap) model.SimpleQu
494494 return model .NewSimpleQuery (model .TrueExpr , true )
495495 }
496496 fieldName := ResolveField (cw .Ctx , k , cw .Schema )
497- whereClause = model .NewInfixExpr (model .NewColumnRef (fieldName ), "=" , model .NewLiteral (sprint (v )))
497+ whereClause = model .NewInfixExpr (model .NewColumnRef (fieldName ), model . MatchOperator , model .NewLiteral (sprint (v )))
498498 return model .NewSimpleQuery (whereClause , true )
499499 }
500500 }
@@ -521,7 +521,7 @@ func (cw *ClickhouseQueryTranslator) parseTerms(queryMap QueryMap) model.SimpleQ
521521 return model .NewSimpleQueryInvalid ()
522522 }
523523 if len (vAsArray ) == 1 {
524- simpleStatement := model .NewInfixExpr (model .NewColumnRef (k ), "=" , model .NewLiteral (sprint (vAsArray [0 ])))
524+ simpleStatement := model .NewInfixExpr (model .NewColumnRef (k ), model . MatchOperator , model .NewLiteral (sprint (vAsArray [0 ])))
525525 return model .NewSimpleQuery (simpleStatement , true )
526526 }
527527 values := make ([]model.Expr , len (vAsArray ))
@@ -579,7 +579,7 @@ func (cw *ClickhouseQueryTranslator) parseMatch(queryMap QueryMap, matchPhrase b
579579 computedIdMatchingQuery := cw .parseIds (QueryMap {"values" : []interface {}{subQuery }})
580580 statements = append (statements , computedIdMatchingQuery .WhereClause )
581581 } else {
582- fullLiteral := model .NewLiteralWithEscapeType ("'" + subQuery + "'" , model .NotEscapedLikeFull )
582+ fullLiteral := model .NewLiteralWithEscapeType (util . SingleQuote ( subQuery ) , model .NotEscapedLikeFull )
583583 simpleStat := model .NewInfixExpr (model .NewColumnRef (fieldName ), model .MatchOperator , fullLiteral )
584584 statements = append (statements , simpleStat )
585585 }
@@ -639,13 +639,12 @@ func (cw *ClickhouseQueryTranslator) parseMultiMatch(queryMap QueryMap) model.Si
639639 subQueries = strings .Split (queryAsString , " " )
640640 }
641641
642- sqls := make ([]model.Expr , len (fields )* len (subQueries ))
643- i := 0
642+ sqls := make ([]model.Expr , 0 , len (fields )* len (subQueries ))
644643 for _ , field := range fields {
645644 for _ , subQ := range subQueries {
646- simpleStat := model . NewInfixExpr ( model . NewColumnRef ( field ), "iLIKE" , model . NewLiteral ( "'%" + subQ + "%'" ))
647- sqls [ i ] = simpleStat
648- i ++
645+ escaped := util . SingleQuote ( util . SurroundWithPercents ( subQ ))
646+ asLiteral := model . NewLiteralWithEscapeType ( escaped , model . FullyEscaped )
647+ sqls = append ( sqls , model . NewInfixExpr ( model . NewColumnRef ( field ), model . MatchOperator , asLiteral ))
649648 }
650649 }
651650 return model .NewSimpleQuery (model .Or (sqls ), true )
@@ -662,12 +661,12 @@ func (cw *ClickhouseQueryTranslator) parsePrefix(queryMap QueryMap) model.Simple
662661 fieldName = ResolveField (cw .Ctx , fieldName , cw .Schema )
663662 switch vCasted := v .(type ) {
664663 case string :
665- simpleStat := model .NewInfixExpr (model .NewColumnRef (fieldName ), "iLIKE" , model .NewLiteralWithEscapeType (vCasted , model .NotEscapedLikePrefix ))
666- return model .NewSimpleQuery (simpleStat , true )
664+ expr := model .NewInfixExpr (model .NewColumnRef (fieldName ), model . MatchOperator , model .NewLiteralWithEscapeType (vCasted , model .NotEscapedLikePrefix ))
665+ return model .NewSimpleQuery (expr , true )
667666 case QueryMap :
668667 token := vCasted ["value" ].(string )
669- simpleStat := model .NewInfixExpr (model .NewColumnRef (fieldName ), "iLIKE" , model .NewLiteralWithEscapeType (token , model .NotEscapedLikePrefix ))
670- return model .NewSimpleQuery (simpleStat , true )
668+ expr := model .NewInfixExpr (model .NewColumnRef (fieldName ), model . MatchOperator , model .NewLiteralWithEscapeType (token , model .NotEscapedLikePrefix ))
669+ return model .NewSimpleQuery (expr , true )
671670 default :
672671 logger .WarnWithCtx (cw .Ctx ).Msgf ("unsupported prefix type: %T, value: %v" , v , v )
673672 return model .NewSimpleQueryInvalid ()
@@ -693,8 +692,9 @@ func (cw *ClickhouseQueryTranslator) parseWildcard(queryMap QueryMap) model.Simp
693692 if vAsMap , ok := v .(QueryMap ); ok {
694693 if value , ok := vAsMap ["value" ]; ok {
695694 if valueAsString , ok := value .(string ); ok {
696- whereStatement := model .NewInfixExpr (model .NewColumnRef (fieldName ), "iLIKE" , model .NewLiteral ("'" + strings .ReplaceAll (valueAsString , "*" , "%" )+ "'" ))
697- return model .NewSimpleQuery (whereStatement , true )
695+ fitForClickhouse := strings .ReplaceAll (valueAsString , "*" , "%" )
696+ expr := model .NewInfixExpr (model .NewColumnRef (fieldName ), model .MatchOperator , model .NewLiteralWithEscapeType (util .SingleQuote (fitForClickhouse ), model .FullyEscaped ))
697+ return model .NewSimpleQuery (expr , true )
698698 } else {
699699 logger .WarnWithCtx (cw .Ctx ).Msgf ("invalid value type: %T, value: %v" , value , value )
700700 return model .NewSimpleQueryInvalid ()
@@ -805,7 +805,7 @@ func (cw *ClickhouseQueryTranslator) parseRange(queryMap QueryMap) model.SimpleQ
805805 // Numbers use just 3rd
806806
807807 var finalValue model.Expr
808- doneParsing , isQuoted := false , len (value ) > 2 && value [ 0 ] == '\'' && value [ len (value )- 1 ] == '\''
808+ doneParsing , isQuoted := false , len (value ) > 2 && util . IsSingleQuoted (value )
809809 switch fieldType {
810810 case clickhouse .DateTime , clickhouse .DateTime64 :
811811 // TODO add support for "time_zone" parameter in ParseDateUsualFormat
@@ -874,7 +874,6 @@ func (cw *ClickhouseQueryTranslator) parseRange(queryMap QueryMap) model.SimpleQ
874874// - The length of the field value exceeded an ignore_above setting in the mapping
875875// - The field value was malformed and ignore_malformed was defined in the mapping
876876func (cw * ClickhouseQueryTranslator ) parseExists (queryMap QueryMap ) model.SimpleQuery {
877- //sql := model.NewSimpleStatement("")
878877 var sql model.Expr
879878 for _ , v := range queryMap {
880879 fieldName , ok := v .(string )
@@ -949,7 +948,7 @@ func (cw *ClickhouseQueryTranslator) extractFields(fields []interface{}) []strin
949948func sprint (i interface {}) string {
950949 switch i .(type ) {
951950 case string :
952- return fmt . Sprintf ( "'%v'" , i )
951+ return util . SingleQuote ( i .( string ) )
953952 case QueryMap :
954953 iface := i
955954 mapType := iface .(QueryMap )
0 commit comments