go-clickhouse icon indicating copy to clipboard operation
go-clickhouse copied to clipboard

Scan Map for struct panic ch: column has empty name [recovered]

Open wilson-wc opened this issue 3 years ago • 4 comments

I have tag all the field with ch:column_name tag , but where db.NewSelect().Model(&result).Limit(1).Scan(ctx) scan the struct panic:ch: column has empty name [recovered]

wilson-wc avatar Jun 28 '22 17:06 wilson-wc

Please provide https://stackoverflow.com/help/minimal-reproducible-example. The query that panics should be enough.

vmihailenco avatar Jun 29 '22 06:06 vmihailenco

sorry , now faced a new problem: I have a table like:

CREATE TABLE test.testTable
(
    `c1` Date,
    `c2` String,
    `c3` String,
    `c4` String,
    `c5` String,
    `c6` String,
    `c7` String,
    `c8` String,
    `c9` String,
    `c10` String,
    `c11` String,
    `c12` String,
    `c13` String,
    `c14` String,
    `c15` String,
    `c16` Float64,
    `c17` Int64,
    `c18` Array(Nullable(Int64)),
    `c19` Array(Nullable(Int64)),
    `c20` Array(Nullable(Int64)),
    `c21` Array(Nullable(Int64)),
    `c22` String,
    `c23` String,
    `c24` String
)engine = MergeTree PARTITION BY c1
        ORDER BY (c2, c3, c4, c5, c6, c7, c8, c9,
                  c10, c11, c12, c13, c14, c15, c16, cityHash64(c9))
        SAMPLE BY cityHash64(c9)
        SETTINGS index_granularity = 8192;

and I my test code is

type testTable struct {
	ch.CHModel `ch:"table:testTable,columnar"`

	c1  time.Time `ch:"c1"`
	c2  string    `ch:"c2"`
	c3  string    `ch:"c3"`
	c4  string    `ch:"c4"`
	c5  string    `ch:"c5"`
	c6  string    `ch:"c6"`
	c7  string    `ch:"c7"`
	c8  string    `ch:"c8"`
	c9  string    `ch:"c9"`
	c10 string    `ch:"c10"`
	c11 string    `ch:"c11"`
	c12 string    `ch:"c12"`
	c13 string    `ch:"c13"`
	c14 string    `ch:"c14"`
	c15 string    `ch:"c15"`
	c16 float64   `ch:"c16"`
	c17 int64     `ch:"c17"`
	c18 []*int64  `ch:"c18"`
	c19 []*int64  `ch:"c19"`
	c20 []*int64  `ch:"c20"`
	c21 []*int64  `ch:"c21"`
	c22 string    `ch:"c22"`
	c23 string    `ch:"c23"`
	c24 string    `ch:"c24"`
}

func TestName(t *testing.T) {
       //some init
	config.InitLog()
	dal.InitGoClickhouseConnection()
	conn := dal.GetGoClickhouseConnection()
	ctx := context.Background()
	dest := new(testTable)
	if err := conn.NewSelect().Model(dest).Limit(1).Scan(ctx); err != nil {
		panic(err)
	}

}

but when I run the code I got this panic message like: image it seems that column tag is not work

wilson-wc avatar Jun 29 '22 11:06 wilson-wc

Change c1 time.Time ch:"c1" to `C1 time.Time `ch:"c1". Unexported fields are ignored.

vmihailenco avatar Jun 29 '22 12:06 vmihailenco

Sorry My misstake ,I have changed the c to C ,then I run it again,got this panic: image

panic trace :

	/Users/wangcong/go/go1.18/src/runtime/panic.go:844 +0x25a
reflect.(*rtype).Elem(0x1a38d60)
	/Users/wangcong/go/go1.18/src/reflect/type.go:965 +0x2ae
github.com/uptrace/go-clickhouse/ch/chschema.(*Table).addFields(0xc0001e8380, {0x1cf0350, 0x1b06200}, {0x0, 0x0, 0x0})
	/Users/wangcong/go/pkg/mod/github.com/uptrace/[email protected]/ch/chschema/table.go:150 +0x8e3
github.com/uptrace/go-clickhouse/ch/chschema.(*Table).initFields(0xc0001e8380)
	/Users/wangcong/go/pkg/mod/github.com/uptrace/[email protected]/ch/chschema/table.go:103 +0x125
github.com/uptrace/go-clickhouse/ch/chschema.newTable({0x1cf0350, 0x1b06200})
	/Users/wangcong/go/pkg/mod/github.com/uptrace/[email protected]/ch/chschema/table.go:61 +0x185
github.com/uptrace/go-clickhouse/ch/chschema.(*tablesMap).Get(0xc0001bb9b0, {0x1cf0350, 0x1b06200})
	/Users/wangcong/go/pkg/mod/github.com/uptrace/[email protected]/ch/chschema/tables.go:32 +0x177
github.com/uptrace/go-clickhouse/ch/chschema.TableForType({0x1cf0350, 0x1b06200})
	/Users/wangcong/go/pkg/mod/github.com/uptrace/[email protected]/ch/chschema/tables.go:12 +0x3d
github.com/uptrace/go-clickhouse/ch.newStructTableModelValue(0xc0000206e0, {0x1b06200, 0xc000583040, 0x199})
	/Users/wangcong/go/pkg/mod/github.com/uptrace/[email protected]/ch/model_table_struct.go:28 +0x58
github.com/uptrace/go-clickhouse/ch.newTableModel(0xc0000206e0, {0x1a212c0, 0xc000583040})
	/Users/wangcong/go/pkg/mod/github.com/uptrace/[email protected]/ch/model.go:109 +0x4fa
github.com/uptrace/go-clickhouse/ch.(*baseQuery).setTableModel(0xc0005831e0, {0x1a212c0, 0xc000583040})
	/Users/wangcong/go/pkg/mod/github.com/uptrace/[email protected]/ch/query_base.go:81 +0x5d
github.com/uptrace/go-clickhouse/ch.(*SelectQuery).Model(0xc0005831e0, {0x1a212c0, 0xc000583040})
	/Users/wangcong/go/pkg/mod/github.com/uptrace/[email protected]/ch/query_select.go:46 +0x47

then I changed the struct definition like :

type testTable struct {
	ch.CHModel `ch:"table:testTable,columnar"`

	C1  *time.Time `ch:"c1,type:Date"`
	C2  *string    `ch:"c2,type:String"`
	C3  *string    `ch:"c3,type:String"`
	C4  *string    `ch:"c4,type:String"`
	C5  *string    `ch:"c5,type:String"`
	C6  *string    `ch:"c6,type:String"`
	C7  *string    `ch:"c7,type:String"`
	C8  *string    `ch:"c8,type:String"`
	C9  *string    `ch:"c9,type:String"`
	C10 *string    `ch:"c10,type:String"`
	C11 *string    `ch:"c11,type:String"`
	C12 *string    `ch:"c12,type:String"`
	C13 *string    `ch:"c13,type:String"`
	C14 *string    `ch:"c14,type:String"`
	C15 *string    `ch:"c15,type:String"`
	C16 *float64   `ch:"c16,type:Float64"`
	C17 *int64     `ch:"c17,type:Int64"`
	C18 []*int64   `ch:"c18,type:Array(Nullable(Int64))"`
	C19 []*int64   `ch:"c19,type:Array(Nullable(Int64))"`
	C20 []*int64   `ch:"c20,type:Array(Nullable(Int64))"`
	C21 []*int64   `ch:"c21,type:Array(Nullable(Int64))"`
	C22 *string    `ch:"c22,type:String"`
	C23 *string    `ch:"c23,type:String"`
	C24 *string    `ch:"c24,type:String"`
}

and I run this code :

func TestName(t *testing.T) {
	config.InitLog()
	dal.InitGoClickhouseConnection()
	conn := dal.GetGoClickhouseConnection()
	ctx := context.Background()
	//dest := new(testTable)
	var dest testTable
	if err := conn.NewSelect().Model(&dest).Where("c1=?", "2022-06-14").Limit(1).Scan(ctx); err != nil {
		panic(err)
	}
	fmt.Println(dest)
}

It works ,there is no panic ,but I got nothing from the db image but there is a lot rows data in the db: image

and when I try run the code like :

func TestName(t *testing.T) {
	config.InitLog()
	dal.InitGoClickhouseConnection()
	conn := dal.GetGoClickhouseConnection()
	ctx := context.Background()
	//dest := new(testTable)
	var dest []testTable
	if err := conn.NewSelect().Model(&dest).Where("c1=?", "2022-06-14").Limit(10).Scan(ctx); err != nil {
		panic(err)
	}
	fmt.Println(dest)
}

to try to scan a array of testTable data,the panic ch: column has empty name [recovered],show it again.

wilson-wc avatar Jun 30 '22 02:06 wilson-wc

Should be improved in v0.3.0

vmihailenco avatar Jan 21 '23 10:01 vmihailenco