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

Retain headers/column names in results #25

Closed
lc9er opened this issue Mar 26, 2021 · 7 comments
Closed

Retain headers/column names in results #25

lc9er opened this issue Mar 26, 2021 · 7 comments

Comments

@lc9er
Copy link

lc9er commented Mar 26, 2021

is it possible to retain the headers/column names in query results? It would be helpful to have column names in the results buffer.

Either way, this plugin is incredibly helpful. Thanks for releasing it.

@mechatroner
Copy link
Owner

Thanks, I appreciate the feedback!
The only way to retain the column names is to use queries like SELECT * or SELECT a1 or SELECT a.column_name in these cases the whole columns will be in the output including the column names. But if you use WHERE filtering or sorting there is currently no way to retain them. Unfortunately, RBQL doesn't "know" what columns are in the output because it is essentially a very shallow parser. In the future, I might add a side logic to guess the column names, but It would require some additional UI elements because the user should opt-in into column name guessing and/or confirm the guessed column names.

Another option that should be more reliable but less convenient is to add AS keyword so the users can write select a1 as new_column_name.

@mechatroner
Copy link
Owner

Actually, it might be possible if we teach RBQL how to be aware of comas and brackets (, [, { in user input. Currently, it is agnostic to them - the only special character RBQL tries to parse is string quotes ', ", ` to properly handle python and JS in-string expressions. I think matching the brackets should be relatively easy, and after that RBQL should be able to tell how many columns are in the output (for this just split the SELECT section by , after isolating the inside-bracket segments) and which of them can retain their names. This would be still shallow parsing, but a little bit deeper than the current version. I will try to prototype them to see if it is really possible - there could be some messy corner cases.

@mechatroner
Copy link
Owner

Another promising direction is to use ast module in Python instead of manually matching the brackets. Unfortunately, there is no built-in equivalent for JS.

@lc9er
Copy link
Author

lc9er commented Apr 9, 2021

I've had some time off, so I'm just getting back this. Thank you for your replies.

The only way to retain the column names is to use queries like SELECT * or SELECT a1 or SELECT a.column_name in these cases the whole columns will be in the output including the column names.

I see this now. My initial tests included sorting, and I missed that the header row was included. I found your notes in the README about using NR to exclude it from the results. Then of course, not sorting leaves the headers in place.

This helps, thanks.

Another option that should be more reliable but less convenient is to add AS keyword so the users can write select a1 as new_column_name.

I'm getting a syntax error when I try to use AS:

syntax error
^@    out_fields = [a1 as LNAME]^@                     ^^@SyntaxError: invalid syntax

This was with the query Select a1 AS LNAME, in a file with the following lines:

LastName,FirstName
Smitty,Doug
McGraw,Finn
Hazel,Mabel

@mechatroner
Copy link
Owner

Sorry, AS is not implemented yet, I was just thinking out loud about possible options. I opened a similar ticket in the RBQL repository: mechatroner/RBQL#31 and currently working on it. I am cautiously optimistic that I will be able to support more intuitive header handling so that they would be preserved even with sort operation.

@mechatroner
Copy link
Owner

Done. To retain headers you can either set g:rbql_with_headers = 1 - this will treat the first line as header (and retain it) in all CSV files by default. And you can also do this on query level by adding either WITH (header) or WITH (noheader) to the end of the query.

@lc9er
Copy link
Author

lc9er commented Jun 11, 2021 via email

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

No branches or pull requests

2 participants