From 2e0292cf5007cc1f8b2a8e07fa4ae4f048404f5c Mon Sep 17 00:00:00 2001 From: Song Gao Date: Mon, 11 Jul 2022 18:57:05 +0800 Subject: [PATCH 1/2] cherry pick #35978 to release-6.1 Signed-off-by: ti-srebot --- .github/licenserc.yml | 1 + executor/analyze_test.go | 26 + executor/testdata/analyze_test_data.sql | 726 ++++++++++++++++++++++++ statistics/cmsketch.go | 2 + statistics/histogram.go | 2 + types/datum.go | 6 + types/etc.go | 5 + util/codec/codec.go | 18 + 8 files changed, 786 insertions(+) create mode 100644 executor/testdata/analyze_test_data.sql diff --git a/.github/licenserc.yml b/.github/licenserc.yml index 9f4890a3a1329..68cb310420edc 100644 --- a/.github/licenserc.yml +++ b/.github/licenserc.yml @@ -31,4 +31,5 @@ header: - 'dumpling/' - 'tidb-binlog/driver/example' - 'tidb-binlog/proto/go-binlog/secondary_binlog.pb.go' + - '**/*.sql' comment: on-failure diff --git a/executor/analyze_test.go b/executor/analyze_test.go index 2aff291c5a818..cf01d3f2bea8f 100644 --- a/executor/analyze_test.go +++ b/executor/analyze_test.go @@ -18,9 +18,14 @@ import ( "context" "encoding/json" "fmt" +<<<<<<< HEAD "strconv" "strings" "sync" +======= + "io/ioutil" + "strings" +>>>>>>> d10d25457... util: let TypeFloat should be decoded as Float32 in Chunk (#35978) "sync/atomic" "testing" "time" @@ -761,6 +766,7 @@ func TestHashInTopN(t *testing.T) { } } +<<<<<<< HEAD func TestNormalAnalyzeOnCommonHandle(t *testing.T) { store, clean := testkit.CreateMockStore(t) defer clean() @@ -3388,4 +3394,24 @@ PARTITION BY RANGE ( a ) ( tk.MustExec("analyze table t partition p0") tbl := h.GetTableStats(tableInfo) require.Equal(t, int64(6), tbl.Columns[tableInfo.Columns[0].ID].Histogram.NDV) +======= +func TestAnalyzePartitionTableForFloat(t *testing.T) { + store, clean := testkit.CreateMockStore(t) + defer clean() + tk := testkit.NewTestKit(t, store) + tk.MustExec("set @@tidb_partition_prune_mode='dynamic'") + tk.MustExec("use test") + tk.MustExec("CREATE TABLE t1 ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, num float(9,8) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin PARTITION BY HASH (id) PARTITIONS 128;") + // To reproduce the error we meet in https://github.com/pingcap/tidb/issues/35910, we should use the data provided in this issue + b, err := ioutil.ReadFile("testdata/analyze_test_data.sql") + require.NoError(t, err) + sqls := strings.Split(string(b), ";") + for _, sql := range sqls { + if len(sql) < 1 { + continue + } + tk.MustExec(sql) + } + tk.MustExec("analyze table t1") +>>>>>>> d10d25457... util: let TypeFloat should be decoded as Float32 in Chunk (#35978) } diff --git a/executor/testdata/analyze_test_data.sql b/executor/testdata/analyze_test_data.sql new file mode 100644 index 0000000000000..3f468ffdd2a63 --- /dev/null +++ b/executor/testdata/analyze_test_data.sql @@ -0,0 +1,726 @@ +INSERT INTO t1 (id,num) VALUES + (6404,0.44816685), + (6532,0.7986926), + (6660,0.00085072), + (6788,0.00002967), + (6916,0.0), + (7044,0.03088332), + (7172,0.00046703), + (7300,0.00568179), + (7428,0.33018935), + (7556,0.00455456); +INSERT INTO t1 (id,num) VALUES + (7684,0.36683157), + (7812,0.6140698), + (7940,0.10177323), + (8068,0.00434905), + (8196,0.07348831), + (8324,0.00006405), + (8452,0.02007892), + (8580,0.01050341), + (8708,0.00030031), + (8836,0.02070964); +INSERT INTO t1 (id,num) VALUES + (8964,0.02303072), + (9092,0.44560316), + (9220,0.00069178), + (9348,0.06521516), + (9476,NULL), + (9604,0.01747504), + (9732,0.6579575), + (9860,0.0025045), + (9988,0.03484399), + (10116,0.00193867); +INSERT INTO t1 (id,num) VALUES + (10244,0.0), + (10372,0.0007868), + (10500,0.00031922), + (10628,0.09769934), + (10756,0.00715676), + (10884,NULL), + (11012,0.30375117), + (11140,0.7572566), + (11268,0.00018916), + (11396,0.00969914); +INSERT INTO t1 (id,num) VALUES + (11524,0.05069033), + (11652,0.25621885), + (11780,0.00468599), + (11908,NULL), + (12036,0.00011619), + (12164,0.05855278), + (12292,0.00367099), + (12420,0.9929275), + (12548,0.15242451), + (12676,NULL); +INSERT INTO t1 (id,num) VALUES + (12804,0.0246472), + (12932,0.00472382), + (13060,NULL), + (13188,0.93716323), + (13316,NULL), + (13444,0.01234986), + (13572,0.00076421), + (13700,0.0), + (13828,NULL), + (13956,0.12757756); +INSERT INTO t1 (id,num) VALUES + (14084,0.00484138), + (14212,0.02226818), + (14340,0.00265866), + (14468,0.0016064), + (14596,0.8862497), + (14724,0.00004634), + (14852,0.00835311), + (14980,0.55768555), + (15108,0.04359115), + (15236,0.00277844); +INSERT INTO t1 (id,num) VALUES + (15364,NULL), + (15492,NULL), + (15620,0.01908805), + (15748,0.12949005), + (15876,0.00305156), + (16004,0.00148819), + (16132,0.5249692), + (16260,0.14020711), + (16388,0.16946065), + (16516,NULL); +INSERT INTO t1 (id,num) VALUES + (16644,0.00692773), + (16772,0.00527069), + (16900,NULL), + (17028,0.00114955), + (17156,0.00134839), + (17284,0.02818464), + (17412,NULL), + (17540,0.0), + (17668,0.00351416), + (17796,0.00437542); +INSERT INTO t1 (id,num) VALUES + (17924,0.0012496), + (18052,0.00040084), + (18180,0.35637453), + (18308,0.0220884), + (18436,0.0020587), + (18564,0.00297463), + (18692,0.00487483), + (18820,0.00246549), + (18948,NULL), + (19076,0.9188499); +INSERT INTO t1 (id,num) VALUES + (19204,0.0168393), + (19332,0.00016338), + (19460,0.0), + (19588,0.28465533), + (19716,0.00008774), + (19844,0.0), + (19972,0.46484554), + (20100,0.00113875), + (20228,0.02503506), + (20356,0.01585903); +INSERT INTO t1 (id,num) VALUES + (20484,0.84865177), + (20612,0.00004633), + (20740,0.00002771), + (20868,0.17606646), + (20996,NULL), + (21124,NULL), + (21252,0.06745174), + (21380,NULL), + (21508,0.06476934), + (21636,NULL); +INSERT INTO t1 (id,num) VALUES + (21764,0.05436462), + (21892,0.0), + (22020,0.01013111), + (22148,0.72076213), + (22276,0.00758889), + (22404,NULL), + (22532,0.01502812), + (22660,0.03836808), + (22788,0.01743244), + (22916,0.90398115); +INSERT INTO t1 (id,num) VALUES + (23044,0.39467624), + (23172,0.9271143), + (23300,0.0), + (23428,NULL), + (23556,0.01379231), + (23684,0.0557244), + (23812,NULL), + (23940,0.00256223), + (24068,0.05586718), + (24196,0.00675368); +INSERT INTO t1 (id,num) VALUES + (24324,0.00235754), + (24452,0.00048123), + (24580,0.00684315), + (24708,0.00022108), + (24836,0.8159851), + (24964,0.00049652), + (25092,0.11392157), + (25220,0.21413554), + (25348,0.00061148), + (25476,0.00622709); +INSERT INTO t1 (id,num) VALUES + (25604,0.00213495), + (25732,NULL), + (25860,NULL), + (25988,0.0), + (26116,0.02214408), + (26244,0.03450824), + (26372,0.85039985), + (26500,0.00969678), + (26628,0.01092109), + (26756,0.07614598); +INSERT INTO t1 (id,num) VALUES + (26884,0.02030184), + (27012,0.0), + (27140,0.7304159), + (27268,0.05062966), + (27396,0.07090286), + (27524,0.00589924), + (27652,0.00982369), + (27780,0.01139362), + (27908,0.12942761), + (28036,NULL); +INSERT INTO t1 (id,num) VALUES + (28164,0.973725), + (28292,0.0), + (28420,0.9658361), + (28548,0.7608606), + (28676,0.00033382), + (28804,0.0006193), + (28932,0.01344005), + (29060,0.00213988), + (29188,0.20606396), + (29316,NULL); +INSERT INTO t1 (id,num) VALUES + (29444,0.00029995), + (29572,0.11128831), + (29700,0.00022049), + (29828,0.09755096), + (29956,0.0), + (30084,0.15368505), + (30212,0.00385346), + (30340,NULL), + (30468,0.03264421), + (30596,NULL); +INSERT INTO t1 (id,num) VALUES + (30724,0.02093143), + (30852,0.00251696), + (30980,NULL), + (31108,0.5892975), + (31236,0.15591888), + (31364,NULL), + (31492,0.13643512), + (31620,0.0), + (31748,0.0124521), + (31876,0.10001199); +INSERT INTO t1 (id,num) VALUES + (32004,0.9572708), + (32132,0.2702167), + (32260,0.0), + (32388,NULL), + (32516,0.00197053), + (32644,NULL), + (32772,0.00294003), + (32900,0.0001796), + (33028,0.00109745), + (33156,0.00084148); +INSERT INTO t1 (id,num) VALUES + (33284,0.02335174), + (33412,NULL), + (33540,NULL), + (33668,0.00069347), + (33796,0.00317112), + (33924,0.00514138), + (34052,0.0), + (34180,0.20418067), + (34308,0.21699235), + (34436,0.00151851); +INSERT INTO t1 (id,num) VALUES + (34564,0.00260781), + (34692,0.00042542), + (34820,NULL), + (34948,0.0001932), + (35076,0.0), + (35204,0.00023503), + (35332,0.27567366), + (35460,0.00029132), + (35588,0.0038553), + (35716,0.00028413); +INSERT INTO t1 (id,num) VALUES + (35844,0.0001824), + (35972,0.8047172), + (36100,0.9117202), + (36228,0.00090282), + (36356,0.00003453), + (36484,0.00177934), + (36612,0.02046586), + (36740,0.00146691), + (36868,0.00613281), + (36996,0.02881232); +INSERT INTO t1 (id,num) VALUES + (37124,0.0), + (37252,0.5468523), + (37380,0.0), + (37508,NULL), + (37636,NULL), + (37764,0.08866176), + (37892,0.01583531), + (38020,0.00258847), + (38148,0.0), + (38276,0.0013263); +INSERT INTO t1 (id,num) VALUES + (38404,0.00610365), + (38532,0.0022153), + (38660,0.00080778), + (38788,0.19915293), + (38916,NULL), + (39044,0.0266057), + (39172,0.02324441), + (39300,0.00193156), + (39428,0.00059531), + (39556,0.10964896); +INSERT INTO t1 (id,num) VALUES + (39684,0.02697241), + (39812,0.00929319), + (39940,0.00053213), + (40068,0.0), + (40196,0.00294272), + (40324,NULL), + (40452,NULL), + (40580,NULL), + (40708,0.08479243), + (40836,NULL); +INSERT INTO t1 (id,num) VALUES + (40964,NULL), + (41092,0.2487916), + (41220,NULL), + (41348,0.00043917), + (41476,0.0), + (41604,0.00049506), + (41732,0.00250602), + (41860,0.00011041), + (41988,0.01406988), + (42116,0.00199188); +INSERT INTO t1 (id,num) VALUES + (42244,0.2924709), + (42372,NULL), + (42500,0.01702607), + (42628,NULL), + (42756,0.15324448), + (42884,0.13135524), + (43012,0.01943198), + (43140,0.00188494), + (43268,0.00071816), + (43396,0.00012918); +INSERT INTO t1 (id,num) VALUES + (43524,0.00498049), + (43652,0.13445085), + (43780,0.14880875), + (43908,0.00077732), + (44036,0.54969823), + (44164,0.02765146), + (44292,0.00238847), + (44420,0.93815935), + (44548,0.00112602), + (44676,0.00970578); +INSERT INTO t1 (id,num) VALUES + (44804,0.00539565), + (44932,0.00198959), + (45060,NULL), + (45188,0.0004845), + (45316,0.11085703), + (45444,0.00036987), + (45572,0.00451057), + (45700,0.00005143), + (45828,NULL), + (45956,0.02388487); +INSERT INTO t1 (id,num) VALUES + (46084,0.00037795), + (46212,NULL), + (46340,0.05443271), + (46468,0.02153216), + (46596,NULL), + (46724,0.0), + (46852,0.38851526), + (46980,0.7427731), + (47108,0.03969904), + (47236,0.00078965); +INSERT INTO t1 (id,num) VALUES + (47364,0.03562958), + (47492,0.00100583), + (47620,0.03697728), + (47748,0.08408318), + (47876,0.00057393), + (48004,0.00710408), + (48132,0.01589785), + (48260,NULL), + (48388,0.0029398), + (48516,0.0168582); +INSERT INTO t1 (id,num) VALUES + (48644,0.15535928), + (48772,0.04729551), + (48900,0.00045675), + (49028,0.111338), + (49156,0.05104741), + (49284,0.00957311), + (49412,0.00032368), + (49540,0.0011836), + (49668,0.59423715), + (49796,0.07930596); +INSERT INTO t1 (id,num) VALUES + (49924,0.00970574), + (50052,0.1389579), + (50180,0.01344621), + (50308,0.02605027), + (50436,0.81112456), + (50564,0.08266076), + (50692,0.00064055), + (50820,0.00506184), + (50948,0.07310118), + (51076,0.08932291); +INSERT INTO t1 (id,num) VALUES + (51204,0.00027035), + (51332,0.00340637), + (51460,0.03478277), + (51588,0.00016421), + (51716,0.01174176), + (51844,0.28394622), + (51972,0.09893544), + (52100,0.9233999), + (52228,0.00086335), + (52356,0.0101455); +INSERT INTO t1 (id,num) VALUES + (52484,0.0), + (52612,0.04069425), + (52740,0.00043829), + (52868,0.00023583), + (52996,0.00259821), + (53124,NULL), + (53252,0.13227282), + (53380,NULL), + (53508,0.00010061), + (53636,0.00038794); +INSERT INTO t1 (id,num) VALUES + (53764,0.34211513), + (53892,0.02905787), + (54020,0.06862675), + (54148,NULL), + (54276,0.0), + (54404,0.0), + (54532,0.00019574), + (54660,0.03211332), + (54788,0.61193645), + (54916,0.00076543); +INSERT INTO t1 (id,num) VALUES + (55044,0.0), + (55172,0.00331305), + (55300,0.28308892), + (55428,0.34937865), + (55556,0.0749846), + (55684,0.00009996), + (55812,0.0188099), + (55940,0.0161851), + (56068,NULL), + (56196,0.4822152); +INSERT INTO t1 (id,num) VALUES + (56324,0.00293867), + (56452,0.00180932), + (56580,0.00598398), + (56708,0.9666414), + (56836,0.01628674), + (56964,0.97586924), + (57092,0.000337), + (57220,0.01060274), + (57348,0.0002598), + (57476,0.00377207); +INSERT INTO t1 (id,num) VALUES + (57604,0.00066885), + (57732,0.12196585), + (57860,0.00030946), + (57988,0.20773576), + (58116,0.00061379), + (58244,0.01043255), + (58372,0.00191419), + (58500,0.001862), + (58628,NULL), + (58756,NULL); +INSERT INTO t1 (id,num) VALUES + (58884,NULL), + (59012,0.0), + (59140,0.00029226), + (59268,0.0), + (59396,0.00449778), + (59524,0.02558722), + (59652,NULL), + (59780,0.01153614), + (59908,0.0), + (60036,0.2763834); +INSERT INTO t1 (id,num) VALUES + (60164,NULL), + (60292,NULL), + (60420,0.00149), + (60548,0.9590044), + (60676,0.0), + (60804,0.02398982), + (60932,0.05412427), + (61060,0.00005242), + (61188,0.01232633), + (61316,0.0005189); +INSERT INTO t1 (id,num) VALUES + (61444,0.0), + (61572,0.00196546), + (61700,0.00716419), + (61828,0.0), + (61956,0.0), + (62084,0.00122224), + (62212,0.00988189), + (62340,NULL), + (62468,0.00006262), + (62596,0.00334187); +INSERT INTO t1 (id,num) VALUES + (62724,NULL), + (62852,0.11244337), + (62980,NULL), + (63108,0.4137224), + (63236,0.00094869), + (63364,0.00037691), + (63492,0.09065232), + (63620,0.70078176), + (63748,0.2610763), + (63876,0.58452475); +INSERT INTO t1 (id,num) VALUES + (64004,0.00037725), + (64132,0.01363691), + (64260,0.03295722), + (64388,0.00775894), + (64516,0.00597924), + (64644,0.00098757), + (64772,0.0), + (64900,0.02986313), + (65028,0.00005731), + (65156,0.259131); +INSERT INTO t1 (id,num) VALUES + (65284,0.00433746), + (65412,0.03780772), + (65540,0.00255001), + (65668,0.33036566), + (65796,0.00004995), + (65924,0.00010118), + (66052,0.0), + (66180,0.00053285), + (66308,NULL), + (66436,NULL); +INSERT INTO t1 (id,num) VALUES + (66564,0.00215946), + (66692,0.00012339), + (66820,0.20895952), + (66948,0.01364484), + (67076,0.00161306), + (67204,0.01784489), + (67332,0.51345026), + (67460,0.00022264), + (67588,0.5755771), + (67716,0.07973811); +INSERT INTO t1 (id,num) VALUES + (67844,0.00075325), + (67972,0.02588654), + (68100,0.0066424), + (68228,0.0), + (68356,NULL), + (68484,0.00452684), + (68612,0.0), + (68740,NULL), + (68868,0.00148165), + (68996,0.00069906); +INSERT INTO t1 (id,num) VALUES + (69124,0.36696395), + (69252,0.00176318), + (69380,0.0), + (69508,0.00322113), + (69636,0.66234267), + (69764,NULL), + (69892,0.17466402), + (70020,NULL), + (70148,0.3849655), + (70276,0.00634581); +INSERT INTO t1 (id,num) VALUES + (70404,0.00898295), + (70532,0.10214821), + (70660,0.03422715), + (70788,0.01116229), + (70916,0.40757605), + (71044,0.76234627), + (71172,0.00059572), + (71300,0.00323015), + (71428,0.00015896), + (71556,0.04674813); +INSERT INTO t1 (id,num) VALUES + (71684,0.00002449), + (71812,0.5066135), + (71940,0.94456), + (72068,0.00022794), + (72196,0.00155764), + (72324,0.00060991), + (72452,NULL), + (72580,0.0), + (72708,0.23283394), + (72836,0.04149162); +INSERT INTO t1 (id,num) VALUES + (72964,0.00059429), + (73092,0.0), + (73220,0.0), + (73348,0.19649409), + (73476,0.0017861), + (73604,0.0), + (73732,NULL), + (73860,0.02864335), + (73988,0.9), + (74116,0.01258155); +INSERT INTO t1 (id,num) VALUES + (74244,0.00925873), + (74372,0.00323526), + (74500,0.00258794), + (74628,0.09800948), + (74756,0.00680334), + (74884,NULL), + (75012,0.02857617), + (75140,0.06355022), + (75268,0.0), + (75396,0.01389665); +INSERT INTO t1 (id,num) VALUES + (75524,0.49072826), + (75652,0.00250901), + (75780,0.00081248), + (75908,0.02927421), + (76036,NULL), + (76164,0.01670666), + (76292,0.01635653), + (76420,NULL), + (76548,0.1040872), + (76676,0.10566042); +INSERT INTO t1 (id,num) VALUES + (76804,NULL), + (76932,0.00663577), + (77060,0.21661888), + (77188,0.45707023), + (77316,0.00272538), + (77444,0.00419203), + (77572,NULL), + (77700,0.00237827), + (77828,0.00146376), + (77956,0.03254667); +INSERT INTO t1 (id,num) VALUES + (78084,0.00237725), + (78212,NULL), + (78340,0.02337047), + (78468,0.6066429), + (78596,NULL), + (78724,0.99989676), + (78852,0.02890464), + (78980,0.07082515), + (79108,NULL), + (79236,0.0019473); +INSERT INTO t1 (id,num) VALUES + (79364,0.00358919), + (79492,NULL), + (79620,0.28481358), + (79748,0.00222982), + (79876,NULL), + (80004,0.00547454), + (80132,NULL), + (80260,0.0002941), + (80388,0.00061776), + (80516,0.00002488); +INSERT INTO t1 (id,num) VALUES + (80644,0.09067673), + (80772,0.97947466), + (80900,NULL), + (81028,0.00611359), + (81156,0.0001545), + (81284,NULL), + (81412,0.02183841), + (81540,0.01013091), + (81668,0.00044435), + (81796,0.00101722); +INSERT INTO t1 (id,num) VALUES + (81924,0.0), + (82052,0.01291962), + (82180,0.00089332), + (82308,NULL), + (82436,0.38697731), + (82564,0.0), + (82692,0.06748839), + (82820,0.00505329), + (82948,0.00197535), + (83076,NULL); +INSERT INTO t1 (id,num) VALUES + (83204,NULL), + (83332,0.02577116), + (83460,0.00040706), + (83588,0.00387944), + (83716,0.00060442), + (83844,0.00140539), + (83972,0.02212065), + (84100,0.0009958), + (84228,0.21305683), + (84356,0.01075263); +INSERT INTO t1 (id,num) VALUES + (84484,NULL), + (84612,0.04209423), + (84740,0.00016298), + (84868,0.00030807), + (84996,0.0033516), + (85124,0.01264506), + (85252,NULL), + (85380,0.00797018), + (85508,0.03014666), + (85636,0.0); +INSERT INTO t1 (id,num) VALUES + (85764,0.0198979), + (85892,NULL), + (86020,NULL), + (86148,0.00323176), + (86276,0.56539536), + (86404,0.04482054), + (86532,0.07097953), + (86660,0.00034572), + (86788,0.0), + (86916,0.00194455); +INSERT INTO t1 (id,num) VALUES + (87044,0.0), + (87172,0.0), + (87300,0.00335881), + (87428,0.6016782), + (87556,0.02347282), + (87684,0.0), + (87812,0.00998413), + (87940,0.10825046), + (88068,0.11218246), + (88196,NULL); +INSERT INTO t1 (id,num) VALUES + (88324,0.16741024), + (88452,0.00222767), + (88580,0.00215809), + (3,0.05589541), + (131,0.0), + (259,0.00747037), + (387,0.00063459), + (515,0.28516522), + (643,0.07687332), + (771,0.00015758); +INSERT INTO t1 (id,num) VALUES + (6019,0.36197448), + (6147,0.00694031), + (6275,NULL), + (6403,0.05160758), + (6531,0.98084956), + (6659,0.0), + (6787,0.00084533), + (6915,0.05876909), + (7043,NULL), + (7171,NULL); diff --git a/statistics/cmsketch.go b/statistics/cmsketch.go index 65569963aaed9..c39999e922320 100644 --- a/statistics/cmsketch.go +++ b/statistics/cmsketch.go @@ -767,6 +767,8 @@ func MergePartTopN2GlobalTopN(sc *stmtctx.StatementContext, version int, topNs [ if types.IsTypeTime(hists[0].Tp.GetType()) { // handle datetime values specially since they are encoded to int and we'll get int values if using DecodeOne. _, d, err = codec.DecodeAsDateTime(val.Encoded, hists[0].Tp.GetType(), sc.TimeZone) + } else if types.IsTypeFloat(hists[0].Tp.GetType()) { + _, d, err = codec.DecodeAsFloat32(val.Encoded, hists[0].Tp.GetType()) } else { _, d, err = codec.DecodeOne(val.Encoded) } diff --git a/statistics/histogram.go b/statistics/histogram.go index 59571fbfbcc63..0c4764b8a90e6 100644 --- a/statistics/histogram.go +++ b/statistics/histogram.go @@ -2120,6 +2120,8 @@ func MergePartitionHist2GlobalHist(sc *stmtctx.StatementContext, hists []*Histog if types.IsTypeTime(hists[0].Tp.GetType()) { // handle datetime values specially since they are encoded to int and we'll get int values if using DecodeOne. _, d, err = codec.DecodeAsDateTime(meta.Encoded, hists[0].Tp.GetType(), sc.TimeZone) + } else if types.IsTypeFloat(hists[0].Tp.GetType()) { + _, d, err = codec.DecodeAsFloat32(meta.Encoded, hists[0].Tp.GetType()) } else { _, d, err = codec.DecodeOne(meta.Encoded) } diff --git a/types/datum.go b/types/datum.go index 8bc8fddafe870..dd3a8f14e9a85 100644 --- a/types/datum.go +++ b/types/datum.go @@ -183,6 +183,12 @@ func (d *Datum) SetFloat32(f float32) { d.i = int64(math.Float64bits(float64(f))) } +// SetFloat32FromF64 sets float32 values from f64 +func (d *Datum) SetFloat32FromF64(f float64) { + d.k = KindFloat32 + d.i = int64(math.Float64bits(f)) +} + // GetString gets string value. func (d *Datum) GetString() string { return string(hack.String(d.b)) diff --git a/types/etc.go b/types/etc.go index c1cfb1aa0877f..4aa5576cfec25 100644 --- a/types/etc.go +++ b/types/etc.go @@ -66,6 +66,11 @@ func IsTypeTime(tp byte) bool { return tp == mysql.TypeDatetime || tp == mysql.TypeDate || tp == mysql.TypeTimestamp } +// IsTypeFloat indicates whether the type is TypeFloat +func IsTypeFloat(tp byte) bool { + return tp == mysql.TypeFloat +} + // IsTypeInteger returns a boolean indicating whether the tp is integer type. func IsTypeInteger(tp byte) bool { switch tp { diff --git a/util/codec/codec.go b/util/codec/codec.go index 7d6104ff592bc..9c5099a4c4ae9 100644 --- a/util/codec/codec.go +++ b/util/codec/codec.go @@ -762,6 +762,8 @@ func DecodeRange(b []byte, size int, idxColumnTypes []byte, loc *time.Location) if types.IsTypeTime(idxColumnTypes[i]) { // handle datetime values specially since they are encoded to int and we'll get int values if using DecodeOne. b, d, err = DecodeAsDateTime(b, idxColumnTypes[i], loc) + } else if types.IsTypeFloat(idxColumnTypes[i]) { + b, d, err = DecodeAsFloat32(b, idxColumnTypes[i]) } else { b, d, err = DecodeOne(b) } @@ -899,6 +901,22 @@ func DecodeAsDateTime(b []byte, tp byte, loc *time.Location) (remain []byte, d t return b, d, nil } +// DecodeAsFloat32 decodes value for mysql.TypeFloat +func DecodeAsFloat32(b []byte, tp byte) (remain []byte, d types.Datum, err error) { + if len(b) < 1 || tp != mysql.TypeFloat { + return nil, d, errors.New("invalid encoded key") + } + flag := b[0] + b = b[1:] + if flag != floatFlag { + return b, d, errors.Errorf("invalid encoded key flag %v for DecodeAsFloat32", flag) + } + var v float64 + b, v, err = DecodeFloat(b) + d.SetFloat32FromF64(v) + return b, d, nil +} + // CutOne cuts the first encoded value from b. // It will return the first encoded item and the remains as byte slice. func CutOne(b []byte) (data []byte, remain []byte, err error) { From 1b3fe86eadcce6d773656a693876fd1304c2f957 Mon Sep 17 00:00:00 2001 From: yisaer Date: Wed, 13 Jul 2022 11:45:47 +0800 Subject: [PATCH 2/2] fix conflict Signed-off-by: yisaer --- executor/analyze_test.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/executor/analyze_test.go b/executor/analyze_test.go index cf01d3f2bea8f..666ee4e921f63 100644 --- a/executor/analyze_test.go +++ b/executor/analyze_test.go @@ -18,14 +18,10 @@ import ( "context" "encoding/json" "fmt" -<<<<<<< HEAD + "io/ioutil" "strconv" "strings" "sync" -======= - "io/ioutil" - "strings" ->>>>>>> d10d25457... util: let TypeFloat should be decoded as Float32 in Chunk (#35978) "sync/atomic" "testing" "time" @@ -766,7 +762,6 @@ func TestHashInTopN(t *testing.T) { } } -<<<<<<< HEAD func TestNormalAnalyzeOnCommonHandle(t *testing.T) { store, clean := testkit.CreateMockStore(t) defer clean() @@ -3394,7 +3389,8 @@ PARTITION BY RANGE ( a ) ( tk.MustExec("analyze table t partition p0") tbl := h.GetTableStats(tableInfo) require.Equal(t, int64(6), tbl.Columns[tableInfo.Columns[0].ID].Histogram.NDV) -======= +} + func TestAnalyzePartitionTableForFloat(t *testing.T) { store, clean := testkit.CreateMockStore(t) defer clean() @@ -3413,5 +3409,4 @@ func TestAnalyzePartitionTableForFloat(t *testing.T) { tk.MustExec(sql) } tk.MustExec("analyze table t1") ->>>>>>> d10d25457... util: let TypeFloat should be decoded as Float32 in Chunk (#35978) }