github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/tests/integration/table_regress_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"strconv"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/ydb-platform/ydb-go-sdk/v3/table"
    16  	"github.com/ydb-platform/ydb-go-sdk/v3/table/result"
    17  	"github.com/ydb-platform/ydb-go-sdk/v3/table/result/indexed"
    18  	"github.com/ydb-platform/ydb-go-sdk/v3/table/result/named"
    19  	"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
    20  )
    21  
    22  type rawInt64 struct {
    23  	val int64
    24  }
    25  
    26  func (r *rawInt64) UnmarshalYDB(raw types.RawValue) error {
    27  	raw.Unwrap()
    28  	r.val = raw.Int64()
    29  	return nil
    30  }
    31  
    32  func TestIssueWideResultWithUnwrap(t *testing.T) {
    33  	const columnsCount = 200
    34  	var columns []string
    35  	for i := 0; i < columnsCount; i++ {
    36  		column := fmt.Sprintf("CAST(%v AS Int64?) as c%v", i, i)
    37  		columns = append(columns, column)
    38  	}
    39  
    40  	query := "SELECT " + strings.Join(columns, ", ")
    41  	t.Run("named", func(t *testing.T) {
    42  		scope := newScope(t)
    43  		var res result.Result
    44  		var err error
    45  		err = scope.Driver().Table().DoTx(scope.Ctx, func(ctx context.Context, tx table.TransactionActor) error {
    46  			res, err = tx.Execute(ctx, query, nil)
    47  			return err
    48  		})
    49  		require.NoError(t, err)
    50  
    51  		res.NextResultSet(scope.Ctx)
    52  		require.NoError(t, res.Err())
    53  		res.NextRow()
    54  		require.NoError(t, res.Err())
    55  
    56  		results := make([]rawInt64, columnsCount)
    57  		resultsPointers := make([]named.Value, columnsCount)
    58  		for i := range results {
    59  			resultsPointers[i] = named.Required("c"+strconv.Itoa(i), &results[i])
    60  		}
    61  		err = res.ScanNamed(resultsPointers...)
    62  		require.NoError(t, err)
    63  
    64  		for i, val := range results {
    65  			require.Equal(t, int64(i), val.val)
    66  		}
    67  	})
    68  	t.Run("indexed", func(t *testing.T) {
    69  		scope := newScope(t)
    70  		var res result.Result
    71  		var err error
    72  		err = scope.Driver().Table().DoTx(scope.Ctx, func(ctx context.Context, tx table.TransactionActor) error {
    73  			res, err = tx.Execute(ctx, query, nil)
    74  			return err
    75  		})
    76  		require.NoError(t, err)
    77  
    78  		res.NextResultSet(scope.Ctx)
    79  		require.NoError(t, res.Err())
    80  		res.NextRow()
    81  		require.NoError(t, res.Err())
    82  
    83  		results := make([]rawInt64, columnsCount)
    84  		resultsPointers := make([]indexed.RequiredOrOptional, columnsCount)
    85  		for i := range results {
    86  			resultsPointers[i] = &results[i]
    87  		}
    88  		err = res.Scan(resultsPointers...)
    89  		require.NoError(t, err)
    90  
    91  		for i, val := range results {
    92  			require.Equal(t, int64(i), val.val)
    93  		}
    94  	})
    95  }