Skip to content

Commit

Permalink
Merge from main
Browse files Browse the repository at this point in the history
  • Loading branch information
yuryfirebolt committed Aug 12, 2022
2 parents 2e053fd + 8accd3a commit 12ecc48
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ func (c *Client) Query(ctx context.Context, engineUrl, databaseName, query strin
}

var queryResponse QueryResponse
if len(response) == 0 {
// response could be empty, which doesn't mean it is an error
return &queryResponse, nil
}

if err = json.Unmarshal(response, &queryResponse); err != nil {
return nil, ConstructNestedError("wrong response", errors.New(string(response)))
}
Expand Down
22 changes: 22 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ func TestConnectionQueryWrong(t *testing.T) {
}
}

// TestConnectionInsertQuery checks simple Insert works
func TestConnectionInsertQuery(t *testing.T) {
markIntegrationTest(t)
conn := fireboltConnection{clientMock, databaseMock, engineUrlMock, map[string]string{}}
createTableSQL := "CREATE FACT TABLE integration_tests (id INT, name STRING) PRIMARY INDEX id"
deleteTableSQL := "DROP TABLE IF EXISTS integration_tests"
insertSQL := "INSERT INTO integration_tests (id, name) VALUES (0, 'some_text')"

if _, err := conn.ExecContext(context.TODO(), createTableSQL, nil); err != nil {
t.Errorf("statement returned an error: %v", err)
}
if _, err := conn.ExecContext(context.TODO(), "SET firebolt_dont_wait_for_upload_to_s3=1", nil); err != nil {
t.Errorf("statement returned an error: %v", err)
}
if _, err := conn.ExecContext(context.TODO(), insertSQL, nil); err != nil {
t.Errorf("statement returned an error: %v", err)
}
if _, err := conn.ExecContext(context.TODO(), deleteTableSQL, nil); err != nil {
t.Errorf("statement returned an error: %v", err)
}
}

// TestConnectionQuery checks simple SELECT query
func TestConnectionQuery(t *testing.T) {
markIntegrationTest(t)
Expand Down
9 changes: 9 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (f *fireboltRows) Next(dest []driver.Value) error {

for i, column := range f.response.Meta {
var err error
//log.Printf("Rows.Next: %s, %v", column.Type, f.response.Data[f.cursorPosition][i])
if dest[i], err = parseValue(column.Type, f.response.Data[f.cursorPosition][i]); err != nil {
return ConstructNestedError("error during fetching Next result", err)
}
Expand Down Expand Up @@ -138,6 +139,7 @@ func parseSingleValue(columnType string, val interface{}) (driver.Value, error)
// uint8, uint32, uint64, int32, int64, float32, float64, string, Time or []driver.Value for arrays
func parseValue(columnType string, val interface{}) (driver.Value, error) {
const (
nullablePrefix = "Nullable("
arrayPrefix = "Array("
dateTime64Prefix = "DateTime64("
decimalPrefix = "Decimal("
Expand All @@ -156,6 +158,13 @@ func parseValue(columnType string, val interface{}) (driver.Value, error) {
return parseSingleValue("DateTime64", val)
} else if strings.HasPrefix(columnType, decimalPrefix) && strings.HasSuffix(columnType, suffix) {
return parseSingleValue("Float64", val)
} else if strings.HasPrefix(columnType, nullablePrefix) && strings.HasSuffix(columnType, suffix) {
if val == nil {
return nil, nil
}

return parseSingleValue(columnType[len(nullablePrefix):len(columnType)-len(suffix)], val)
}

return parseSingleValue(columnType, val)
}
6 changes: 3 additions & 3 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func mockRows() driver.Rows {
resultJson := "{" +
"\"query\":{\"query_id\":\"16FF2A0300ECA753\"}," +
"\"meta\":[" +
" {\"name\":\"int_col\",\"type\":\"Int32\"}," +
" {\"name\":\"int_col\",\"type\":\"Nullable(Int32)\"}," +
" {\"name\":\"bigint_col\",\"type\":\"Int64\"}," +
" {\"name\":\"float_col\",\"type\":\"Float32\"}," +
" {\"name\":\"double_col\",\"type\":\"Float64\"}," +
Expand All @@ -30,7 +30,7 @@ func mockRows() driver.Rows {
" {\"name\":\"array_col\",\"type\":\"Array(Int32)\"}," +
" {\"name\":\"nested_array_col\",\"type\":\"Array(Array(String))\"}]," +
"\"data\":[" +
" [2,1,0.312321,123213.321321,\"text\", \"2080-12-31\",\"1989-04-15 01:02:03\",1,[1,2,3],[[]]]," +
" [null,1,0.312321,123213.321321,\"text\", \"2080-12-31\",\"1989-04-15 01:02:03\",1,[1,2,3],[[]]]," +
" [2,1,0.312321,123213.321321,\"text\",\"1970-01-01\",\"1970-01-01 00:00:00\",1,[1,2,3],[[]]]," +
" [3,5,0.312321,123213.321321,\"text\",\"1970-01-01\",\"1970-01-01 00:00:00\",1,[5,2,3,2],[[\"TEST\",\"TEST1\"],[\"TEST3\"]]]]," +
"\"rows\":3," +
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestRowsNext(t *testing.T) {
loc, _ := time.LoadLocation("UTC")

assert(err == nil, t, "Next shouldn't return an error")
assert(dest[0] == int32(2), t, "results not equal for int32")
assert(dest[0] == nil, t, "results not equal for int32")
assert(dest[1] == int64(1), t, "results not equal for int64")
assert(dest[2] == float32(0.312321), t, "results not equal for float32")
assert(dest[3] == float64(123213.321321), t, "results not equal for float64")
Expand Down
4 changes: 3 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ func formatValue(value driver.Value) (string, error) {
return "0", nil
}
case time.Time:
return fmt.Sprintf("'%s'", value.(time.Time).Format("2006-01-02 15:04:05")), nil
return fmt.Sprintf("'%s'", value.(time.Time).Format("2006-01-02 15:04:05 -07:00")), nil
case nil:
return "NULL", nil
default:
return "", fmt.Errorf("not supported type: %v", v)
}
Expand Down
5 changes: 3 additions & 2 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func runTestFormatValue(t *testing.T, value driver.Value, expected string) {
}

func TestFormatValue(t *testing.T) {
loc, _ := time.LoadLocation("UTC")
loc, _ := time.LoadLocation("Europe/Berlin")

runTestFormatValue(t, "", "''")
runTestFormatValue(t, "abcd", "'abcd'")
Expand All @@ -109,7 +109,8 @@ func TestFormatValue(t *testing.T) {
runTestFormatValue(t, true, "1")
runTestFormatValue(t, false, "0")
runTestFormatValue(t, -10, "-10")
runTestFormatValue(t, time.Date(2022, 01, 10, 1, 3, 2, 0, loc), "'2022-01-10 01:03:02'")
runTestFormatValue(t, nil, "NULL")
runTestFormatValue(t, time.Date(2022, 01, 10, 1, 3, 2, 0, loc), "'2022-01-10 01:03:02 +01:00'")

// not passing, but should: runTestFormatValue(t, 1.1234567, "1.1234567")
// not passing, but should: runTestFormatValue(t, 1.123, "1.123")
Expand Down

0 comments on commit 12ecc48

Please sign in to comment.