MCPcopy Create free account
hub / github.com/bytebase/bytebase / querySinglePartiQL

Method querySinglePartiQL

backend/plugin/db/dynamodb/dynamodb.go:208–290  ·  view source on GitHub ↗
(ctx context.Context, statement string, queryContext db.QueryContext)

Source from the content-addressed store, hash-verified

206}
207
208func (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

Callers 1

QueryConnMethod · 0.95

Implementers 15

MockDriverbackend/plugin/advisor/utils_for_tests
Driverbackend/plugin/db/bigquery/bigquery.go
Driverbackend/plugin/db/mongodb/mongodb.go
Driverbackend/plugin/db/trino/trino.go
Driverbackend/plugin/db/redshift/redshift.go
Driverbackend/plugin/db/oracle/oracle.go
Driverbackend/plugin/db/dynamodb/dynamodb.go
Driverbackend/plugin/db/cosmosdb/cosmosdb.go
Driverbackend/plugin/db/spanner/spanner.go
Driverbackend/plugin/db/mssql/mssql.go
Driverbackend/plugin/db/mysql/mysql.go
Driverbackend/plugin/db/pg/pg.go

Calls 1

Tested by

no test coverage detected