Skip to content

Commit

Permalink
Merge pull request #68114 from ajwerner/backport21.1-67712
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwerner authored Aug 2, 2021
2 parents 80bf47b + 9e29be2 commit f395e17
Show file tree
Hide file tree
Showing 10 changed files with 1,021 additions and 372 deletions.
1 change: 1 addition & 0 deletions docs/generated/settings/settings-for-tenants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ server.web_session_timeout duration 168h0m0s the duration that a newly created w
sql.cross_db_fks.enabled boolean false if true, creating foreign key references across databases is allowed
sql.cross_db_sequence_owners.enabled boolean false if true, creating sequences owned by tables from other databases is allowed
sql.cross_db_views.enabled boolean false if true, creating views that refer to other databases is allowed
sql.defaults.copy_partitioning_when_deinterleaving_table.enabled boolean false default value for enable_copying_partitioning_when_deinterleaving_table session variable
sql.defaults.default_int_size integer 8 the size, in bytes, of an INT type
sql.defaults.disallow_full_table_scans.enabled boolean false setting to true rejects queries that have planned a full table scan
sql.defaults.idle_in_session_timeout duration 0s default value for the idle_in_session_timeout; default value for the idle_in_session_timeout session setting; controls the duration a session is permitted to idle before the session is terminated; if set to 0, there is no timeout
Expand Down
1 change: 1 addition & 0 deletions docs/generated/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<tr><td><code>sql.cross_db_fks.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if true, creating foreign key references across databases is allowed</td></tr>
<tr><td><code>sql.cross_db_sequence_owners.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if true, creating sequences owned by tables from other databases is allowed</td></tr>
<tr><td><code>sql.cross_db_views.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if true, creating views that refer to other databases is allowed</td></tr>
<tr><td><code>sql.defaults.copy_partitioning_when_deinterleaving_table.enabled</code></td><td>boolean</td><td><code>false</code></td><td>default value for enable_copying_partitioning_when_deinterleaving_table session variable</td></tr>
<tr><td><code>sql.defaults.default_int_size</code></td><td>integer</td><td><code>8</code></td><td>the size, in bytes, of an INT type</td></tr>
<tr><td><code>sql.defaults.disallow_full_table_scans.enabled</code></td><td>boolean</td><td><code>false</code></td><td>setting to true rejects queries that have planned a full table scan</td></tr>
<tr><td><code>sql.defaults.idle_in_session_timeout</code></td><td>duration</td><td><code>0s</code></td><td>default value for the idle_in_session_timeout; default value for the idle_in_session_timeout session setting; controls the duration a session is permitted to idle before the session is terminated; if set to 0, there is no timeout</td></tr>
Expand Down
509 changes: 509 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/partitioning_deinterleave

Large diffs are not rendered by default.

97 changes: 97 additions & 0 deletions pkg/sql/alter_primary_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"context"
"strings"

"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/resolver"
Expand All @@ -26,6 +27,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/errors"
"github.com/gogo/protobuf/proto"
)

// alterPrimaryKeyLocalitySwap contains metadata on a locality swap for
Expand Down Expand Up @@ -339,6 +341,12 @@ func (p *planner) AlterPrimaryKey(
if err != nil {
return err
}
} else {
if err := maybeCopyPartitioningWhenDeinterleaving(
ctx, p, tableDesc, newPrimaryIndexDesc,
); err != nil {
return err
}
}

if partitionAllBy != nil {
Expand Down Expand Up @@ -545,6 +553,95 @@ func (p *planner) AlterPrimaryKey(
return nil
}

func maybeCopyPartitioningWhenDeinterleaving(
ctx context.Context,
p *planner,
tableDesc *tabledesc.Mutable,
newPrimaryIndexDesc *descpb.IndexDescriptor,
) error {
if tableDesc.GetPrimaryIndex().NumInterleaveAncestors() == 0 ||
!p.SessionData().CopyPartitioningWhenDeinterleavingTable ||
len(newPrimaryIndexDesc.Interleave.Ancestors) > 0 {
return nil
}

// The old primary key was interleaved in a parent and the new one is not.
// In this case, we need to clone out the old primary key's partitioning
// and zone configs and apply them to the new primary index. We do this
// if the old primary index and the new primary index have the exact same
// columns. That also allows us to side-step discussions of what to do
// about partitioning for any newly created unique index we might create
// below.

root := tableDesc.GetPrimaryIndex().GetInterleaveAncestor(0)
interleaveRoot, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, root.TableID, tree.ObjectLookupFlags{
CommonLookupFlags: tree.CommonLookupFlags{
Required: true,
AvoidCached: true,
},
DesiredObjectKind: tree.TableObject,
})
if err != nil {
return errors.Wrap(err, "looking up interleaved root")
}
rootIndex, err := interleaveRoot.FindIndexWithID(root.IndexID)
if err != nil {
return errors.Wrap(err, "looking up interleaved root index")
}

// If the new primary key does not have the interleave root as a prefix,
// do not copy the interleave.
if rootKeys := rootIndex.IndexDesc().ColumnIDs; !descpb.ColumnIDs.Equals(
rootKeys, newPrimaryIndexDesc.ColumnIDs[:len(rootKeys)],
) {
return nil
}

// The parent is not partitioned, return.
if rootIndex.GetPartitioning().NumColumns == 0 {
return nil
}
part := rootIndex.GetPartitioning()
newPrimaryIndexDesc.Partitioning = *protoutil.Clone(&part).(*descpb.PartitioningDescriptor)
rootCfg, err := getZoneConfigRaw(ctx, p.txn, p.execCfg.Codec, root.TableID)
if err != nil {
return errors.Wrapf(err, "retrieving zone config for table %s [%d]",
interleaveRoot.GetName(), interleaveRoot.GetID())
}
tableCfg, err := getZoneConfigRaw(ctx, p.txn, p.execCfg.Codec, tableDesc.GetID())
if err != nil {
return errors.Wrapf(err, "retrieving zone config for table %s [%d]",
tableDesc.GetName(), tableDesc.GetID())
}
// Initialize the zone config for the child. We expect it to be nil because
// the table was an interleaved child and we did not allow such children to
// have zone configs. It may be a subzone placeholder because other indexes
// might be partitioned and have zone configs.
if tableCfg == nil {
// Marking NumReplicas as 0 indicates that this zone config is a
// subzone placeholder. We assume that the value in copying out the
// partitioning is to copy out the configuration as it applies to the
// partitions of the primary index.
tableCfg = &zonepb.ZoneConfig{
NumReplicas: proto.Int(0),
}
} else if !tableCfg.IsSubzonePlaceholder() {
return errors.AssertionFailedf("child table %s [%d] of interleave was not a subzone placeholder",
tableDesc.GetName(), tableDesc.GetID())
}

for _, s := range rootCfg.Subzones {
if s.IndexID == uint32(root.IndexID) {
s.IndexID = uint32(newPrimaryIndexDesc.ID)
tableCfg.Subzones = append(tableCfg.Subzones, s)
}
}
_, err = writeZoneConfig(
ctx, p.txn, tableDesc.GetID(), tableDesc, tableCfg, p.execCfg, true,
)
return err
}

// Given the current table descriptor and the new primary keys
// index descriptor this function determines if the two are
// equivalent and if any index creation operations are needed
Expand Down
14 changes: 13 additions & 1 deletion pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ var experimentalComputedColumnRewrites = settings.RegisterValidatedStringSetting
},
)

var copyPartitioningWhenDeinterleavingTable = settings.RegisterBoolSetting(
`sql.defaults.copy_partitioning_when_deinterleaving_table.enabled`,
`default value for enable_copying_partitioning_when_deinterleaving_table session variable`,
false,
).WithPublic()

// ExperimentalDistSQLPlanningClusterSettingName is the name for the cluster
// setting that controls experimentalDistSQLPlanningClusterMode below.
const ExperimentalDistSQLPlanningClusterSettingName = "sql.defaults.experimental_distsql_planning"
Expand Down Expand Up @@ -2444,11 +2450,17 @@ func (m *sessionDataMutator) initSequenceCache() {
m.data.SequenceCache = sessiondata.SequenceCache{}
}

// SetStubCatalogTableEnabled sets default value for stub_catalog_tables.
// SetStubCatalogTablesEnabled sets default value for stub_catalog_tables.
func (m *sessionDataMutator) SetStubCatalogTablesEnabled(enabled bool) {
m.data.StubCatalogTablesEnabled = enabled
}

// SetCopyPartitioningWhenDeinterleavingTable sets the value for
// CopyPartitioningWhenDeinterleavingTable.
func (m *sessionDataMutator) SetCopyPartitioningWhenDeinterleavingTable(b bool) {
m.data.CopyPartitioningWhenDeinterleavingTable = b
}

func (m *sessionDataMutator) SetExperimentalComputedColumnRewrites(val string) {
m.data.ExperimentalComputedColumnRewrites = val
}
Expand Down
153 changes: 77 additions & 76 deletions pkg/sql/logictest/testdata/logic_test/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -3596,82 +3596,83 @@ subtest variables
query TT colnames
SELECT * FROM information_schema.session_variables where variable not in ('crdb_version', 'session_id', 'distsql', 'vectorize', 'experimental_distsql_planning')
----
variable value
allow_prepare_as_opt_plan off
application_name ·
bytea_output hex
client_encoding UTF8
client_min_messages notice
database test
datestyle ISO, MDY
default_int_size 8
default_tablespace ·
default_transaction_isolation serializable
default_transaction_priority normal
default_transaction_read_only off
default_transaction_use_follower_reads off
disable_partially_distributed_plans off
disallow_full_table_scans off
enable_drop_enum_value off
enable_experimental_alter_column_type_general off
enable_experimental_stream_replication off
enable_implicit_select_for_update on
enable_insert_fast_path on
enable_seqscan on
enable_zigzag_join on
escape_string_warning on
experimental_computed_column_rewrites ·
experimental_enable_hash_sharded_indexes off
experimental_enable_implicit_column_partitioning off
experimental_enable_temp_tables off
experimental_enable_unique_without_index_constraints on
experimental_use_new_schema_changer off
extra_float_digits 0
force_savepoint_restart off
foreign_key_cascades_limit 10000
idle_in_session_timeout 0
idle_in_transaction_session_timeout 0
integer_datetimes on
intervalstyle postgres
locality region=test,dc=dc1
locality_optimized_partitioned_index_scan on
lock_timeout 0
max_identifier_length 128
max_index_keys 32
node_id 1
optimizer on
optimizer_improve_disjunction_selectivity off
optimizer_use_histograms on
optimizer_use_multicol_stats on
override_multi_region_zone_config off
prefer_lookup_joins_for_fks off
reorder_joins_limit 8
require_explicit_primary_keys off
results_buffer_size 16384
row_security off
save_tables_prefix ·
search_path $user,public
serial_normalization rowid
server_encoding UTF8
server_version 13.0.0
server_version_num 130000
session_authorization root
session_user root
sql_safe_updates off
ssl_renegotiation_limit 0
standard_conforming_strings on
statement_timeout 0
stub_catalog_tables on
synchronize_seqscans on
synchronous_commit on
testing_vectorize_inject_panics off
timezone UTC
tracing off
transaction_isolation serializable
transaction_priority normal
transaction_read_only off
transaction_status NoTxn
vectorize_row_count_threshold 0
variable value
allow_prepare_as_opt_plan off
application_name ·
bytea_output hex
client_encoding UTF8
client_min_messages notice
database test
datestyle ISO, MDY
default_int_size 8
default_tablespace ·
default_transaction_isolation serializable
default_transaction_priority normal
default_transaction_read_only off
default_transaction_use_follower_reads off
disable_partially_distributed_plans off
disallow_full_table_scans off
enable_copying_partitioning_when_deinterleaving_table off
enable_drop_enum_value off
enable_experimental_alter_column_type_general off
enable_experimental_stream_replication off
enable_implicit_select_for_update on
enable_insert_fast_path on
enable_seqscan on
enable_zigzag_join on
escape_string_warning on
experimental_computed_column_rewrites ·
experimental_enable_hash_sharded_indexes off
experimental_enable_implicit_column_partitioning off
experimental_enable_temp_tables off
experimental_enable_unique_without_index_constraints on
experimental_use_new_schema_changer off
extra_float_digits 0
force_savepoint_restart off
foreign_key_cascades_limit 10000
idle_in_session_timeout 0
idle_in_transaction_session_timeout 0
integer_datetimes on
intervalstyle postgres
locality region=test,dc=dc1
locality_optimized_partitioned_index_scan on
lock_timeout 0
max_identifier_length 128
max_index_keys 32
node_id 1
optimizer on
optimizer_improve_disjunction_selectivity off
optimizer_use_histograms on
optimizer_use_multicol_stats on
override_multi_region_zone_config off
prefer_lookup_joins_for_fks off
reorder_joins_limit 8
require_explicit_primary_keys off
results_buffer_size 16384
row_security off
save_tables_prefix ·
search_path $user,public
serial_normalization rowid
server_encoding UTF8
server_version 13.0.0
server_version_num 130000
session_authorization root
session_user root
sql_safe_updates off
ssl_renegotiation_limit 0
standard_conforming_strings on
statement_timeout 0
stub_catalog_tables on
synchronize_seqscans on
synchronous_commit on
testing_vectorize_inject_panics off
timezone UTC
tracing off
transaction_isolation serializable
transaction_priority normal
transaction_read_only off
transaction_status NoTxn
vectorize_row_count_threshold 0

# information_schema can be used with the anonymous database.
# It should show information across all databases.
Expand Down
Loading

0 comments on commit f395e17

Please sign in to comment.