Skip to content

Commit 4fd5965

Browse files
authored
Auto-format .sql for new exercises (#195)
Use [sql-formatter](https://github.com/sql-formatter-org/sql-formatter) to automatically format files. Related to #13. [no important files changed]
1 parent 8ce9509 commit 4fd5965

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+3863
-1586
lines changed

exercises/practice/acronym/.meta/example.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ UPDATE acronym
22
SET
33
result = (
44
WITH RECURSIVE
5-
rcte(string, abbr) AS (
5+
rcte (string, abbr) AS (
66
VALUES
77
(' ' || phrase, '')
88
UNION ALL
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
UPDATE "flatten-array"
2-
SET result = (
3-
SELECT JSON_GROUP_ARRAY(j.value)
4-
FROM JSON_TREE(JSON(array)) j
5-
WHERE j.type <> 'array'
6-
AND j.value NOTNULL
7-
)
8-
;
2+
SET
3+
result = (
4+
SELECT
5+
JSON_GROUP_ARRAY(j.value)
6+
FROM
7+
JSON_TREE(JSON(array)) j
8+
WHERE
9+
j.type <> 'array'
10+
AND j.value NOTNULL
11+
);
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
DROP TABLE IF EXISTS "flatten-array";
2+
23
CREATE TABLE "flatten-array" (
3-
array TEXT NOT NULL, -- json array
4-
result TEXT -- json array
4+
array TEXT NOT NULL, -- json array
5+
result TEXT -- json array
56
);
67

78
.mode csv
89
.import ./data.csv "flatten-array"
9-
10-
UPDATE "flatten-array" SET result = NULL;
11-
10+
UPDATE "flatten-array"
11+
SET
12+
result = NULL;
Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
DROP TABLE IF EXISTS tests;
2+
23
CREATE TABLE IF NOT EXISTS tests (
34
-- uuid and description are taken from the test.toml file
45
uuid TEXT PRIMARY KEY,
@@ -10,21 +11,76 @@ CREATE TABLE IF NOT EXISTS tests (
1011
test_code TEXT,
1112
task_id INTEGER DEFAULT NULL,
1213
-- Here are columns for the actual tests
13-
array TEXT NOT NULL, -- json array
14-
expected TEXT NOT NULL -- json array
14+
array TEXT NOT NULL, -- json array
15+
expected TEXT NOT NULL -- json array
1516
);
1617

17-
INSERT INTO tests (uuid, description, array, expected)
18+
INSERT INTO
19+
tests (uuid, description, array, expected)
1820
VALUES
19-
('8c71dabd-da60-422d-a290-4a571471fb14', 'empty', '[]', '[]'),
20-
('d268b919-963c-442d-9f07-82b93f1b518c', 'no nesting', '[0,1,2]', '[0,1,2]'),
21-
('3f15bede-c856-479e-bb71-1684b20c6a30', 'flattens a nested array', '[[[]]]', '[]'),
22-
('c84440cc-bb3a-48a6-862c-94cf23f2815d', 'flattens array with just integers present', '[1,[2,3,4,5,6,7],8]', '[1,2,3,4,5,6,7,8]'),
23-
('d3d99d39-6be5-44f5-a31d-6037d92ba34f', '5 level nesting', '[0,2,[[2,3],8,100,4,[[[50]]]],-2]', '[0,2,2,3,8,100,4,50,-2]'),
24-
('d572bdba-c127-43ed-bdcd-6222ac83d9f7', '6 level nesting', '[1,[2,[[3]],[4,[[5]]],6,7],8]', '[1,2,3,4,5,6,7,8]'),
25-
('0705a8e5-dc86-4cec-8909-150c5e54fa9c', 'null values are omitted from the final result', '[1,2,null]', '[1,2]'),
26-
('bc72da10-5f55-4ada-baf3-50e4da02ec8e', 'consecutive null values at the front of the array are omitted from the final result', '[null,null,3]', '[3]'),
27-
('6991836d-0d9b-4703-80a0-3f1f23eb5981', 'consecutive null values in the middle of the array are omitted from the final result', '[1,null,null,4]', '[1,4]'),
28-
('dc90a09c-5376-449c-a7b3-c2d20d540069', '6 level nested array with null values', '[0,2,[[2,3],8,[[100]],null,[[null]]],-2]', '[0,2,2,3,8,100,-2]'),
29-
('51f5d9af-8f7f-4fb5-a156-69e8282cb275', 'all values in nested array are null', '[null,[[[null]]],null,null,[[null,null],null],null]', '[]');
30-
21+
(
22+
'8c71dabd-da60-422d-a290-4a571471fb14',
23+
'empty',
24+
'[]',
25+
'[]'
26+
),
27+
(
28+
'd268b919-963c-442d-9f07-82b93f1b518c',
29+
'no nesting',
30+
'[0,1,2]',
31+
'[0,1,2]'
32+
),
33+
(
34+
'3f15bede-c856-479e-bb71-1684b20c6a30',
35+
'flattens a nested array',
36+
'[[[]]]',
37+
'[]'
38+
),
39+
(
40+
'c84440cc-bb3a-48a6-862c-94cf23f2815d',
41+
'flattens array with just integers present',
42+
'[1,[2,3,4,5,6,7],8]',
43+
'[1,2,3,4,5,6,7,8]'
44+
),
45+
(
46+
'd3d99d39-6be5-44f5-a31d-6037d92ba34f',
47+
'5 level nesting',
48+
'[0,2,[[2,3],8,100,4,[[[50]]]],-2]',
49+
'[0,2,2,3,8,100,4,50,-2]'
50+
),
51+
(
52+
'd572bdba-c127-43ed-bdcd-6222ac83d9f7',
53+
'6 level nesting',
54+
'[1,[2,[[3]],[4,[[5]]],6,7],8]',
55+
'[1,2,3,4,5,6,7,8]'
56+
),
57+
(
58+
'0705a8e5-dc86-4cec-8909-150c5e54fa9c',
59+
'null values are omitted from the final result',
60+
'[1,2,null]',
61+
'[1,2]'
62+
),
63+
(
64+
'bc72da10-5f55-4ada-baf3-50e4da02ec8e',
65+
'consecutive null values at the front of the array are omitted from the final result',
66+
'[null,null,3]',
67+
'[3]'
68+
),
69+
(
70+
'6991836d-0d9b-4703-80a0-3f1f23eb5981',
71+
'consecutive null values in the middle of the array are omitted from the final result',
72+
'[1,null,null,4]',
73+
'[1,4]'
74+
),
75+
(
76+
'dc90a09c-5376-449c-a7b3-c2d20d540069',
77+
'6 level nested array with null values',
78+
'[0,2,[[2,3],8,[[100]],null,[[null]]],-2]',
79+
'[0,2,2,3,8,100,-2]'
80+
),
81+
(
82+
'51f5d9af-8f7f-4fb5-a156-69e8282cb275',
83+
'all values in nested array are null',
84+
'[null,[[[null]]],null,null,[[null,null],null],null]',
85+
'[]'
86+
);
Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,63 @@
11
-- Create database:
22
.read ./create_fixture.sql
3-
43
-- Read user student solution and save any output as markdown in user_output.md:
54
.mode markdown
65
.output user_output.md
76
.read ./flatten-array.sql
87
.output
9-
108
-- Create a clean testing environment:
119
.read ./create_test_table.sql
12-
1310
-- Comparison of user input and the tests updates the status for each test:
1411
UPDATE tests
15-
SET status = 'pass'
16-
FROM (SELECT array, result FROM "flatten-array") AS actual
17-
WHERE (actual.array, JSON(actual.result)) =
18-
(tests.array, JSON(tests.expected));
12+
SET
13+
status = 'pass'
14+
FROM
15+
(
16+
SELECT
17+
array,
18+
result
19+
FROM
20+
"flatten-array"
21+
) AS actual
22+
WHERE
23+
(actual.array, JSON(actual.result)) = (tests.array, JSON(tests.expected));
1924

2025
-- Update message for failed tests to give helpful information:
2126
UPDATE tests
22-
SET message = (
23-
'Result for "' || tests.array || '"' || ' is <'
24-
|| COALESCE(actual.result, 'NULL')
25-
|| '> but should be <' || tests.expected || '>'
26-
)
27-
FROM (SELECT array, result FROM "flatten-array") AS actual
28-
WHERE actual.array = tests.array AND tests.status = 'fail';
27+
SET
28+
message = (
29+
'Result for "' || tests.array || '"' || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
30+
)
31+
FROM
32+
(
33+
SELECT
34+
array,
35+
result
36+
FROM
37+
"flatten-array"
38+
) AS actual
39+
WHERE
40+
actual.array = tests.array
41+
AND tests.status = 'fail';
2942

3043
-- Save results to ./output.json (needed by the online test-runner)
3144
.mode json
3245
.once './output.json'
33-
SELECT description, status, message, output, test_code, task_id
34-
FROM tests;
46+
SELECT
47+
description,
48+
status,
49+
message,
50+
output,
51+
test_code,
52+
task_id
53+
FROM
54+
tests;
3555

3656
-- Display test results in readable form for the student:
3757
.mode table
38-
SELECT description, status, message
39-
FROM tests;
58+
SELECT
59+
description,
60+
status,
61+
message
62+
FROM
63+
tests;
Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,60 @@
11
DROP TABLE IF EXISTS verses;
2+
23
CREATE TEMPORARY TABLE verses AS
3-
WITH cte (idx, sentence) AS (
4-
VALUES
5-
( 1, 'in the house that Jack built' ),
6-
( 2, 'malt that lay' ),
7-
( 3, 'rat that ate the' ),
8-
( 4, 'cat that killed the' ),
9-
( 5, 'dog that worried the' ),
10-
( 6, 'cow with the crumpled horn that tossed the' ),
11-
( 7, 'maiden all forlorn that milked the' ),
12-
( 8, 'man all tattered and torn that kissed the' ),
13-
( 9, 'priest all shaven and shorn that married the' ),
14-
(10, 'rooster that crowed in the morn that woke the' ),
15-
(11, 'farmer sowing his corn that kept the' ),
16-
(12, 'horse and the hound and the horn that belonged to the' )
17-
)
18-
SELECT idx,
19-
IIF(idx = 1, REPLACE(verse, ' the in the ', ' the '), verse) AS verse
20-
FROM (
21-
SELECT idx,
22-
PRINTF(
23-
'This is the %s.',
24-
GROUP_CONCAT(sentence, ' ')
25-
OVER (ORDER BY idx DESC ROWS BETWEEN 0 PRECEDING AND 12 FOLLOWING)
26-
) AS verse
27-
FROM cte
28-
ORDER BY idx ASC
4+
WITH
5+
cte (idx, sentence) AS (
6+
VALUES
7+
(1, 'in the house that Jack built'),
8+
(2, 'malt that lay'),
9+
(3, 'rat that ate the'),
10+
(4, 'cat that killed the'),
11+
(5, 'dog that worried the'),
12+
(6, 'cow with the crumpled horn that tossed the'),
13+
(7, 'maiden all forlorn that milked the'),
14+
(8, 'man all tattered and torn that kissed the'),
15+
(9, 'priest all shaven and shorn that married the'),
16+
(
17+
10,
18+
'rooster that crowed in the morn that woke the'
19+
),
20+
(11, 'farmer sowing his corn that kept the'),
21+
(
22+
12,
23+
'horse and the hound and the horn that belonged to the'
24+
)
2925
)
30-
;
26+
SELECT
27+
idx,
28+
IIF(
29+
idx = 1,
30+
REPLACE(verse, ' the in the ', ' the '),
31+
verse
32+
) AS verse
33+
FROM
34+
(
35+
SELECT
36+
idx,
37+
PRINTF(
38+
'This is the %s.',
39+
GROUP_CONCAT(sentence, ' ') OVER (
40+
ORDER BY
41+
idx DESC ROWS BETWEEN 0 PRECEDING
42+
AND 12 FOLLOWING
43+
)
44+
) AS verse
45+
FROM
46+
cte
47+
ORDER BY
48+
idx ASC
49+
);
3150

3251
UPDATE house
33-
SET result = (
34-
SELECT GROUP_CONCAT(verse, CHAR(10))
35-
FROM verses
36-
WHERE idx BETWEEN start_verse AND end_verse
37-
)
38-
;
52+
SET
53+
result = (
54+
SELECT
55+
GROUP_CONCAT(verse, CHAR(10))
56+
FROM
57+
verses
58+
WHERE
59+
idx BETWEEN start_verse AND end_verse
60+
);
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
DROP TABLE IF EXISTS house;
2+
23
CREATE TABLE house (
3-
start_verse INTEGER NOT NULL,
4-
end_verse INTEGER NOT NULL,
5-
result TEXT
4+
start_verse INTEGER NOT NULL,
5+
end_verse INTEGER NOT NULL,
6+
result TEXT
67
);
78

89
.mode csv
910
.import ./data.csv house
10-
11-
UPDATE house SET result = NULL;
11+
UPDATE house
12+
SET
13+
result = NULL;

0 commit comments

Comments
 (0)