@@ -137,14 +137,10 @@ func computeNumberSamples(ctx context.Context, numRows uint64, st *cluster.Setti
137137 minSampleSize = minAutoHistogramSamples .Default ()
138138 }
139139
140- numSamples := math .Max (
141- math .Min (
142- 582.0 * math .Pow (float64 (numRows ), 0.29 ),
143- float64 (maxSampleSize ),
144- ),
140+ return uint32 (max (
141+ min (582.0 * math .Pow (float64 (numRows ), 0.29 ), float64 (maxSampleSize )),
145142 float64 (minSampleSize ),
146- )
147- return uint32 (numSamples )
143+ ))
148144}
149145
150146func (dsp * DistSQLPlanner ) createAndAttachSamplers (
@@ -167,7 +163,6 @@ func (dsp *DistSQLPlanner) createAndAttachSamplers(
167163 if autoStatsFractionStaleRowsForTable , ok := desc .AutoStatsFractionStaleRows (); ok {
168164 overhead = autoStatsFractionStaleRowsForTable
169165 }
170- // Convert to a signed integer first to make the linter happy.
171166 if details .UsingExtremes {
172167 rowsExpected = uint64 (int64 (
173168 // The total expected number of rows is the estimated number of stale
@@ -188,23 +183,19 @@ func (dsp *DistSQLPlanner) createAndAttachSamplers(
188183 sampler := & execinfrapb.SamplerSpec {
189184 Sketches : sketchSpec ,
190185 InvertedSketches : invSketchSpec ,
186+ MaxFractionIdle : details .MaxFractionIdle ,
191187 }
192- sampler .MaxFractionIdle = details .MaxFractionIdle
193- // For partial statistics this loop should only iterate once
194- // since we only support one reqStat at a time.
188+ // For partial statistics this loop should only iterate once since we only
189+ // support one reqStat at a time.
195190 for _ , s := range reqStats {
196191 if s .histogram {
197192 var histogramSamplesCount uint32
198193 if tableSampleCount , ok := desc .HistogramSamplesCount (); ok {
199194 histogramSamplesCount = tableSampleCount
200- } else if clusterSampleCount := histogramSamples .Get (& dsp .st .SV ); clusterSampleCount != histogramSamples . Default () {
195+ } else if clusterSampleCount := histogramSamples .Get (& dsp .st .SV ); clusterSampleCount != 0 {
201196 histogramSamplesCount = uint32 (clusterSampleCount )
202197 } else {
203- histogramSamplesCount = computeNumberSamples (
204- ctx ,
205- rowsExpected ,
206- dsp .st ,
207- )
198+ histogramSamplesCount = computeNumberSamples (ctx , rowsExpected , dsp .st )
208199 log .Dev .Infof (ctx , "using computed sample size of %d for histogram construction" , histogramSamplesCount )
209200 }
210201 sampler .SampleSize = histogramSamplesCount
@@ -219,7 +210,7 @@ func (dsp *DistSQLPlanner) createAndAttachSamplers(
219210
220211 // The sampler outputs the original columns plus a rank column, five
221212 // sketch columns, and two inverted histogram columns.
222- outTypes := make ([]* types.T , 0 , len (p .GetResultTypes ())+ 5 )
213+ outTypes := make ([]* types.T , 0 , len (p .GetResultTypes ())+ 8 )
223214 outTypes = append (outTypes , p .GetResultTypes ()... )
224215 // An INT column for the rank of each row.
225216 outTypes = append (outTypes , types .Int )
@@ -292,18 +283,15 @@ func (dsp *DistSQLPlanner) createPartialStatsPlan(
292283 // so we only support one requested stat at a time here.
293284 if len (reqStats ) > 1 {
294285 return nil , unimplemented .NewWithIssue (
295- 128904 ,
296- "cannot process multiple partial statistics requests at once" ,
286+ 128904 , "cannot process multiple partial statistics requests at once" ,
297287 )
298288 }
299289
300290 reqStat := reqStats [0 ]
301-
302291 if len (reqStat .columns ) > 1 {
303- // TODO (faizaanmadhani ): Add support for creating multi-column stats
292+ // TODO(#94076 ): add support for creating multi-column stats.
304293 return nil , pgerror .Newf (pgcode .FeatureNotSupported , "multi-column partial statistics are not currently supported" )
305294 }
306-
307295 if ! reqStat .histogram {
308296 return nil , pgerror .Newf (pgcode .FeatureNotSupported , "partial statistics without histograms are not supported" )
309297 }
@@ -324,9 +312,9 @@ func (dsp *DistSQLPlanner) createPartialStatsPlan(
324312 return nil , err
325313 }
326314
327- // Calculate the column we need to scan
328- // TODO (faizaanmadhani ): Iterate through all columns in a requested stat when
329- // when we add support for multi-column statistics.
315+ // Calculate the column we need to scan.
316+ // TODO(#94076 ): iterate through all columns in a requested stat when we add
317+ // support for multi-column statistics.
330318 var colCfg scanColumnsConfig
331319 colCfg .wantedColumns = append (colCfg .wantedColumns , column .GetID ())
332320
@@ -341,19 +329,18 @@ func (dsp *DistSQLPlanner) createPartialStatsPlan(
341329 if err != nil {
342330 return nil , err
343331 }
344- // Map the ColumnIDs to their ordinals in scan.cols
345- // This loop should only iterate once, since we only
346- // handle single column partial statistics.
347- // TODO(faizaanmadhani): Add support for multi-column partial stats next
332+ // Map the ColumnIDs to their ordinals in scan.cols. This loop should only
333+ // iterate once, since we only handle single column partial statistics.
334+ // TODO(#94076): add support for creating multi-column stats.
348335 var colIdxMap catalog.TableColMap
349336 for i , c := range scan .catalogCols {
350337 colIdxMap .Set (c .GetID (), i )
351338 }
352339
353340 var stat * stats.TableStatistic
354341 // Find the statistic from the newest table statistic for our column that is
355- // not partial and not forecasted. The first one we find will be the latest
356- // due to the newest to oldest ordering property of the cache.
342+ // not partial, not merged, and not forecasted. The first one we find will
343+ // be the latest due to the newest to oldest ordering property of the cache.
357344 for _ , t := range tableStats {
358345 if len (t .ColumnIDs ) == 1 && column .GetID () == t .ColumnIDs [0 ] &&
359346 ! t .IsPartial () && ! t .IsMerged () && ! t .IsForecast () {
@@ -380,7 +367,8 @@ func (dsp *DistSQLPlanner) createPartialStatsPlan(
380367 return nil , pgerror .Newf (
381368 pgcode .ObjectNotInPrerequisiteState ,
382369 "column %s does not have a prior statistic" ,
383- column .GetName ())
370+ column .GetName (),
371+ )
384372 }
385373 if len (stat .Histogram ) == 1 && stat .Histogram [0 ].UpperBound == tree .DNull {
386374 return nil , pgerror .Newf (
@@ -398,8 +386,9 @@ func (dsp *DistSQLPlanner) createPartialStatsPlan(
398386 planCtx .EvalContext (), planCtx .ExtendedEvalCtx .Codec , desc , scan .index ,
399387 )
400388
401- lowerBound , upperBound , err := bounds .GetUsingExtremesBounds (ctx ,
402- planCtx .EvalContext (), stat .Histogram )
389+ lowerBound , upperBound , err := bounds .GetUsingExtremesBounds (
390+ ctx , planCtx .EvalContext (), stat .Histogram ,
391+ )
403392 if err != nil {
404393 return nil , err
405394 }
@@ -413,13 +402,13 @@ func (dsp *DistSQLPlanner) createPartialStatsPlan(
413402 }
414403 prevLowerBound = lowerBound
415404
416- extremesSpans , err := bounds .ConstructUsingExtremesSpans (lowerBound ,
417- upperBound , scan .index )
405+ extremesSpans , err := bounds .ConstructUsingExtremesSpans (
406+ lowerBound , upperBound , scan .index ,
407+ )
418408 if err != nil {
419409 return nil , err
420410 }
421411 predicate = bounds .ConstructUsingExtremesPredicate (lowerBound , upperBound , column .GetName ())
422- // Get roachpb.Spans from constraint.Spans
423412 scan .spans , err = sb .SpansFromConstraintSpan (& extremesSpans , span .NoopSplitter ())
424413 if err != nil {
425414 return nil , err
@@ -456,9 +445,9 @@ func (dsp *DistSQLPlanner) createPartialStatsPlan(
456445 spec .FullStatisticID = stat .StatisticID
457446 }
458447
459- // For now, this loop should iterate only once, as we only
460- // handle single-column partial statistics.
461- // TODO(faizaanmadhani ): Add support for multi-column partial stats next
448+ // For now, this loop should iterate only once, as we only handle
449+ // single-column partial statistics.
450+ // TODO(#94076 ): add support for creating multi-column stats.
462451 for i , colID := range reqStat .columns {
463452 colIdx , ok := colIdxMap .Get (colID )
464453 if ! ok {
@@ -497,16 +486,9 @@ func (dsp *DistSQLPlanner) createPartialStatsPlan(
497486 sketchSpec = append (sketchSpec , spec )
498487 }
499488 return dsp .createAndAttachSamplers (
500- ctx ,
501- p ,
502- desc ,
503- tableStats ,
504- details ,
505- sampledColumnIDs ,
506- jobID ,
507- reqStats ,
508- sketchSpec , invSketchSpec ,
509- numIndexes , curIndex ), nil
489+ ctx , p , desc , tableStats , details , sampledColumnIDs , jobID , reqStats ,
490+ sketchSpec , invSketchSpec , numIndexes , curIndex ,
491+ ), nil
510492}
511493
512494func (dsp * DistSQLPlanner ) createStatsPlan (
@@ -748,16 +730,9 @@ func (dsp *DistSQLPlanner) createStatsPlan(
748730 }
749731
750732 return dsp .createAndAttachSamplers (
751- ctx ,
752- p ,
753- desc ,
754- tableStats ,
755- details ,
756- sampledColumnIDs ,
757- jobID ,
758- reqStats ,
759- sketchSpecs , invSketchSpecs ,
760- numIndexes , curIndex ), nil
733+ ctx , p , desc , tableStats , details , sampledColumnIDs , jobID , reqStats ,
734+ sketchSpecs , invSketchSpecs , numIndexes , curIndex ,
735+ ), nil
761736}
762737
763738// createPlanForCreateStats creates the DistSQL plan to perform the table stats
0 commit comments