From f3c0253b5f1bfa74fb0ca117933464a3106d2199 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 17 Nov 2026 16:10:49 -0600 Subject: [PATCH v5 1/1] Add IMPLIES logical operator. This commit adds an IMPLIES operator for boolean values. "a IMPLIES b" is equivalent to "NOT a OR b", but it is easier to read in conditional rules like CHECK constraints. For example, CHECK (NOT shipped OR ship_date IS NOT NULL) can now be written as CHECK (shipped IMPLIES ship_date IS NOT NULL) NULL handling follows from the equivalence above, which means that IMPLIES uses Kleene's definition of implication for three-valued logic. Like AND and OR, there is no guarantee that the left operand is evaluated before the right operand. IMPLIES is non-associative, so "a IMPLIES b IMPLIES c" is a syntax error. There is no consensus on how such chains ought to be grouped, and requiring parentheses leaves the door open for choosing an associativity later (e.g., if the SQL standard picks one). To allow views, constraints, etc. to be deparsed and dumped as written, IMPLIES is stored as a new BoolExprType instead of being expanded during parse analysis. The planner replaces it with the equivalent NOT and OR during expression preprocessing, so the executor and most of the planner never see it. XXX: Bump catversion. Author: Vik Fearing Co-authored-by: Nathan Bossart Reviewed-by: Zsolt Parragi Tested-by: solai v Discussion: https://postgr.es/m/37c76707-e6fa-4ee3-b57f-aa0bf1b0bba8%40postgresfriends.org --- contrib/postgres_fdw/deparse.c | 3 ++ doc/src/sgml/func/func-logical.sgml | 61 +++++++++++++++++++++-- doc/src/sgml/syntax.sgml | 6 +++ src/backend/nodes/outfuncs.c | 3 ++ src/backend/nodes/readfuncs.c | 2 + src/backend/optimizer/util/clauses.c | 33 +++++++++++- src/backend/parser/gram.y | 10 +++- src/backend/parser/parse_expr.c | 3 ++ src/backend/utils/adt/ruleutils.c | 22 +++++++- src/include/nodes/primnodes.h | 11 ++-- src/include/parser/kwlist.h | 1 + src/test/regress/expected/boolean.out | 61 +++++++++++++++++++++++ src/test/regress/expected/create_view.out | 8 ++- src/test/regress/sql/boolean.sql | 15 ++++++ src/test/regress/sql/create_view.sql | 4 +- 15 files changed, 228 insertions(+), 15 deletions(-) diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.c index ff9fe0f87e4..82775ab78c7 100644 --- a/contrib/postgres_fdw/deparse.c +++ b/contrib/postgres_fdw/deparse.c @@ -3749,6 +3749,9 @@ deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context) deparseExpr(linitial(node->args), context); appendStringInfoChar(buf, ')'); return; + default: + elog(ERROR, "unrecognized boolop: %d", (int) node->boolop); + break; } appendStringInfoChar(buf, '('); diff --git a/doc/src/sgml/func/func-logical.sgml b/doc/src/sgml/func/func-logical.sgml index 28d49b61a89..8fc2de853e0 100644 --- a/doc/src/sgml/func/func-logical.sgml +++ b/doc/src/sgml/func/func-logical.sgml @@ -13,7 +13,7 @@ - The usual logical operators are available: + The following logical operators are available: AND (operator) @@ -39,10 +39,19 @@ negation + + IMPLIES (operator) + + + + implication + + boolean AND boolean boolean boolean OR boolean boolean NOT boolean boolean +boolean IMPLIES boolean boolean SQL uses a three-valued logic system with true, @@ -143,14 +152,56 @@ + + + + + + IMPLIES + TRUE + FALSE + NULL + + + + + + TRUE + TRUE + FALSE + NULL + + + + FALSE + TRUE + TRUE + TRUE + + + + NULL + TRUE + NULL + NULL + + + + The operators AND and OR are commutative, that is, you can switch the left and right operands - without affecting the result. (However, it is not guaranteed that - the left operand is evaluated before the right operand. See for more information about the - order of evaluation of subexpressions.) + without affecting the result. IMPLIES is not + commutative. + + + + Note that it is not guaranteed that the left operand of + AND, OR, or + IMPLIES is evaluated before the right operand. See + for more information about the + order of evaluation of subexpressions. diff --git a/doc/src/sgml/syntax.sgml b/doc/src/sgml/syntax.sgml index a697218c8b3..49d7f98b0b8 100644 --- a/doc/src/sgml/syntax.sgml +++ b/doc/src/sgml/syntax.sgml @@ -1103,6 +1103,12 @@ CAST ( 'string' AS type ) left logical disjunction + + + IMPLIES + + logical implication + diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 40990143927..396b340c01e 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -422,6 +422,9 @@ _outBoolExpr(StringInfo str, const BoolExpr *node) case NOT_EXPR: opstr = "not"; break; + case IMPLIES_EXPR: + opstr = "implies"; + break; } appendStringInfoString(str, " :boolop "); outToken(str, opstr); diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index 2839a711f9e..8f2aa6c7cbe 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -295,6 +295,8 @@ _readBoolExpr(ReadNodeContext *ctx) local_node->boolop = OR_EXPR; else if (length == 3 && strncmp(token, "not", 3) == 0) local_node->boolop = NOT_EXPR; + else if (length == 7 && strncmp(token, "implies", 7) == 0) + local_node->boolop = IMPLIES_EXPR; else elog(ERROR, "unrecognized boolop \"%.*s\"", length, token); diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index 702fa0aefdd..68618f816c0 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -1142,7 +1142,8 @@ contain_nonstrict_functions_walker(Node *node, void *context) { case AND_EXPR: case OR_EXPR: - /* AND, OR are inherently non-strict */ + case IMPLIES_EXPR: + /* AND, OR, IMPLIES are inherently non-strict */ return true; default: break; @@ -1950,6 +1951,20 @@ find_nonnullable_vars_walker(Node *node, bool top_level) result = find_nonnullable_vars_walker((Node *) expr->args, false); break; + case IMPLIES_EXPR: + + /* + * "a IMPLIES b" means "NOT a OR b", so we handle the + * arguments just like the NOT and OR cases would. Note that + * we can get here before the planner has replaced IMPLIES + * (see query_outputs_are_not_nullable()). + */ + result = find_nonnullable_vars_walker(linitial(expr->args), + false); + result = mbms_int_members(result, + find_nonnullable_vars_walker(lsecond(expr->args), + top_level)); + break; default: elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop); break; @@ -3393,6 +3408,22 @@ eval_const_expressions_mutator(Node *node, */ return negate_clause(arg); } + case IMPLIES_EXPR: + { + Expr *newexpr; + + /* + * Replace "a IMPLIES b" with "NOT a OR b" and + * simplify that, so that nothing downstream needs + * to know about IMPLIES. + */ + Assert(list_length(expr->args) == 2); + newexpr = make_notclause(linitial(expr->args)); + newexpr = make_orclause(list_make2(newexpr, + lsecond(expr->args))); + return eval_const_expressions_mutator((Node *) newexpr, + context); + } default: elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop); diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 0563453fe24..f370da66c28 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -749,7 +749,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); HANDLER HAVING HEADER_P HOLD HOUR_P - IDENTITY_P IF_P IGNORE_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IMPORT_P IN_P INCLUDE + IDENTITY_P IF_P IGNORE_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IMPLIES IMPORT_P IN_P INCLUDE INCLUDING INCREMENT INDENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION @@ -844,6 +844,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); /* Precedence: lowest to highest */ %left UNION EXCEPT %left INTERSECT +%nonassoc IMPLIES %left OR %left AND %right NOT @@ -15374,6 +15375,11 @@ a_expr: c_expr { $$ = $1; } { $$ = makeAndExpr($1, $3, @2); } | a_expr OR a_expr { $$ = makeOrExpr($1, $3, @2); } + | a_expr IMPLIES a_expr + { + $$ = (Node *) makeBoolExpr(IMPLIES_EXPR, + list_make2($1, $3), @2); + } | NOT a_expr { $$ = makeNotExpr($2, @1); } | NOT_LA a_expr %prec NOT @@ -18190,6 +18196,7 @@ unreserved_keyword: | IMMEDIATE | IMMUTABLE | IMPLICIT_P + | IMPLIES | IMPORT_P | INCLUDE | INCLUDING @@ -18782,6 +18789,7 @@ bare_label_keyword: | IMMEDIATE | IMMUTABLE | IMPLICIT_P + | IMPLIES | IMPORT_P | IN_P | INCLUDE diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 728392f436f..bb487396633 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -1428,6 +1428,9 @@ transformBoolExpr(ParseState *pstate, BoolExpr *a) case NOT_EXPR: opname = "NOT"; break; + case IMPLIES_EXPR: + opname = "IMPLIES"; + break; default: elog(ERROR, "unrecognized boolop: %d", (int) a->boolop); opname = NULL; /* keep compiler quiet */ diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index e9c1c9db1b1..49ce3d318d3 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -9087,13 +9087,19 @@ isSimpleNode(Node *node, Node *parentNode, int prettyFlags) { case NOT_EXPR: case AND_EXPR: - if (parentType == AND_EXPR || parentType == OR_EXPR) + if (parentType == AND_EXPR || + parentType == OR_EXPR || + parentType == IMPLIES_EXPR) return true; break; case OR_EXPR: - if (parentType == OR_EXPR) + if (parentType == OR_EXPR || + parentType == IMPLIES_EXPR) return true; break; + case IMPLIES_EXPR: + /* IMPLIES always needs parentheses. */ + break; } } return false; @@ -9569,6 +9575,18 @@ get_rule_expr(Node *node, deparse_context *context, appendStringInfoChar(buf, ')'); break; + case IMPLIES_EXPR: + if (!PRETTY_PAREN(context)) + appendStringInfoChar(buf, '('); + get_rule_expr_paren(first_arg, context, + false, node); + appendStringInfoString(buf, " IMPLIES "); + get_rule_expr_paren(lsecond(expr->args), context, + false, node); + if (!PRETTY_PAREN(context)) + appendStringInfoChar(buf, ')'); + break; + default: elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop); diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 726c186c7d2..ce800ccecf5 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -938,15 +938,20 @@ typedef struct ScalarArrayOpExpr } ScalarArrayOpExpr; /* - * BoolExpr - expression node for the basic Boolean operators AND, OR, NOT + * BoolExpr - expression node for the basic Boolean operators AND, OR, NOT, + * and IMPLIES * * Notice the arguments are given as a List. For NOT, of course the list * must always have exactly one element. For AND and OR, there can be two - * or more arguments. + * or more arguments. For IMPLIES, the list must always have exactly two + * elements. + * + * The planner replaces IMPLIES with the equivalent NOT and OR during + * expression preprocessing, so execution never sees an IMPLIES_EXPR. */ typedef enum BoolExprType { - AND_EXPR, OR_EXPR, NOT_EXPR + AND_EXPR, OR_EXPR, NOT_EXPR, IMPLIES_EXPR } BoolExprType; typedef struct BoolExpr diff --git a/src/include/parser/kwlist.h b/src/include/parser/kwlist.h index 53ae96c0399..94d2aef1bb1 100644 --- a/src/include/parser/kwlist.h +++ b/src/include/parser/kwlist.h @@ -207,6 +207,7 @@ PG_KEYWORD("ilike", ILIKE, TYPE_FUNC_NAME_KEYWORD, BARE_LABEL) PG_KEYWORD("immediate", IMMEDIATE, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("immutable", IMMUTABLE, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("implicit", IMPLICIT_P, UNRESERVED_KEYWORD, BARE_LABEL) +PG_KEYWORD("implies", IMPLIES, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("import", IMPORT_P, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("in", IN_P, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("include", INCLUDE, UNRESERVED_KEYWORD, BARE_LABEL) diff --git a/src/test/regress/expected/boolean.out b/src/test/regress/expected/boolean.out index 0e99eb7ffc0..43c65151a1d 100644 --- a/src/test/regress/expected/boolean.out +++ b/src/test/regress/expected/boolean.out @@ -566,6 +566,67 @@ SELECT isnul OR istrue OR isfalse FROM booltbl4; t (1 row) +-- IMPLIES expression need to return null if there's any nulls, the +-- left value is not false, and the right value is not true +SELECT istrue IMPLIES isnul FROM booltbl4; + ?column? +---------- + (null) +(1 row) + +SELECT isnul IMPLIES isfalse FROM booltbl4; + ?column? +---------- + (null) +(1 row) + +SELECT isnul IMPLIES isnul FROM booltbl4; + ?column? +---------- + (null) +(1 row) + +SELECT istrue IMPLIES istrue FROM booltbl4; + ?column? +---------- + t +(1 row) + +SELECT istrue IMPLIES isfalse FROM booltbl4; + ?column? +---------- + f +(1 row) + +SELECT isfalse IMPLIES istrue FROM booltbl4; + ?column? +---------- + t +(1 row) + +SELECT isfalse IMPLIES isfalse FROM booltbl4; + ?column? +---------- + t +(1 row) + +SELECT isfalse IMPLIES isnul FROM booltbl4; + ?column? +---------- + t +(1 row) + +SELECT isnul IMPLIES istrue FROM booltbl4; + ?column? +---------- + t +(1 row) + +-- IMPLIES is non-associative +SELECT istrue IMPLIES istrue IMPLIES istrue AS error FROM booltbl4; +ERROR: syntax error at or near "IMPLIES" +LINE 1: SELECT istrue IMPLIES istrue IMPLIES istrue AS error FROM bo... + ^ -- Casts SELECT 0::boolean; bool diff --git a/src/test/regress/expected/create_view.out b/src/test/regress/expected/create_view.out index c2892dfb6d0..cfbf9f4e725 100644 --- a/src/test/regress/expected/create_view.out +++ b/src/test/regress/expected/create_view.out @@ -2257,7 +2257,9 @@ select x + y + z as c1, (x,y) <= ANY (values(1,2),(3,4)) as c11, (x>0 OR y>0) IN (SELECT false) AS c12, (x < y) <> ALL (SELECT false) AS c13, - true = (y = ANY (SELECT 1)) AS c14 + true = (y = ANY (SELECT 1)) AS c14, + (x > y OR y > z) IMPLIES NOT (x > z) AS c15, + NOT (x > y IMPLIES y > z) IMPLIES (x > z) AS c16 from (values(1,2,3)) v(x,y,z); select pg_get_viewdef('tt26v', true); pg_get_viewdef @@ -2275,7 +2277,9 @@ select pg_get_viewdef('tt26v', true); ((x, y) <= ANY ( VALUES (1,2), (3,4))) AS c11,+ ((x > 0 OR y > 0) IN ( SELECT false)) AS c12, + ((x < y) <> ALL ( SELECT false)) AS c13, + - true = ((y IN ( SELECT 1))) AS c14 + + true = ((y IN ( SELECT 1))) AS c14, + + x > y OR y > z IMPLIES NOT x > z AS c15, + + NOT (x > y IMPLIES y > z) IMPLIES x > z AS c16+ FROM ( VALUES (1,2,3)) v(x, y, z); (1 row) diff --git a/src/test/regress/sql/boolean.sql b/src/test/regress/sql/boolean.sql index 85c6b019882..5a5764a0312 100644 --- a/src/test/regress/sql/boolean.sql +++ b/src/test/regress/sql/boolean.sql @@ -250,6 +250,21 @@ SELECT isfalse OR isnul OR istrue FROM booltbl4; SELECT istrue OR isfalse OR isnul FROM booltbl4; SELECT isnul OR istrue OR isfalse FROM booltbl4; +-- IMPLIES expression need to return null if there's any nulls, the +-- left value is not false, and the right value is not true +SELECT istrue IMPLIES isnul FROM booltbl4; +SELECT isnul IMPLIES isfalse FROM booltbl4; +SELECT isnul IMPLIES isnul FROM booltbl4; +SELECT istrue IMPLIES istrue FROM booltbl4; +SELECT istrue IMPLIES isfalse FROM booltbl4; +SELECT isfalse IMPLIES istrue FROM booltbl4; +SELECT isfalse IMPLIES isfalse FROM booltbl4; +SELECT isfalse IMPLIES isnul FROM booltbl4; +SELECT isnul IMPLIES istrue FROM booltbl4; + +-- IMPLIES is non-associative +SELECT istrue IMPLIES istrue IMPLIES istrue AS error FROM booltbl4; + -- Casts SELECT 0::boolean; SELECT 1::boolean; diff --git a/src/test/regress/sql/create_view.sql b/src/test/regress/sql/create_view.sql index f80b9823f00..8eda33f45c6 100644 --- a/src/test/regress/sql/create_view.sql +++ b/src/test/regress/sql/create_view.sql @@ -829,7 +829,9 @@ select x + y + z as c1, (x,y) <= ANY (values(1,2),(3,4)) as c11, (x>0 OR y>0) IN (SELECT false) AS c12, (x < y) <> ALL (SELECT false) AS c13, - true = (y = ANY (SELECT 1)) AS c14 + true = (y = ANY (SELECT 1)) AS c14, + (x > y OR y > z) IMPLIES NOT (x > z) AS c15, + NOT (x > y IMPLIES y > z) IMPLIES (x > z) AS c16 from (values(1,2,3)) v(x,y,z); select pg_get_viewdef('tt26v', true); -- 2.55.0