Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Error while scanning column of type `array<decimal(10,2)>` into Go type `[]float64`

Đang mở
#213 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
go, sql
Lĩnh vực
databases

Hướng nghiên cứu

Chạy reproduction đã được báo cáo của Test_Float64_Conversion, sau đó đọc internal/rows/arrowbased/columnValues.go quanh listValueContainer.Value, được xác định là nguồn của giá trị string. Hoàn thành khi việc scan một cột array<decimal(10,2)> vào []float64 thành công mà không gặp lỗi chuyển đổi database/sql đã được báo cáo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug

I have a table that has a column AMOUNTS with a type array<decimal(10,2)>, which I expect to scan into a field type []float64.
This is the test I wrote to reproduce it:

func (s *DatabricksRepositorySuite) Test_Float64_Conversion() {
	query := `SELECT AMOUNTS FROM components WHERE AMOUNTS IS NOT NULL AND array_size(AMOUNTS) > 0 LIMIT 1`
	var result []float64
	err := s.repository.db.QueryRow(query).Scan(&result)
	s.NoError(err)
}

When I run my query, I get this error:

sql: Scan error on column index 0, name "AMOUNTS": unsupported Scan, storing driver.Value type string into type *[]float64

Source

This error comes from the standard library in Go: the value for that field is provided by the SDK as a string, but the destination is []float64, so it fails. I also dove into Databricks' SDK source code and found the place [2] where the string is generated.

[1] Golang source - convertAssignRows
[2] Databricks SDK - listValueContainer.Value

Workaround

I created a custom type for the fields I have that use arrays:

import (
	"database/sql/driver"
	"encoding/json"
	"errors"
)

type databricksArray[T any] struct {
	parsedValue []T
}

func (f *databricksArray[T]) Scan(value interface{}) error {
	switch typedValue := value.(type) {
	case []T:
		f.parsedValue = typedValue
		return nil
	case string:
		return f.parseString(typedValue)
	default:
		return errors.New("error while handling databricks type")
	}
}

func (f *databricksArray[T]) parseString(value string) error {
	// This is a workaround for Databricks returning float arrays as strings
	// Example: `[1.0, 2.0, 3.0]`
	// We need to convert it to an array
	// This should be removed if/when Databricks fixes this issue
	valueBytes := []byte(value)
	var parsedValue []T
	if err := json.Unmarshal(valueBytes, &parsedValue); err != nil {
		return err
	}
	f.parsedValue = parsedValue
	return nil
}

func (f *databricksArray[T]) Value() (driver.Value, error) {
	return f.parsedValue, nil
}

func (f *databricksArray[T]) Slice() []T {
	if f == nil {
		return nil
	}
	return f.parsedValue
}

So my field

Amount []float64 `db:"AMOUNTS"`

is now

Amount databricksArray[float64] `db:"AMOUNTS"`

and I call Amount.Slice() when I want to read its value.

Ngôn ngữ chính
Go
Star
53
Fork
66
Merge trung bình
21 giờ 29 phút
Pull request đã merge (30 ngày)
12

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của databricks/databricks-sql-go

Tất cả issue của databricks/databricks-sql-go

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.