Named columns and convenience SQL query interface#6543
Merged
sougou merged 1 commit intovitessio:masterfrom Aug 7, 2020
Merged
Named columns and convenience SQL query interface#6543sougou merged 1 commit intovitessio:masterfrom
sougou merged 1 commit intovitessio:masterfrom
Conversation
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR offers a more modern interface to SQL query results based on named columns, as well as convenience methods for casting result values into popular types. Some breakdown:
Convenience methods in
ValuePreviously, after
selecting an integer column, the process to convert it toint64was:New convenience methods support the following:
There is no longer a need to call an external
evalenginefunction.supported convenience methods are:
ToInt64()ToUint64()ToBool()ToString()pre-existed.A new type:
NamedResultResulttype now has aNamed()method that returns aNamedResulttype. It has same fields, insert ID, rows affected asResult. It has same Rows data, but the data is an array of maps, where value is found by column name as opposed to ordinal position.Again iterating on the above example:
Note:
trueinconn.Exec()to get field namestm.Rows[0]["ts"]means "columntsin first row", and now the ordinal of the column is unimportant. In this particular example we return a single column, so maybe this example is underwhelming, but in a result set with 10 columns, names are a safer approach to access columns than ordinals.Convenience methods in
NamedResult:We further make access more convenient like so:
or
.ToInt64("ts")is a syntactic sugar shortcut.AsInt64("ts", 0)provides default value (0in this case) and returns with no error. If column does not exist, or if there's a casting error, the function returns the default value silently.We likewise have
ToString,AsString,ToUint64,AsUint64,ToBool,AsBoolfunctions.Single
Row()Single row queries are common. Examples:
SELECT UNIX_TIMESTAMP()SELECT @@global.read_only, @@global.gtid_modeSELECT COUNT(*) FROM my_table WHERE status=1The
Row()function returns the first and single row in a result set, ornilif this isn't a single row result set.Again iterating on our example:
Optimistic example
When we
SELECT UNIX_TIMESTAMP()it is safe to assume that the query either returns with an error, or returns with a single row that has a single integer column. Therefore, we can cleanup the above code to read: