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

Handle TimeStamp columns by converting int64 to time.Time and vice versa #15

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

echarlus
Copy link

@echarlus echarlus commented May 9, 2017

Since one cannot yet replace the driver.DefaultParameterConverter to properly handle type conversions (see golang/go#18415)
I've made a quick patch to automatically handle TimeStamp (int64) columns conversion to time.Time by using the column type provided by Crate within the query’s results and to convert back to int64 any time.

…erly handle type conversions, handle at least time.Time columns properly by using the column type provided by Crate within the query’s results. driver.DefaultParameterConverter override is scheduled for GO 1.9 (see golang/go#18415)
@sourcelevel-bot
Copy link

Hello, @echarlus! This is your first Pull Request that will be reviewed by Ebert, an automatic Code Review service. It will leave comments on this diff with potential issues and style violations found in the code as you push new commits. You can also see all the issues found on this Pull Request on its review page. Please check our documentation for more information.

crate.go Outdated
sec := v/int64(1000)
dest[i] = time.Unix(sec, (v-sec*int64(1000))*int64(1000000))
} else {
return errors.New(fmt.Sprintf("Failed to convert column %s=%T to time\n", r.columns[i], r.values[r.pos][i]))

Choose a reason for hiding this comment

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

should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...)

@herenow
Copy link
Owner

herenow commented May 9, 2017

If we changed the output/parsing of time columns from int64 to time.Time, it would cause breaking changes.

Although it would be nice to parse time.Time to int64 when receiving arguments.

Let me give this some thought.

@echarlus
Copy link
Author

Hello,
the code I provided also converts time.Time found in query arguments to int64 so that Time objects can be transparently used by the upper layers.
I'm not sure I'm following what you mean by "If we changed the output/parsing of time columns from int64 to time.Time, it would cause breaking changes." could you please elaborate ?
Thanks !

@herenow
Copy link
Owner

herenow commented May 10, 2017

Wouldn't this change the value we are assigning to each row column?

https://golang.org/pkg/database/sql/#Rows.Scan

So if we did:

// create table users ( created_at timestmap );
rows, err := db.Query("select created_at from users limit 1")

for rows.Next() {
	var created_at time.Time

	// Now scan, expects a time.Time destination value
	rows.Scan(&created_at)
}

From what I understood, this PR changes what we are scanning into the destination we pass into rows.Scan(), so if the destination is an int64, it would fail when we tried to assign a time.Time into it, wouldn't it?

Maybe I'm confused, and got this all wrong, I haven't worked on this for a while.

I'll give this code a test.

@echarlus
Copy link
Author

Since the conversion from int64 to time.Time is performed in the query method (which is called by Query) the row type would indeed by time.Time and your sample should work as expected. So I believe that my change does not break the Scan function. Let me know what your findings show ...
Thanks !

crate.go Outdated
sec := v / int64(1000)
dest[i] = time.Unix(sec, (v-sec*int64(1000))*int64(1000000))
} else {
return fmt.Errorf("Failed to convert column %s=%T to time\n", r.columns[i], r.values[r.pos][i])

Choose a reason for hiding this comment

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

error strings should not be capitalized or end with punctuation or a newline

crate.go Outdated
@@ -53,6 +57,39 @@ type endpointQuery struct {
Args []driver.Value `json:"args,omitempty"`
}

//CER : Convert map, Time & GeoPoint arguments to DB format.

Choose a reason for hiding this comment

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

comment on exported method CrateDriver.CheckNamedValue should be of the form "CheckNamedValue ..."

crate.go Outdated
)

// Crate conn structure
type CrateDriver struct {
Url string // Crate http endpoint url
}

type GeoPoint [2]float64

Choose a reason for hiding this comment

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

exported type GeoPoint should have comment or be unexported

Fixed code according to the GitHub boat comments
Added Scan method on GeoPoint to allow gorm to handle GeoPoint embedded in an object
}

//CrateArray represents an Array column type
type CrateArray []interface{}

Choose a reason for hiding this comment

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

type name will be used as crate.CrateArray by other packages, and that stutters; consider calling this Array

… handle floats with null decimals to prevent creating an int typed column in crate when a float is required
crate.go Outdated
fv := v.Float()
i := float64(int32(fv))
if i == fv {
buf.WriteString(fmt.Sprintf("%0.1f", v))

Choose a reason for hiding this comment

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

arg v for printf verb %f of wrong type: reflect.Value

crate.go Outdated
}

//MarshalJSON custom JSON marshal function to properly handle arrays of floats with decimal part equals to 0
func (v CrateArray) MarshalJSON() ([]byte, error) {

Choose a reason for hiding this comment

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

receiver name v should be consistent with previous receiver name arr for CrateArray

…1000000000000003 ...

//See https://floating-point-gui.de/
//Float values are now truncated to 10^-6 which is more than enough for our needs
…n maps):

Prevents rounding errors seen with floats like 0.01*41 which is 0.41000000000000003 ...
See https://floating-point-gui.de/
Float values are now truncated to 10^-6 which is more than enough for our needs
Speed up float processing by removing un-necessary conversion & comparison
}

//MarshalJSON custom JSON marshal function to properly handle arrays of floats with decimal part equals to 0
func (v CrateArray) MarshalJSON() ([]byte, error) {

Choose a reason for hiding this comment

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

receiver name v should be consistent with previous receiver name arr for CrateArray

@sourcelevel-bot
Copy link

Ebert has finished reviewing this Pull Request and has found:

  • 2 possible new issues (including those that may have been commented here).

But beware that this branch is 23 commits behind the herenow:master branch, and a review of an up to date branch would produce more accurate results.

You can see more details about this review at https://ebertapp.io/github/herenow/go-crate/pulls/15.

chughpiyush added a commit to chughpiyush/go-crate that referenced this pull request May 31, 2019
chughpiyush added a commit to chughpiyush/go-crate that referenced this pull request May 31, 2019
@sourcelevel-bot
Copy link

SourceLevel has finished reviewing this Pull Request and has found:

  • 6 possible new issues (including those that may have been commented here).
  • 2 fixed issues! 🎉

But beware that this branch is 27 commits behind the herenow:master branch, and a review of an up to date branch would produce more accurate results.

See more details about this review.

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

Successfully merging this pull request may close these issues.

2 participants