|
| 1 | +exec-ddl |
| 2 | +CREATE TABLE t ( |
| 3 | + k INT PRIMARY KEY, |
| 4 | + i INT, |
| 5 | + s STRING, |
| 6 | + j JSONB, |
| 7 | + INVERTED INDEX isj (i, s, j) |
| 8 | +) |
| 9 | +---- |
| 10 | + |
| 11 | +exec-ddl |
| 12 | +ALTER TABLE t INJECT STATISTICS '[ |
| 13 | + { |
| 14 | + "columns": ["i"], |
| 15 | + "created_at": "2018年01月01日 1:00:00.00000+00:00", |
| 16 | + "row_count": 2000, |
| 17 | + "distinct_count": 41, |
| 18 | + "null_count": 0 |
| 19 | + }, |
| 20 | + { |
| 21 | + "columns": ["s"], |
| 22 | + "created_at": "2018年01月01日 1:00:00.00000+00:00", |
| 23 | + "row_count": 2000, |
| 24 | + "distinct_count": 40, |
| 25 | + "null_count": 100, |
| 26 | + "histo_col_type": "string", |
| 27 | + "histo_buckets": [ |
| 28 | + {"num_eq": 0, "num_range": 0, "distinct_range": 0, "upper_bound": "apple"}, |
| 29 | + {"num_eq": 100, "num_range": 200, "distinct_range": 9, "upper_bound": "banana"}, |
| 30 | + {"num_eq": 100, "num_range": 300, "distinct_range": 9, "upper_bound": "cherry"}, |
| 31 | + {"num_eq": 200, "num_range": 400, "distinct_range": 9, "upper_bound": "mango"}, |
| 32 | + {"num_eq": 200, "num_range": 400, "distinct_range": 9, "upper_bound": "pineapple"} |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "columns": ["j"], |
| 37 | + "created_at": "2018年01月01日 1:00:00.00000+00:00", |
| 38 | + "row_count": 2000, |
| 39 | + "distinct_count": 10, |
| 40 | + "null_count": 0, |
| 41 | + "histo_col_type": "BYTES", |
| 42 | + "histo_buckets": [ |
| 43 | + {"distinct_range": 0, "num_eq": 10, "num_range": 0, "upper_bound": "\\x37000138"}, |
| 44 | + {"distinct_range": 0, "num_eq": 10, "num_range": 0, "upper_bound": "\\x37000139"}, |
| 45 | + {"distinct_range": 0, "num_eq": 990, "num_range": 0, "upper_bound": "\\x37000300012a0200"}, |
| 46 | + {"distinct_range": 0, "num_eq": 100, "num_range": 0, "upper_bound": "\\x37000300012a0400"}, |
| 47 | + {"distinct_range": 0, "num_eq": 10, "num_range": 0, "upper_bound": "\\x37000300012a0600"}, |
| 48 | + {"distinct_range": 0, "num_eq": 990, "num_range": 0, "upper_bound": "\\x3761000112620001"}, |
| 49 | + {"distinct_range": 0, "num_eq": 100, "num_range": 0, "upper_bound": "\\x3763000112640001"}, |
| 50 | + {"distinct_range": 0, "num_eq": 10, "num_range": 0, "upper_bound": "\\x3765000112660001"} |
| 51 | + ] |
| 52 | + } |
| 53 | +]' |
| 54 | +---- |
| 55 | + |
| 56 | +# Test a multi-column inverted index scan where the leading prefix column i |
| 57 | +# spans multiple values but the second prefix column s is held to a single |
| 58 | +# value. s's histogram should be used even though s is outside the exact prefix. |
| 59 | +opt |
| 60 | +SELECT k FROM t@isj WHERE i IN (200, 300) AND s = 'banana' AND j @> '{"a": "b"}' |
| 61 | +---- |
| 62 | +project |
| 63 | + ├── columns: k:1(int!null) |
| 64 | + ├── immutable |
| 65 | + ├── stats: [rows=1] |
| 66 | + ├── key: (1) |
| 67 | + └── scan t@isj,inverted |
| 68 | + ├── columns: k:1(int!null) |
| 69 | + ├── constraint: /2/3 |
| 70 | + │ ├── [/200/'banana' - /200/'banana'] |
| 71 | + │ └── [/300/'banana' - /300/'banana'] |
| 72 | + ├── inverted constraint: /7/1 |
| 73 | + │ └── spans: ["a"/"b", "a"/"b"] |
| 74 | + ├── flags: force-index=isj |
| 75 | + ├── stats: [rows=2.41463, distinct(2)=2, null(2)=0, distinct(3)=1, null(3)=0, distinct(7)=1, null(7)=0, distinct(3,7)=1, null(3,7)=0, distinct(2,3,7)=2, null(2,3,7)=0] |
| 76 | + │ histogram(3)= 0 2.4146 |
| 77 | + │ <--- 'banana' |
| 78 | + │ histogram(7)= 0 2.4146 0 0 |
| 79 | + │ <--- '\x3761000112620001' --- '\x3761000112620002' |
| 80 | + └── key: (1) |
| 81 | + |
| 82 | +exec-ddl |
| 83 | +CREATE TABLE rbr ( |
| 84 | + k INT PRIMARY KEY, |
| 85 | + s STRING, |
| 86 | + j JSONB, |
| 87 | + INVERTED INDEX sj (s, j) |
| 88 | +) LOCALITY REGIONAL BY ROW |
| 89 | +---- |
| 90 | + |
| 91 | +exec-ddl |
| 92 | +ALTER TABLE rbr INJECT STATISTICS '[ |
| 93 | + { |
| 94 | + "columns": ["crdb_region"], |
| 95 | + "created_at": "2018年01月01日 1:00:00.00000+00:00", |
| 96 | + "row_count": 2000, |
| 97 | + "distinct_count": 3, |
| 98 | + "null_count": 0 |
| 99 | + }, |
| 100 | + { |
| 101 | + "columns": ["s"], |
| 102 | + "created_at": "2018年01月01日 1:00:00.00000+00:00", |
| 103 | + "row_count": 2000, |
| 104 | + "distinct_count": 40, |
| 105 | + "null_count": 100, |
| 106 | + "histo_col_type": "string", |
| 107 | + "histo_buckets": [ |
| 108 | + {"num_eq": 0, "num_range": 0, "distinct_range": 0, "upper_bound": "apple"}, |
| 109 | + {"num_eq": 100, "num_range": 200, "distinct_range": 9, "upper_bound": "banana"}, |
| 110 | + {"num_eq": 100, "num_range": 300, "distinct_range": 9, "upper_bound": "cherry"}, |
| 111 | + {"num_eq": 200, "num_range": 400, "distinct_range": 9, "upper_bound": "mango"}, |
| 112 | + {"num_eq": 200, "num_range": 400, "distinct_range": 9, "upper_bound": "pineapple"} |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "columns": ["j"], |
| 117 | + "created_at": "2018年01月01日 1:00:00.00000+00:00", |
| 118 | + "row_count": 2000, |
| 119 | + "distinct_count": 10, |
| 120 | + "null_count": 0, |
| 121 | + "histo_col_type": "BYTES", |
| 122 | + "histo_buckets": [ |
| 123 | + {"distinct_range": 0, "num_eq": 10, "num_range": 0, "upper_bound": "\\x37000138"}, |
| 124 | + {"distinct_range": 0, "num_eq": 10, "num_range": 0, "upper_bound": "\\x37000139"}, |
| 125 | + {"distinct_range": 0, "num_eq": 990, "num_range": 0, "upper_bound": "\\x37000300012a0200"}, |
| 126 | + {"distinct_range": 0, "num_eq": 100, "num_range": 0, "upper_bound": "\\x37000300012a0400"}, |
| 127 | + {"distinct_range": 0, "num_eq": 10, "num_range": 0, "upper_bound": "\\x37000300012a0600"}, |
| 128 | + {"distinct_range": 0, "num_eq": 990, "num_range": 0, "upper_bound": "\\x3761000112620001"}, |
| 129 | + {"distinct_range": 0, "num_eq": 100, "num_range": 0, "upper_bound": "\\x3763000112640001"}, |
| 130 | + {"distinct_range": 0, "num_eq": 10, "num_range": 0, "upper_bound": "\\x3765000112660001"} |
| 131 | + ] |
| 132 | + } |
| 133 | +]' |
| 134 | +---- |
| 135 | + |
| 136 | +# Same scenario on a REGIONAL BY ROW table where the query does not constrain |
| 137 | +# crdb_region. The optimizer enumerates crdb_region into one span per region, all |
| 138 | +# pinning s='banana', so crdb_region varies while s is a constant column outside |
| 139 | +# the exact prefix. |
| 140 | +opt |
| 141 | +SELECT k FROM rbr@sj WHERE s = 'banana' AND j @> '{"a": "b"}' |
| 142 | +---- |
| 143 | +project |
| 144 | + ├── columns: k:1(int!null) |
| 145 | + ├── immutable |
| 146 | + ├── stats: [rows=11.1111] |
| 147 | + ├── key: (1) |
| 148 | + └── scan rbr@sj,inverted |
| 149 | + ├── columns: k:1(int!null) crdb_region:4(string!null) |
| 150 | + ├── constraint: /4/2 |
| 151 | + │ ├── [/'central'/'banana' - /'central'/'banana'] |
| 152 | + │ ├── [/'east'/'banana' - /'east'/'banana'] |
| 153 | + │ └── [/'west'/'banana' - /'west'/'banana'] |
| 154 | + ├── inverted constraint: /7/1 |
| 155 | + │ └── spans: ["a"/"b", "a"/"b"] |
| 156 | + ├── flags: force-index=sj |
| 157 | + ├── stats: [rows=49.5, distinct(2)=1, null(2)=0, distinct(4)=3, null(4)=0, distinct(7)=1, null(7)=0, distinct(2,4,7)=3, null(2,4,7)=0] |
| 158 | + │ histogram(2)= 0 49.5 |
| 159 | + │ <--- 'banana' |
| 160 | + │ histogram(7)= 0 49.5 0 0 |
| 161 | + │ <--- '\x3761000112620001' --- '\x3761000112620002' |
| 162 | + ├── key: (1) |
| 163 | + └── fd: (1)-->(4) |
0 commit comments