66 "quesma/logger"
77 "quesma/model"
88 "quesma/model/pipeline_aggregations"
9+ "strings"
910)
1011
1112// CAUTION: maybe "return" everywhere isn't corrent, as maybe there can be multiple pipeline aggregations at one level.
@@ -168,26 +169,29 @@ func (cw *ClickhouseQueryTranslator) parseBucketScriptBasic(queryMap QueryMap) (
168169 if ! ok {
169170 return
170171 }
171- if bucketsPath != pipeline_aggregations .BucketsPathCount {
172+ if ! strings . HasSuffix ( bucketsPath , pipeline_aggregations .BucketsPathCount ) {
172173 logger .WarnWithCtx (cw .Ctx ).Msgf ("buckets_path is not '_count', but %s. Skipping this aggregation" , bucketsPath )
173174 return
174175 }
175176
176- // if ["script"]["source"] != "_value", skip the aggregation
177177 scriptRaw , exists := bucketScript ["script" ]
178178 if ! exists {
179179 logger .WarnWithCtx (cw .Ctx ).Msg ("no script in bucket_script. Skipping this aggregation" )
180180 return
181181 }
182+ if script , ok := scriptRaw .(string ); ok {
183+ return pipeline_aggregations .NewBucketScript (cw .Ctx , script ), true
184+ }
185+
182186 script , ok := scriptRaw .(QueryMap )
183187 if ! ok {
184188 logger .WarnWithCtx (cw .Ctx ).Msgf ("script is not a map, but %T, value: %v. Skipping this aggregation" , scriptRaw , scriptRaw )
185189 return
186190 }
187191 if sourceRaw , exists := script ["source" ]; exists {
188192 if source , ok := sourceRaw .(string ); ok {
189- if source != "_value" {
190- logger .WarnWithCtx (cw .Ctx ).Msgf ("source is not '_value', but %s. Skipping this aggregation" , source )
193+ if source != "_value" && source != "count * 1" {
194+ logger .WarnWithCtx (cw .Ctx ).Msgf ("source is not '_value'/'count * 1' , but %s. Skipping this aggregation" , source )
191195 return
192196 }
193197 } else {
@@ -200,10 +204,10 @@ func (cw *ClickhouseQueryTranslator) parseBucketScriptBasic(queryMap QueryMap) (
200204 }
201205
202206 // okay, we've checked everything, it's indeed a simple count
203- return pipeline_aggregations .NewBucketScript (cw .Ctx ), true
207+ return pipeline_aggregations .NewBucketScript (cw .Ctx , "" ), true
204208}
205209
206- func (cw * ClickhouseQueryTranslator ) parseBucketsPath (shouldBeQueryMap any , aggregationName string ) (bucketsPath string , success bool ) {
210+ func (cw * ClickhouseQueryTranslator ) parseBucketsPath (shouldBeQueryMap any , aggregationName string ) (bucketsPathStr string , success bool ) {
207211 queryMap , ok := shouldBeQueryMap .(QueryMap )
208212 if ! ok {
209213 logger .WarnWithCtx (cw .Ctx ).Msgf ("%s is not a map, but %T, value: %v" , aggregationName , shouldBeQueryMap , shouldBeQueryMap )
@@ -214,10 +218,27 @@ func (cw *ClickhouseQueryTranslator) parseBucketsPath(shouldBeQueryMap any, aggr
214218 logger .WarnWithCtx (cw .Ctx ).Msg ("no buckets_path in avg_bucket" )
215219 return
216220 }
217- bucketsPath , ok = bucketsPathRaw .(string )
218- if ! ok {
219- logger .WarnWithCtx (cw .Ctx ).Msgf ("buckets_path is not a string, but %T, value: %v" , bucketsPathRaw , bucketsPathRaw )
220- return
221+
222+ switch bucketsPath := bucketsPathRaw .(type ) {
223+ case string :
224+ return bucketsPath , true
225+ case QueryMap :
226+ // TODO: handle arbitrary nr of keys (and arbitrary scripts, because we also handle only one special case)
227+ if len (bucketsPath ) == 1 || len (bucketsPath ) == 2 {
228+ for _ , bucketPath := range bucketsPath {
229+ if _ , ok = bucketPath .(string ); ! ok {
230+ logger .WarnWithCtx (cw .Ctx ).Msgf ("buckets_path is not a map with string values, but %T. Skipping this aggregation" , bucketPath )
231+ return
232+ }
233+ // Kinda weird to return just the first value, but seems working on all cases so far.
234+ // After fixing the TODO above, it should also get fixed.
235+ return bucketPath .(string ), true
236+ }
237+ } else {
238+ logger .WarnWithCtx (cw .Ctx ).Msgf ("buckets_path is not a map with one or two keys, but %d. Skipping this aggregation" , len (bucketsPath ))
239+ }
221240 }
222- return bucketsPath , true
241+
242+ logger .WarnWithCtx (cw .Ctx ).Msgf ("buckets_path in wrong format, type: %T, value: %v" , bucketsPathRaw , bucketsPathRaw )
243+ return
223244}
0 commit comments