# “Write You A Query Language" —— To introduce the power of MLStyle.jl

**URL:** <https://discourse.julialang.org/t/write-you-a-query-language-to-introduce-the-power-of-mlstyle-jl/20590>\
**Category:** Package Announcements\
**Tags:** announcement, data, dataframes\
**Created:** [February 8, 2019, 5:18pm UTC](https://discourse.julialang.org/t/write-you-a-query-language-to-introduce-the-power-of-mlstyle-jl/20590 "2019-02-08T17:18:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [February 8, 2019, 5:18pm UTC](https://discourse.julialang.org/t/write-you-a-query-language-to-introduce-the-power-of-mlstyle-jl/20590/1 "2019-02-08T17:18:42Z")

</div>

Few days ago I get some interests on Query.jl, and I found it incredible exciting to implement it on my own and finally I did.  
I’m here to present this article, [Write You A Query Langauge](https://thautwarm.github.io/MLStyle.jl/latest/tutorials/query-lang/), where I made a tutorial for [MLStyle.jl](https://github.com/thautwarm/MLStyle.jl) to process complex macros gracefully and brought about a distinct prototype and some ideas for query languages in Julia.  
I don’t mean to announce a new package about query language, although my prototype might be quite clean, efficient, robust and provided with more functionalities and better extensibilities, this is mainly aimed at telling you about some super useful tools for AST manipulations.

The main advantages of this prototype(called `MQuery`, tentatively) are

- Support field names that’re not regular identifiers, e.g.,  
`@select _."middle school"`.

- No `NamedTuple` usage when executing computations, it might lead to better efficiency when the scale becomes extreme large.

- Unlike providing some fixed syntactic supports like `startswith`, `endswith`, `occursin` in Query.jl, MQuery allows arbitrary fieldname filter functions, e.g.,

- Support to extend query commands like `@select`, `@where`, and so on via [registered\_ops](https://github.com/thautwarm/MLStyle-Playground/blob/9cb3609a3cbadec2aa310ca2ba3499e078069bbf/MQuery/MQuery.MacroProcessor.jl#L74).

- Support closure reference in arbitray query expression.

- Allow you to add custom query support for your own datatypes via [3 interface functions: get\_fields, get\_records, build\_result](https://github.com/thautwarm/MLStyle-Playground/blob/9cb3609a3cbadec2aa310ca2ba3499e078069bbf/MQuery/MQuery.Interfaces.jl#L2). Codes below implement the query for `DataFrame`s:

- The code base is pretty small, and MQuery just depends on `MLStyle.jl`(depends on nothing) and `DataStructures.jl`. So MQuery’s friendly for you to integrate with.

The reason why MQuery could be that powerful is, [MLStyle.jl](https://github.com/thautwarm/MLStyle.jl) could help with all sorts of AST manipulations and make them become extremely easy, as a result, I can focus myself on other aspects like functionalities and extensibilities.

```julia
include("MQuery/MQuery.jl")
using Base.Enums
@enum TypeChecking Dynamic Static
df = DataFrame(
        Symbol("Type checking") => [
            Dynamic, Static, Static, Dynamic, Static, Dynamic, Dynamic, Static
        ],
        :name => [
            "Julia", "C#", "F#", "Ruby", "Java", "JavaScript", "Python", "Haskell"
        ],
        :year => [
            2012, 2000, 2005, 1995, 1995, 1995, 1990, 1990
        ]
)

df |>
@where !startswith(_.name, "Java"),
@groupby _."Type checking" => TC,
@having TC === Dynamic,
@select join(_.name, " and ") => result

│ Row │ result │
│ │ String │
├─────┼───────────────────────────┤
│ 1 │ Julia and Ruby and Python │

```

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [February 8, 2019, 7:46pm UTC](https://discourse.julialang.org/t/write-you-a-query-language-to-introduce-the-power-of-mlstyle-jl/20590/2 "2019-02-08T19:46:11Z")

</div>

Anyone has the tasks about AST manipulations can ask me for the solution to amazingly simplify(basically, could make the logics more clear) your codes.  
Why not have a try? 🙂
