forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemtable_predicate_extractor.go
1176 lines (1088 loc) · 37 KB
/
memtable_predicate_extractor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package core
import (
"bytes"
"fmt"
"math"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/cznic/mathutil"
"github.com/pingcap/kvproto/pkg/coprocessor"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/rowcodec"
"github.com/pingcap/tidb/util/set"
"github.com/pingcap/tidb/util/stringutil"
"github.com/pingcap/tipb/go-tipb"
)
// MemTablePredicateExtractor is used to extract some predicates from `WHERE` clause
// and push the predicates down to the data retrieving on reading memory table stage.
//
// e.g:
// SELECT * FROM cluster_config WHERE type='tikv' AND instance='192.168.1.9:2379'
// We must request all components in the cluster via HTTP API for retrieving
// configurations and filter them by `type/instance` columns.
//
// The purpose of defining a `MemTablePredicateExtractor` is to optimize this
// 1. Define a `ClusterConfigTablePredicateExtractor`
// 2. Extract the `type/instance` columns on the logic optimizing stage and save them via fields.
// 3. Passing the extractor to the `ClusterReaderExecExec` executor
// 4. Executor sends requests to the target components instead of all of the components
type MemTablePredicateExtractor interface {
// Extracts predicates which can be pushed down and returns the remained predicates
Extract(sessionctx.Context, *expression.Schema, []*types.FieldName, []expression.Expression) (remained []expression.Expression)
explainInfo(p *PhysicalMemTable) string
}
// extractHelper contains some common utililty functions for all extractor.
// define an individual struct instead of a bunch of un-exported functions
// to avoid polluting the global scope of current package.
type extractHelper struct{}
func (helper extractHelper) extractColInConsExpr(extractCols map[int64]*types.FieldName, expr *expression.ScalarFunction) (string, []types.Datum) {
args := expr.GetArgs()
col, isCol := args[0].(*expression.Column)
if !isCol {
return "", nil
}
name, found := extractCols[col.UniqueID]
if !found {
return "", nil
}
// All expressions in IN must be a constant
// SELECT * FROM t1 WHERE c IN ('1', '2')
var results []types.Datum
for _, arg := range args[1:] {
constant, ok := arg.(*expression.Constant)
if !ok || constant.DeferredExpr != nil || constant.ParamMarker != nil {
return "", nil
}
results = append(results, constant.Value)
}
return name.ColName.L, results
}
func (helper extractHelper) extractColBinaryOpConsExpr(extractCols map[int64]*types.FieldName, expr *expression.ScalarFunction) (string, []types.Datum) {
args := expr.GetArgs()
var col *expression.Column
var colIdx int
// c = 'rhs'
// 'lhs' = c
for i := 0; i < 2; i++ {
var isCol bool
col, isCol = args[i].(*expression.Column)
if isCol {
colIdx = i
break
}
}
if col == nil {
return "", nil
}
name, found := extractCols[col.UniqueID]
if !found {
return "", nil
}
// The `lhs/rhs` of EQ expression must be a constant
// SELECT * FROM t1 WHERE c='rhs'
// SELECT * FROM t1 WHERE 'lhs'=c
constant, ok := args[1-colIdx].(*expression.Constant)
if !ok || constant.DeferredExpr != nil || constant.ParamMarker != nil {
return "", nil
}
return name.ColName.L, []types.Datum{constant.Value}
}
// extract the OR expression, e.g:
// SELECT * FROM t1 WHERE c1='a' OR c1='b' OR c1='c'
func (helper extractHelper) extractColOrExpr(extractCols map[int64]*types.FieldName, expr *expression.ScalarFunction) (string, []types.Datum) {
args := expr.GetArgs()
lhs, ok := args[0].(*expression.ScalarFunction)
if !ok {
return "", nil
}
rhs, ok := args[1].(*expression.ScalarFunction)
if !ok {
return "", nil
}
// Define an inner function to avoid populate the outer scope
var extract = func(extractCols map[int64]*types.FieldName, fn *expression.ScalarFunction) (string, []types.Datum) {
switch fn.FuncName.L {
case ast.EQ:
return helper.extractColBinaryOpConsExpr(extractCols, fn)
case ast.LogicOr:
return helper.extractColOrExpr(extractCols, fn)
case ast.In:
return helper.extractColInConsExpr(extractCols, fn)
default:
return "", nil
}
}
lhsColName, lhsDatums := extract(extractCols, lhs)
if lhsColName == "" {
return "", nil
}
rhsColName, rhsDatums := extract(extractCols, rhs)
if lhsColName == rhsColName {
return lhsColName, append(lhsDatums, rhsDatums...)
}
return "", nil
}
// merges `lhs` and `datums` with CNF logic
// 1. Returns `datums` set if the `lhs` is an empty set
// 2. Returns the intersection of `datums` and `lhs` if the `lhs` is not an empty set
func (helper extractHelper) merge(lhs set.StringSet, datums []types.Datum, toLower bool) set.StringSet {
tmpNodeTypes := set.NewStringSet()
for _, datum := range datums {
s, err := datum.ToString()
if err != nil {
return nil
}
if toLower {
s = strings.ToLower(s)
}
tmpNodeTypes.Insert(s)
}
if len(lhs) > 0 {
return lhs.Intersection(tmpNodeTypes)
}
return tmpNodeTypes
}
func (helper extractHelper) extractCol(
schema *expression.Schema,
names []*types.FieldName,
predicates []expression.Expression,
extractColName string,
valueToLower bool,
) (
remained []expression.Expression,
skipRequest bool,
result set.StringSet,
) {
remained = make([]expression.Expression, 0, len(predicates))
result = set.NewStringSet()
extractCols := helper.findColumn(schema, names, extractColName)
if len(extractCols) == 0 {
return predicates, false, result
}
// We should use INTERSECTION of sets because of the predicates is CNF array
for _, expr := range predicates {
fn, ok := expr.(*expression.ScalarFunction)
if !ok {
continue
}
var colName string
var datums []types.Datum // the memory of datums should not be reused, they will be put into result.
switch fn.FuncName.L {
case ast.EQ:
colName, datums = helper.extractColBinaryOpConsExpr(extractCols, fn)
case ast.In:
colName, datums = helper.extractColInConsExpr(extractCols, fn)
case ast.LogicOr:
colName, datums = helper.extractColOrExpr(extractCols, fn)
}
if colName == extractColName {
result = helper.merge(result, datums, valueToLower)
skipRequest = len(result) == 0
} else {
remained = append(remained, expr)
}
// There are no data if the low-level executor skip request, so the filter can be droped
if skipRequest {
remained = remained[:0]
break
}
}
return
}
// extracts the string pattern column, e.g:
// SELECT * FROM t WHERE c LIKE '%a%'
// SELECT * FROM t WHERE c LIKE '%a%' AND c REGEXP '.*xxx.*'
// SELECT * FROM t WHERE c LIKE '%a%' OR c REGEXP '.*xxx.*'
func (helper extractHelper) extractLikePatternCol(
schema *expression.Schema,
names []*types.FieldName,
predicates []expression.Expression,
extractColName string,
) (
remained []expression.Expression,
patterns []string,
) {
remained = make([]expression.Expression, 0, len(predicates))
extractCols := helper.findColumn(schema, names, extractColName)
if len(extractCols) == 0 {
return predicates, nil
}
// We use a string array to save multiple patterns because the Golang and Rust don't
// support perl-like CNF regular expression: (?=expr1)(?=expr2).
// e.g:
// SELECT * FROM t WHERE c LIKE '%a%' AND c LIKE '%b%' AND c REGEXP 'gc.*[0-9]{10,20}'
for _, expr := range predicates {
fn, ok := expr.(*expression.ScalarFunction)
if !ok {
remained = append(remained, expr)
continue
}
var canBuildPattern bool
var pattern string
// We use '|' to combine DNF regular expression: .*a.*|.*b.*
// e.g:
// SELECT * FROM t WHERE c LIKE '%a%' OR c LIKE '%b%'
if fn.FuncName.L == ast.LogicOr {
canBuildPattern, pattern = helper.extractOrLikePattern(fn, extractColName, extractCols)
} else {
canBuildPattern, pattern = helper.extractLikePattern(fn, extractColName, extractCols)
}
if canBuildPattern {
patterns = append(patterns, pattern)
} else {
remained = append(remained, expr)
}
}
return
}
func (helper extractHelper) extractOrLikePattern(
orFunc *expression.ScalarFunction,
extractColName string,
extractCols map[int64]*types.FieldName,
) (
ok bool,
pattern string,
) {
predicates := expression.SplitDNFItems(orFunc)
if len(predicates) == 0 {
return false, ""
}
patternBuilder := make([]string, 0, len(predicates))
for _, predicate := range predicates {
fn, ok := predicate.(*expression.ScalarFunction)
if !ok {
return false, ""
}
ok, partPattern := helper.extractLikePattern(fn, extractColName, extractCols)
if !ok {
return false, ""
}
patternBuilder = append(patternBuilder, partPattern)
}
return true, strings.Join(patternBuilder, "|")
}
func (helper extractHelper) extractLikePattern(
fn *expression.ScalarFunction,
extractColName string,
extractCols map[int64]*types.FieldName,
) (
ok bool,
pattern string,
) {
var colName string
var datums []types.Datum
switch fn.FuncName.L {
case ast.EQ, ast.Like, ast.Regexp:
colName, datums = helper.extractColBinaryOpConsExpr(extractCols, fn)
}
if colName == extractColName {
switch fn.FuncName.L {
case ast.EQ:
return true, "^" + regexp.QuoteMeta(datums[0].GetString()) + "$"
case ast.Like:
return true, stringutil.CompileLike2Regexp(datums[0].GetString())
case ast.Regexp:
return true, datums[0].GetString()
default:
return false, ""
}
} else {
return false, ""
}
}
func (helper extractHelper) findColumn(schema *expression.Schema, names []*types.FieldName, colName string) map[int64]*types.FieldName {
extractCols := make(map[int64]*types.FieldName)
for i, name := range names {
if name.ColName.L == colName {
extractCols[schema.Columns[i].UniqueID] = name
}
}
return extractCols
}
// getTimeFunctionName is used to get the (time) function name.
// For the expression that push down to the coprocessor, the function name is different with normal compare function,
// Then getTimeFunctionName will do a sample function name convert.
// Currently, this is used to support query `CLUSTER_SLOW_QUERY` at any time.
func (helper extractHelper) getTimeFunctionName(fn *expression.ScalarFunction) string {
switch fn.Function.PbCode() {
case tipb.ScalarFuncSig_GTTime:
return ast.GT
case tipb.ScalarFuncSig_GETime:
return ast.GE
case tipb.ScalarFuncSig_LTTime:
return ast.LT
case tipb.ScalarFuncSig_LETime:
return ast.LE
case tipb.ScalarFuncSig_EQTime:
return ast.EQ
default:
return fn.FuncName.L
}
}
// extracts the time range column, e.g:
// SELECT * FROM t WHERE time='2019-10-10 10:10:10'
// SELECT * FROM t WHERE time>'2019-10-10 10:10:10' AND time<'2019-10-11 10:10:10'
func (helper extractHelper) extractTimeRange(
ctx sessionctx.Context,
schema *expression.Schema,
names []*types.FieldName,
predicates []expression.Expression,
extractColName string,
timezone *time.Location,
) (
remained []expression.Expression,
// unix timestamp in nanoseconds
startTime int64,
endTime int64,
) {
remained = make([]expression.Expression, 0, len(predicates))
extractCols := helper.findColumn(schema, names, extractColName)
if len(extractCols) == 0 {
return predicates, startTime, endTime
}
for _, expr := range predicates {
fn, ok := expr.(*expression.ScalarFunction)
if !ok {
remained = append(remained, expr)
continue
}
var colName string
var datums []types.Datum
fnName := helper.getTimeFunctionName(fn)
switch fnName {
case ast.GT, ast.GE, ast.LT, ast.LE, ast.EQ:
colName, datums = helper.extractColBinaryOpConsExpr(extractCols, fn)
}
if colName == extractColName {
timeType := types.NewFieldType(mysql.TypeDatetime)
timeType.Decimal = 6
timeDatum, err := datums[0].ConvertTo(ctx.GetSessionVars().StmtCtx, timeType)
if err != nil || timeDatum.Kind() == types.KindNull {
remained = append(remained, expr)
continue
}
mysqlTime := timeDatum.GetMysqlTime()
timestamp := time.Date(mysqlTime.Year(),
time.Month(mysqlTime.Month()),
mysqlTime.Day(),
mysqlTime.Hour(),
mysqlTime.Minute(),
mysqlTime.Second(),
mysqlTime.Microsecond()*1000,
timezone,
).UnixNano()
switch fnName {
case ast.EQ:
startTime = mathutil.MaxInt64(startTime, timestamp)
if endTime == 0 {
endTime = timestamp
} else {
endTime = mathutil.MinInt64(endTime, timestamp)
}
case ast.GT:
// FixMe: add 1ms is not absolutely correct here, just because the log search precision is millisecond.
startTime = mathutil.MaxInt64(startTime, timestamp+int64(time.Millisecond))
case ast.GE:
startTime = mathutil.MaxInt64(startTime, timestamp)
case ast.LT:
if endTime == 0 {
endTime = timestamp - int64(time.Millisecond)
} else {
endTime = mathutil.MinInt64(endTime, timestamp-int64(time.Millisecond))
}
case ast.LE:
if endTime == 0 {
endTime = timestamp
} else {
endTime = mathutil.MinInt64(endTime, timestamp)
}
default:
remained = append(remained, expr)
}
} else {
remained = append(remained, expr)
}
}
return
}
func (helper extractHelper) parseQuantiles(quantileSet set.StringSet) []float64 {
quantiles := make([]float64, 0, len(quantileSet))
for k := range quantileSet {
v, err := strconv.ParseFloat(k, 64)
if err != nil {
// ignore the parse error won't affect result.
continue
}
quantiles = append(quantiles, v)
}
sort.Float64s(quantiles)
return quantiles
}
func (helper extractHelper) extractCols(
schema *expression.Schema,
names []*types.FieldName,
predicates []expression.Expression,
excludeCols set.StringSet,
valueToLower bool) ([]expression.Expression, bool, map[string]set.StringSet) {
cols := map[string]set.StringSet{}
remained := predicates
skipRequest := false
// Extract the label columns.
for _, name := range names {
if excludeCols.Exist(name.ColName.L) {
continue
}
var values set.StringSet
remained, skipRequest, values = helper.extractCol(schema, names, remained, name.ColName.L, valueToLower)
if skipRequest {
return nil, true, nil
}
if len(values) == 0 {
continue
}
cols[name.ColName.L] = values
}
return remained, skipRequest, cols
}
func (helper extractHelper) convertToTime(t int64) time.Time {
if t == 0 || t == math.MaxInt64 {
return time.Now()
}
return time.Unix(0, t)
}
// ClusterTableExtractor is used to extract some predicates of cluster table.
type ClusterTableExtractor struct {
extractHelper
// SkipRequest means the where clause always false, we don't need to request any component
SkipRequest bool
// NodeTypes represents all components types we should send request to.
// e.g:
// 1. SELECT * FROM cluster_config WHERE type='tikv'
// 2. SELECT * FROM cluster_config WHERE type in ('tikv', 'tidb')
NodeTypes set.StringSet
// Instances represents all components instances we should send request to.
// e.g:
// 1. SELECT * FROM cluster_config WHERE instance='192.168.1.7:2379'
// 2. SELECT * FROM cluster_config WHERE type in ('192.168.1.7:2379', '192.168.1.9:2379')
Instances set.StringSet
}
// Extract implements the MemTablePredicateExtractor Extract interface
func (e *ClusterTableExtractor) Extract(_ sessionctx.Context,
schema *expression.Schema,
names []*types.FieldName,
predicates []expression.Expression,
) []expression.Expression {
remained, typeSkipRequest, nodeTypes := e.extractCol(schema, names, predicates, "type", true)
remained, addrSkipRequest, instances := e.extractCol(schema, names, remained, "instance", false)
e.SkipRequest = typeSkipRequest || addrSkipRequest
e.NodeTypes = nodeTypes
e.Instances = instances
return remained
}
func (e *ClusterTableExtractor) explainInfo(p *PhysicalMemTable) string {
if e.SkipRequest {
return "skip_request:true"
}
r := new(bytes.Buffer)
if len(e.NodeTypes) > 0 {
r.WriteString(fmt.Sprintf("node_types:[%s], ", extractStringFromStringSet(e.NodeTypes)))
}
if len(e.Instances) > 0 {
r.WriteString(fmt.Sprintf("instances:[%s], ", extractStringFromStringSet(e.Instances)))
}
// remove the last ", " in the message info
s := r.String()
if len(s) > 2 {
return s[:len(s)-2]
}
return s
}
// ClusterLogTableExtractor is used to extract some predicates of `cluster_config`
type ClusterLogTableExtractor struct {
extractHelper
// SkipRequest means the where clause always false, we don't need to request any component
SkipRequest bool
// NodeTypes represents all components types we should send request to.
// e.g:
// 1. SELECT * FROM cluster_log WHERE type='tikv'
// 2. SELECT * FROM cluster_log WHERE type in ('tikv', 'tidb')
NodeTypes set.StringSet
// Instances represents all components instances we should send request to.
// e.g:
// 1. SELECT * FROM cluster_log WHERE instance='192.168.1.7:2379'
// 2. SELECT * FROM cluster_log WHERE instance in ('192.168.1.7:2379', '192.168.1.9:2379')
Instances set.StringSet
// StartTime represents the beginning time of log message
// e.g: SELECT * FROM cluster_log WHERE time>'2019-10-10 10:10:10.999'
StartTime int64
// EndTime represents the ending time of log message
// e.g: SELECT * FROM cluster_log WHERE time<'2019-10-11 10:10:10.999'
EndTime int64
// Pattern is used to filter the log message
// e.g:
// 1. SELECT * FROM cluster_log WHERE message like '%gc%'
// 2. SELECT * FROM cluster_log WHERE message regexp '.*'
Patterns []string
LogLevels set.StringSet
}
// Extract implements the MemTablePredicateExtractor Extract interface
func (e *ClusterLogTableExtractor) Extract(
ctx sessionctx.Context,
schema *expression.Schema,
names []*types.FieldName,
predicates []expression.Expression,
) []expression.Expression {
// Extract the `type/instance` columns
remained, typeSkipRequest, nodeTypes := e.extractCol(schema, names, predicates, "type", true)
remained, addrSkipRequest, instances := e.extractCol(schema, names, remained, "instance", false)
remained, levlSkipRequest, logLevels := e.extractCol(schema, names, remained, "level", true)
e.SkipRequest = typeSkipRequest || addrSkipRequest || levlSkipRequest
e.NodeTypes = nodeTypes
e.Instances = instances
e.LogLevels = logLevels
if e.SkipRequest {
return nil
}
remained, startTime, endTime := e.extractTimeRange(ctx, schema, names, remained, "time", time.Local)
// The time unit for search log is millisecond.
startTime = startTime / int64(time.Millisecond)
endTime = endTime / int64(time.Millisecond)
e.StartTime = startTime
e.EndTime = endTime
if startTime != 0 && endTime != 0 {
e.SkipRequest = startTime > endTime
}
if e.SkipRequest {
return nil
}
remained, patterns := e.extractLikePatternCol(schema, names, remained, "message")
e.Patterns = patterns
return remained
}
func (e *ClusterLogTableExtractor) explainInfo(p *PhysicalMemTable) string {
if e.SkipRequest {
return "skip_request: true"
}
r := new(bytes.Buffer)
st, et := e.StartTime, e.EndTime
if st > 0 {
st := time.Unix(0, st*1e6)
r.WriteString(fmt.Sprintf("start_time:%v, ", st.In(p.ctx.GetSessionVars().StmtCtx.TimeZone).Format(MetricTableTimeFormat)))
}
if et > 0 {
et := time.Unix(0, et*1e6)
r.WriteString(fmt.Sprintf("end_time:%v, ", et.In(p.ctx.GetSessionVars().StmtCtx.TimeZone).Format(MetricTableTimeFormat)))
}
if len(e.NodeTypes) > 0 {
r.WriteString(fmt.Sprintf("node_types:[%s], ", extractStringFromStringSet(e.NodeTypes)))
}
if len(e.Instances) > 0 {
r.WriteString(fmt.Sprintf("instances:[%s], ", extractStringFromStringSet(e.Instances)))
}
if len(e.LogLevels) > 0 {
r.WriteString(fmt.Sprintf("log_levels:[%s], ", extractStringFromStringSet(e.LogLevels)))
}
// remove the last ", " in the message info
s := r.String()
if len(s) > 2 {
return s[:len(s)-2]
}
return s
}
// MetricTableExtractor is used to extract some predicates of metrics_schema tables.
type MetricTableExtractor struct {
extractHelper
// SkipRequest means the where clause always false, we don't need to request any component
SkipRequest bool
// StartTime represents the beginning time of metric data.
StartTime time.Time
// EndTime represents the ending time of metric data.
EndTime time.Time
// LabelConditions represents the label conditions of metric data.
LabelConditions map[string]set.StringSet
Quantiles []float64
}
func newMetricTableExtractor() *MetricTableExtractor {
e := &MetricTableExtractor{}
e.StartTime, e.EndTime = e.getTimeRange(0, 0)
return e
}
// Extract implements the MemTablePredicateExtractor Extract interface
func (e *MetricTableExtractor) Extract(
ctx sessionctx.Context,
schema *expression.Schema,
names []*types.FieldName,
predicates []expression.Expression,
) []expression.Expression {
// Extract the `quantile` columns
remained, skipRequest, quantileSet := e.extractCol(schema, names, predicates, "quantile", true)
e.Quantiles = e.parseQuantiles(quantileSet)
e.SkipRequest = skipRequest
if e.SkipRequest {
return nil
}
// Extract the `time` columns
remained, startTime, endTime := e.extractTimeRange(ctx, schema, names, remained, "time", ctx.GetSessionVars().StmtCtx.TimeZone)
e.StartTime, e.EndTime = e.getTimeRange(startTime, endTime)
e.SkipRequest = e.StartTime.After(e.EndTime)
if e.SkipRequest {
return nil
}
excludeCols := set.NewStringSet("quantile", "time", "value")
_, skipRequest, extractCols := e.extractCols(schema, names, remained, excludeCols, false)
e.SkipRequest = skipRequest
if e.SkipRequest {
return nil
}
e.LabelConditions = extractCols
// For some metric, the metric reader can't use the predicate, so keep all label conditions remained.
return remained
}
func (e *MetricTableExtractor) getTimeRange(start, end int64) (time.Time, time.Time) {
const defaultMetricQueryDuration = 10 * time.Minute
var startTime, endTime time.Time
if start == 0 && end == 0 {
endTime = time.Now()
return endTime.Add(-defaultMetricQueryDuration), endTime
}
if start != 0 {
startTime = e.convertToTime(start)
}
if end != 0 {
endTime = e.convertToTime(end)
}
if start == 0 {
startTime = endTime.Add(-defaultMetricQueryDuration)
}
if end == 0 {
endTime = startTime.Add(defaultMetricQueryDuration)
}
return startTime, endTime
}
func (e *MetricTableExtractor) explainInfo(p *PhysicalMemTable) string {
if e.SkipRequest {
return "skip_request: true"
}
promQL := e.GetMetricTablePromQL(p.ctx, p.Table.Name.L)
startTime, endTime := e.StartTime, e.EndTime
step := time.Second * time.Duration(p.ctx.GetSessionVars().MetricSchemaStep)
return fmt.Sprintf("PromQL:%v, start_time:%v, end_time:%v, step:%v",
promQL,
startTime.In(p.ctx.GetSessionVars().StmtCtx.TimeZone).Format(MetricTableTimeFormat),
endTime.In(p.ctx.GetSessionVars().StmtCtx.TimeZone).Format(MetricTableTimeFormat),
step,
)
}
// GetMetricTablePromQL uses to get the promQL of metric table.
func (e *MetricTableExtractor) GetMetricTablePromQL(sctx sessionctx.Context, lowerTableName string) string {
quantiles := e.Quantiles
def, err := infoschema.GetMetricTableDef(lowerTableName)
if err != nil {
return ""
}
if len(quantiles) == 0 {
quantiles = []float64{def.Quantile}
}
var buf bytes.Buffer
for i, quantile := range quantiles {
promQL := def.GenPromQL(sctx, e.LabelConditions, quantile)
if i > 0 {
buf.WriteByte(',')
}
buf.WriteString(promQL)
}
return buf.String()
}
// MetricSummaryTableExtractor is used to extract some predicates of metrics_schema tables.
type MetricSummaryTableExtractor struct {
extractHelper
// SkipRequest means the where clause always false, we don't need to request any component
SkipRequest bool
MetricsNames set.StringSet
Quantiles []float64
}
// Extract implements the MemTablePredicateExtractor Extract interface
func (e *MetricSummaryTableExtractor) Extract(
_ sessionctx.Context,
schema *expression.Schema,
names []*types.FieldName,
predicates []expression.Expression,
) (remained []expression.Expression) {
remained, quantileSkip, quantiles := e.extractCol(schema, names, predicates, "quantile", false)
remained, metricsNameSkip, metricsNames := e.extractCol(schema, names, predicates, "metrics_name", true)
e.SkipRequest = quantileSkip || metricsNameSkip
e.Quantiles = e.parseQuantiles(quantiles)
e.MetricsNames = metricsNames
return remained
}
func (e *MetricSummaryTableExtractor) explainInfo(p *PhysicalMemTable) string {
return ""
}
// InspectionResultTableExtractor is used to extract some predicates of `inspection_result`
type InspectionResultTableExtractor struct {
extractHelper
// SkipInspection means the where clause always false, we don't need to request any component
SkipInspection bool
// Rules represents rules applied to, and we should apply all inspection rules if there is no rules specified
// e.g: SELECT * FROM inspection_result WHERE rule in ('ddl', 'config')
Rules set.StringSet
// Items represents items applied to, and we should apply all inspection item if there is no rules specified
// e.g: SELECT * FROM inspection_result WHERE item in ('ddl.lease', 'raftstore.threadpool')
Items set.StringSet
}
// Extract implements the MemTablePredicateExtractor Extract interface
func (e *InspectionResultTableExtractor) Extract(
_ sessionctx.Context,
schema *expression.Schema,
names []*types.FieldName,
predicates []expression.Expression,
) (remained []expression.Expression) {
// Extract the `rule/item` columns
remained, ruleSkip, rules := e.extractCol(schema, names, predicates, "rule", true)
remained, itemSkip, items := e.extractCol(schema, names, remained, "item", true)
e.SkipInspection = ruleSkip || itemSkip
e.Rules = rules
e.Items = items
return remained
}
func (e *InspectionResultTableExtractor) explainInfo(p *PhysicalMemTable) string {
if e.SkipInspection {
return "skip_inspection:true"
}
s := make([]string, 0, 2)
s = append(s, fmt.Sprintf("rules:[%s]", extractStringFromStringSet(e.Rules)))
s = append(s, fmt.Sprintf("items:[%s]", extractStringFromStringSet(e.Items)))
return strings.Join(s, ", ")
}
// InspectionSummaryTableExtractor is used to extract some predicates of `inspection_summary`
type InspectionSummaryTableExtractor struct {
extractHelper
// SkipInspection means the where clause always false, we don't need to request any component
SkipInspection bool
// Rules represents rules applied to, and we should apply all inspection rules if there is no rules specified
// e.g: SELECT * FROM inspection_summary WHERE rule in ('ddl', 'config')
Rules set.StringSet
MetricNames set.StringSet
Quantiles []float64
}
// Extract implements the MemTablePredicateExtractor Extract interface
func (e *InspectionSummaryTableExtractor) Extract(
_ sessionctx.Context,
schema *expression.Schema,
names []*types.FieldName,
predicates []expression.Expression,
) (remained []expression.Expression) {
// Extract the `rule` columns
_, ruleSkip, rules := e.extractCol(schema, names, predicates, "rule", true)
// Extract the `metric_name` columns
_, metricNameSkip, metricNames := e.extractCol(schema, names, predicates, "metrics_name", true)
// Extract the `quantile` columns
remained, quantileSkip, quantileSet := e.extractCol(schema, names, predicates, "quantile", false)
e.SkipInspection = ruleSkip || quantileSkip || metricNameSkip
e.Rules = rules
e.Quantiles = e.parseQuantiles(quantileSet)
e.MetricNames = metricNames
return remained
}
func (e *InspectionSummaryTableExtractor) explainInfo(p *PhysicalMemTable) string {
if e.SkipInspection {
return "skip_inspection: true"
}
r := new(bytes.Buffer)
if len(e.Rules) > 0 {
r.WriteString(fmt.Sprintf("rules:[%s], ", extractStringFromStringSet(e.Rules)))
}
if len(e.MetricNames) > 0 {
r.WriteString(fmt.Sprintf("metric_names:[%s], ", extractStringFromStringSet(e.MetricNames)))
}
if len(e.Quantiles) > 0 {
r.WriteString("quantiles:[")
for i, quantile := range e.Quantiles {
if i > 0 {
r.WriteByte(',')
}
r.WriteString(fmt.Sprintf("%f", quantile))
}
r.WriteString("], ")
}
// remove the last ", " in the message info
s := r.String()
if len(s) > 2 {
return s[:len(s)-2]
}
return s
}
// InspectionRuleTableExtractor is used to extract some predicates of `inspection_rules`
type InspectionRuleTableExtractor struct {
extractHelper
SkipRequest bool
Types set.StringSet
}
// Extract implements the MemTablePredicateExtractor Extract interface
func (e *InspectionRuleTableExtractor) Extract(
_ sessionctx.Context,
schema *expression.Schema,
names []*types.FieldName,
predicates []expression.Expression,
) (remained []expression.Expression) {
// Extract the `type` columns
remained, tpSkip, tps := e.extractCol(schema, names, predicates, "type", true)
e.SkipRequest = tpSkip
e.Types = tps
return remained
}
func (e *InspectionRuleTableExtractor) explainInfo(p *PhysicalMemTable) string {
if e.SkipRequest {
return "skip_request: true"
}
r := new(bytes.Buffer)
if len(e.Types) > 0 {
r.WriteString(fmt.Sprintf("node_types:[%s]", extractStringFromStringSet(e.Types)))
}
return r.String()
}
// SlowQueryExtractor is used to extract some predicates of `slow_query`
type SlowQueryExtractor struct {
extractHelper
SkipRequest bool
TimeRanges []*TimeRange
// Enable is true means the executor should use the time range to locate the slow-log file that need to be parsed.
// Enable is false, means the executor should keep the behavior compatible with before, which is only parse the
// current slow-log file.
Enable bool
Desc bool
}
// TimeRange is used to check whether a given log should be extracted.
type TimeRange struct {
StartTime time.Time
EndTime time.Time
}
// Extract implements the MemTablePredicateExtractor Extract interface
func (e *SlowQueryExtractor) Extract(
ctx sessionctx.Context,
schema *expression.Schema,
names []*types.FieldName,
predicates []expression.Expression,
) []expression.Expression {
remained, startTime, endTime := e.extractTimeRange(ctx, schema, names, predicates, "time", ctx.GetSessionVars().StmtCtx.TimeZone)
e.setTimeRange(startTime, endTime)
e.SkipRequest = e.Enable && e.TimeRanges[0].StartTime.After(e.TimeRanges[0].EndTime)
if e.SkipRequest {
return nil
}
return remained
}
func (e *SlowQueryExtractor) setTimeRange(start, end int64) {
const defaultSlowQueryDuration = 24 * time.Hour
var startTime, endTime time.Time
if start == 0 && end == 0 {
return
}
if start != 0 {
startTime = e.convertToTime(start)
}
if end != 0 {
endTime = e.convertToTime(end)
}
if start == 0 {
startTime = endTime.Add(-defaultSlowQueryDuration)
}
if end == 0 {
endTime = startTime.Add(defaultSlowQueryDuration)
}
timeRange := &TimeRange{
StartTime: startTime,
EndTime: endTime,
}
e.TimeRanges = append(e.TimeRanges, timeRange)
e.Enable = true
}