Evaluate Boolean Expression Problem
Description
LeetCode Problem 1440.
Table Variables:
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| name | varchar |
| value | int |
+---------------+---------+
name is the primary key for this table.
This table contains the stored variables and their values.
Table Expressions:
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| left_operand | varchar |
| operator | enum |
| right_operand | varchar |
+---------------+---------+
(left_operand, operator, right_operand) is the primary key for this table.
This table contains a boolean expression that should be evaluated.
operator is an enum that takes one of the values ('<', '>', '=')
The values of left_operand and right_operand are guaranteed to be in the Variables table.
Write an SQL query to evaluate the boolean expressions in Expressions table.
Return the result table in any order.
The query result format is in the following example.
Variables table:
+------+-------+
| name | value |
+------+-------+
| x | 66 |
| y | 77 |
+------+-------+
Expressions table:
+--------------+----------+---------------+
| left_operand | operator | right_operand |
+--------------+----------+---------------+
| x | > | y |
| x | < | y |
| x | = | y |
| y | > | x |
| y | < | x |
| x | = | x |
+--------------+----------+---------------+
Result table:
+--------------+----------+---------------+-------+
| left_operand | operator | right_operand | value |
+--------------+----------+---------------+-------+
| x | > | y | false |
| x | < | y | true |
| x | = | y | false |
| y | > | x | true |
| y | < | x | false |
| x | = | x | true |
+--------------+----------+---------------+-------+
As shown, you need find the value of each boolean exprssion in the table using the variables table.
Solution
Approach: Join Variables Twice and Use CASE
Each expression names two variables, so it needs two values: join Variables once for the left operand (alias l) and once more for the right operand (alias r). Every operand is guaranteed to exist in Variables, so plain inner joins keep every expression.
SQL cannot use the text in operator as an operator, so a CASE checks which one it is and does that comparison:
'<'andl.value < r.value,'>'andl.value > r.value,'='andl.value = r.value.
If any branch matches, the value is 'true'; otherwise 'false'. The result must show the words true / false, so return them as strings instead of the 1 / 0 MySQL gives for a comparison.
The Pandas version merges variables twice under renamed columns, builds the same three conditions as boolean Series, and maps True / False to the two strings.
SELECT e.left_operand,
e.operator,
e.right_operand,
CASE
WHEN e.operator = '<' AND l.value < r.value THEN 'true'
WHEN e.operator = '>' AND l.value > r.value THEN 'true'
WHEN e.operator = '=' AND l.value = r.value THEN 'true'
ELSE 'false'
END AS value
FROM Expressions AS e
JOIN Variables AS l
ON l.name = e.left_operand
JOIN Variables AS r
ON r.name = e.right_operand;