1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
# name: test/sql/window/test_fill_orderby.test
# description: Test Fill function with secondary orderings
# group: [window]
#
# Error checks
#
# Only one ordering allowed
statement error
select fill(i order by 10-i, i * i) over (order by i) from range(3) tbl(i);
----
FILL functions must have only one ORDER BY expression
# Argument must be numeric
statement error
select fill(i::VARCHAR order by i) over (order by i) from range(3) tbl(i);
----
FILL argument must support subtraction
# Ordering must be numeric
statement error
select fill(i order by i::VARCHAR) over (order by i) from range(3) tbl(i);
----
FILL ordering must support subtraction
#
# Simple interpolation coverage tests
#
loop nulled 0 5
query III
with source as (
select
i,
i * 3 % 5 as permuted,
if(permuted = ${nulled}, NULL, permuted) as missing
from range(5) tbl(i)
)
select
i,
permuted,
fill(missing order by permuted) over (order by i) as filled
from source
qualify filled <> permuted
----
endloop
# Single values
query III
with source as (
select
i,
i * 3 % 5 as permuted,
if(permuted > 0, NULL, permuted) as missing
from range(5) tbl(i)
)
select
i,
permuted,
fill(missing order by permuted) over (order by i) as filled
from source
qualify filled <> permuted
----
1 3 0
2 1 0
3 4 0
4 2 0
# No values in partition
query III
with source as (
select
i,
i * 5 % 11 as permuted,
if(permuted < 6, NULL, permuted) as missing
from range(11) tbl(i)
)
select
i,
permuted,
fill(missing order by permuted) over (partition by permuted // 5 order by i) as filled
from source
qualify filled is distinct from permuted
order by i
----
0 0 NULL
3 4 NULL
5 3 NULL
7 2 NULL
9 1 NULL
# No values in partition chunk
statement ok
with null_chunk as (
select
1 as p,
s,
NULL::DOUBLE as v,
from range(2050) tbl(s)
union all
select
2 as p,
s,
s::DOUBLE as v,
from range(16) tbl(s)
)
select
p,
s,
fill(v order by -s) over(partition by p order by s) as f,
from null_chunk
# Order by duplication optimisation
query III
with null_chunk as (
select
1 as p,
s,
NULL::DOUBLE as v,
from range(8) tbl(s)
union all
select
2 as p,
s,
s::DOUBLE as v,
from range(8) tbl(s)
)
select
p,
s,
fill(v order by s) over(partition by p order by s) as f,
from null_chunk
order by all
----
1 0 NULL
1 1 NULL
1 2 NULL
1 3 NULL
1 4 NULL
1 5 NULL
1 6 NULL
1 7 NULL
2 0 0.0
2 1 1.0
2 2 2.0
2 3 3.0
2 4 4.0
2 5 5.0
2 6 6.0
2 7 7.0
# Outside valid sort values
query III
with source as (
select
i,
i * 5 % 11 as permuted,
if(permuted = 2, NULL, permuted) as missing,
if(permuted < 4, NULL, permuted) as unsorted,
from range(11) tbl(i)
)
select
i,
permuted,
fill(missing order by unsorted) over (order by i) as filled
from source
qualify filled is distinct from permuted
order by i
----
7 2 NULL
# NULL sort key coverage
query III
with source as (
select
i,
(i + 1) * 3 % 5 as permuted,
if(permuted = 0, NULL, permuted) as missing
from (
from range(5) tbl(i)
union all
select NULL::INTEGER as i
) t(i)
)
select
i,
permuted,
fill(missing order by permuted asc nulls first) over (order by i) as filled
from source
qualify filled is distinct from permuted
----
query III
with source as (
select
i,
(i + 1) * 3 % 5 as permuted,
if(permuted = 4, NULL, permuted) as missing
from (
from range(5) tbl(i)
union all
select NULL::INTEGER as i
) t(i)
)
select
i,
permuted,
fill(missing order by permuted asc nulls last) over (order by i) as filled
from source
qualify filled is distinct from permuted
----
query III
select
i,
permuted,
fill(missing order by permuted asc nulls last) over (order by i) as filled
from (values
(0, 1, NULL),
(1, NULL, 0)
) source(i, missing, permuted)
order by i
----
0 NULL 1
1 0 NULL
query III
select
i,
permuted,
fill(missing order by permuted asc nulls first) over (order by i) as filled
from (values
(0, NULL, 2),
(1, 0, NULL),
(2, 1, 1),
) source(i, missing, permuted)
order by i
----
0 2 1
1 NULL 0
2 1 1
#
# Unusable values
#
# If we use an unsuable value, the interpolation will produce strange values
# Infinity/NaN
foreach ordertype float double
foreach unusable 'infinity' '-infinity' 'NaN'
loop nullable 0 5
query II
with source as (
select
i,
(i + 1) * 3 % 5 as permuted,
if(permuted = ${nullable}, NULL, permuted) as missing
from (
select range::${ordertype} as i from range(5)
union all
select ${unusable}::${ordertype} as i
) t(i)
)
select
i,
fill(missing order by permuted) over (order by i) as filled
from source
qualify filled is distinct from permuted
order by i
----
endloop
endloop
endloop
# Temporal infinities
foreach ordertype date timestamp
foreach unusable 'infinity' '-infinity'
loop nullable 0 5
query II
with source as (
(
select
('2025-01-01'::DATE + INTERVAL (i) DAY)::${ordertype} as t,
('2025-01-01'::DATE + INTERVAL ((i + 1) * 3 % 5) DAY)::${ordertype} as permuted,
if(i = ${nullable}, NULL, permuted) as missing
from range(5) tbl(i)
)
union all
(
select
${unusable}::${ordertype} as t,
t as permuted,
permuted as missing
)
)
select
t,
fill(missing order by permuted) over (order by t) as filled
from source
qualify filled is distinct from permuted
order by t
----
endloop
endloop
endloop
|