-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.sql
More file actions
99 lines (91 loc) · 3.04 KB
/
program.sql
File metadata and controls
99 lines (91 loc) · 3.04 KB
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
-- ============================================================
-- Branchless Programming Examples in MariaDB SQL
--
-- "Branchless" means avoiding IF/CASE/WHEN conditional logic.
-- Instead, we use arithmetic expressions that always compute
-- the answer the same way, regardless of input values.
--
-- SQL-SPECIFIC NOTE:
-- In MariaDB/MySQL, boolean expressions like (a < b) evaluate
-- to 1 (true) or 0 (false) in numeric context. This is the
-- foundation of all branchless tricks in SQL:
--
-- SELECT (5 > 3); -- Returns 1 (true)
-- SELECT (3 > 5); -- Returns 0 (false)
--
-- We multiply by these 0/1 results to "select" values
-- without needing CASE or IF statements.
-- ============================================================
-- ----- getSign -----
-- Returns 1 for positive/zero, 0 for negative.
--
-- HOW IT WORKS:
-- (num >= 0) returns 1 when true, 0 when false.
-- That's it! No branches, just a boolean expression.
--
-- EXAMPLES:
-- getSign(42) → (42 >= 0) → 1 ✓
-- getSign(-7) → (-7 >= 0) → 0 ✓
-- getSign(0) → (0 >= 0) → 1 ✓
DELIMITER //
CREATE FUNCTION getSign(num INT) RETURNS INT DETERMINISTIC
BEGIN
RETURN (num >= 0);
END//
DELIMITER ;
-- ----- minBranchless -----
-- Returns the smaller of two integers.
--
-- HOW IT WORKS:
-- (a < b) is either 1 or 0.
-- We multiply each value by its "winning condition":
-- a * (a < b) → keeps a when a IS smaller, zeroes it out otherwise
-- b * (a >= b) → keeps b when b IS smaller or equal, zeroes it out otherwise
-- Adding them gives us the minimum.
--
-- WORKED EXAMPLE (a=15, b=9):
-- a * (a < b) = 15 * (15 < 9) = 15 * 0 = 0
-- b * (a >= b) = 9 * (15 >= 9) = 9 * 1 = 9
-- Result: 0 + 9 = 9 ✓
--
-- WORKED EXAMPLE (a=3, b=7):
-- a * (a < b) = 3 * (3 < 7) = 3 * 1 = 3
-- b * (a >= b) = 7 * (3 >= 7) = 7 * 0 = 0
-- Result: 3 + 0 = 3 ✓
DELIMITER //
CREATE FUNCTION minBranchless(a INT, b INT) RETURNS INT DETERMINISTIC
BEGIN
RETURN a * (a < b) + b * (a >= b);
END//
DELIMITER ;
-- ----- maxBranchless -----
-- Returns the larger of two integers.
--
-- Same idea as minBranchless, but we flip the conditions:
-- a * (a > b) → keeps a when a IS larger
-- b * (a <= b) → keeps b when b IS larger or equal
--
-- WORKED EXAMPLE (a=15, b=9):
-- 15 * (15 > 9) + 9 * (15 <= 9) = 15 * 1 + 9 * 0 = 15 ✓
DELIMITER //
CREATE FUNCTION maxBranchless(a INT, b INT) RETURNS INT DETERMINISTIC
BEGIN
RETURN a * (a > b) + b * (a <= b);
END//
DELIMITER ;
-- ===== Test Queries =====
SELECT '--- Sign Tests ---' AS test_section;
SELECT 42 AS num, getSign(42) AS sign
UNION ALL SELECT -7, getSign(-7)
UNION ALL SELECT 0, getSign(0);
SELECT '--- Min/Max Tests ---' AS test_section;
SELECT 15 AS a, 9 AS b,
minBranchless(15, 9) AS min_result,
maxBranchless(15, 9) AS max_result;
SELECT 3 AS a, 7 AS b,
minBranchless(3, 7) AS min_result,
maxBranchless(3, 7) AS max_result;
-- ===== Cleanup =====
DROP FUNCTION IF EXISTS getSign;
DROP FUNCTION IF EXISTS minBranchless;
DROP FUNCTION IF EXISTS maxBranchless;