(ctx context.Context, statement string, queryContext db.QueryContext)
| 206 | } |
| 207 | |
| 208 | func (d *Driver) querySinglePartiQL(ctx context.Context, statement string, queryContext db.QueryContext) (*v1pb.QueryResult, error) { |
| 209 | result := &v1pb.QueryResult{} |
| 210 | input := &dynamodb.ExecuteStatementInput{ |
| 211 | Statement: &statement, |
| 212 | } |
| 213 | if queryContext.Limit > 0 { |
| 214 | input.Limit = new(int32(queryContext.Limit)) |
| 215 | } |
| 216 | |
| 217 | var nextToken *string |
| 218 | rowMap := make(map[string][]*v1pb.RowValue) |
| 219 | // TODO(zp): Our proto is not designed for NoSQL, whose data is not fixed. So we only use the last row to determine the column type. |
| 220 | columnTypeMap := make(map[string]string) |
| 221 | totalRowCount := 0 |
| 222 | for { |
| 223 | input.NextToken = nextToken |
| 224 | output, err := d.client.ExecuteStatement(ctx, input) |
| 225 | if err != nil { |
| 226 | return nil, err |
| 227 | } |
| 228 | for _, item := range output.Items { |
| 229 | totalRowCount++ |
| 230 | meta := convertAttributeValueMapToRow(item) |
| 231 | allKeySet := make(map[string]bool) |
| 232 | for key := range rowMap { |
| 233 | allKeySet[key] = true |
| 234 | } |
| 235 | curKeySet := make(map[string]*dynamodbQueryResultMeta, len(meta)) |
| 236 | for key, value := range meta { |
| 237 | curKeySet[key] = value |
| 238 | allKeySet[key] = true |
| 239 | } |
| 240 | for key := range allKeySet { |
| 241 | _, inRowMap := rowMap[key] |
| 242 | _, inCurKeySet := curKeySet[key] |
| 243 | // 1. The key appears in the rowMap, and appears in the current row, we append the value to the row. |
| 244 | if inRowMap && inCurKeySet { |
| 245 | rowMap[key] = append(rowMap[key], curKeySet[key].value) |
| 246 | columnTypeMap[key] = curKeySet[key].columnType |
| 247 | } |
| 248 | // 2.If the key appears in the row map, but does not appear in the current row, we append nil to the row. |
| 249 | if inRowMap && !inCurKeySet { |
| 250 | rowMap[key] = append(rowMap[key], nil) |
| 251 | } |
| 252 | // 3. If the key appears in the current row, but does not appear in the row map, it means that the current row has a new column, we should |
| 253 | // backfill the previous rows with nil. |
| 254 | if !inRowMap && inCurKeySet { |
| 255 | for i := 0; i < totalRowCount-1; i++ { |
| 256 | rowMap[key] = append(rowMap[key], nil) |
| 257 | } |
| 258 | rowMap[key] = append(rowMap[key], curKeySet[key].value) |
| 259 | columnTypeMap[key] = curKeySet[key].columnType |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | nextToken = output.NextToken |
| 264 | if nextToken == nil { |
| 265 | break |
no test coverage detected