Column-wise macros
Column-wise macros allow using symbols instead of columns. The order of the arguments is always the same: the first argument is the table and the last argument is the expression (can be a begin ... end
block). If the table is omitted, the macro is automatically curried (useful for piping).
Shared features across all row-wise macros:
- Symbols refer to fields of the row.
_
refers to the whole table.- To use actual symbols, escape them with
^
, as in^(:a)
. - Use
cols(c)
to refer to field c wherec
is a variable that evaluates to a symbol.c
must be available in the scope where the macro is called. - An optional grouping argument is allowed: see Column-wise macros with grouping argument
- Out-of-core tables are not supported out of the box, except when grouping
Replace symbols with columns
JuliaDBMeta.@with
— Macro.@with(d, x)
Replace all symbols in expression x
with the respective column in d
. In this context, _
refers to the whole table d
. To use actual symbols, escape them with ^
, as in ^(:a)
. Use cols(c)
to refer to column c
where c
is a variable that evaluates to a symbol. c
must be available in the scope where the macro is called.
Examples
julia> t = table((a = [1,2,3], b = ["x","y","z"]));
julia> @with t mean(:a)
2.0
julia> @with t mean(:a)*length(_)
6.0
julia> @with t join(:b)
"xyz"
julia> @with t @show ^(:a) != :a
:a != getfield(JuliaDBMeta.columns(t), :a) = true
true
julia> c = :a
:a
julia> @with t cols(c)
3-element Array{Int64,1}:
1
2
3
Note that you can use this syntax to modify columns in place as well:
julia> @with t :b .= :b .* string.(:a)
3-element Array{String,1}:
"x1"
"y2"
"z3"
julia> t
Table with 3 rows, 2 columns:
a b
───────
1 "x1"
2 "y2"
3 "z3"
Add or modify a column
JuliaDBMeta.@transform_vec
— Macro.@transform_vec(d, x)
Replace all symbols in expression x
with the respective column in d
: the result has to be a NamedTuple
of vectors or a table and is horizontally merged with d
. In this context, _
refers to the whole table d
. To use actual symbols, escape them with ^
, as in ^(:a)
. Use {}
syntax for automatically named NamedTuples
. Use cols(c)
to refer to column c
where c
is a variable that evaluates to a symbol. c
must be available in the scope where the macro is called.
Examples
julia> t = table((a = [1,2,3], b = ["x","y","z"]));
julia> @transform_vec t {:a .+ 1}
Table with 3 rows, 3 columns:
a b a .+ 1
──────────────
1 "x" 2
2 "y" 3
3 "z" 4
Select data
JuliaDBMeta.@where_vec
— Macro.@where_vec(d, x)
Replace all symbols in expression x
with the respective column in d
: the result has to be an Array
of booleans which is used to get a view of d
. In this context, _
refers to the whole row. To use actual symbols, escape them with ^
, as in ^(:a)
. The result has to be a NamedTuple
of vectors or a table and is horizontally merged with d
. Use {}
syntax for automatically named NamedTuples
. Use cols(c)
to refer to column c
where c
is a variable that evaluates to a symbol. c
must be available in the scope where the macro is called.
Examples
julia> t = table((a = [1,2,3], b = ["x","y","z"]));
julia> @where_vec t (:a .>= 2) .& (:b .!= "y")
Table with 1 rows, 2 columns:
a b
──────
3 "z"