Skip to content

sql: add support for create index inside new schema#72610

Merged
craig[bot] merged 4 commits intocockroachdb:masterfrom
fqazi:createTableNew
Nov 12, 2021
Merged

sql: add support for create index inside new schema#72610
craig[bot] merged 4 commits intocockroachdb:masterfrom
fqazi:createTableNew

Conversation

@fqazi
Copy link
Copy Markdown
Collaborator

@fqazi fqazi commented Nov 10, 2021

Fixes: #67264

Previously, create index was not supported inside the
new schema changer. This was inadequate because to
help support transaction schema changes in the future
we need all schema changes inside the new schema changer.
To address this, this patch adds CREATE INDEX inside
the new schema changer.

Release note: None

@fqazi fqazi requested review from a team and ajwerner November 10, 2021 14:41
@fqazi fqazi requested a review from a team as a code owner November 10, 2021 14:41
@fqazi fqazi requested review from msbutler and removed request for a team November 10, 2021 14:41
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@fqazi fqazi force-pushed the createTableNew branch 3 times, most recently from 8ab4ddc to 931a93c Compare November 10, 2021 15:48
Comment on lines +83 to +84
nodeDepEdgesFrom: map[*scpb.Node]*btree.BTree{},
nodeDepEdgesTo: map[*scpb.Node]*btree.BTree{},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

my suggestion was that you get rid of the map altogether. You can iterate the prefix of the tree corresponding to this node.

Copy link
Copy Markdown

@postamar postamar left a comment

Choose a reason for hiding this comment

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

I should have reviewed this the first time around, but better late than never. Thanks for doing this, I definitely appreciate the effort this must have involved.

I didn't look deeply into the scplan stuff because I mostly don't understand it deeply.

I did find a few things which seemed questionable to me and worth changing before merging. Fixing them should mostly be about moving stuff around so hopefully no big deal. Nits are things I just couldn't help noticing, feel free to disregard them if addressing them is a waste of your time.

statement ok
CREATE INDEX id1 on defaultdb.t1(id, name) storing (money) PARTITION BY LIST (id) (
PARTITION p1 VALUES IN (1)
);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

formatting

settings: settings,
ieFactory: ieFactory,
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shouldn't you be defining indexValidator in scdeps instead? That way you don't need to add the index validator to the exec cfg. It just seems simpler? Perhaps I'm missing something.

settings: settings,
evalContext: evalContext,
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same comment as indexValidator really, I'm not sure why this all isn't in scdeps.

}
res := tree.Serialize(modBuckets(hashedColumnsExpr()))
return &res
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why did this need to be moved here? Is it because of a dependency circularity issue? If not I'd be inclined to leave it where it was.

}

if table.IsView() && !table.MaterializedView() {
panic(pgerror.Newf(pgcode.WrongObjectType, "%q is not a table or materialized view", &n.Table))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This seems wrong, I believe you should be testing for !table.IsPhysicalTable() || table.IsSequence() and modify the error message accordingly.

Index: *idx,
PrimaryIndex: newPrimaryIdxID,
})
// Computed columns do not exist inside the primary index,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: I believe you mean Virtual computed columns?

}
tabledesc.UpdateIndexPartitioning(indexDesc, false, newImplicitCols, newPartitioning)
return nil
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why is this defined as a method on txnDeps? Isn't the whole point of having d.partitioner.CreatePartitioning no dependencies would be required?

}

// AddPartitioning implements the scmutationexec.CatalogReader interface.
func (s *TestState) AddPartitioning(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same comment as for txnDeps.

log.Errorf(ctx, "not implemented")
default:
panic("unimplemented")
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: the logic in this switch case deserves to be refactored into functions, one per case.

if err != nil {
return err
}
storeColNames = append(storeColNames, column.GetName())
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: define a private function to map a column ID to a name perhaps? As we grow the schema changer, patterns like these are going to keep emerging, and we should take care that this particular source file doesn't turn into an unwieldy mess.

@msbutler msbutler requested review from rhu713 and removed request for msbutler November 10, 2021 18:10
@fqazi fqazi requested a review from a team as a code owner November 10, 2021 20:49
Copy link
Copy Markdown
Collaborator Author

@fqazi fqazi 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! 0 of 0 LGTMs obtained (waiting on @ajwerner, @postamar, and @rhu713)


pkg/ccl/logictestccl/testdata/logic_test/new_schema_changer, line 12 at r4 (raw file):

Previously, postamar (Marius Posta) wrote…

formatting

Done.


pkg/sql/backfill.go, line 1658 at r4 (raw file):

Previously, postamar (Marius Posta) wrote…

Shouldn't you be defining indexValidator in scdeps instead? That way you don't need to add the index validator to the exec cfg. It just seems simpler? Perhaps I'm missing something.

Done.


pkg/sql/schemachanger/scbuild/index.go, line 87 at r4 (raw file):

Previously, postamar (Marius Posta) wrote…

Why did this need to be moved here? Is it because of a dependency circularity issue? If not I'd be inclined to leave it where it was.

Yes, this was a circular dependency issue.


pkg/sql/schemachanger/scbuild/index.go, line 172 at r4 (raw file):

Previously, postamar (Marius Posta) wrote…

This seems wrong, I believe you should be testing for !table.IsPhysicalTable() || table.IsSequence() and modify the error message accordingly.

The error was taken from regular CREATE INDEX should we do the same there?


pkg/sql/schemachanger/scbuild/index.go, line 176 at r4 (raw file):

Previously, postamar (Marius Posta) wrote…

nit: combine the two ifs?

The second condition below also relies on it being materialized.


pkg/sql/schemachanger/scdeps/exec_deps.go, line 153 at r4 (raw file):

Previously, postamar (Marius Posta) wrote…

Why is this defined as a method on txnDeps? Isn't the whole point of having d.partitioner.CreatePartitioning no dependencies would be required?

Done.


pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go, line 453 at r4 (raw file):

Previously, postamar (Marius Posta) wrote…

Same comment as for txnDeps.

Done.


pkg/sql/schemachanger/scgraph/graph.go, line 84 at r2 (raw file):

Previously, ajwerner wrote…

my suggestion was that you get rid of the map altogether. You can iterate the prefix of the tree corresponding to this node.

Done.


pkg/sql/schema_changer_ccl.go, line 60 at r4 (raw file):

Previously, postamar (Marius Posta) wrote…

Same comment as indexValidator really, I'm not sure why this all isn't in scdeps.

Done.

@fqazi fqazi requested a review from ajwerner November 10, 2021 20:58
@fqazi fqazi requested a review from postamar November 10, 2021 20:58
visit = func(n *scpb.Node) {
permanent, marked := marks[n]
if marked && !permanent {
panic("not a dag")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thoughts on catching this panic and converting it to an error and then having this method return an error?

@@ -28,7 +31,19 @@ type MakeAddedIndexDeleteOnly struct {
TableID descpb.ID

// Index represents the index as it should appear in the mutation.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This comment is now stale

TableID descpb.ID

// Index is the descriptor as it should be added as part of the mutation. The
// IndexID is the descriptor as it should be added as part of the mutation. The
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment is now stale

Comment on lines +81 to +87
if this.Unique {
return &scop.ValidateUniqueIndex{
TableID: this.TableID,
IndexID: this.IndexId,
}
}
return &scop.NoOpInfo{}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My preference is that we remove this optimization. The main reason is that today we validate all indexes, unique or otherwise. My secondary reason is that this NoOp smells a bit. Lastly, I think we'll want to represent not doing it elsewhere.

@fqazi fqazi force-pushed the createTableNew branch 3 times, most recently from 31079ec to 7317f74 Compare November 11, 2021 04:27
@fqazi fqazi requested a review from ajwerner November 11, 2021 05:13
@fqazi fqazi force-pushed the createTableNew branch 4 times, most recently from 7e9ff10 to c28b27a Compare November 11, 2021 18:16
Copy link
Copy Markdown
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

:lgtm:, let's do it

Reviewed 1 of 60 files at r5, 3 of 33 files at r6, 3 of 52 files at r7, 1 of 8 files at r10, 6 of 33 files at r11, 25 of 54 files at r12, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ajwerner, @fqazi, @postamar, and @rhu713)

Previously, the new schema changer attempted to use
a bubble sort which compared the nodes linked to ops
to generate an approximate order. Unfortunately, this
approach had its limitations when it came to elements
that had no edge linking them, which could lead to
incorrect order relative to dependency edges. To
address this, this patch moves the sorting to a simpler
topological sort which guarantees dependency order.

Release note: None
Fixes: cockroachdb#72562, cockroachdb#72561

Previously, when we updated the sorting inside the new schema changer
to a rank based approach we incorrectly assumed that DepEdges will always
be generated in a deterministic fashion. This does not happen because
the database and rule matching is not fully determentistic.
Unfortunately, this turned ouut to be false, which can lead
to ranks for neighbour edges to be unstable. To address this,
this patch iterates over DepEdges in a ordered fashion based
on the To nodes, fixing intermittent failures inside the new
schema changer tests.

Release note: None
Fixes: cockroachdb#67264

Previously, create index was not supported inside the
new shcema changer. This was inadequate because, to
help support transaction schema changes in the future
we need all schema changes inside the new schema changer.
To address this, this patch adds CREATE INDEX inside
the new schema changer.

Release note: None
Previously, the unique index validation inside the
new schema changer was stubbed out. This was inadequate
because during index addition operations we were not
properly confirming the unique constraints were properly
implemented. To address this, this patch adds support in
the new schema changer for the valdiation logic.

Release note: None
@fqazi
Copy link
Copy Markdown
Collaborator Author

fqazi commented Nov 11, 2021

@ajwerner TFTR!

@fqazi
Copy link
Copy Markdown
Collaborator Author

fqazi commented Nov 11, 2021

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Nov 12, 2021

Build failed (retrying...):

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Nov 12, 2021

Build succeeded:

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: implement create index inside the new schema changer

4 participants