Skip to content

Commit

Permalink
fix: add test for nullable type; make insert into work and test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
yuryfirebolt committed Aug 10, 2022
1 parent 72add3e commit 1bdace2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ func (c *Client) Query(engineUrl, databaseName, query string, setStatements *map
return ConstructNestedError("error during query request", err)
}

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

if err = json.Unmarshal(response, &queryResponse); err != nil {
return 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 @@ -40,6 +40,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 @@ -90,6 +91,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 @@ -108,6 +110,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)
}
4 changes: 2 additions & 2 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func assert(val bool, t *testing.T, err string) {
}

func mockRows() driver.Rows {
resultJson := "{\"query\":{\"query_id\":\"16FF2A0300ECA753\"},\"meta\":[{\"name\":\"int_col\",\"type\":\"Int32\"},{\"name\":\"bigint_col\",\"type\":\"Int64\"},{\"name\":\"float_col\",\"type\":\"Float32\"},{\"name\":\"double_col\",\"type\":\"Float64\"},{\"name\":\"text_col\",\"type\":\"String\"},{\"name\":\"date_col\",\"type\":\"Date\"},{\"name\":\"timestamp_col\",\"type\":\"DateTime\"},{\"name\":\"bool_col\",\"type\":\"UInt8\"},{\"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],[[]]],[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,\"statistics\":{\"elapsed\":0.001797702,\"rows_read\":3,\"bytes_read\":293,\"time_before_execution\":0.001251613,\"time_to_execute\":0.000544098,\"scanned_bytes_cache\":2003,\"scanned_bytes_storage\":0}}"
resultJson := "{\"query\":{\"query_id\":\"16FF2A0300ECA753\"},\"meta\":[{\"name\":\"int_col\",\"type\":\"Nullable(Int32)\"},{\"name\":\"bigint_col\",\"type\":\"Int64\"},{\"name\":\"float_col\",\"type\":\"Float32\"},{\"name\":\"double_col\",\"type\":\"Float64\"},{\"name\":\"text_col\",\"type\":\"String\"},{\"name\":\"date_col\",\"type\":\"Date\"},{\"name\":\"timestamp_col\",\"type\":\"DateTime\"},{\"name\":\"bool_col\",\"type\":\"UInt8\"},{\"name\":\"array_col\",\"type\":\"Array(Int32)\"},{\"name\":\"nested_array_col\",\"type\":\"Array(Array(String))\"}],\"data\":[[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,\"statistics\":{\"elapsed\":0.001797702,\"rows_read\":3,\"bytes_read\":293,\"time_before_execution\":0.001251613,\"time_to_execute\":0.000544098,\"scanned_bytes_cache\":2003,\"scanned_bytes_storage\":0}}"
var response QueryResponse
err := json.Unmarshal([]byte(resultJson), &response)
if err != nil {
Expand Down Expand Up @@ -56,7 +56,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

0 comments on commit 1bdace2

Please sign in to comment.