Skip to content

Commit f890229

Browse files
authored
feat: show tag value command support order by clause (#212)
Signed-off-by: Young Xu <[email protected]>
1 parent f19dc1d commit f890229

File tree

2 files changed

+123
-23
lines changed

2 files changed

+123
-23
lines changed

opengemini/command_test.go

+111-23
Original file line numberDiff line numberDiff line change
@@ -177,34 +177,36 @@ func TestClient_ShowTagValues(t *testing.T) {
177177
}
178178
time.Sleep(time.Second * 5)
179179

180-
// SHOW TAG VALUES FROM measurement WITH KEY = location
180+
// SHOW TAG VALUES FROM measurement WITH KEY = location ORDER BY location ASC
181181
tagValueResult, err := c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
182-
With("location"))
182+
With("location").OrderBy("location", Asc))
183183
assert.Nil(t, err)
184184
assert.Equal(t, 4, len(tagValueResult))
185185
expValues := []string{"c1", "c2", "u1", "u2"}
186-
sort.Strings(expValues)
187-
sort.Strings(tagValueResult)
186+
assert.EqualValues(t, expValues, tagValueResult)
187+
188+
// SHOW TAG VALUES FROM measurement WITH KEY = location ORDER BY location DESC
189+
tagValueResult, err = c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
190+
With("location").OrderBy("location", Desc))
191+
assert.Nil(t, err)
192+
assert.Equal(t, 4, len(tagValueResult))
193+
expValues = []string{"u2", "u1", "c2", "c1"}
188194
assert.EqualValues(t, expValues, tagValueResult)
189195

190196
// SHOW TAG VALUES FROM measurement WITH KEY = location LIMIT 2 OFFSET 0
191197
tagValueResult, err = c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
192-
With("location").Limit(2).Offset(0))
198+
With("location").Limit(2).Offset(0).OrderBy("location", Asc))
193199
assert.Nil(t, err)
194200
assert.Equal(t, 2, len(tagValueResult))
195201
expValues = []string{"c1", "c2"}
196-
sort.Strings(expValues)
197-
sort.Strings(tagValueResult)
198202
assert.EqualValues(t, expValues, tagValueResult)
199203

200204
// SHOW TAG VALUES FROM measurement WITH KEY = location LIMIT 2 OFFSET 2
201205
tagValueResult, err = c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
202-
With("location").Limit(2).Offset(2))
206+
With("location").Limit(2).Offset(2).OrderBy("location", Asc))
203207
assert.Nil(t, err)
204208
assert.Equal(t, 2, len(tagValueResult))
205209
expValues = []string{"u1", "u2"}
206-
sort.Strings(expValues)
207-
sort.Strings(tagValueResult)
208210
assert.EqualValues(t, expValues, tagValueResult)
209211

210212
// SHOW TAG VALUES FROM measurement WITH KEY = location LIMIT 2 OFFSET 2 WHERE country = cn
@@ -281,34 +283,36 @@ func TestClient_ShowTagValues_WithRegexp(t *testing.T) {
281283
}
282284
time.Sleep(time.Second * 5)
283285

284-
// SHOW TAG VALUES FROM measurement WITH KEY = /loc.*/
286+
// SHOW TAG VALUES FROM measurement WITH KEY = /loc.*/ order by location ASC
285287
tagValueResult, err := c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
286-
With("/loc.*/"))
288+
With("/loc.*/").OrderBy("location", Asc))
287289
assert.Nil(t, err)
288290
assert.Equal(t, 4, len(tagValueResult))
289291
expValues := []string{"c1", "c2", "u1", "u2"}
290-
sort.Strings(expValues)
291-
sort.Strings(tagValueResult)
292+
assert.EqualValues(t, expValues, tagValueResult)
293+
294+
// SHOW TAG VALUES FROM measurement WITH KEY = /loc.*/ order by location ASC
295+
tagValueResult, err = c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
296+
With("/loc.*/").OrderBy("location", Desc))
297+
assert.Nil(t, err)
298+
assert.Equal(t, 4, len(tagValueResult))
299+
expValues = []string{"u2", "u1", "c2", "c1"}
292300
assert.EqualValues(t, expValues, tagValueResult)
293301

294302
// SHOW TAG VALUES FROM measurement WITH KEY = /loc./ LIMIT 2 OFFSET 0
295303
tagValueResult, err = c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
296-
With("/loc.*/").Limit(2).Offset(0))
304+
With("/loc.*/").Limit(2).Offset(0).OrderBy("location", Asc))
297305
assert.Nil(t, err)
298306
assert.Equal(t, 2, len(tagValueResult))
299307
expValues = []string{"c1", "c2"}
300-
sort.Strings(expValues)
301-
sort.Strings(tagValueResult)
302308
assert.EqualValues(t, expValues, tagValueResult)
303309

304310
// SHOW TAG VALUES FROM measurement WITH KEY = /loc./ LIMIT 2 OFFSET 2
305311
tagValueResult, err = c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
306-
With("/loc.*/").Limit(2).Offset(2))
312+
With("/loc.*/").Limit(2).Offset(2).OrderBy("location", Asc))
307313
assert.Nil(t, err)
308314
assert.Equal(t, 2, len(tagValueResult))
309315
expValues = []string{"u1", "u2"}
310-
sort.Strings(expValues)
311-
sort.Strings(tagValueResult)
312316
assert.EqualValues(t, expValues, tagValueResult)
313317

314318
// SHOW TAG VALUES FROM measurement WITH KEY = /loc./ LIMIT 2 OFFSET 2 WHERE country = cn
@@ -387,7 +391,7 @@ func TestClient_ShowTagValues_WithIn(t *testing.T) {
387391

388392
// SHOW TAG VALUES FROM measurement WITH KEY IN (location, country)
389393
tagValueResult, err := c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
390-
With("location", "country"))
394+
With("location", "country").OrderBy("location", Asc).OrderBy("country", Asc))
391395
assert.Nil(t, err)
392396
assert.Equal(t, 6, len(tagValueResult))
393397
expValues := []string{"c1", "c2", "u1", "u2", "cn", "us"}
@@ -397,13 +401,13 @@ func TestClient_ShowTagValues_WithIn(t *testing.T) {
397401

398402
// SHOW TAG VALUES FROM measurement WITH KEY IN (location, country) LIMIT 2 OFFSET 0
399403
tagValueResult, err = c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
400-
With("location", "country").Limit(2).Offset(0))
404+
With("location", "country").Limit(2).Offset(0).OrderBy("location", Asc))
401405
assert.Nil(t, err)
402406
assert.Equal(t, 2, len(tagValueResult))
403407

404408
// SHOW TAG VALUES FROM measurement WITH KEY IN (location, country) LIMIT 2 OFFSET 2
405409
tagValueResult, err = c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
406-
With("location", "country").Limit(2).Offset(2))
410+
With("location", "country").Limit(2).Offset(2).OrderBy("location", Asc))
407411
assert.Nil(t, err)
408412
assert.Equal(t, 2, len(tagValueResult))
409413

@@ -421,6 +425,90 @@ func TestClient_ShowTagValues_Error_NoWithKey(t *testing.T) {
421425
assert.Equal(t, ErrEmptyTagKey, err)
422426
}
423427

428+
func TestClient_ShowTagValues_OrderBy(t *testing.T) {
429+
c := testDefaultClient(t)
430+
databaseName := randomDatabaseName()
431+
err := c.CreateDatabase(databaseName)
432+
require.Nil(t, err)
433+
measurement := randomMeasurement()
434+
defer func() {
435+
err := c.DropDatabase(databaseName)
436+
assert.Nil(t, err)
437+
}()
438+
callback := func(err error) {
439+
assert.Nil(t, err)
440+
}
441+
442+
points := []*Point{
443+
{
444+
Measurement: measurement,
445+
Tags: map[string]string{
446+
"location": "c1",
447+
"country": "cn",
448+
},
449+
Fields: map[string]interface{}{
450+
"weather": "sun",
451+
"temperature": 25.0,
452+
},
453+
},
454+
{
455+
Measurement: measurement,
456+
Tags: map[string]string{
457+
"location": "c2",
458+
"country": "cn",
459+
},
460+
Fields: map[string]interface{}{
461+
"weather": "sun",
462+
"temperature": 26.0,
463+
},
464+
},
465+
{
466+
Measurement: measurement,
467+
Tags: map[string]string{
468+
"location": "u1",
469+
"country": "us",
470+
},
471+
Fields: map[string]interface{}{
472+
"weather": "sun",
473+
"temperature": 35.0,
474+
},
475+
},
476+
{
477+
Measurement: measurement,
478+
Tags: map[string]string{
479+
"location": "u2",
480+
"country": "us",
481+
},
482+
Fields: map[string]interface{}{
483+
"weather": "sun",
484+
"temperature": 36.0,
485+
},
486+
},
487+
}
488+
489+
for _, point := range points {
490+
err := c.WritePoint(databaseName, point, callback)
491+
assert.Nil(t, err)
492+
}
493+
time.Sleep(time.Second * 5)
494+
495+
// SHOW TAG VALUES FROM measurement WITH KEY = location order by location asc
496+
tagValueResult, err := c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
497+
With("location").OrderBy("location", Asc))
498+
assert.Nil(t, err)
499+
assert.Equal(t, 4, len(tagValueResult))
500+
expValues := []string{"c1", "c2", "u1", "u2"}
501+
assert.EqualValues(t, expValues, tagValueResult)
502+
503+
// SHOW TAG VALUES FROM measurement WITH KEY = location order by location desc
504+
tagValueResult, err = c.ShowTagValues(NewShowTagValuesBuilder().Database(databaseName).Measurement(measurement).
505+
With("location").OrderBy("location", Desc))
506+
assert.Nil(t, err)
507+
assert.Equal(t, 4, len(tagValueResult))
508+
expValues = []string{"u2", "u1", "c2", "c1"}
509+
assert.EqualValues(t, expValues, tagValueResult)
510+
}
511+
424512
func TestClient_ShowSeries(t *testing.T) {
425513
c := testDefaultClient(t)
426514
databaseName := randomDatabaseName()

opengemini/measurement_builder.go

+12
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ type ShowTagValuesBuilder interface {
395395
Limit(limit int) ShowTagValuesBuilder
396396
// Offset specify offset
397397
Offset(offset int) ShowTagValuesBuilder
398+
// OrderBy specify order by
399+
OrderBy(field string, order SortOrder) ShowTagValuesBuilder
398400
// With supports specifying a tag key, a regular expression or multiple tag keys, if set multiple keys, it will
399401
// return all tag field values, if set keys length is one and such as /regex/ it will match the regex, otherwise it
400402
// show one tag field values.
@@ -410,10 +412,16 @@ type showTagValuesBuilder struct {
410412
measurementBase
411413
limit int
412414
offset int
415+
orders []string
413416
withKey []string
414417
where *ComparisonCondition
415418
}
416419

420+
func (s *showTagValuesBuilder) OrderBy(field string, order SortOrder) ShowTagValuesBuilder {
421+
s.orders = append(s.orders, fmt.Sprintf("%s %s", field, order))
422+
return s
423+
}
424+
417425
func NewShowTagValuesBuilder() ShowTagValuesBuilder {
418426
return &showTagValuesBuilder{}
419427
}
@@ -484,6 +492,10 @@ func (s *showTagValuesBuilder) build() (string, error) {
484492
buff.WriteString(" WHERE " + s.where.build())
485493
}
486494

495+
if len(s.orders) != 0 {
496+
buff.WriteString(" ORDER BY " + strings.Join(s.orders, ","))
497+
}
498+
487499
if s.limit > 0 {
488500
buff.WriteString(" LIMIT " + strconv.Itoa(s.limit))
489501
}

0 commit comments

Comments
 (0)