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

Add some default macros #45

Merged
merged 2 commits into from
Sep 17, 2021
Merged

Add some default macros #45

merged 2 commits into from
Sep 17, 2021

Conversation

andresmgot
Copy link
Collaborator

Fixes #43

I have added default macros and a suggested implementation (that should work for any SQL server). It's expected from the data sources to re-implement them if necessary, to adjust to their needs.

The goal is to set a common ground so all the SQL datasources implement at least a common set of macros following the pattern defined here.

Copy link
Contributor

@yesoreyeram yesoreyeram left a comment

Choose a reason for hiding this comment

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

LGTM. Can we add few lines in README about this? otherwise will become difficult to track these changes in future.

macros.go Outdated
to = query.TimeRange.To.UTC().Format(time.RFC3339)
)

return fmt.Sprintf("%s BETWEEN '%s' AND '%s'", column, from, to), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think all the databases support BETWEEN. Some require syntax like timefield >= starttime AND timefield < endtime. Wondering what is the most common approach.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it seems to be part of the standard but that should also work.

macros.go Outdated
return "", errors.WithMessagef(ErrorBadArgumentCount, "expected 1 argument, received %d", len(args))
}

return fmt.Sprintf("%s > '%s'", args[0], query.TimeRange.From.UTC().Format(time.RFC3339)), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be >= instead >?

Copy link
Collaborator

@kminehart kminehart left a comment

Choose a reason for hiding this comment

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

Maybe it's worth continuing the discussion. I'm skeptical that any kind of default macro is going to work for every database.

I wonder if all databases even support comparing times with > or <.

Perhaps for those that don't, they could overwrite the key in the DefaultMacros map? If that's a possible use case we should document that :)

go.mod Outdated Show resolved Hide resolved
macros.go Outdated Show resolved Hide resolved
@andresmgot
Copy link
Collaborator Author

Maybe it's worth continuing the discussion. I'm skeptical that any kind of default macro is going to work for every database.

I wonder if all databases even support comparing times with > or <.

Perhaps for those that don't, they could overwrite the key in the DefaultMacros map? If that's a possible use case we should document that :)

Yes, my intention here is to add macros that work in most databases (for example the > or < operand is valid for all the databases that use sqlds at the moment) but they can be overwritten by the datasources (and that would be the most common case for macros like $__timeGroup). In the tests there is an example of how to override a macro, they just need to re-define it:

https://github.com/grafana/sqlds/pull/45/files#diff-f12dec668f034baf10152ef12db851fe27e9b65b8b5a515ec08359027326c648R29

@andresmgot
Copy link
Collaborator Author

LGTM. Can we add few lines in README about this? otherwise will become difficult to track these changes in future.

Good point. I will add that.

Copy link
Collaborator

@sunker sunker left a comment

Choose a reason for hiding this comment

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

Nice work @andresmgot!

@andresmgot
Copy link
Collaborator Author

Drone keeps not building my PRs :/ checked the tests locally and it passes so merging this to unblock me.

@andresmgot andresmgot merged commit c1cab59 into main Sep 17, 2021
@sasklacz sasklacz deleted the defaultMacros branch January 9, 2023 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discussion: Macros standardization
4 participants