Commit 5efccc1c authored by Tom Lane's avatar Tom Lane

Avoid useless "x = ANY(ARRAY[])" test for empty partition list.

This arises in practice if the partition only admits NULL values.

Jeevan Ladhe

Discussion: https://postgr.es/m/CAOgcT0OChrN--uuqH6wG6Z8+nxnCWJ+2Q-uhnK4KOANdRRxuAw@mail.gmail.com
parent 00c5e511
...@@ -1311,6 +1311,12 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec) ...@@ -1311,6 +1311,12 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec)
List *arrelems = NIL; List *arrelems = NIL;
bool list_has_null = false; bool list_has_null = false;
/*
* Only single-column list partitioning is supported, so we are worried
* only about the partition key with index 0.
*/
Assert(key->partnatts == 1);
/* Construct Var or expression representing the partition column */ /* Construct Var or expression representing the partition column */
if (key->partattrs[0] != 0) if (key->partattrs[0] != 0)
keyCol = (Expr *) makeVar(1, keyCol = (Expr *) makeVar(1,
...@@ -1333,6 +1339,8 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec) ...@@ -1333,6 +1339,8 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec)
arrelems = lappend(arrelems, copyObject(val)); arrelems = lappend(arrelems, copyObject(val));
} }
if (arrelems)
{
/* Construct an ArrayExpr for the non-null partition values */ /* Construct an ArrayExpr for the non-null partition values */
arr = makeNode(ArrayExpr); arr = makeNode(ArrayExpr);
arr->array_typeid = !type_is_array(key->parttypid[0]) arr->array_typeid = !type_is_array(key->parttypid[0])
...@@ -1347,6 +1355,12 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec) ...@@ -1347,6 +1355,12 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec)
/* Generate the main expression, i.e., keyCol = ANY (arr) */ /* Generate the main expression, i.e., keyCol = ANY (arr) */
opexpr = make_partition_op_expr(key, 0, BTEqualStrategyNumber, opexpr = make_partition_op_expr(key, 0, BTEqualStrategyNumber,
keyCol, (Expr *) arr); keyCol, (Expr *) arr);
}
else
{
/* If there are no partition values, we don't need an = ANY expr */
opexpr = NULL;
}
if (!list_has_null) if (!list_has_null)
{ {
...@@ -1361,7 +1375,7 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec) ...@@ -1361,7 +1375,7 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec)
nulltest->argisrow = false; nulltest->argisrow = false;
nulltest->location = -1; nulltest->location = -1;
result = list_make2(nulltest, opexpr); result = opexpr ? list_make2(nulltest, opexpr) : list_make1(nulltest);
} }
else else
{ {
...@@ -1369,17 +1383,22 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec) ...@@ -1369,17 +1383,22 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec)
* Gin up a "col IS NULL" test that will be OR'd with the main * Gin up a "col IS NULL" test that will be OR'd with the main
* expression. * expression.
*/ */
Expr *or;
nulltest = makeNode(NullTest); nulltest = makeNode(NullTest);
nulltest->arg = keyCol; nulltest->arg = keyCol;
nulltest->nulltesttype = IS_NULL; nulltest->nulltesttype = IS_NULL;
nulltest->argisrow = false; nulltest->argisrow = false;
nulltest->location = -1; nulltest->location = -1;
if (opexpr)
{
Expr *or;
or = makeBoolExpr(OR_EXPR, list_make2(nulltest, opexpr), -1); or = makeBoolExpr(OR_EXPR, list_make2(nulltest, opexpr), -1);
result = list_make1(or); result = list_make1(or);
} }
else
result = list_make1(nulltest);
}
return result; return result;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment