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

float returned from query incorrectly #34

Closed
jdezego opened this issue Jul 16, 2019 · 3 comments
Closed

float returned from query incorrectly #34

jdezego opened this issue Jul 16, 2019 · 3 comments
Labels

Comments

@jdezego
Copy link

jdezego commented Jul 16, 2019

I have this query:

SELECT m_disks.used_size / m_disks.total_size * 100 AS DISK_UTILIZATION

The data that gets returned form HANA looks like this:

"DISK_UTILIZATION": "7q\u0002\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u000080"

I can work around this by using a CAST AS INT in my SQL query, but it seems this might be an issue with the driver.

@stfnmllr
Copy link
Contributor

Can you please share a source code example (test) to reproduce the issue?
Thanks an best regards

@jdezego
Copy link
Author

jdezego commented Jul 17, 2019

If you want to actually test this you'll need a running instance of HANA:

package main

import (
	"bytes"
	"compress/gzip"
	"database/sql"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"

	_ "github.com/SAP/go-hdb/driver"
)

//structure to hold JSON data
type insightsEvent struct {
	EventType string `json:"eventType"`
	DiskUsage string `json:"diskUsage"`
}

func main() {

	//initialize event object and set EventType
	var event insightsEvent
	event.EventType = "ServiceCheck"

	dsn := "hdb://USERNAME:PASSWORD@HOST:PORT"
	db, err := sql.Open("hdb", dsn)
	defer db.Close()

	if err != nil {
		fmt.Println("Connection failed")
	}

	q := "SELECT m_disks.used_size / m_disks.total_size * 100 FROM sys.m_disks"

	//fill remaining event fields with data returned from query
	err = db.QueryRow(q).Scan(&event.DiskUsage)

	if err != nil {
		fmt.Println(err)
	}

	//convert event to json
	e, err := json.Marshal(event)
	if err != nil {
		fmt.Println(err)
		return
	}

	stringer := string(e)
	fmt.Println(stringer)

This is what gets printed:

{"eventType":"ServiceCheck","diskUsage":"4q\u0002\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u000080"}

@stfnmllr
Copy link
Contributor

please use the expected go type (e.g. float) for scan field event.DiskUsage - with the current string type the result is the string representation of the underlying 'raw' value provided by the hdb interface

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

No branches or pull requests

2 participants