// Next returns the next row of the sub result. // If no more row to return, data would be nil. func (pr *partialResult) Next() (handle int64, data []types.Datum, err error) { if !pr.fetched { err = <-pr.done pr.fetched = true if err != nil { return 0, nil, err } } if len(pr.resp.Chunks) > 0 { // For new resp rows structure. chunk := pr.getChunk() if chunk == nil { return 0, nil, nil } rowMeta := chunk.RowsMeta[pr.cursor] if !pr.ignoreData { rowData := chunk.RowsData[pr.dataOffset : pr.dataOffset+rowMeta.Length] data, err = tablecodec.DecodeValues(rowData, pr.fields, pr.index) if err != nil { return 0, nil, errors.Trace(err) } pr.dataOffset += rowMeta.Length } if data == nil { data = dummyData } if !pr.aggregate { handle = rowMeta.Handle } pr.cursor++ return } if pr.cursor >= len(pr.resp.Rows) { return 0, nil, nil } row := pr.resp.Rows[pr.cursor] if !pr.ignoreData { data, err = tablecodec.DecodeValues(row.Data, pr.fields, pr.index) if err != nil { return 0, nil, errors.Trace(err) } } if data == nil { // When no column is referenced, the data may be nil, like 'select count(*) from t'. // In this case, we need to create a zero length datum slice, // as caller will check if data is nil to finish iteration. // data = make([]types.Datum, 0) data = dummyData } if !pr.aggregate { handleBytes := row.GetHandle() _, datum, err := codec.DecodeOne(handleBytes) if err != nil { return 0, nil, errors.Trace(err) } handle = datum.GetInt64() } pr.cursor++ return }
// Next returns the next row of the sub result. // If no more row to return, data would be nil. func (pr *partialResult) Next() (handle int64, data []types.Datum, err error) { if !pr.fetched { select { case err = <-pr.done: } pr.fetched = true if err != nil { return 0, nil, err } } if pr.cursor >= len(pr.resp.Rows) { return 0, nil, nil } row := pr.resp.Rows[pr.cursor] data, err = tablecodec.DecodeValues(row.Data, pr.fields, pr.index) if err != nil { return 0, nil, errors.Trace(err) } if data == nil { // When no column is referenced, the data may be nil, like 'select count(*) from t'. // In this case, we need to create a zero length datum slice, // as caller will check if data is nil to finish iteration. data = make([]types.Datum, 0) } if !pr.aggregate { handleBytes := row.GetHandle() datums, err := codec.Decode(handleBytes) if err != nil { return 0, nil, errors.Trace(err) } handle = datums[0].GetInt64() } pr.cursor++ return }
// Next returns the next row of the sub result. // If no more row to return, data would be nil. func (r *SubResult) Next() (handle int64, data []types.Datum, err error) { if r.resp == nil { r.resp = new(tipb.SelectResponse) var b []byte b, err = ioutil.ReadAll(r.reader) r.reader.Close() if err != nil { return 0, nil, errors.Trace(err) } err = proto.Unmarshal(b, r.resp) if err != nil { return 0, nil, errors.Trace(err) } if r.resp.Error != nil { return 0, nil, errInvalidResp.Gen("[%d %s]", r.resp.Error.GetCode(), r.resp.Error.GetMsg()) } } if r.cursor >= len(r.resp.Rows) { return 0, nil, nil } row := r.resp.Rows[r.cursor] data, err = tablecodec.DecodeValues(row.Data, r.fields, r.index) if err != nil { return 0, nil, errors.Trace(err) } if data == nil { // When no column is referenced, the data may be nil, like 'select count(*) from t'. // In this case, we need to create a zero length datum slice, // as caller will check if data is nil to finish iteration. data = make([]types.Datum, 0) } if !r.aggregate { handleBytes := row.GetHandle() datums, err := codec.Decode(handleBytes) if err != nil { return 0, nil, errors.Trace(err) } handle = datums[0].GetInt64() } r.cursor++ return }