func readResult(rs ast.RecordSet, count int) { for count > 0 { x, err := rs.Next() if err != nil { log.Fatal(err) } if x == nil { log.Fatal(count) } count-- } rs.Close() }
// GetRows gets all the rows from a RecordSet. func GetRows(rs ast.RecordSet) ([][]types.Datum, error) { if rs == nil { return nil, nil } var rows [][]types.Datum defer rs.Close() // Negative limit means no limit. for { row, err := rs.Next() if err != nil { return nil, errors.Trace(err) } if row == nil { break } rows = append(rows, row.Data) } return rows, nil }
// collectSamples collects sample from the result set, using Reservoir Sampling algorithm. // See https://en.wikipedia.org/wiki/Reservoir_sampling func (e *SimpleExec) collectSamples(result ast.RecordSet) (count int64, samples []*ast.Row, err error) { for { var row *ast.Row row, err = result.Next() if err != nil { return count, samples, errors.Trace(err) } if row == nil { break } if len(samples) < maxSampleCount { samples = append(samples, row) } else { shouldAdd := rand.Int63n(count) < maxSampleCount if shouldAdd { idx := rand.Intn(maxSampleCount) samples[idx] = row } } count++ } return count, samples, nil }