Skip to content

sql: fix the handling of tuples in distsql#28143

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
knz:20180801-cleanup-tuple-arrays
Aug 2, 2018
Merged

sql: fix the handling of tuples in distsql#28143
craig[bot] merged 1 commit intocockroachdb:masterfrom
knz:20180801-cleanup-tuple-arrays

Conversation

@knz
Copy link
Copy Markdown
Contributor

@knz knz commented Aug 1, 2018

Fixes #26624.
Supersedes #28100.

This patch fixes the bug where 0-valued and 1-valued tuples were not
properly serialized in distsql processor/flow specs, causing these
special tuples to not be properly supported for distributed execution.

It also fixes the bug where labeled DTuples were not serialized with
their tuples, causing labeled tuples to be broken with distributed
execution.

Release note: None

@knz knz requested review from a team, BramGruneir, jordanlewis, justinj and rjnn August 1, 2018 14:00
@knz knz requested a review from a team as a code owner August 1, 2018 14:00
@knz knz requested review from a team August 1, 2018 14:00
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@knz
Copy link
Copy Markdown
Contributor Author

knz commented Aug 1, 2018

Ok I was a bit too trigger happy pushing this PR to everyone. There are really multiple changes/fixes in there. I will split them, add some missing tests and submit the changes separately.

rjnn
rjnn previously requested changes Aug 1, 2018
Copy link
Copy Markdown
Contributor

@rjnn rjnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm_strong: on the basic approach, thank you for fixing the underlying problem. However, I agree that this PR should be split up, as there are multiple failing tests on different issues, (which @knz and I discussed offline). All the various little fixes are good, but should probably not be packaged in a single PR.

Reviewed 31 of 31 files at r1.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained


pkg/sql/sem/tree/expr.go, line 741 at r1 (raw file):

	// col_name.go. It is also used when pretty-printing for human
	// consumption, see pretty.go.
	Row bool

Is this bool needed any more?

craig bot pushed a commit that referenced this pull request Aug 1, 2018
28128: sql: casting to timestamp should strip tz info r=jordanlewis a=jordanlewis

And binary operations between timestamp and timestamptz should strip the
tz from the timestamptz.

Confirmed that all of this behavior matches postgres (and that it didn't
before).

Release note (bug fix): correct casts and binary operators between
timestamptz and timestamp in some cases.

28149: sql: avoid using Tuple in RangePartition r=knz a=knz

Forked off  #28143, needed for #25522 / #26624.

The `Tuple` AST node is really for scalar tuples. RangePartition is
not using a scalar tuple. So split them.

Release note: None

Co-authored-by: Jordan Lewis <jordanthelewis@gmail.com>
Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
for i := len(tuple); i < len(cols); i++ {
copyTuple()
tuple.Exprs = append(tuple.Exprs, defaultExpr(len(tuple.Exprs)))
ret.Tuples[tIdx] = append(ret.Tuples[tIdx], defaultExpr(len(tuple)))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arjunravinarayan to follow-up on our conversation, this is the line where I introduced a bug.

The code should read instead:

tuple = append(tuple, defaultExpr(len(tuple)))
ret.Tuples[tIdx] = tuple

@knz knz force-pushed the 20180801-cleanup-tuple-arrays branch from 65a18f0 to 64fd0df Compare August 2, 2018 09:13
@knz knz changed the title sql: clean up / fix tuple and array handling sql: fix the handling of tuples in distsql Aug 2, 2018
@knz
Copy link
Copy Markdown
Contributor Author

knz commented Aug 2, 2018

OK PR has been split and simplified. RFAL!

@knz
Copy link
Copy Markdown
Contributor Author

knz commented Aug 2, 2018

(the first 3 commits are from different PRs but are prerequisites to this. I will rebase when those merge.)

@knz knz force-pushed the 20180801-cleanup-tuple-arrays branch from 64fd0df to 022fb74 Compare August 2, 2018 09:19
Copy link
Copy Markdown
Contributor Author

@knz knz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained


pkg/sql/sem/tree/expr.go, line 741 at r1 (raw file):

Previously, arjunravinarayan (Arjun Narayan) wrote…

Is this bool needed any more?

Yes it is needed to derive a column name automatically like pg in e.g. select row(123).

@knz knz force-pushed the 20180801-cleanup-tuple-arrays branch 4 times, most recently from 5914000 to 909764b Compare August 2, 2018 10:39
@knz knz force-pushed the 20180801-cleanup-tuple-arrays branch from 909764b to b36f852 Compare August 2, 2018 12:00
craig bot pushed a commit that referenced this pull request Aug 2, 2018
28191:  sql: avoid using Tuple in ValuesClause r=knz a=knz

Forked off from #28143.
Needed for #26624.

The `Tuple` AST node is really about scalar tuples. The `VALUES`
clause operands are not really scalar tuples. So instead use
`Exprs` for `ValuesClause`.

This will simplify later patch to fix the handling of scalar tuples.

Release note: None



Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
@knz knz force-pushed the 20180801-cleanup-tuple-arrays branch from b36f852 to 936fe37 Compare August 2, 2018 12:21
craig bot pushed a commit that referenced this pull request Aug 2, 2018
28192: sql/parser: parsing ROW produces *Tuple, not Tuple r=knz a=knz

Forked off from #28143.
Needed for #26624.

The tree.Tuple result of parsing tuples was immediately re-allocated
on the heap in the parent action rule. This patch simplifies this by
allocating on the heap upfront.

Release note: None

Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
@knz knz force-pushed the 20180801-cleanup-tuple-arrays branch from 936fe37 to 56db7a0 Compare August 2, 2018 13:31
Copy link
Copy Markdown
Member

@jordanlewis jordanlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM though I'm concerned that some of the CLI outputs don't match postgres. See comments

Reviewed 1 of 31 files at r1, 34 of 34 files at r2.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained


pkg/sql/logictest/testdata/logic_test/srfs, line 418 at r2 (raw file):

("c",1)
("b",2)
("a",3)

I think this probably is fine, but FYI this is not what postgres outputs.

jordan=# SELECT information_schema._pg_expandarray(ARRAY['c', 'b', 'a']);
 _pg_expandarray
-----------------
 (c,1)
 (b,2)
 (a,3)
(3 rows)

pkg/sql/logictest/testdata/logic_test/srfs, line 545 at r2 (raw file):

("(3,1)",123)
("(2,2)",123)
("(1,3)",123)
jordan=# SELECT ((i.keys).*, 123) FROM (SELECT information_schema._pg_expandarray(ARRAY[3,2,1]) AS keys) AS i;
    row
-----------
 (3,1,123)
 (2,2,123)
 (1,3,123)

pkg/sql/logictest/testdata/logic_test/tuple, line 10 at r2 (raw file):

----
t                u
(1,2,"hello",,)  (t,,"(f,6.6,f)")
jordan=# SELECT (1, 2, 'hello', NULL, NULL) AS t, (true, NULL, (false, 6.6, false)) AS u;
       t       |        u
---------------+------------------
 (1,2,hello,,) | (t,,"(f,6.6,f)")

pkg/sql/sem/tree/expr.go, line 763 at r3 (raw file):

	ctx.WriteByte('(')
	ctx.FormatNode(&node.Exprs)
	if len(node.Exprs) == 1 {

nit: The equivalent in DTuple has a comment explaining this - if you're going to rebase this again plz add that comment here too.


pkg/sql/sem/tree/format.go, line 87 at r2 (raw file):

	// If set, strings will be formatted for being contents of ARRAYs.
	// Used internally in combination with FmtPgwireText defined below.
	pgwireFormat

Nice idea.

But this flag does more than the comment says, now. It also prints NULL as the empty string, right?

Copy link
Copy Markdown
Contributor Author

@knz knz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained


pkg/sql/logictest/testdata/logic_test/srfs, line 418 at r2 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

I think this probably is fine, but FYI this is not what postgres outputs.

jordan=# SELECT information_schema._pg_expandarray(ARRAY['c', 'b', 'a']);
 _pg_expandarray
-----------------
 (c,1)
 (b,2)
 (a,3)
(3 rows)

Discussion for #28151.
My comment there: "it's ugly but it is correct".


pkg/sql/logictest/testdata/logic_test/srfs, line 545 at r2 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…
jordan=# SELECT ((i.keys).*, 123) FROM (SELECT information_schema._pg_expandarray(ARRAY[3,2,1]) AS keys) AS i;
    row
-----------
 (3,1,123)
 (2,2,123)
 (1,3,123)

That's a separate bug, well spotted: #28203


pkg/sql/logictest/testdata/logic_test/tuple, line 10 at r2 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…
jordan=# SELECT (1, 2, 'hello', NULL, NULL) AS t, (true, NULL, (false, 6.6, false)) AS u;
       t       |        u
---------------+------------------
 (1,2,hello,,) | (t,,"(f,6.6,f)")

Discussion for #28151. Same comment as above.


pkg/sql/sem/tree/expr.go, line 763 at r3 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

nit: The equivalent in DTuple has a comment explaining this - if you're going to rebase this again plz add that comment here too.

Done.


pkg/sql/sem/tree/format.go, line 87 at r2 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

Nice idea.

But this flag does more than the comment says, now. It also prints NULL as the empty string, right?

Changed the comment. Thanks for noticing.

Copy link
Copy Markdown
Member

@jordanlewis jordanlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 2 of 27 files at r4, 25 of 25 files at r5.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale)

@knz knz force-pushed the 20180801-cleanup-tuple-arrays branch from 9412d4a to 822aea1 Compare August 2, 2018 17:07
@knz
Copy link
Copy Markdown
Contributor Author

knz commented Aug 2, 2018

I'll hold off this while Bram prepares his all-eng prez.

craig bot pushed a commit that referenced this pull request Aug 2, 2018
28151: sql: fix the pg text representation of tuples/arrays r=knz a=knz

Forked off #28143.
Fixes #25522.

This patch fixes the conversion of arrays and tuples to text for the
purpose of emitting the arrays/tuples back to a client through pgwire.
This now properly supports nested arrays, arrays containing tuples,
tuples containing arrays, etc. Labels in tuples are never emitted
through pgwire.

Release note (bug fix): CockroachDB supports a wider range of tuple
and array values in query results.

Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
This patch fixes the bug where 0-valued and 1-valued tuples were not
properly serialized in distsql processor/flow specs, causing these
special tuples to not be properly supported for distributed execution.

It also fixes the bug where labeled DTuples were not serialized with
their tuples, causing labeled tuples to be broken with distributed
execution.

In order to achieve this it also extends CockroachDB to support empty
tuples (notation: `()`), 1-valued tuples without the `row`
keyword (notation: `(x,)`), and empty tuples on the RHS of `IN`
comparisons.

Release note (sql change): CockroachDB now supports empty tuples with
the syntax `()`, 1-valued tuples with the syntax `(x,)` in addition to
`row(x)`, and the ability to use `IN` with an empty tuple as right
operand.
@knz knz force-pushed the 20180801-cleanup-tuple-arrays branch from 822aea1 to c35d8e4 Compare August 2, 2018 18:35
@knz knz dismissed rjnn’s stale review August 2, 2018 18:35

lgtm received

@knz
Copy link
Copy Markdown
Contributor Author

knz commented Aug 2, 2018

Thanks all

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Aug 2, 2018

Build failed (retrying...)

craig bot pushed a commit that referenced this pull request Aug 2, 2018
28143: sql: fix the handling of tuples in distsql r=knz a=knz

Fixes #26624.
Supersedes #28100.

This patch fixes the bug where 0-valued and 1-valued tuples were not
properly serialized in distsql processor/flow specs, causing these
special tuples to not be properly supported for distributed execution.

It also fixes the bug where labeled DTuples were not serialized with
their tuples, causing labeled tuples to be broken with distributed
execution.

Release note: None

28217: storage: dissallow empty HeartbeatRequest.Now r=andreimatei a=andreimatei

Release note: None

28219: distsql: fix a missing context in log message r=arjunravinarayan a=arjunravinarayan

Release note: None

Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
Co-authored-by: Andrei Matei <andrei@cockroachlabs.com>
Co-authored-by: Arjun Narayan <arjun@cockroachlabs.com>
@craig
Copy link
Copy Markdown
Contributor

craig bot commented Aug 2, 2018

Build succeeded

@craig craig bot merged commit c35d8e4 into cockroachdb:master Aug 2, 2018
@knz knz deleted the 20180801-cleanup-tuple-arrays branch August 2, 2018 21:59
craig bot pushed a commit that referenced this pull request Aug 3, 2018
28183: sql: support casting arrays and tuples to strings r=knz a=knz

Fixes #24746.
Forked off #28143.

The previous patch to fix pgwire text encoding for tuples/arrays
really defines a different conversion algorithm from array/tuples to
strings.

This incidentally is the general-purpose algorithm used in PostgreSQL
to convert arrays and tuples to strings, not just for pgwire.

This patch utilizes this observation to define the new
(and now correct) casts from array and tuple types to strings
based on the new algorithm, like PostgreSQL does.

Release note (sql change): CockroachDB now supports converting arrays
and tuples to strings for compatibility with PostgreSQL.

Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: tuples not properly preserved during serialization

4 participants