From ceb5cd52da7c2d6758144da1a18a188162d4cab0 Mon Sep 17 00:00:00 2001
From: Rahul Aggarwal
Date: Thu, 20 Jul 2023 16:12:26 -0400
Subject: [PATCH] builtins: `crdb_internal.sstable_metrics` fixes
This pr has the following fixes for the builtin `crdb_internal.sstable_metrics`
1. Remove the ',' from `node_id`
2. Change `approximate_span_bytes` to be a `uint64` instead of `[]byte`
3. Convert the `MVCCTimeInterval` user property to be human readable.
Informs: #102604
Release-note: None
---
docs/generated/sql/functions.md | 2 +-
pkg/sql/sem/builtins/fixed_oids.go | 2 +-
pkg/sql/sem/builtins/generator_builtins.go | 6 +++---
pkg/sql/sem/builtins/generator_builtins_test.go | 6 ++++--
pkg/storage/enginepb/rocksdb.proto | 2 +-
pkg/storage/pebble.go | 7 +++++--
6 files changed, 15 insertions(+), 10 deletions(-)
diff --git a/docs/generated/sql/functions.md b/docs/generated/sql/functions.md
index 6a9f9758cc97..13d7eb6fde2c 100644
--- a/docs/generated/sql/functions.md
+++ b/docs/generated/sql/functions.md
@@ -3310,7 +3310,7 @@ table. Returns an error if validation fails.
crdb_internal.sstable_metrics(node_id: int, store_id: int, start_key: bytes, end_key: bytes) → tuple{int AS node_id,, int AS store_id, int AS level, int AS file_num, bytes AS approximate_span_bytes, jsonb AS metrics} | Returns statistics for the sstables containing keys in the range start_key and end_key for the provided node id.
+crdb_internal.sstable_metrics(node_id: int, store_id: int, start_key: bytes, end_key: bytes) → tuple{int AS node_id, int AS store_id, int AS level, int AS file_num, int AS approximate_span_bytes, jsonb AS metrics} | Returns statistics for the sstables containing keys in the range start_key and end_key for the provided node id.
| Stable |
crdb_internal.table_span(table_id: int) → bytes[] | This function returns the span that contains the keys for the given table.
| Leakproof |
diff --git a/pkg/sql/sem/builtins/fixed_oids.go b/pkg/sql/sem/builtins/fixed_oids.go
index 1238ff66e360..450817dc988c 100644
--- a/pkg/sql/sem/builtins/fixed_oids.go
+++ b/pkg/sql/sem/builtins/fixed_oids.go
@@ -2424,7 +2424,7 @@ var builtinOidsArray = []string{
2451: `st_asmvtgeom(geometry: geometry, bbox: box2d) -> geometry`,
2452: `crdb_internal.repaired_descriptor(descriptor: bytes, valid_descriptor_ids: int[], valid_job_ids: int[]) -> bytes`,
2453: `crdb_internal.reset_activity_tables() -> bool`,
- 2454: `crdb_internal.sstable_metrics(node_id: int, store_id: int, start_key: bytes, end_key: bytes) -> tuple{int AS node_id,, int AS store_id, int AS level, int AS file_num, bytes AS approximate_span_bytes, jsonb AS metrics}`,
+ 2454: `crdb_internal.sstable_metrics(node_id: int, store_id: int, start_key: bytes, end_key: bytes) -> tuple{int AS node_id, int AS store_id, int AS level, int AS file_num, int AS approximate_span_bytes, jsonb AS metrics}`,
2455: `crdb_internal.repair_catalog_corruption(descriptor_id: int, corruption: string) -> bool`,
2456: `crdb_internal.merge_aggregated_stmt_metadata(input: jsonb[]) -> jsonb`,
2457: `crdb_internal.request_job_execution_details(jobID: int) -> bool`,
diff --git a/pkg/sql/sem/builtins/generator_builtins.go b/pkg/sql/sem/builtins/generator_builtins.go
index e88d66d3dee3..3122e63f2ed1 100644
--- a/pkg/sql/sem/builtins/generator_builtins.go
+++ b/pkg/sql/sem/builtins/generator_builtins.go
@@ -3125,8 +3125,8 @@ func (tssi *tableSpanStatsIterator) ResolvedType() *types.T {
}
var tableMetricsGeneratorType = types.MakeLabeledTuple(
- []*types.T{types.Int, types.Int, types.Int, types.Int, types.Bytes, types.Json},
- []string{"node_id,", "store_id", "level", "file_num", "approximate_span_bytes", "metrics"},
+ []*types.T{types.Int, types.Int, types.Int, types.Int, types.Int, types.Json},
+ []string{"node_id", "store_id", "level", "file_num", "approximate_span_bytes", "metrics"},
)
// tableMetricsIterator implements tree.ValueGenerator; it returns a set of
@@ -3186,7 +3186,7 @@ func (tmi *tableMetricsIterator) Values() (tree.Datums, error) {
tree.NewDInt(tree.DInt(tmi.storeID)),
tree.NewDInt(tree.DInt(metricsInfo.Level)),
tree.NewDInt(tree.DInt(metricsInfo.TableID)),
- tree.NewDBytes(tree.DBytes(metricsInfo.ApproximateSpanBytes)),
+ tree.NewDInt(tree.DInt(metricsInfo.ApproximateSpanBytes)),
metricsJson,
}, nil
}
diff --git a/pkg/sql/sem/builtins/generator_builtins_test.go b/pkg/sql/sem/builtins/generator_builtins_test.go
index 64febcf6e721..fedaf4e29b8c 100644
--- a/pkg/sql/sem/builtins/generator_builtins_test.go
+++ b/pkg/sql/sem/builtins/generator_builtins_test.go
@@ -90,7 +90,7 @@ func TestGetSSTableMetricsMultiNode(t *testing.T) {
var storeID int
var level int
var fileNum int
- var approximateSpanBytes []byte
+ var approximateSpanBytes uint64
var metrics []byte
for idx, id := range tc.NodeIDs() {
@@ -127,6 +127,7 @@ func TestGetSSTableMetricsMultiNode(t *testing.T) {
require.Equal(t, nodeID, nodeIDArg)
require.Equal(t, storeID, storeIDArg)
require.NotEqual(t, fileNum, 0)
+ require.NotEqual(t, approximateSpanBytes, 0)
count++
}
@@ -184,7 +185,7 @@ func TestGetSSTableMetricsSingleNode(t *testing.T) {
var storeID int
var level int
var fileNum int
- var approximateSpanBytes []byte
+ var approximateSpanBytes uint64
var metrics []byte
for rows.Next() {
@@ -193,6 +194,7 @@ func TestGetSSTableMetricsSingleNode(t *testing.T) {
require.Equal(t, nodeID, nodeIDArg)
require.Equal(t, storeID, storeIDArg)
require.NotEqual(t, fileNum, 0)
+ require.NotEqual(t, approximateSpanBytes, 0)
count++
}
require.Equal(t, 1, count)
diff --git a/pkg/storage/enginepb/rocksdb.proto b/pkg/storage/enginepb/rocksdb.proto
index cb51ff2b1797..a5ef99d7ae2a 100644
--- a/pkg/storage/enginepb/rocksdb.proto
+++ b/pkg/storage/enginepb/rocksdb.proto
@@ -23,7 +23,7 @@ message SSTableMetricsInfo {
// table_info_json contains sstable properties, encoded as JSON
bytes table_info_json = 3 [(gogoproto.customname) = "TableInfoJSON"];
// approximate_span_bytes represents the total number of bytes that overlap the given keyspan
- bytes approximate_span_bytes = 4 [(gogoproto.customname) = "ApproximateSpanBytes"];
+ uint64 approximate_span_bytes = 4 [(gogoproto.customname) = "ApproximateSpanBytes"];
}
// SSTUserProperties contains the user-added properties of a single sstable.
diff --git a/pkg/storage/pebble.go b/pkg/storage/pebble.go
index 620bd8efc899..5f7f2a1adda5 100644
--- a/pkg/storage/pebble.go
+++ b/pkg/storage/pebble.go
@@ -2032,8 +2032,11 @@ func (p *Pebble) GetTableMetrics(start, end roachpb.Key) ([]enginepb.SSTableMetr
}
tableID := sstableInfo.TableInfo.FileNum
- approximateSpanBytes := sstableInfo.Properties.UserProperties["approximate-span-bytes"]
- metricsInfo = append(metricsInfo, enginepb.SSTableMetricsInfo{TableID: uint64(tableID), Level: int32(level), ApproximateSpanBytes: []byte(approximateSpanBytes), TableInfoJSON: marshalTableInfo})
+ approximateSpanBytes, err := strconv.ParseUint(sstableInfo.Properties.UserProperties["approximate-span-bytes"], 10, 64)
+ if err != nil {
+ return []enginepb.SSTableMetricsInfo{}, err
+ }
+ metricsInfo = append(metricsInfo, enginepb.SSTableMetricsInfo{TableID: uint64(tableID), Level: int32(level), ApproximateSpanBytes: approximateSpanBytes, TableInfoJSON: marshalTableInfo})
}
}
return metricsInfo, nil
|