Dynamic @select with Query.jl

Hello,

I’m using Query,jl with LINQ style query command.
I try to pass a dynamic selection of variables to @select

For example, instead of writing this:

data_table =
    @from i in data begin
    @select {i.id, i.lastname, i.login} 
    @collect JuliaDB.table
 end

I would like to dynamically build the curly brackets argument passed to @select.
This first attempt doesn’t work:

parsed_ex = Meta.parse("{i.id, i.lastname, i.login}")
data_table =
    @from i in data begin
    @select parsed_ex
    @collect JuliaDB.table
 end

Any idea?

You should be able to do:

@eval @from i in data begin
    @select $parsed_ex
    @collect JuliaDB.table
end

This is because the macro only gets passed the variable name and nothing else. @eval will insert the expression before it is compiled. It’s a bit hacky, but should work. Next time, please include a self contained example, this will make it much easier to help you.

1 Like

The code you gave is almost working, it’s just missing the ‘$’ sign on one of the variable.

Here is the corrected version:

parsed_ex = Meta.parse("{i.id, i.lastname, i.login}")
data_table = @eval @from i in $data begin
    @select $parsed_ex
    @collect JuliaDB.table
 end

NOTE:
The performance of this are actually terrible. My original motivation was to rename data using a dynamic set of names. What I ended up doing is renaming the table after the linq filter:

 rename!(data, f => t for (f, t) = zip(vector_of_old_symbols,
                                                       vector_of_new_symbols))