vec_comparators.eg.go 14.9 KB
Newer Older
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
// Code generated by execgen; DO NOT EDIT.
// Copyright 2019 The Cockroach Authors.
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package colexec

import (
	"bytes"
	"fmt"
	"math"

	"github.com/cockroachdb/cockroach/pkg/col/coldata"
	"github.com/cockroachdb/cockroach/pkg/col/coldataext"
	"github.com/cockroachdb/cockroach/pkg/col/typeconv"
	"github.com/cockroachdb/cockroach/pkg/sql/colexecbase/colexecerror"
	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
	"github.com/cockroachdb/cockroach/pkg/sql/types"
)

// vecComparator is a helper for the ordered synchronizer. It stores multiple
// column vectors of a single type and facilitates comparing values between
// them.
type vecComparator interface {
	// compare compares values from two vectors. vecIdx is the index of the vector
	// and valIdx is the index of the value in that vector to compare. Returns -1,
	// 0, or 1.
	compare(vecIdx1, vecIdx2 int, valIdx1, valIdx2 int) int

	// set sets the value of the vector at dstVecIdx at index dstValIdx to the value
	// at the vector at srcVecIdx at index srcValIdx.
	// NOTE: whenever set is used, the caller is responsible for updating the
	// memory accounts.
	set(srcVecIdx, dstVecIdx int, srcValIdx, dstValIdx int)

	// setVec updates the vector at idx.
	setVec(idx int, vec coldata.Vec)
}

type BoolVecComparator struct {
45
	vecs  []coldata.Bools
46 47 48 49 50 51 52 53 54 55 56 57 58
	nulls []*coldata.Nulls
}

func (c *BoolVecComparator) compare(vecIdx1, vecIdx2 int, valIdx1, valIdx2 int) int {
	n1 := c.nulls[vecIdx1].MaybeHasNulls() && c.nulls[vecIdx1].NullAt(valIdx1)
	n2 := c.nulls[vecIdx2].MaybeHasNulls() && c.nulls[vecIdx2].NullAt(valIdx2)
	if n1 && n2 {
		return 0
	} else if n1 {
		return -1
	} else if n2 {
		return 1
	}
59 60
	left := c.vecs[vecIdx1].Get(valIdx1)  //gcassert:inline
	right := c.vecs[vecIdx2].Get(valIdx2) //gcassert:inline
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	var cmp int

	if !left && right {
		cmp = -1
	} else if left && !right {
		cmp = 1
	} else {
		cmp = 0
	}

	return cmp
}

func (c *BoolVecComparator) setVec(idx int, vec coldata.Vec) {
	c.vecs[idx] = vec.Bool()
	c.nulls[idx] = vec.Nulls()
}

func (c *BoolVecComparator) set(srcVecIdx, dstVecIdx int, srcIdx, dstIdx int) {
	if c.nulls[srcVecIdx].MaybeHasNulls() && c.nulls[srcVecIdx].NullAt(srcIdx) {
		c.nulls[dstVecIdx].SetNull(dstIdx)
	} else {
		c.nulls[dstVecIdx].UnsetNull(dstIdx)
84
		v := c.vecs[srcVecIdx].Get(srcIdx) //gcassert:inline
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
		c.vecs[dstVecIdx][dstIdx] = v
	}
}

type BytesVecComparator struct {
	vecs  []*coldata.Bytes
	nulls []*coldata.Nulls
}

func (c *BytesVecComparator) compare(vecIdx1, vecIdx2 int, valIdx1, valIdx2 int) int {
	n1 := c.nulls[vecIdx1].MaybeHasNulls() && c.nulls[vecIdx1].NullAt(valIdx1)
	n2 := c.nulls[vecIdx2].MaybeHasNulls() && c.nulls[vecIdx2].NullAt(valIdx2)
	if n1 && n2 {
		return 0
	} else if n1 {
		return -1
	} else if n2 {
		return 1
	}
104 105
	left := c.vecs[vecIdx1].Get(valIdx1)  //gcassert:inline
	right := c.vecs[vecIdx2].Get(valIdx2) //gcassert:inline
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
	var cmp int
	cmp = bytes.Compare(left, right)
	return cmp
}

func (c *BytesVecComparator) setVec(idx int, vec coldata.Vec) {
	c.vecs[idx] = vec.Bytes()
	c.nulls[idx] = vec.Nulls()
}

func (c *BytesVecComparator) set(srcVecIdx, dstVecIdx int, srcIdx, dstIdx int) {
	if c.nulls[srcVecIdx].MaybeHasNulls() && c.nulls[srcVecIdx].NullAt(srcIdx) {
		c.nulls[dstVecIdx].SetNull(dstIdx)
	} else {
		c.nulls[dstVecIdx].UnsetNull(dstIdx)
		// Since flat Bytes cannot be set at arbitrary indices (data needs to be
		// moved around), we use CopySlice to accept the performance hit.
		// Specifically, this is a performance hit because we are overwriting the
		// variable number of bytes in `dstVecIdx`, so we will have to either shift
		// the bytes after that element left or right, depending on how long the
		// source bytes slice is. Refer to the CopySlice comment for an example.
		c.vecs[dstVecIdx].CopySlice(c.vecs[srcVecIdx], dstIdx, srcIdx, srcIdx+1)
	}
}

type DecimalVecComparator struct {
132
	vecs  []coldata.Decimals
133 134 135 136 137 138 139 140 141 142 143 144 145
	nulls []*coldata.Nulls
}

func (c *DecimalVecComparator) compare(vecIdx1, vecIdx2 int, valIdx1, valIdx2 int) int {
	n1 := c.nulls[vecIdx1].MaybeHasNulls() && c.nulls[vecIdx1].NullAt(valIdx1)
	n2 := c.nulls[vecIdx2].MaybeHasNulls() && c.nulls[vecIdx2].NullAt(valIdx2)
	if n1 && n2 {
		return 0
	} else if n1 {
		return -1
	} else if n2 {
		return 1
	}
146 147
	left := c.vecs[vecIdx1].Get(valIdx1)  //gcassert:inline
	right := c.vecs[vecIdx2].Get(valIdx2) //gcassert:inline
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	var cmp int
	cmp = tree.CompareDecimals(&left, &right)
	return cmp
}

func (c *DecimalVecComparator) setVec(idx int, vec coldata.Vec) {
	c.vecs[idx] = vec.Decimal()
	c.nulls[idx] = vec.Nulls()
}

func (c *DecimalVecComparator) set(srcVecIdx, dstVecIdx int, srcIdx, dstIdx int) {
	if c.nulls[srcVecIdx].MaybeHasNulls() && c.nulls[srcVecIdx].NullAt(srcIdx) {
		c.nulls[dstVecIdx].SetNull(dstIdx)
	} else {
		c.nulls[dstVecIdx].UnsetNull(dstIdx)
163
		v := c.vecs[srcVecIdx].Get(srcIdx) //gcassert:inline
164 165 166 167 168
		c.vecs[dstVecIdx][dstIdx].Set(&v)
	}
}

type Int16VecComparator struct {
169
	vecs  []coldata.Int16s
170 171 172 173 174 175 176 177 178 179 180 181 182
	nulls []*coldata.Nulls
}

func (c *Int16VecComparator) compare(vecIdx1, vecIdx2 int, valIdx1, valIdx2 int) int {
	n1 := c.nulls[vecIdx1].MaybeHasNulls() && c.nulls[vecIdx1].NullAt(valIdx1)
	n2 := c.nulls[vecIdx2].MaybeHasNulls() && c.nulls[vecIdx2].NullAt(valIdx2)
	if n1 && n2 {
		return 0
	} else if n1 {
		return -1
	} else if n2 {
		return 1
	}
183 184
	left := c.vecs[vecIdx1].Get(valIdx1)  //gcassert:inline
	right := c.vecs[vecIdx2].Get(valIdx2) //gcassert:inline
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
	var cmp int

	{
		a, b := int64(left), int64(right)
		if a < b {
			cmp = -1
		} else if a > b {
			cmp = 1
		} else {
			cmp = 0
		}
	}

	return cmp
}

func (c *Int16VecComparator) setVec(idx int, vec coldata.Vec) {
	c.vecs[idx] = vec.Int16()
	c.nulls[idx] = vec.Nulls()
}

func (c *Int16VecComparator) set(srcVecIdx, dstVecIdx int, srcIdx, dstIdx int) {
	if c.nulls[srcVecIdx].MaybeHasNulls() && c.nulls[srcVecIdx].NullAt(srcIdx) {
		c.nulls[dstVecIdx].SetNull(dstIdx)
	} else {
		c.nulls[dstVecIdx].UnsetNull(dstIdx)
211
		v := c.vecs[srcVecIdx].Get(srcIdx) //gcassert:inline
212 213 214 215 216
		c.vecs[dstVecIdx][dstIdx] = v
	}
}

type Int32VecComparator struct {
217
	vecs  []coldata.Int32s
218 219 220 221 222 223 224 225 226 227 228 229 230
	nulls []*coldata.Nulls
}

func (c *Int32VecComparator) compare(vecIdx1, vecIdx2 int, valIdx1, valIdx2 int) int {
	n1 := c.nulls[vecIdx1].MaybeHasNulls() && c.nulls[vecIdx1].NullAt(valIdx1)
	n2 := c.nulls[vecIdx2].MaybeHasNulls() && c.nulls[vecIdx2].NullAt(valIdx2)
	if n1 && n2 {
		return 0
	} else if n1 {
		return -1
	} else if n2 {
		return 1
	}
231 232
	left := c.vecs[vecIdx1].Get(valIdx1)  //gcassert:inline
	right := c.vecs[vecIdx2].Get(valIdx2) //gcassert:inline
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
	var cmp int

	{
		a, b := int64(left), int64(right)
		if a < b {
			cmp = -1
		} else if a > b {
			cmp = 1
		} else {
			cmp = 0
		}
	}

	return cmp
}

func (c *Int32VecComparator) setVec(idx int, vec coldata.Vec) {
	c.vecs[idx] = vec.Int32()
	c.nulls[idx] = vec.Nulls()
}

func (c *Int32VecComparator) set(srcVecIdx, dstVecIdx int, srcIdx, dstIdx int) {
	if c.nulls[srcVecIdx].MaybeHasNulls() && c.nulls[srcVecIdx].NullAt(srcIdx) {
		c.nulls[dstVecIdx].SetNull(dstIdx)
	} else {
		c.nulls[dstVecIdx].UnsetNull(dstIdx)
259
		v := c.vecs[srcVecIdx].Get(srcIdx) //gcassert:inline
260 261 262 263 264
		c.vecs[dstVecIdx][dstIdx] = v
	}
}

type Int64VecComparator struct {
265
	vecs  []coldata.Int64s
266 267 268 269 270 271 272 273 274 275 276 277 278
	nulls []*coldata.Nulls
}

func (c *Int64VecComparator) compare(vecIdx1, vecIdx2 int, valIdx1, valIdx2 int) int {
	n1 := c.nulls[vecIdx1].MaybeHasNulls() && c.nulls[vecIdx1].NullAt(valIdx1)
	n2 := c.nulls[vecIdx2].MaybeHasNulls() && c.nulls[vecIdx2].NullAt(valIdx2)
	if n1 && n2 {
		return 0
	} else if n1 {
		return -1
	} else if n2 {
		return 1
	}
279 280
	left := c.vecs[vecIdx1].Get(valIdx1)  //gcassert:inline
	right := c.vecs[vecIdx2].Get(valIdx2) //gcassert:inline
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
	var cmp int

	{
		a, b := int64(left), int64(right)
		if a < b {
			cmp = -1
		} else if a > b {
			cmp = 1
		} else {
			cmp = 0
		}
	}

	return cmp
}

func (c *Int64VecComparator) setVec(idx int, vec coldata.Vec) {
	c.vecs[idx] = vec.Int64()
	c.nulls[idx] = vec.Nulls()
}

func (c *Int64VecComparator) set(srcVecIdx, dstVecIdx int, srcIdx, dstIdx int) {
	if c.nulls[srcVecIdx].MaybeHasNulls() && c.nulls[srcVecIdx].NullAt(srcIdx) {
		c.nulls[dstVecIdx].SetNull(dstIdx)
	} else {
		c.nulls[dstVecIdx].UnsetNull(dstIdx)
307
		v := c.vecs[srcVecIdx].Get(srcIdx) //gcassert:inline
308 309 310 311 312
		c.vecs[dstVecIdx][dstIdx] = v
	}
}

type Float64VecComparator struct {
313
	vecs  []coldata.Float64s
314 315 316 317 318 319 320 321 322 323 324 325 326
	nulls []*coldata.Nulls
}

func (c *Float64VecComparator) compare(vecIdx1, vecIdx2 int, valIdx1, valIdx2 int) int {
	n1 := c.nulls[vecIdx1].MaybeHasNulls() && c.nulls[vecIdx1].NullAt(valIdx1)
	n2 := c.nulls[vecIdx2].MaybeHasNulls() && c.nulls[vecIdx2].NullAt(valIdx2)
	if n1 && n2 {
		return 0
	} else if n1 {
		return -1
	} else if n2 {
		return 1
	}
327 328
	left := c.vecs[vecIdx1].Get(valIdx1)  //gcassert:inline
	right := c.vecs[vecIdx2].Get(valIdx2) //gcassert:inline
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	var cmp int

	{
		a, b := float64(left), float64(right)
		if a < b {
			cmp = -1
		} else if a > b {
			cmp = 1
		} else if a == b {
			cmp = 0
		} else if math.IsNaN(a) {
			if math.IsNaN(b) {
				cmp = 0
			} else {
				cmp = -1
			}
		} else {
			cmp = 1
		}
	}

	return cmp
}

func (c *Float64VecComparator) setVec(idx int, vec coldata.Vec) {
	c.vecs[idx] = vec.Float64()
	c.nulls[idx] = vec.Nulls()
}

func (c *Float64VecComparator) set(srcVecIdx, dstVecIdx int, srcIdx, dstIdx int) {
	if c.nulls[srcVecIdx].MaybeHasNulls() && c.nulls[srcVecIdx].NullAt(srcIdx) {
		c.nulls[dstVecIdx].SetNull(dstIdx)
	} else {
		c.nulls[dstVecIdx].UnsetNull(dstIdx)
363
		v := c.vecs[srcVecIdx].Get(srcIdx) //gcassert:inline
364 365 366 367 368
		c.vecs[dstVecIdx][dstIdx] = v
	}
}

type TimestampVecComparator struct {
369
	vecs  []coldata.Times
370 371 372 373 374 375 376 377 378 379 380 381 382
	nulls []*coldata.Nulls
}

func (c *TimestampVecComparator) compare(vecIdx1, vecIdx2 int, valIdx1, valIdx2 int) int {
	n1 := c.nulls[vecIdx1].MaybeHasNulls() && c.nulls[vecIdx1].NullAt(valIdx1)
	n2 := c.nulls[vecIdx2].MaybeHasNulls() && c.nulls[vecIdx2].NullAt(valIdx2)
	if n1 && n2 {
		return 0
	} else if n1 {
		return -1
	} else if n2 {
		return 1
	}
383 384
	left := c.vecs[vecIdx1].Get(valIdx1)  //gcassert:inline
	right := c.vecs[vecIdx2].Get(valIdx2) //gcassert:inline
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	var cmp int

	if left.Before(right) {
		cmp = -1
	} else if right.Before(left) {
		cmp = 1
	} else {
		cmp = 0
	}
	return cmp
}

func (c *TimestampVecComparator) setVec(idx int, vec coldata.Vec) {
	c.vecs[idx] = vec.Timestamp()
	c.nulls[idx] = vec.Nulls()
}

func (c *TimestampVecComparator) set(srcVecIdx, dstVecIdx int, srcIdx, dstIdx int) {
	if c.nulls[srcVecIdx].MaybeHasNulls() && c.nulls[srcVecIdx].NullAt(srcIdx) {
		c.nulls[dstVecIdx].SetNull(dstIdx)
	} else {
		c.nulls[dstVecIdx].UnsetNull(dstIdx)
407
		v := c.vecs[srcVecIdx].Get(srcIdx) //gcassert:inline
408 409 410 411 412
		c.vecs[dstVecIdx][dstIdx] = v
	}
}

type IntervalVecComparator struct {
413
	vecs  []coldata.Durations
414 415 416 417 418 419 420 421 422 423 424 425 426
	nulls []*coldata.Nulls
}

func (c *IntervalVecComparator) compare(vecIdx1, vecIdx2 int, valIdx1, valIdx2 int) int {
	n1 := c.nulls[vecIdx1].MaybeHasNulls() && c.nulls[vecIdx1].NullAt(valIdx1)
	n2 := c.nulls[vecIdx2].MaybeHasNulls() && c.nulls[vecIdx2].NullAt(valIdx2)
	if n1 && n2 {
		return 0
	} else if n1 {
		return -1
	} else if n2 {
		return 1
	}
427 428
	left := c.vecs[vecIdx1].Get(valIdx1)  //gcassert:inline
	right := c.vecs[vecIdx2].Get(valIdx2) //gcassert:inline
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	var cmp int
	cmp = left.Compare(right)
	return cmp
}

func (c *IntervalVecComparator) setVec(idx int, vec coldata.Vec) {
	c.vecs[idx] = vec.Interval()
	c.nulls[idx] = vec.Nulls()
}

func (c *IntervalVecComparator) set(srcVecIdx, dstVecIdx int, srcIdx, dstIdx int) {
	if c.nulls[srcVecIdx].MaybeHasNulls() && c.nulls[srcVecIdx].NullAt(srcIdx) {
		c.nulls[dstVecIdx].SetNull(dstIdx)
	} else {
		c.nulls[dstVecIdx].UnsetNull(dstIdx)
444
		v := c.vecs[srcVecIdx].Get(srcIdx) //gcassert:inline
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
		c.vecs[dstVecIdx][dstIdx] = v
	}
}

type DatumVecComparator struct {
	vecs  []coldata.DatumVec
	nulls []*coldata.Nulls
}

func (c *DatumVecComparator) compare(vecIdx1, vecIdx2 int, valIdx1, valIdx2 int) int {
	n1 := c.nulls[vecIdx1].MaybeHasNulls() && c.nulls[vecIdx1].NullAt(valIdx1)
	n2 := c.nulls[vecIdx2].MaybeHasNulls() && c.nulls[vecIdx2].NullAt(valIdx2)
	if n1 && n2 {
		return 0
	} else if n1 {
		return -1
	} else if n2 {
		return 1
	}
	left := c.vecs[vecIdx1].Get(valIdx1)
	right := c.vecs[vecIdx2].Get(valIdx2)
	var cmp int

	cmp = left.(*coldataext.Datum).CompareDatum(c.vecs[vecIdx1], right)

	return cmp
}

func (c *DatumVecComparator) setVec(idx int, vec coldata.Vec) {
	c.vecs[idx] = vec.Datum()
	c.nulls[idx] = vec.Nulls()
}

func (c *DatumVecComparator) set(srcVecIdx, dstVecIdx int, srcIdx, dstIdx int) {
	if c.nulls[srcVecIdx].MaybeHasNulls() && c.nulls[srcVecIdx].NullAt(srcIdx) {
		c.nulls[dstVecIdx].SetNull(dstIdx)
	} else {
		c.nulls[dstVecIdx].UnsetNull(dstIdx)
		v := c.vecs[srcVecIdx].Get(srcIdx)
		c.vecs[dstVecIdx].Set(dstIdx, v)
	}
}

func GetVecComparator(t *types.T, numVecs int) vecComparator {
	switch typeconv.TypeFamilyToCanonicalTypeFamily(t.Family()) {
	case types.BoolFamily:
		switch t.Width() {
		case -1:
		default:
			return &BoolVecComparator{
495
				vecs:  make([]coldata.Bools, numVecs),
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
				nulls: make([]*coldata.Nulls, numVecs),
			}
		}
	case types.BytesFamily:
		switch t.Width() {
		case -1:
		default:
			return &BytesVecComparator{
				vecs:  make([]*coldata.Bytes, numVecs),
				nulls: make([]*coldata.Nulls, numVecs),
			}
		}
	case types.DecimalFamily:
		switch t.Width() {
		case -1:
		default:
			return &DecimalVecComparator{
513
				vecs:  make([]coldata.Decimals, numVecs),
514 515 516 517 518 519 520
				nulls: make([]*coldata.Nulls, numVecs),
			}
		}
	case types.IntFamily:
		switch t.Width() {
		case 16:
			return &Int16VecComparator{
521
				vecs:  make([]coldata.Int16s, numVecs),
522 523 524 525
				nulls: make([]*coldata.Nulls, numVecs),
			}
		case 32:
			return &Int32VecComparator{
526
				vecs:  make([]coldata.Int32s, numVecs),
527 528 529 530 531
				nulls: make([]*coldata.Nulls, numVecs),
			}
		case -1:
		default:
			return &Int64VecComparator{
532
				vecs:  make([]coldata.Int64s, numVecs),
533 534 535 536 537 538 539 540
				nulls: make([]*coldata.Nulls, numVecs),
			}
		}
	case types.FloatFamily:
		switch t.Width() {
		case -1:
		default:
			return &Float64VecComparator{
541
				vecs:  make([]coldata.Float64s, numVecs),
542 543 544 545 546 547 548 549
				nulls: make([]*coldata.Nulls, numVecs),
			}
		}
	case types.TimestampTZFamily:
		switch t.Width() {
		case -1:
		default:
			return &TimestampVecComparator{
550
				vecs:  make([]coldata.Times, numVecs),
551 552 553 554 555 556 557 558
				nulls: make([]*coldata.Nulls, numVecs),
			}
		}
	case types.IntervalFamily:
		switch t.Width() {
		case -1:
		default:
			return &IntervalVecComparator{
559
				vecs:  make([]coldata.Durations, numVecs),
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
				nulls: make([]*coldata.Nulls, numVecs),
			}
		}
	case typeconv.DatumVecCanonicalTypeFamily:
		switch t.Width() {
		case -1:
		default:
			return &DatumVecComparator{
				vecs:  make([]coldata.DatumVec, numVecs),
				nulls: make([]*coldata.Nulls, numVecs),
			}
		}
	}
	colexecerror.InternalError(fmt.Sprintf("unhandled type %s", t))
	// This code is unreachable, but the compiler cannot infer that.
	return nil
}