AST Checks¶
As you could read in the tutorial, there are two families of SCT functions in sqlwhat:
- result-based checks, that look at the result of the student’s query and the solution query
- AST-based checks, that look at the abstract syntax tree representation of the query and allow you to check for elements.
This article gives an overview of all AST-based checks, that are typically used as the second argument to check_correct()
.
Warning
If a student submission cannot be parsed properly because of a syntax error,
all AST-based checks will not run! It is therefore vital that you include your
AST-based checks in the second argument of check_correct()
so they can serve
as ‘diagnosing’ SCTs rather than as ‘correctness verifying’ SCTs.
Checking the tree¶
Once you’ve used a combination of check_node()
and check_edge()
to zoom in on a part of interest
you can use has_equal_ast()
to verify whether the elements correspond.
Continuing from the INNER JOIN
example, we can verify whether the snippets of SQL code that have been zoomed in have a matching AST representation:
Ex(). \
check_node('SelectStmt'). \
check_edge('from_clause'). \
check_edge('cond'). \
has_equal_ast()
You can supplement this with a check_or()
call and a manually specified sql
snippet if you want to allow for multiple ways of specifying the condition:
Ex(). \
check_node('SelectStmt'). \
check_edge('from_clause'). \
check_edge('cond'). \
check_or(
has_equal_ast(),
has_equal_ast(sql = "countries.code = cities.code")
)
Now, using either ON cities.code = countries.code
or countries.code = cities.code
will be accepted.
For a more complete and robust example of an INNER JOIN
query, visit the glossary.
In addition to has_equal_ast()
, you can also use has_code()
to
look at the actual code of a part of the SQL query and verify it with a regular expression,
but you will rarely find yourself using it.