anse1 / sqlsmith

A random SQL query generator
GNU General Public License v3.0
754 stars 128 forks source link

[Question] Where to find AST mutation implementation? #30

Closed zhangysh1995 closed 3 years ago

zhangysh1995 commented 4 years ago

Hi, I read the talk slides and want to see the implementation of AST mutation. Could you give me any hints to start with?

anse1 commented 4 years ago

Yushan ZHANG writes:

Hi, I read the talk slides and want to see the implementation of AST mutation. Could you give me any hints to start with?

The AST is never changed in way after construction. Maybe I'm misunderstanding what you mean by mutation? Nevertheless some pointers: Construction of the AST happens top-down, the root node being created by

shared_ptr<prod> statement_factory(struct scope *s);

The generation of SQL is done by calling the root node's

void out(std::ostream &out);

Walking of the AST (without modifying it) is done via

virtual void accept(prod_visitor *v);

This is done for the impedance matching module to collect statistics on consistently failing grammar rules after the test result is known. But there are also some productions that walk parts of the AST to figure out whether they are legal in the current ast (e.g., window function are mostly expressions, but not leagal in all expression contexts).

regards, Andreas

zhangysh1995 commented 4 years ago

I want to ask where to find the implementation of generating new SQL query. Sorry for the ambiguous question.

df7cb commented 3 years ago

statement_factory() is in grammar.cc.