Skip to content

Commit

Permalink
Merge #57644
Browse files Browse the repository at this point in the history
57644: sql,colexec: preliminary changes for wrapping LocalPlanNode r=yuzefovich a=yuzefovich

**sql: order the output of some SHOW queries deterministically**

This commit modifies several internal queries for SHOW commands to make
the ordering of the output deterministic. The following had been
augmented:
- SHOW PARTITIONS
- SHOW CREATE
- SHOW INDEXES
- SHOW COLUMNS (this also had an addition of ORDER BY clause of
array_agg that computes the set of indexes)
- SHOW CONSTRAINTS.

Additionally, several logic tests were adjusted to use `rowsort`
directive when ORDER BY isn't specified in the query as well as some
EXPLAIN tests were moved into the execbuilder ones.

This work was prompted by the fact that the vectorized engine produces
the output for those queries with a different order (which is valid
given omitted ORDER BY clauses before) than the row-by-row engine.

Release note (sql change): Several SHOW commands have been adjusted to
enforce a particular ordering of the output rows.

**colexec: use datum-backed type for Oids**

Previously, we would use `Int64` canonical type family to represents
Oids. However, such conversion is lossful because we're losing some
information (like semantic type and resolvable name), so if we were to
do `columnarize -> materialize` conversion on an Oid, we wouldn't get
the same thing back in the general case. To go around this issue this
commit switches Oid type family to be handled by the datum-backed type.
This will have some performance hit, but the queries with Oids aren't
expected to process a lot of data, so that hit could be safely ignored.

Release note: None

Co-authored-by: Yahor Yuzefovich <[email protected]>
  • Loading branch information
craig[bot] and yuzefovich committed Dec 8, 2020
2 parents 178ae48 + daffbe4 commit b7ef888
Show file tree
Hide file tree
Showing 36 changed files with 200 additions and 305 deletions.
2 changes: 1 addition & 1 deletion pkg/col/typeconv/typeconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TypeFamilyToCanonicalTypeFamily(family types.Family) types.Family {
return types.BytesFamily
case types.DecimalFamily:
return types.DecimalFamily
case types.IntFamily, types.DateFamily, types.OidFamily:
case types.IntFamily, types.DateFamily:
return types.IntFamily
case types.FloatFamily:
return types.FloatFamily
Expand Down
9 changes: 0 additions & 9 deletions pkg/sql/colconv/datum_to_vec.eg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 0 additions & 78 deletions pkg/sql/colconv/vec_to_datum.eg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/sql/colencoding/key_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func decodeTableKeyToCol(
rkey, i, err = encoding.DecodeVarintDescending(key)
}
vec.Bool()[idx] = i != 0
case types.IntFamily, types.DateFamily, types.OidFamily:
case types.IntFamily, types.DateFamily:
var i int64
if dir == descpb.IndexDescriptor_ASC {
rkey, i, err = encoding.DecodeVarintAscending(key)
Expand Down Expand Up @@ -319,7 +319,7 @@ func UnmarshalColumnValueToCol(
var v []byte
v, err = value.GetBytes()
vec.Bytes().Set(idx, v)
case types.DateFamily, types.OidFamily:
case types.DateFamily:
var v int64
v, err = value.GetInt()
vec.Int64()[idx] = v
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/colencoding/value_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func decodeUntaggedDatumToCol(
var data []byte
buf, data, err = encoding.DecodeUntaggedBytesValue(buf)
vec.Bytes().Set(idx, data)
case types.DateFamily, types.OidFamily:
case types.DateFamily:
var i int64
buf, i, err = encoding.DecodeUntaggedIntValue(buf)
vec.Int64()[idx] = i
Expand Down
1 change: 0 additions & 1 deletion pkg/sql/colexec/execgen/cmd/execgen/rowstovec_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ var rowsToVecConversionTmpls = map[familyWidthPair]string{
{types.IntFamily, anyWidth}: `int64(*%[1]s.(*tree.DInt))`,
{types.DateFamily, anyWidth}: `%[1]s.(*tree.DDate).UnixEpochDaysWithOrig()`,
{types.FloatFamily, anyWidth}: `float64(*%[1]s.(*tree.DFloat))`,
{types.OidFamily, anyWidth}: `int64(%[1]s.(*tree.DOid).DInt)`,
{types.StringFamily, anyWidth}: `encoding.UnsafeConvertStringToBytes(string(*%[1]s.(*tree.DString)))`,
{types.DecimalFamily, anyWidth}: `%[1]s.(*tree.DDecimal).Decimal`,
{types.UuidFamily, anyWidth}: `%[1]s.(*tree.DUuid).UUID.GetBytesMut()`,
Expand Down
3 changes: 1 addition & 2 deletions pkg/sql/colexec/execgen/cmd/execgen/vec_to_datum_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ var vecToDatumConversionTmpls = map[types.Family]string{
types.BytesFamily: `// Note that there is no need for a copy since DBytes uses a string
// as underlying storage, which will perform the copy for us.
%[1]s = %[4]s.NewDBytes(tree.DBytes(%[2]s.Get(%[3]s)))`,
types.OidFamily: `%[1]s = %[4]s.NewDOid(tree.MakeDOid(tree.DInt(%[2]s[%[3]s])))`,
types.UuidFamily: ` // Note that there is no need for a copy because uuid.FromBytes
// will perform a copy.
id, err := uuid.FromBytes(%[2]s.Get(%[3]s))
Expand Down Expand Up @@ -121,7 +120,7 @@ func genVecToDatum(inputFileContents string, wr io.Writer) error {
// the template explicitly, so it is omitted from this slice.
optimizedTypeFamilies := []types.Family{
types.BoolFamily, types.IntFamily, types.FloatFamily, types.DecimalFamily,
types.DateFamily, types.BytesFamily, types.OidFamily, types.UuidFamily,
types.DateFamily, types.BytesFamily, types.UuidFamily,
types.TimestampFamily, types.TimestampTZFamily, types.IntervalFamily,
}
for _, typeFamily := range optimizedTypeFamilies {
Expand Down
24 changes: 0 additions & 24 deletions pkg/sql/colexec/rowstovec.eg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/sql/colfetcher/cfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ type cFetcher struct {
// colvecs to avoid having to cast the vec to decimal on every write.
timestampCol []apd.Decimal
// tableoidCol is the same as timestampCol but for the tableoid system column.
tableoidCol []int64
tableoidCol coldata.DatumVec
}

typs []*types.T
Expand Down Expand Up @@ -326,7 +326,7 @@ func (rf *cFetcher) resetBatch(timestampOutputIdx, tableOidOutputIdx int) {
rf.machine.timestampCol = rf.machine.colvecs[timestampOutputIdx].Decimal()
}
if tableOidOutputIdx != noOutputColumn {
rf.machine.tableoidCol = rf.machine.colvecs[tableOidOutputIdx].Int64()
rf.machine.tableoidCol = rf.machine.colvecs[tableOidOutputIdx].Datum()
}
}
}
Expand Down Expand Up @@ -1002,7 +1002,7 @@ func (rf *cFetcher) nextBatch(ctx context.Context) (coldata.Batch, error) {
rf.machine.timestampCol[rf.machine.rowIdx] = tree.TimestampToDecimal(rf.table.rowLastModified)
}
if rf.table.oidOutputIdx != noOutputColumn {
rf.machine.tableoidCol[rf.machine.rowIdx] = int64(rf.table.desc.GetID())
rf.machine.tableoidCol.Set(rf.machine.rowIdx, tree.NewDOid(tree.DInt(rf.table.desc.GetID())))
}

// We're finished with a row. Bump the row index, fill the row in with
Expand Down
12 changes: 8 additions & 4 deletions pkg/sql/delegate/show_partitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (d *delegator) delegateShowPartitions(n *tree.ShowPartitions) (tree.Stateme

// We use the raw_config_sql from the partition_lookup result to get the
// official zone config for the partition, and use the full_config_sql from the zones table
// which is the result of looking up the partition's inherited zone configuraion.
// which is the result of looking up the partition's inherited zone configuration.
const showTablePartitionsQuery = `
SELECT
tables.database_name,
Expand All @@ -65,7 +65,9 @@ func (d *delegator) delegateShowPartitions(n *tree.ShowPartitions) (tree.Stateme
AND partition_lookup.index_name = table_indexes.index_name
AND partition_lookup.partition_name = partitions.name
WHERE
tables.name = %[1]s AND tables.database_name = %[2]s;
tables.name = %[1]s AND tables.database_name = %[2]s
ORDER BY
1, 2, 3, 4, 5, 6, 7, 8, 9;
`
return parse(fmt.Sprintf(showTablePartitionsQuery,
lex.EscapeSQLString(resName.Table()),
Expand Down Expand Up @@ -100,7 +102,7 @@ func (d *delegator) delegateShowPartitions(n *tree.ShowPartitions) (tree.Stateme
WHERE
tables.database_name = %[2]s
ORDER BY
tables.name, partitions.name;
tables.name, partitions.name, 1, 4, 5, 6, 7, 8, 9;
`
// Note: n.Database.String() != string(n.Database)
return parse(fmt.Sprintf(showDatabasePartitionsQuery, n.Database.String(), lex.EscapeSQLString(string(n.Database))))
Expand Down Expand Up @@ -158,7 +160,9 @@ func (d *delegator) delegateShowPartitions(n *tree.ShowPartitions) (tree.Stateme
AND partition_lookup.index_name = table_indexes.index_name
AND partition_lookup.partition_name = partitions.name
WHERE
table_indexes.index_name = %[1]s AND tables.name = %[2]s;
table_indexes.index_name = %[1]s AND tables.name = %[2]s
ORDER BY
1, 2, 3, 4, 5, 6, 7, 8, 9;
`
return parse(fmt.Sprintf(showIndexPartitionsQuery,
lex.EscapeSQLString(n.Index.Index.String()),
Expand Down
Loading

0 comments on commit b7ef888

Please sign in to comment.