Row-wise macros
Row-wise macros allow using symbols to refer to fields of a row. 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 row.- To use actual symbols, escape them with
^, as in^(:a). - Use
cols(c)to refer to field c wherecis a variable that evaluates to a symbol.cmust be available in the scope where the macro is called. - Out-of-core tables are supported out of the box
Modify data in place
JuliaDBMeta.@byrow! — Macro.@byrow!(d, x)
Apply the expression x row by row in d (to modify d in place). Symbols refer to fields of the row. In this context, _ refers to the whole row. To use actual symbols, escape them with ^, as in ^(:a). Use cols(c) to refer to field 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> @byrow! t :b = :b*string(:a)
Table with 3 rows, 2 columns:
a b
───────
1 "x1"
2 "y2"
3 "z3"
julia> @byrow! t begin
:a = :a*2
:b = "x"^:a
end
Table with 3 rows, 2 columns:
a b
───────────
2 "xx"
4 "xxxx"
6 "xxxxxx"Apply a function
JuliaDBMeta.@map — Macro.@map(d, x)
Apply the expression x row by row in d: return the result as an array or as a table (if the elements are Tuples or NamedTuples). Use {} syntax for automatically named NamedTuples. Symbols refer to fields of the row. In this context, _ refers to the whole row. To use actual symbols, escape them with ^, as in ^(:a). Use cols(c) to refer to field 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> @map t :b*string(:a)
3-element Array{String,1}:
"x1"
"y2"
"z3"
julia> @map t {:a, copy = :a, :b}
Table with 3 rows, 3 columns:
a copy b
────────────
1 1 "x"
2 2 "y"
3 3 "z"Add or modify a column
JuliaDBMeta.@transform — Macro.@transform(d, x)
Apply the expression x row by row in d: collect the result as a table (elements returned by x must be NamedTuples) and merge it horizontally with d. In this context, _ refers to the whole row. To use actual symbols, escape them with ^, as in ^(:a).
Use {} syntax for automatically named NamedTuples. Use cols(c) to refer to field 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 t {:a + 1}
Table with 3 rows, 3 columns:
a b a + 1
──────────────
1 "x" 2
2 "y" 3
3 "z" 4Select data
JuliaDBMeta.@where — Macro.@where(d, x)
Apply the expression x row by row in d: collect the result as an Array (elements returned by x must be booleans) and use it to get a view of d. In this context, _ refers to the whole row. To use actual symbols, escape them with ^, as in ^(:a).
Use {} syntax for automatically named NamedTuples. Use cols(c) to refer to field 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 t :a <= 2
Table with 2 rows, 2 columns:
a b
──────
1 "x"
2 "y"JuliaDBMeta.@filter — Macro.@filter(d, x)
Filter rows according to the expression x row by row in d. Symbols refer to fields of the row. In this context, _ refers to the whole row. To use actual symbols, escape them with ^, as in ^(:a). Use cols(c) to refer to field 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=[2,3,4]));
julia> @filter t :a < 3
Table with 2 rows, 2 columns:
a b
────
1 2
2 3
julia> @filter t 2*:a > :b
Table with 2 rows, 2 columns:
a b
────
2 3
3 4