Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: error when executing INSERT: ERROR: got 7 values but expected 6 #98602

Open
rytaft opened this issue Mar 14, 2023 · 1 comment
Open

sql: error when executing INSERT: ERROR: got 7 values but expected 6 #98602

rytaft opened this issue Mar 14, 2023 · 1 comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-qa T-sql-queries SQL Queries Team

Comments

@rytaft
Copy link
Collaborator

rytaft commented Mar 14, 2023

Describe the problem

On master, I'm getting an error when I try to perform an insert: "ERROR: got 7 values but expected 6". This query succeeds in Postgres.

To Reproduce

On a single-node local cluster, run:

CREATE TABLE employees (
	emp_no     INT8 NOT NULL,
	birth_date DATE NOT NULL,
	first_name VARCHAR(14) NOT NULL,
	last_name  VARCHAR(16) NOT NULL,
	gender     CHAR NOT NULL,
	hire_date  DATE NOT NULL,
	PRIMARY KEY (emp_no)
);

CREATE TABLE titles (
	emp_no    INT8 NOT NULL,
	title     VARCHAR(50) NOT NULL,
	from_date DATE NOT NULL,
	to_date   DATE,
	FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
	PRIMARY KEY (emp_no, title, from_date)
);

CREATE TABLE salaries (
	emp_no    INT8 NOT NULL,
	salary    INT8 NOT NULL,
	from_date DATE NOT NULL,
	to_date   DATE NOT NULL,
	FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
	PRIMARY KEY (emp_no, from_date)
);

CREATE TABLE salary_audit (
	audit_date DATE NOT NULL,
	emp_no     INT8 NOT NULL,
	sal_date   DATE NOT NULL,
	title      VARCHAR(50) NOT NULL,
	title_date DATE NOT NULL,
	valid      BOOL NOT NULL,
	FOREIGN KEY (emp_no, sal_date) REFERENCES salaries (emp_no, from_date),
	FOREIGN KEY (emp_no, title, title_date) REFERENCES titles (emp_no, title, from_date),
	FOREIGN KEY (emp_no) REFERENCES employees (emp_no),
	PRIMARY KEY (audit_date, emp_no, sal_date)
);

CREATE OR REPLACE FUNCTION f_random_text(
    length integer
)
RETURNS text AS
$body$
SELECT string_agg(_char, '')
FROM (SELECT _char FROM (SELECT unnest(string_to_array('A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9', ' ')) AS _char) chars ORDER BY random() LIMIT $1) charlist;
$body$
LANGUAGE sql;

CREATE OR REPLACE FUNCTION f_random_gender()
RETURNS char AS
$body$
SELECT string_agg(_char, '')
FROM (SELECT _char FROM (SELECT unnest(string_to_array('M F N', ' ')) AS _char) chars ORDER BY random() LIMIT 1) charlist;
$body$
LANGUAGE sql;

INSERT INTO employees
  SELECT 
    generate_series(0, 100), 
    (now() - trunc(random() * 365)::int * '1 day'::interval - 20 * '1 year'::interval - trunc(random() * 60)::int * '1 year'::interval)::date, 
    f_random_text(10), 
    f_random_text(10), 
    f_random_gender(), 
    (now() - trunc(random() * 365)::int * '1 day'::interval - trunc(random() * 10)::int * '1 year'::interval)::date;
   
INSERT INTO titles
  SELECT 
    generate_series(0, 100),
    f_random_text(20),
    (now() - trunc(random() * 365)::int * '1 day'::interval - trunc(random() * 5)::int * '1 year'::interval)::date,
    (now() - trunc(random() * 365)::int * '1 day'::interval - trunc(random() * 5)::int * '1 year'::interval)::date;

INSERT INTO salaries 
  SELECT 
    generate_series(0, 100),
    random() * 200000 + 50000,
    (now() - trunc(random() * 365)::int * '1 day'::interval - trunc(random() * 5)::int * '1 year'::interval)::date,
    (now() - trunc(random() * 365)::int * '1 day'::interval - trunc(random() * 5)::int * '1 year'::interval)::date;

INSERT INTO salary_audit
  SELECT
    (now() - trunc(random() * 365)::int * '1 day'::interval)::date,
    s.emp_no,
    s.from_date,
    t.title,
    t.from_date,
    't'::bool
  FROM salaries s, titles t
  WHERE s.emp_no = t.emp_no
  ORDER BY random()
  LIMIT 100;

The last command fails with "ERROR: got 7 values but expected 6". This should not happen.

Expected behavior
There should be no error. As a workaround, I can successfully run:

INSERT INTO salary_audit
  SELECT * FROM (SELECT
    (now() - trunc(random() * 365)::int * '1 day'::interval)::date,
    s.emp_no,
    s.from_date,
    t.title,
    t.from_date,
    't'::bool
  FROM salaries s, titles t
  WHERE s.emp_no = t.emp_no
  ORDER BY random()
  LIMIT 100);

(Note I've just put the original query into a subquery.)

Environment:

  • CockroachDB version: master (i.e. 23.1 alpha)
  • Server OS: MacOS Ventura (Apple M1 Pro)
  • Client app: cockroach sql

Jira issue: CRDB-25354

@rytaft rytaft added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-qa labels Mar 14, 2023
@blathers-crl blathers-crl bot added the T-sql-queries SQL Queries Team label Mar 14, 2023
craig bot pushed a commit that referenced this issue Apr 6, 2023
100598: upgrade/upgrades: fix TestUpgradeSchemaChangerElements race condition r=fqazi a=fqazi

Previously, TestUpgradeSchemaChangerElements started a schema change
and intentionally caused it to be paused. It then resumed this job
and started a version upgrade. The version upgrade being tested is
meant to pause schema change jobs intentionally, which could lead
to the job getting paused again before it resumed. This patch, adds
one more channel to ensure synchronization in this sequence.

Additionally, we will further limit the scope of version to make
this test a bit shorter, so that we are only focused on the upgrade
for schema changes

Fixes: #98602

Release note: None

100675: importer: fix flaky TestImportIntoCSV r=yuzefovich a=yuzefovich

This commit fixes an oversight of 117f712 that made TestImportIntoCSV flaky. In particular, that commit made it so that if the import fails but is actually paused, the corresponding import job is resumed. This is needed in order to bring the table back online before it can be safely dropped. However, resuming of the job might fail if the job still has the "pause-requested" status, thus, this commit adds a waiting mechanism before the job transitions out of this status. Additionally, that resume can hang (or take a very long time), so this commit switches to canceling the job instead.

Fixes: #100366.
Fixes: #100477.

Release note: None

100761: flowinfra: fix a rare bug that could make drain be stuck forever r=yuzefovich a=yuzefovich

This commit fixes a long-standing bug around the flow registry that
could make the drain loop be stuck forever. Drain process works by
draining several components in a loop until each component reports that
there was no more remaining work when the drain iteration was initiated.
One of the components to be drained is the flow registry: namely, we
want to make sure that there are no more remote flows present on the
node. We track that by having a map from the `FlowID` to the `flowEntry`
object.

Previously, it was possible for a `flowEntry` to become "stale" and
remain in the map forever. In particular, this was the case when
- `ConnectInboundStream` was called before the flow was scheduled
- the gRPC "handshake" failed in `ConnectInboundStream` (most likely due
to a network fluke)
- the flow never arrived (perhaps it was canceled before
`Flow.StartInternal` is called), or it arrived too late when the
registry was marked as "draining".

With such a scenario we would create a `flowEntry` with ref count of
zero and add it to the map in `ConnectInboundStream`, but no one would
ever remove it. This commit fixes this oversight by adjusting the ref
counting logic a bit so that we always hold a reference throughout (and
only until the end of) `ConnectInboundStream`.

Fixes: #100710.

Release note (bug fix): A rare bug with distributed plans shutdown has
been fixed that previously could make the graceful drain of cockroach
nodes be retrying forever. The bug has been present since before 22.1.
The drain process is affected by this bug if you see messages in the
logs like `drain details: distSQL execution flows:` with non-zero number
of flows that isn't going down over long period of time.

100768: build: append `-dirty` suffix if relevant r=rail a=rickystewart

This command and the `rev-parse HEAD` command behave identically except if the workspace is dirty, this command adds a `-dirty` suffix.

Epic: none
Release note: None

Co-authored-by: Faizan Qazi <[email protected]>
Co-authored-by: Yahor Yuzefovich <[email protected]>
Co-authored-by: Ricky Stewart <[email protected]>
@craig craig bot closed this as completed in b8ffd10 Apr 6, 2023
@rytaft
Copy link
Collaborator Author

rytaft commented Apr 6, 2023

This issue is not fixed. I think it was closed by accident.

@rytaft rytaft reopened this Apr 6, 2023
@mgartner mgartner moved this to Bugs to Fix in SQL Queries Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-qa T-sql-queries SQL Queries Team
Projects
Status: Bugs to Fix
Development

No branches or pull requests

1 participant