-
Notifications
You must be signed in to change notification settings - Fork 2
/
sql_judge.py
302 lines (262 loc) · 14.1 KB
/
sql_judge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""sql judge main script."""
import os
import sys
from judge.dodona_command import (
Annotation,
AnnotationSeverity,
Context,
DodonaException,
ErrorType,
Judgement,
MessageFormat,
MessagePermission,
Tab,
TestCase,
)
from judge.dodona_config import DodonaConfig
from judge.sql_database import SQLDatabase, sql_run_pragma_startup_queries
from judge.sql_judge_non_select_feedback import non_select_feedback
from judge.sql_judge_select_feedback import select_feedback
from judge.sql_query import SQLQuery
from judge.sql_query_result import SQLQueryResult
from judge.translator import Translator
# extract info from exercise configuration
config = DodonaConfig.from_json(sys.stdin.read())
with Judgement():
config.sanity_check()
# Initiate translator
config.translator = Translator.from_str(config.natural_language)
# Set 'max_rows' to 100 if not set
config.max_rows = int(getattr(config, "max_rows", 100))
# Set 'semicolon_warning' to True if not set
config.semicolon_warning = bool(getattr(config, "semicolon_warning", True))
# Set 'order_unordered_rows' to False if not set
config.order_unordered_rows = bool(getattr(config, "order_unordered_rows", False))
# Set 'strict_identical_order_by' to True if not set
config.strict_identical_order_by = bool(getattr(config, "strict_identical_order_by", True))
# Set 'allow_different_column_order' to True if not set
config.allow_different_column_order = bool(getattr(config, "allow_different_column_order", True))
# Set 'pragma_startup_queries' to "" if not set
config.pragma_startup_queries = str(getattr(config, "pragma_startup_queries", ""))
# Set 'pre_execution_forbidden_symbolregex' to [".*sqlite_(temp_)?(master|schema).*", "pragma"] if not set
defaults = [".*sqlite_(temp_)?(master|schema).*", "pragma"]
config.pre_execution_forbidden_symbolregex = list(getattr(config, "pre_execution_forbidden_symbolregex", defaults))
# Set 'pre_execution_mandatory_symbolregex' to [] if not set
config.pre_execution_mandatory_symbolregex = list(getattr(config, "pre_execution_mandatory_symbolregex", []))
# Set 'pre_execution_forbidden_fullregex' to [] if not set
config.pre_execution_forbidden_fullregex = list(getattr(config, "pre_execution_forbidden_fullregex", []))
# Set 'pre_execution_mandatory_fullregex' to [] if not set
config.pre_execution_mandatory_fullregex = list(getattr(config, "pre_execution_mandatory_fullregex", []))
# Set 'post_execution_forbidden_symbolregex' to [] if not set
config.post_execution_forbidden_symbolregex = list(getattr(config, "post_execution_forbidden_symbolregex", []))
# Set 'post_execution_mandatory_symbolregex' to [] if not set
config.post_execution_mandatory_symbolregex = list(getattr(config, "post_execution_mandatory_symbolregex", []))
# Set 'post_execution_forbidden_fullregex' to [] if not set
config.post_execution_forbidden_fullregex = list(getattr(config, "post_execution_forbidden_fullregex", []))
# Set 'post_execution_mandatory_fullregex' to [] if not set
config.post_execution_mandatory_fullregex = list(getattr(config, "post_execution_mandatory_fullregex", []))
if hasattr(config, "database_files"):
config.database_files = [
(str(filename), os.path.join(config.resources, filename)) for filename in config.database_files
]
for _, file in config.database_files:
if not os.path.exists(file):
raise DodonaException(
config.translator.error_status(ErrorType.INTERNAL_ERROR),
permission=MessagePermission.STAFF,
description=f"Could not find database file: '{file}'.",
format=MessageFormat.TEXT,
)
else:
# Set 'database_dir' to "." if not set
config.database_dir = str(getattr(config, "database_dir", "."))
config.database_dir = os.path.join(config.resources, config.database_dir)
if not os.path.exists(config.database_dir):
raise DodonaException(
config.translator.error_status(ErrorType.INTERNAL_ERROR),
permission=MessagePermission.STAFF,
description=f"Could not find database directory: '{config.database_dir}'.",
format=MessageFormat.TEXT,
)
config.database_files = [
(filename, os.path.join(config.database_dir, filename))
for filename in sorted(os.listdir(config.database_dir))
if filename.endswith(".sqlite")
]
if len(config.database_files) == 0:
raise DodonaException(
config.translator.error_status(ErrorType.INTERNAL_ERROR),
permission=MessagePermission.STAFF,
description="Could not find database files. "
"Make sure that the database directory contains '*.sqlite' "
"files or a valid 'database_files' option is provided.",
format=MessageFormat.TEXT,
)
# Set 'solution_sql' to "./solution.sql" if not set
config.solution_sql = str(getattr(config, "solution_sql", "./solution.sql"))
config.solution_sql = os.path.join(config.resources, config.solution_sql)
if not os.path.exists(config.solution_sql):
raise DodonaException(
config.translator.error_status(ErrorType.INTERNAL_ERROR),
permission=MessagePermission.STAFF,
description=f"Could not find solution file: '{config.solution_sql}'.",
format=MessageFormat.TEXT,
)
# Parse solution query
with open(config.solution_sql, "r", encoding="utf-8") as sql_file:
config.raw_solution_file = sql_file.read()
config.solution_queries = SQLQuery.from_raw_input(config.raw_solution_file)
if len(config.solution_queries) == 0:
raise DodonaException(
config.translator.error_status(ErrorType.INTERNAL_ERROR),
permission=MessagePermission.STAFF,
description="Solution file is empty.",
format=MessageFormat.TEXT,
)
# Parse submission query
with open(config.source, "r", encoding="utf-8") as sql_file:
config.raw_submission_file = sql_file.read()
config.submission_queries = SQLQuery.from_raw_input(config.raw_submission_file)
if len(config.submission_queries) > len(config.solution_queries):
raise DodonaException(
config.translator.error_status(ErrorType.RUNTIME_ERROR),
permission=MessagePermission.STUDENT,
description=config.translator.translate(
Translator.Text.SUBMISSION_CONTAINS_MORE_QUERIES,
submitted=len(config.submission_queries),
expected=len(config.solution_queries),
),
format=MessageFormat.CALLOUT_DANGER,
)
if config.semicolon_warning and (
len(config.submission_queries) == 0 or not config.submission_queries[-1].has_ending_semicolon
):
with Annotation(
row=config.raw_submission_file.rstrip().count("\n"),
type=AnnotationSeverity.WARNING,
text=config.translator.translate(Translator.Text.ADD_A_SEMICOLON),
):
pass
for query_nr, solution_query in enumerate(config.solution_queries): # noqa: C901
with Tab(f"Query {1 + query_nr}"):
if query_nr >= len(config.submission_queries):
raise DodonaException(
config.translator.error_status(ErrorType.RUNTIME_ERROR),
permission=MessagePermission.STUDENT,
description=config.translator.translate(
Translator.Text.SUBMISSION_CONTAINS_LESS_QUERIES,
expected=len(config.solution_queries),
submitted=len(config.submission_queries),
),
format=MessageFormat.CALLOUT_DANGER,
)
submission_query = config.submission_queries[query_nr]
if solution_query.query_type != submission_query.query_type:
raise DodonaException(
config.translator.error_status(ErrorType.RUNTIME_ERROR),
permission=MessagePermission.STUDENT,
description=config.translator.translate(
Translator.Text.SUBMISSION_WRONG_QUERY_TYPE,
submitted=submission_query.query_type,
),
format=MessageFormat.CALLOUT_DANGER,
)
match = submission_query.match_multi_regex(
config.pre_execution_forbidden_symbolregex,
config.pre_execution_mandatory_symbolregex,
config.pre_execution_forbidden_fullregex,
config.pre_execution_mandatory_fullregex,
)
if match is not None:
raise DodonaException(
config.translator.error_status(ErrorType.RUNTIME_ERROR),
permission=MessagePermission.STUDENT,
description=config.translator.translate(
match[0],
value=match[1],
),
format=MessageFormat.CALLOUT_DANGER,
)
for db_name, db_file in config.database_files:
with Context(), TestCase(
format=MessageFormat.SQL,
description=f"-- sqlite3 {db_name}\n{submission_query.without_comments}",
) as testcase:
expected_output: SQLQueryResult
generated_output: SQLQueryResult
with SQLDatabase(db_file, config.workdir, db_name) as db:
cursor = db.solution_cursor()
try:
if config.pragma_startup_queries != "":
sql_run_pragma_startup_queries(cursor, config.pragma_startup_queries)
except Exception as err:
raise DodonaException(
config.translator.error_status(ErrorType.INTERNAL_ERROR),
permission=MessagePermission.STAFF,
description=f"Startup script is not working ({type(err).__name__}):\n {err}",
format=MessageFormat.CODE,
) from err
# RUN SOLUTION QUERY
try:
cursor.execute(solution_query.without_comments)
except Exception as err:
raise DodonaException(
config.translator.error_status(ErrorType.INTERNAL_ERROR),
permission=MessagePermission.STAFF,
description=f"Solution is not working ({type(err).__name__}):\n {err}",
format=MessageFormat.CODE,
) from err
# RENDER SOLUTION QUERY OUTPUT
expected_output = SQLQueryResult.from_cursor(config.max_rows, cursor)
cursor = db.submission_cursor()
try:
if config.pragma_startup_queries != "":
sql_run_pragma_startup_queries(cursor, config.pragma_startup_queries)
except Exception as err:
raise DodonaException(
config.translator.error_status(ErrorType.INTERNAL_ERROR),
permission=MessagePermission.STAFF,
description=f"Startup script is not working ({type(err).__name__}):\n {err}",
format=MessageFormat.CODE,
) from err
# RUN SUBMISSION QUERY
try:
cursor.execute(submission_query.without_comments)
except Exception as err:
raise DodonaException(
config.translator.error_status(ErrorType.COMPILATION_ERROR),
permission=MessagePermission.STUDENT,
description=f"{type(err).__name__}:\n {err}",
format=MessageFormat.CODE,
) from err
# RENDER SUBMISSION QUERY OUTPUT
generated_output = SQLQueryResult.from_cursor(config.max_rows, cursor)
if not solution_query.is_select:
non_select_feedback(config, testcase, db_name, db_file, solution_query)
else:
select_feedback(
config,
testcase,
expected_output,
generated_output,
solution_query,
submission_query,
)
if getattr(testcase, "accepted", True): # Only run if all other tests are OK
match = submission_query.match_multi_regex(
config.post_execution_forbidden_symbolregex,
config.post_execution_mandatory_symbolregex,
config.post_execution_forbidden_fullregex,
config.post_execution_mandatory_fullregex,
)
if match is not None:
raise DodonaException(
config.translator.error_status(ErrorType.WRONG),
recover_at=Context, # Continue testing all other contexts
permission=MessagePermission.STUDENT,
description=config.translator.translate(
match[0],
value=match[1],
),
format=MessageFormat.CALLOUT_DANGER,
)