Skip to content

Commit

Permalink
Fix normalization of NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen committed Dec 12, 2024
1 parent 0aa74fa commit e6028fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/pg_query_normalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ fill_in_constant_lengths(pgssConstLocations *jstate, const char *query)
if (tok == 0)
break; /* out of inner for-loop */

/* Ignore NULL constants */
if (tok == NULL_P) {
continue;
}

/*
* We should find the token position exactly, but if we somehow
* run past it, work with that.
Expand Down Expand Up @@ -213,15 +218,7 @@ fill_in_constant_lengths(pgssConstLocations *jstate, const char *query)

locs[i].val = (char *)palloc(buf_size * sizeof(char));
snprintf(locs[i].val, buf_size, "%d", val);
}
else if (tok == NULL_P)
{
/*
* Do not extract NULL literals as those mess with
* Postgres type inference.
*/
locs[i].length = -1;
}
}

break; /* out of inner for-loop */
}
Expand Down Expand Up @@ -399,6 +396,15 @@ static bool const_record_walker(Node *node, pgssConstLocations *jstate)
switch (nodeTag(node))
{
case T_A_Const:

/*
* Do not extract NULL constants as those mess with
* Postgres type inference.
*/
if (castNode(A_Const, node)->isnull) {
return false;
}

RecordConstLocation(jstate, castNode(A_Const, node)->location);
break;
case T_ParamRef:
Expand Down
2 changes: 2 additions & 0 deletions test/normalize_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const char* tests[] = {
"SELECT $1; ALTER USER a WITH PASSWORD 'b'",
"SELECT U&'d!0061t!+000061' UESCAPE '!', -2147483647, -2147483648, x'beef', b'010101'",
"SELECT $1, $2, $3, $4, $5",
"VALUES (1, NULL, 2)",
"VALUES ($1, NULL, $2)",
};

size_t testsLength = __LINE__ - 7;
2 changes: 2 additions & 0 deletions test/normalize_utility_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const char* tests[] = {
"CLOSE cursor_a",
"SELECT 1; ALTER USER a WITH PASSWORD 'b'",
"SELECT 1; ALTER USER a WITH PASSWORD $1",
"VALUES (1, NULL, 2)",
"VALUES (1, NULL, 2)",
};

size_t testsLength = __LINE__ - 8;

0 comments on commit e6028fb

Please sign in to comment.