Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
111713: sql: fix nil-pointer error in local retry r=DrewKimball a=DrewKimball

#### tree: return correct parse error for pg_lsn

This patch changes the error returned upon failing to parse a PG_LSN
value to match postgres. Previously, the error was an internal error.

Informs #111327

Release note: None

#### sql: fix nil-pointer error in local retry

In #105451, we added logic to locally retry a distributed query
after an error. However, the retry logic unconditionally updated a
field of `DistSQLReceiver` that may be nil, which could cause a
nil-pointer error in some code paths (e.g. apply-join). This patch
adds a check that the field is non-nil, as is done for other places
where it is updated.

There is no release note because the change has not yet made it into
a release.

Fixes #111327

Release note: None

112654: opt: fix inverted index constrained scans for equality filters r=mgartner a=mgartner

#### opt: fix inverted index constrained scans for equality filters

This commit fixes a bug introduced in #101178 that allows the optimizer
to generated inverted index scans on columns that are not filtered by
the query. For example, an inverted index over the column `j1` could be
scanned for a filter involving a different column, like `j2 = '5'`. The
bug is caused by a simple omission of code that must check that the
column in the filter is an indexed column.

Fixes #111963

There is no release note because this bug is not present in any
releases.

Release note: None

#### randgen: generate single-column indexes more often

This commit makes `randgen` more likely to generate single-column
indexes. It is motivated by the bug #111963, which surprisingly lived on
the master branch for sixth months without being detected. It's not
entirely clear why TLP or other randomized tests did not catch the bug,
which has such a simple reproduction.

One theory is that indexes tend to be multi-column and constrained scans
on multi-column inverted indexes are not commonly planned for randomly
generated queries because the set of requirements to generate the scan
are very specific: the query must hold each prefix column constant, e.g.
`a=1 AND b=2 AND j='5'::JSON`. The likelihood of randomly generating
such an expression may be so low that the bug was not caught.

By making 10% of indexes single-column, this bug may have been more
likely to be caught because only the inverted index column needs to be
constrained by an equality filter.

Release note: None


112690: sql: disallow invocation of procedures outside of CALL r=mgartner a=mgartner

#### sql: disallow invocation of procedures outside of CALL

This commit adds some missing checks to ensure that procedures cannot be
invoked in any context besides as the root expression in `CALL`
statements.

Epic: CRDB-25388

Release note: None

#### sql: add tests with function invocation in procedure argument

This commit adds a couple of tests that show that functions can be used
in procedure argument expressions.

Release note: None


112698: sql: clarify comments/naming of descriptorChanged flag r=rafiss a=rafiss

fixes #110727
Release note: None

112701: sql/logictest: fix flakes in select_for_update_read_committed r=mgartner a=mgartner

The `select_for_update_read_committed` tests were flaking because not
all statements were being run under READ COMMITTED isolation. The logic
test infrastructure does not allow fine-grained control of sessions, and
setting the isolation level in one statement would only apply to a
single session. Subsequent statements are not guaranteed to run in the
same session because they could run in any session in the connection
pool. This commit wraps each statement in an explicitly transaction with
an explicit isolation level to ensure READ COMMITTED is used.

In the future, we should investigate allowing fine-grained and explicit
control of sessions in logic tests.

Fixes #112677

Release note: None


112726: sql: make tests error if a leaf txn is not created when expected r=rharding6373 a=rharding6373

This adds a test-only error if a leaf transaction is expected to be used by a plan but a root transaction is used instead.

Epic: none
Informs: #111097

Release note: None

112767: log: fix stacktrace test goroutine counts r=rickystewart a=dhartunian

Previously, we would use the count of the string `goroutine ` as a proxy for the number of goroutines in the stacktrace. This stopped working in go 1.21 due to this change:
golang/go@51225f6

We should consider using a stacktrace parser in the future.

Supports #112088

Epic: None
Release note: None

Co-authored-by: Drew Kimball <[email protected]>
Co-authored-by: Marcus Gartner <[email protected]>
Co-authored-by: Rafi Shamim <[email protected]>
Co-authored-by: rharding6373 <[email protected]>
Co-authored-by: David Hartunian <[email protected]>
  • Loading branch information
6 people committed Oct 20, 2023
8 parents 6b6790d + 939f66d + 92e57af + e67fddb + 28ff1e6 + 430cffc + a40a557 + f4cb9c5 commit 48baa96
Show file tree
Hide file tree
Showing 18 changed files with 432 additions and 53 deletions.
13 changes: 9 additions & 4 deletions pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,10 @@ func dropColumnImpl(
return droppedViews, validateDescriptor(params.ctx, params.p, tableDesc)
}

// handleTTLStorageParamChange changes TTL storage parameters. descriptorChanged
// must be true if the descriptor was modified directly. The caller
// (alterTableNode), has a separate check to see if any mutations were
// enqueued.
func handleTTLStorageParamChange(
params runParams, tn *tree.TableName, tableDesc *tabledesc.Mutable, after *catpb.RowLevelTTL,
) (descriptorChanged bool, err error) {
Expand Down Expand Up @@ -1957,15 +1961,16 @@ func handleTTLStorageParamChange(
// Adding TTL requires adding the TTL job before adding the TTL fields.
// Removing TTL requires removing the TTL job before removing the TTL fields.
var direction descpb.DescriptorMutation_Direction
directlyModifiesDescriptor := false
switch {
case before == nil && after != nil:
direction = descpb.DescriptorMutation_ADD
case before != nil && after == nil:
direction = descpb.DescriptorMutation_DROP
default:
descriptorChanged = true
directlyModifiesDescriptor = true
}
if !descriptorChanged {
if !directlyModifiesDescriptor {
// Add TTL mutation so that job is scheduled in SchemaChanger.
tableDesc.AddModifyRowLevelTTLMutation(
&descpb.ModifyRowLevelTTL{RowLevelTTL: after},
Expand All @@ -1984,11 +1989,11 @@ func handleTTLStorageParamChange(
}

// Modify the TTL fields here because it will not be done in a mutation.
if descriptorChanged {
if directlyModifiesDescriptor {
tableDesc.RowLevelTTL = after
}

return descriptorChanged, nil
return directlyModifiesDescriptor, nil
}

// tryRemoveFKBackReferences determines whether the provided unique constraint
Expand Down
8 changes: 7 additions & 1 deletion pkg/sql/distsql_running.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,10 @@ func (dsp *DistSQLPlanner) Run(
"unexpected concurrency for a flow that was forced to be planned locally"))
return
}
if buildutil.CrdbTestBuild && txn != nil && localState.MustUseLeafTxn() && flow.GetFlowCtx().Txn.Type() != kv.LeafTxn {
recv.SetError(errors.AssertionFailedf("unexpected root txn used when leaf txn expected"))
return
}

noWait := planCtx.getPortalPauseInfo() != nil
flow.Run(ctx, noWait)
Expand Down Expand Up @@ -1253,7 +1257,9 @@ func (r *DistSQLReceiver) resetForLocalRerun(stats topLevelQueryStats) {
r.closed = false
r.stats = stats
r.egressCounter = nil
atomic.StoreUint64(r.progressAtomic, math.Float64bits(0))
if r.progressAtomic != nil {
atomic.StoreUint64(r.progressAtomic, math.Float64bits(0))
}
}

// Release releases this DistSQLReceiver back to the pool.
Expand Down
71 changes: 71 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/apply_join
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,74 @@ CREATE MATERIALIZED VIEW v1 AS
UNION
SELECT aid, pid FROM cte2
);

# Regression test for #111327 - the query shouldn't cause a nil-pointer error.
statement ok
CREATE TYPE greeting AS ENUM ('hello', 'howdy', 'hi', 'good day', 'morning');

statement ok
CREATE TABLE IF NOT EXISTS seed AS
SELECT
g :: INT2 AS _int2,
g :: INT4 AS _int4,
g :: INT8 AS _int8,
g :: FLOAT4 AS _float4,
g :: FLOAT8 AS _float8,
'2001-01-01' :: DATE + g AS _date,
'2001-01-01' :: TIMESTAMP + g * '1 day'::INTERVAL AS _timestamp,
'2001-01-01' :: TIMESTAMPTZ + g * '1 day'::INTERVAL AS _timestamptz,
g * '1 day' :: INTERVAL AS _interval,
g % 2 = 1 AS _bool,
g :: DECIMAL AS _decimal,
g :: STRING AS _string,
g :: STRING :: BYTES AS _bytes,
substring('00000000-0000-0000-0000-' || g :: STRING || '00000000000', 1, 36):: UUID AS _uuid,
'0.0.0.0' :: INET + g AS _inet,
g :: STRING :: JSONB AS _jsonb,
enum_range('hello' :: greeting) [g] as _enum
FROM
generate_series(1, 5) AS g;

statement ok
INSERT INTO seed DEFAULT VALUES;

statement ok
CREATE INDEX on seed (_int8, _float8, _date);

statement ok
CREATE INVERTED INDEX on seed (_jsonb);

skipif config local-mixed-23.1
statement error pgcode 22P02 pq: pg_lsn\(\): invalid input syntax for type pg_lsn: \"1\"
SELECT
tab378984.crdb_internal_mvcc_timestamp AS "%pcol857759",
'48 years 7 mons 894 days 13:39:26.674765':::INTERVAL AS col857760,
tab378983.tableoid AS "coL857761",
tab378983.crdb_internal_mvcc_timestamp AS col857762,
(SELECT (-4999644074744333745):::INT8 AS "co""l857763" LIMIT 1:::INT8) AS col857764,
tab378983._inet AS col857765,
'2011-06-28 16:37:44.000635+00':::TIMESTAMPTZ AS col857766,
tab378984._bool AS col857767,
tab378983.tableoid AS col857768,
tab378984._timestamptz AS col857769,
NULL AS "Co😽l857770",
tab378984._string AS "c)ol857771",
NULL AS col857772,
tab378983._int4 AS c🙃ol857773,
tab378984._interval AS " col857774",
tab378984.crdb_internal_mvcc_timestamp AS "c%69ol'857775"
FROM
seed@seed__int8__float8__date_idx AS tab378983,
seed@[0] AS tab378984
WHERE
(
true
AND (
'9E82DF40/BC8A8379':::PG_LSN::PG_LSN NOT IN (
SELECT pg_lsn(tab378984._string::STRING)::PG_LSN::PG_LSN AS col857758
FROM seed@[0] AS "%ptAb%v378985" WHERE "%ptAb%v378985"._bool LIMIT 22:::INT8
)
)
)
ORDER BY tab378983._timestamptz ASC NULLS FIRST
LIMIT 78:::INT8;
25 changes: 19 additions & 6 deletions pkg/sql/logictest/testdata/logic_test/pg_lsn
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ SELECT 'A01F0/1AAA'::pg_lsn
----
A01F0/1AAA

statement error could not parse pg_lsn
SELECT 'A/G'::pg_lsn

statement error could not parse pg_lsn
SELECT '0G'::pg_lsn

statement ok
CREATE TABLE pg_lsn_table(id pg_lsn PRIMARY KEY, val pg_lsn)

Expand Down Expand Up @@ -80,3 +74,22 @@ SELECT * FROM ( VALUES
23021024706816
18446744073709551615
-18446744073709551615

statement error pgcode 22P02 pq: invalid input syntax for type pg_lsn: \"A/G\"
SELECT 'A/G'::pg_lsn

statement error pgcode 22P02 pq: invalid input syntax for type pg_lsn: \"0G\"
SELECT '0G'::pg_lsn

statement error pgcode 22P02 pq: invalid input syntax for type pg_lsn: \"ab\"
SELECT 'ab'::PG_LSN;

statement error pgcode 22P02 pq: pg_lsn\(\): invalid input syntax for type pg_lsn: \"ab\"
SELECT pg_lsn('ab');

statement error pgcode 22P02 pq: invalid input syntax for type pg_lsn: \"1\"
SELECT '1'::PG_LSN;

statement error pgcode 22P02 pq: invalid input syntax for type pg_lsn: \"\"
SELECT ''::PG_LSN;

73 changes: 73 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/procedure
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,39 @@ SELECT currval('s')
statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
SELECT p()

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
CALL p(p())

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
SELECT * FROM (VALUES (1), (2), (3)) LIMIT p()

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
SELECT * FROM (VALUES (1), (2), (3)) ORDER BY p()

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
SELECT * FROM (VALUES (1), (2), (3)) v(i) WHERE i = p()

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
SELECT * FROM (VALUES (1), (2), (3)) v(i) GROUP BY i HAVING i > p()

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
SELECT * FROM (VALUES (1), (2)) v(i) JOIN (VALUES (2), (3)) w(j) ON i = p()

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
SELECT * FROM generate_series(1, p())

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
SELECT * FROM generate_series(1, p())

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
SELECT abs(p())

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
SELECT nth_value(1, p()) OVER () FROM (VALUES (1), (2))

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
SELECT nth_value(1, i) OVER (ORDER BY p()) FROM (VALUES (1), (2)) v(i)

statement ok
CREATE OR REPLACE PROCEDURE p() LANGUAGE SQL AS ''

Expand Down Expand Up @@ -85,6 +118,27 @@ CREATE OR REPLACE PROCEDURE p2() LANGUAGE SQL AS $$
CALL p();
$$

statement error pgcode 42883 unknown function: p\(\)
CREATE FUNCTION err(i INT) RETURNS VOID LANGUAGE SQL AS 'SELECT p()'

statement error pgcode 0A000 unimplemented: CALL usage inside a function definition
CREATE FUNCTION err(i INT) RETURNS VOID LANGUAGE SQL AS 'CALL p()'

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
CREATE TABLE err (i INT DEFAULT p())

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
CREATE TABLE err (i INT AS (p()) STORED)

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
CREATE TABLE err (i INT, INDEX (p()))

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
CREATE TABLE err (a INT, b INT, INDEX (a, (b + p())))

statement error pgcode 42809 p\(\) is a procedure\nHINT: To call a procedure, use CALL.
CREATE TABLE err (a INT, INDEX (a) WHERE p() = 1)

statement ok
CREATE TABLE t (
k INT PRIMARY KEY,
Expand All @@ -108,6 +162,25 @@ SELECT * FROM t
----
1 11

statement ok
CREATE FUNCTION one() RETURNS INT LANGUAGE SQL AS 'SELECT 1'

statement ok
CALL t_update(one(), 12)

query II
SELECT * FROM t
----
1 12

statement ok
CALL t_update(one(), one()+12)

query II
SELECT * FROM t
----
1 13

statement ok
CREATE FUNCTION t_update() RETURNS INT LANGUAGE SQL AS 'SELECT 1'

Expand Down
Loading

0 comments on commit 48baa96

Please sign in to comment.