Skip to content

Commit 7d1e3e7

Browse files
sre-botzz-jason
authored andcommittedAug 5, 2019
ranger: BuildColumnRange should merge ranges when column has p… (#11565)
1 parent b6643fe commit 7d1e3e7

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed
 

‎executor/join_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -1381,3 +1381,13 @@ func (s *testSuite2) TestInjectProjOnTopN(c *C) {
13811381
"2",
13821382
))
13831383
}
1384+
1385+
func (s *testSuite2) TestIssue11544(c *C) {
1386+
tk := testkit.NewTestKit(c, s.store)
1387+
tk.MustExec("use test")
1388+
tk.MustExec("create table 11544t(a int)")
1389+
tk.MustExec("create table 11544tt(a int, b varchar(10), index idx(a, b(3)))")
1390+
tk.MustExec("insert into 11544t values(1)")
1391+
tk.MustExec("insert into 11544tt values(1, 'aaaaaaa'), (1, 'aaaabbb'), (1, 'aaaacccc')")
1392+
tk.MustQuery("select /*+ TIDB_INLJ(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and (tt.b = 'aaaaaaa' or tt.b = 'aaaabbb')").Check(testkit.Rows("1 1 aaaaaaa", "1 1 aaaabbb"))
1393+
}

‎util/ranger/ranger.go

+4
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ func buildColumnRange(accessConditions []expression.Expression, sc *stmtctx.Stat
270270
ran.HighExclude = false
271271
}
272272
}
273+
ranges, err = unionRanges(sc, ranges)
274+
if err != nil {
275+
return nil, err
276+
}
273277
}
274278
return ranges, nil
275279
}

‎util/ranger/ranger_test.go

+42-1
Original file line numberDiff line numberDiff line change
@@ -747,132 +747,151 @@ func (s *testRangerSuite) TestColumnRange(c *C) {
747747
accessConds string
748748
filterConds string
749749
resultStr string
750+
length int
750751
}{
751752
{
752753
colPos: 0,
753754
exprStr: "a = 1 and b > 1",
754755
accessConds: "[eq(test.t.a, 1)]",
755756
filterConds: "[gt(test.t.b, 1)]",
756757
resultStr: "[[1,1]]",
758+
length: types.UnspecifiedLength,
757759
},
758760
{
759761
colPos: 1,
760762
exprStr: "b > 1",
761763
accessConds: "[gt(test.t.b, 1)]",
762764
filterConds: "[]",
763765
resultStr: "[(1,+inf]]",
766+
length: types.UnspecifiedLength,
764767
},
765768
{
766769
colPos: 0,
767770
exprStr: "1 = a",
768771
accessConds: "[eq(1, test.t.a)]",
769772
filterConds: "[]",
770773
resultStr: "[[1,1]]",
774+
length: types.UnspecifiedLength,
771775
},
772776
{
773777
colPos: 0,
774778
exprStr: "a != 1",
775779
accessConds: "[ne(test.t.a, 1)]",
776780
filterConds: "[]",
777781
resultStr: "[[-inf,1) (1,+inf]]",
782+
length: types.UnspecifiedLength,
778783
},
779784
{
780785
colPos: 0,
781786
exprStr: "1 != a",
782787
accessConds: "[ne(1, test.t.a)]",
783788
filterConds: "[]",
784789
resultStr: "[[-inf,1) (1,+inf]]",
790+
length: types.UnspecifiedLength,
785791
},
786792
{
787793
colPos: 0,
788794
exprStr: "a > 1",
789795
accessConds: "[gt(test.t.a, 1)]",
790796
filterConds: "[]",
791797
resultStr: "[(1,+inf]]",
798+
length: types.UnspecifiedLength,
792799
},
793800
{
794801
colPos: 0,
795802
exprStr: "1 < a",
796803
accessConds: "[lt(1, test.t.a)]",
797804
filterConds: "[]",
798805
resultStr: "[(1,+inf]]",
806+
length: types.UnspecifiedLength,
799807
},
800808
{
801809
colPos: 0,
802810
exprStr: "a >= 1",
803811
accessConds: "[ge(test.t.a, 1)]",
804812
filterConds: "[]",
805813
resultStr: "[[1,+inf]]",
814+
length: types.UnspecifiedLength,
806815
},
807816
{
808817
colPos: 0,
809818
exprStr: "1 <= a",
810819
accessConds: "[le(1, test.t.a)]",
811820
filterConds: "[]",
812821
resultStr: "[[1,+inf]]",
822+
length: types.UnspecifiedLength,
813823
},
814824
{
815825
colPos: 0,
816826
exprStr: "a < 1",
817827
accessConds: "[lt(test.t.a, 1)]",
818828
filterConds: "[]",
819829
resultStr: "[[-inf,1)]",
830+
length: types.UnspecifiedLength,
820831
},
821832
{
822833
colPos: 0,
823834
exprStr: "1 > a",
824835
accessConds: "[gt(1, test.t.a)]",
825836
filterConds: "[]",
826837
resultStr: "[[-inf,1)]",
838+
length: types.UnspecifiedLength,
827839
},
828840
{
829841
colPos: 0,
830842
exprStr: "a <= 1",
831843
accessConds: "[le(test.t.a, 1)]",
832844
filterConds: "[]",
833845
resultStr: "[[-inf,1]]",
846+
length: types.UnspecifiedLength,
834847
},
835848
{
836849
colPos: 0,
837850
exprStr: "1 >= a",
838851
accessConds: "[ge(1, test.t.a)]",
839852
filterConds: "[]",
840853
resultStr: "[[-inf,1]]",
854+
length: types.UnspecifiedLength,
841855
},
842856
{
843857
colPos: 0,
844858
exprStr: "(a)",
845859
accessConds: "[test.t.a]",
846860
filterConds: "[]",
847861
resultStr: "[[-inf,0) (0,+inf]]",
862+
length: types.UnspecifiedLength,
848863
},
849864
{
850865
colPos: 0,
851866
exprStr: "a in (1, 3, NULL, 2)",
852867
accessConds: "[in(test.t.a, 1, 3, <nil>, 2)]",
853868
filterConds: "[]",
854869
resultStr: "[[1,1] [2,2] [3,3]]",
870+
length: types.UnspecifiedLength,
855871
},
856872
{
857873
colPos: 0,
858874
exprStr: `a IN (8,8,81,45)`,
859875
accessConds: "[in(test.t.a, 8, 8, 81, 45)]",
860876
filterConds: "[]",
861877
resultStr: `[[8,8] [45,45] [81,81]]`,
878+
length: types.UnspecifiedLength,
862879
},
863880
{
864881
colPos: 0,
865882
exprStr: "a between 1 and 2",
866883
accessConds: "[ge(test.t.a, 1) le(test.t.a, 2)]",
867884
filterConds: "[]",
868885
resultStr: "[[1,2]]",
886+
length: types.UnspecifiedLength,
869887
},
870888
{
871889
colPos: 0,
872890
exprStr: "a not between 1 and 2",
873891
accessConds: "[or(lt(test.t.a, 1), gt(test.t.a, 2))]",
874892
filterConds: "[]",
875893
resultStr: "[[-inf,1) (2,+inf]]",
894+
length: types.UnspecifiedLength,
876895
},
877896
//{
878897
// `a > null` will be converted to `castAsString(a) > null` which can not be extracted as access condition.
@@ -885,97 +904,119 @@ func (s *testRangerSuite) TestColumnRange(c *C) {
885904
accessConds: "[ge(test.t.a, 2) le(test.t.a, 1)]",
886905
filterConds: "[]",
887906
resultStr: "[]",
907+
length: types.UnspecifiedLength,
888908
},
889909
{
890910
colPos: 0,
891911
exprStr: "a not between 2 and 1",
892912
accessConds: "[or(lt(test.t.a, 2), gt(test.t.a, 1))]",
893913
filterConds: "[]",
894914
resultStr: "[[-inf,+inf]]",
915+
length: types.UnspecifiedLength,
895916
},
896917
{
897918
colPos: 0,
898919
exprStr: "a IS NULL",
899920
accessConds: "[isnull(test.t.a)]",
900921
filterConds: "[]",
901922
resultStr: "[[NULL,NULL]]",
923+
length: types.UnspecifiedLength,
902924
},
903925
{
904926
colPos: 0,
905927
exprStr: "a IS NOT NULL",
906928
accessConds: "[not(isnull(test.t.a))]",
907929
filterConds: "[]",
908930
resultStr: "[[-inf,+inf]]",
931+
length: types.UnspecifiedLength,
909932
},
910933
{
911934
colPos: 0,
912935
exprStr: "a IS TRUE",
913936
accessConds: "[istrue(test.t.a)]",
914937
filterConds: "[]",
915938
resultStr: "[[-inf,0) (0,+inf]]",
939+
length: types.UnspecifiedLength,
916940
},
917941
{
918942
colPos: 0,
919943
exprStr: "a IS NOT TRUE",
920944
accessConds: "[not(istrue(test.t.a))]",
921945
filterConds: "[]",
922946
resultStr: "[[NULL,NULL] [0,0]]",
947+
length: types.UnspecifiedLength,
923948
},
924949
{
925950
colPos: 0,
926951
exprStr: "a IS FALSE",
927952
accessConds: "[isfalse(test.t.a)]",
928953
filterConds: "[]",
929954
resultStr: "[[0,0]]",
955+
length: types.UnspecifiedLength,
930956
},
931957
{
932958
colPos: 0,
933959
exprStr: "a IS NOT FALSE",
934960
accessConds: "[not(isfalse(test.t.a))]",
935961
filterConds: "[]",
936962
resultStr: "[[NULL,0) (0,+inf]]",
963+
length: types.UnspecifiedLength,
937964
},
938965
{
939966
colPos: 1,
940967
exprStr: `b in (1, '2.1')`,
941968
accessConds: "[in(test.t.b, 1, 2.1)]",
942969
filterConds: "[]",
943970
resultStr: "[[1,1] [2.1,2.1]]",
971+
length: types.UnspecifiedLength,
944972
},
945973
{
946974
colPos: 0,
947975
exprStr: `a > 9223372036854775807`,
948976
accessConds: "[gt(test.t.a, 9223372036854775807)]",
949977
filterConds: "[]",
950978
resultStr: "[(9223372036854775807,+inf]]",
979+
length: types.UnspecifiedLength,
951980
},
952981
{
953982
colPos: 2,
954983
exprStr: `c > 111.11111111`,
955984
accessConds: "[gt(test.t.c, 111.11111111)]",
956985
filterConds: "[]",
957986
resultStr: "[[111.111115,+inf]]",
987+
length: types.UnspecifiedLength,
958988
},
959989
{
960990
colPos: 3,
961991
exprStr: `d > 'aaaaaaaaaaaaaa'`,
962992
accessConds: "[gt(test.t.d, aaaaaaaaaaaaaa)]",
963993
filterConds: "[]",
964994
resultStr: "[(\"aaaaaaaaaaaaaa\",+inf]]",
995+
length: types.UnspecifiedLength,
965996
},
966997
{
967998
colPos: 4,
968999
exprStr: `e > 18446744073709500000`,
9691000
accessConds: "[gt(test.t.e, 18446744073709500000)]",
9701001
filterConds: "[]",
9711002
resultStr: "[(18446744073709500000,+inf]]",
1003+
length: types.UnspecifiedLength,
9721004
},
9731005
{
9741006
colPos: 4,
9751007
exprStr: `e > -2147483648`,
9761008
accessConds: "[gt(test.t.e, -2147483648)]",
9771009
filterConds: "[]",
9781010
resultStr: "[[0,+inf]]",
1011+
length: types.UnspecifiedLength,
1012+
},
1013+
{
1014+
colPos: 3,
1015+
exprStr: "d = 'aab' or d = 'aac'",
1016+
accessConds: "[or(eq(test.t.d, aab), eq(test.t.d, aac))]",
1017+
filterConds: "[]",
1018+
resultStr: "[[\"a\",\"a\"]]",
1019+
length: 1,
9791020
},
9801021
}
9811022

@@ -1002,7 +1043,7 @@ func (s *testRangerSuite) TestColumnRange(c *C) {
10021043
c.Assert(col, NotNil)
10031044
conds = ranger.ExtractAccessConditionsForColumn(conds, col.UniqueID)
10041045
c.Assert(fmt.Sprintf("%s", conds), Equals, tt.accessConds, Commentf("wrong access conditions for expr: %s", tt.exprStr))
1005-
result, err := ranger.BuildColumnRange(conds, new(stmtctx.StatementContext), col.RetType, types.UnspecifiedLength)
1046+
result, err := ranger.BuildColumnRange(conds, new(stmtctx.StatementContext), col.RetType, tt.length)
10061047
c.Assert(err, IsNil)
10071048
got := fmt.Sprintf("%v", result)
10081049
c.Assert(got, Equals, tt.resultStr, Commentf("different for expr %s, col: %v", tt.exprStr, col))

0 commit comments

Comments
 (0)