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

Support bytes #75

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this file

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sry, this file removed now

// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"args": [
"-short"
]
}
]
}
4 changes: 2 additions & 2 deletions trino/serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package trino

import (
"encoding/hex"
"encoding/json"
"fmt"
"reflect"
Expand Down Expand Up @@ -147,9 +148,8 @@ func Serial(v interface{}) (string, error) {
case string:
return "'" + strings.Replace(x, "'", "''", -1) + "'", nil

// TODO - []byte should probably be matched to 'VARBINARY' in trino
case []byte:
return "", UnsupportedArgError{"[]byte"}
return "X'" + strings.ToUpper(hex.EncodeToString(x)) + "'", nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds support for using []byte in query params, right? What about reading VARBINARY columns in query results?

Also, please update the README, there's a section there with a list of supported and not supported data types.

Copy link
Author

@chein-huang chein-huang Apr 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading VARBINARY columns in query results we need to implement sql.Scanner interface.
like

type TrinoVARBINARY []byte

func (h *TrinoVARBINARY) Scan(src any) (err error) {
	switch s := src.(type) {
	case string:
		var bytes []byte
		bytes, err = base64.StdEncoding.DecodeString(s)
		if err == nil {
			*h = bytes
		}
	default:
		err = fmt.Errorf("unsupported type: %T", src)
	}
	return
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would to like to try in this PR? I know I'm asking to extend the scope of work, so it's also ok to do this in a follow up PR

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do this in next PR. I hope to also support driver.Valuer interface


case trinoDate:
return fmt.Sprintf("DATE '%04d-%02d-%02d'", x.year, x.month, x.day), nil
Expand Down
10 changes: 10 additions & 0 deletions trino/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ func TestSerial(t *testing.T) {
value: false,
expectedSerial: "false",
},
{
name: "bytes",
value: []byte{1, 255},
expectedSerial: "X'01FF'",
},
{
name: "empty bytes",
value: []byte{},
expectedSerial: "X''",
},
{
name: "date",
value: Date(2017, 7, 10),
Expand Down