Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid reading value for Date32 #1066

Closed
Alucardinio opened this issue Aug 16, 2023 · 3 comments · Fixed by #1069
Closed

invalid reading value for Date32 #1066

Alucardinio opened this issue Aug 16, 2023 · 3 comments · Fixed by #1069
Assignees
Labels

Comments

@Alucardinio
Copy link

Describe the bug

When reading a value from the Date32 column, the value shifts back 45 years, or jumps into the future

Steps to reproduce

  1. Create a table with a Date32 column
  2. Insert data (for example, '2010-10-10') into this column using the SQL package
  3. Select the newly inserted data. For 2010-10-10 it will be 1965-10-10

Expected behaviour

For 2010-10-10 returns 2010-10-10

Code example

	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()
	ei := 2	
	c.ExecContext(ctx, "TRUNCATE `new`")
	c.ExecContext(ctx, "INSERT INTO `new`	(`timestamp`, event_id)	VALUES(?, ?)", time.Date(2010, 10, 10, 0, 0, 0, 0, time.UTC), ei)

	var (
		event_id int
		t        time.Time
	)
	err = c.QueryRowContext(ctx, "SELECT `timestamp`, event_id FROM `new` where event_id=? limit 1", ei).Scan(&t, &event_id)

	switch {
	case err == sql.ErrNoRows:
		log.Printf("no row with id %d\n", ei)
	case err != nil:
		log.Fatalf("query error: %v\n", err)
	default:
		log.Printf("timestamp is %v, event_id %d\n", t, event_id)
	}

Configuration

Environment

  • Client version: v2.13.0
  • Language version: go1.20.2 windows/amd64
  • OS: win
  • Interface: database/sql compatible driver

ClickHouse server

  • ClickHouse Server version: 23.2.1.2537
  • CREATE TABLE statements for tables involved:
    CREATE TABLE new
    (
    timestamp Date32,
    event_id UInt8
    )
    ENGINE = MergeTree
    ORDER BY event_id
@jkaflik
Copy link
Contributor

jkaflik commented Aug 16, 2023

Hi @Alucardinio

I've reproduced this issue with following code:

package issues

import (
	"context"
	"github.com/ClickHouse/clickhouse-go/v2"
	clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"testing"
	"time"
)

func Test1066(t *testing.T) {
	var (
		conn, err = clickhouse_tests.GetConnection("issues", clickhouse.Settings{
			"max_execution_time": 60,
		}, nil, &clickhouse.Compression{
			Method: clickhouse.CompressionLZ4,
		})
	)
	ctx := context.Background()
	require.NoError(t, err)
	const ddl = `
		CREATE TABLE test_1066 (
			Col1 Date32
		) Engine MergeTree() ORDER BY tuple()
		`
	defer func() {
		conn.Exec(ctx, "DROP TABLE IF EXISTS test_1066")
	}()
	require.NoError(t, conn.Exec(ctx, ddl))

	expectedDate := time.Date(2010, 10, 10, 0, 0, 0, 0, time.UTC)

	require.NoError(t, conn.Exec(ctx, `INSERT INTO test_1066 (Col1) VALUES(?)`, expectedDate))

	row := conn.QueryRow(ctx, "SELECT Col1 FROM test_1066")
	require.NoError(t, err)
	var actualDate time.Time
	require.NoError(t, row.Scan(&actualDate))

	assert.Equal(t, expectedDate, actualDate)
}

It seems there is an issue in Date32 native format decoding. Will continue investigating.

@jkaflik
Copy link
Contributor

jkaflik commented Aug 17, 2023

The root cause is wrong Date32 implementation in ch-go: ClickHouse/ch-go#319

@jkaflik
Copy link
Contributor

jkaflik commented Aug 17, 2023

This will be fixed by ClickHouse/ch-go#320

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants