go-hep.org/x/hep@v0.38.1/groot/rsql/rsqldrv/expr_test.go (about) 1 // Copyright ©2019 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rsqldrv // import "go-hep.org/x/hep/groot/rsql/rsqldrv" 6 7 import ( 8 "database/sql" 9 "reflect" 10 "testing" 11 12 "github.com/xwb1989/sqlparser" 13 ) 14 15 type vctxType map[any]any 16 17 func TestExpr(t *testing.T) { 18 for _, tc := range []struct { 19 expr string 20 vctx vctxType 21 want any 22 err error 23 }{ 24 { 25 expr: `select (x, y) from tbl`, 26 vctx: vctxType{"x": int32(1), "y": int32(2)}, 27 want: []any{int32(1), int32(2)}, 28 }, 29 { 30 expr: `select (x + "foo") from tbl`, 31 vctx: vctxType{"x": "bar"}, 32 want: "barfoo", 33 }, 34 { 35 expr: `select ("bar" + "foo") from tbl`, 36 want: "barfoo", 37 }, 38 { 39 expr: `select (1 + 1) from tbl`, 40 want: idealInt(2), 41 }, 42 { 43 expr: `select (10 - 1) from tbl`, 44 want: idealInt(9), 45 }, 46 { 47 expr: `select (10 - 0xb) from tbl`, 48 want: idealInt(-1), 49 }, 50 { 51 expr: `select (10 - 0xB) from tbl`, 52 want: idealInt(-1), 53 }, 54 { 55 expr: `select (10 - 0XB) from tbl`, 56 want: idealInt(-1), 57 }, 58 { 59 expr: `select (2*2) from tbl`, 60 want: idealInt(4), 61 }, 62 { 63 expr: `select (30 / 2) from tbl`, 64 want: idealInt(15), 65 }, 66 { 67 expr: `select (31 / 2.0) from tbl`, 68 want: idealFloat(15.5), 69 }, 70 { 71 expr: `select (31 / 2e1) from tbl`, 72 want: idealFloat(1.55), 73 }, 74 { 75 expr: `select (31 / 2E1) from tbl`, 76 want: idealFloat(1.55), 77 }, 78 { 79 expr: `select (31 / 2) from tbl`, 80 want: idealInt(15), 81 }, 82 { 83 expr: `select (2.0 + 3.5) from tbl`, 84 want: idealFloat(5.5), 85 }, 86 { 87 expr: `select (2.0 - 3.5) from tbl`, 88 want: idealFloat(-1.5), 89 }, 90 { 91 expr: `select (1.0 + 1) from tbl`, 92 want: idealFloat(2), 93 }, 94 { 95 expr: `select (2.0 * 2) from tbl`, 96 want: idealFloat(4), 97 }, 98 { 99 expr: `select (2.0 / 4.0) from tbl`, 100 want: idealFloat(0.5), 101 }, 102 { 103 expr: `select (2.0 < 4.0) from tbl`, 104 want: true, 105 }, 106 { 107 expr: `select (2.0 <= 4.0) from tbl`, 108 want: true, 109 }, 110 { 111 expr: `select (2.0 > 4.0) from tbl`, 112 want: false, 113 }, 114 { 115 expr: `select (2.0 >= 4.0) from tbl`, 116 want: false, 117 }, 118 { 119 expr: `select (2.0 = 4.0) from tbl`, 120 want: false, 121 }, 122 { 123 expr: `select (2.0 != 4.0) from tbl`, 124 want: true, 125 }, 126 { 127 expr: `select (2 < 4) from tbl`, 128 want: true, 129 }, 130 { 131 expr: `select (2 <= 4) from tbl`, 132 want: true, 133 }, 134 { 135 expr: `select (2 > 4) from tbl`, 136 want: false, 137 }, 138 { 139 expr: `select (2 >= 4) from tbl`, 140 want: false, 141 }, 142 { 143 expr: `select (2 = 4) from tbl`, 144 want: false, 145 }, 146 { 147 expr: `select (2 != 4) from tbl`, 148 want: true, 149 }, 150 { 151 expr: `select ("ab" < "bb") from tbl`, 152 want: true, 153 }, 154 { 155 expr: `select ("ab" <= "bb") from tbl`, 156 want: true, 157 }, 158 { 159 expr: `select ("ab" > "bb") from tbl`, 160 want: false, 161 }, 162 { 163 expr: `select ("ab" >= "bb") from tbl`, 164 want: false, 165 }, 166 { 167 expr: `select ("ab" = "bb") from tbl`, 168 want: false, 169 }, 170 { 171 expr: `select ("ab" != "bb") from tbl`, 172 want: true, 173 }, 174 { 175 expr: `select (TRUE || true) from tbl`, 176 want: true, 177 }, 178 { 179 expr: `select (false || FALSE) from tbl`, 180 want: false, 181 }, 182 { 183 expr: `select (TRUE != FALSE) from tbl`, 184 want: true, 185 }, 186 { 187 expr: `select (TRUE = FALSE) from tbl`, 188 want: false, 189 }, 190 { 191 expr: `select (TRUE = true) from tbl`, 192 want: true, 193 }, 194 { 195 expr: `select (FALSE = false) from tbl`, 196 want: true, 197 }, 198 { 199 expr: `select (false && true) from tbl`, 200 want: false, 201 }, 202 { 203 expr: `select (false || true) from tbl`, 204 want: true, 205 }, 206 // idealUint 207 { 208 expr: `select (x) from tbl`, 209 vctx: vctxType{"x": idealUint(5)}, 210 want: idealUint(5), 211 }, 212 { 213 expr: `select (x + y) from tbl`, 214 vctx: vctxType{"x": idealUint(6), "y": idealUint(5)}, 215 want: idealUint(11), 216 }, 217 { 218 expr: `select (x - y) from tbl`, 219 vctx: vctxType{"x": idealUint(6), "y": idealUint(5)}, 220 want: idealUint(1), 221 }, 222 { 223 expr: `select (x / y) from tbl`, 224 vctx: vctxType{"x": idealUint(6), "y": idealUint(5)}, 225 want: idealUint(1), 226 }, 227 { 228 expr: `select (x * y) from tbl`, 229 vctx: vctxType{"x": idealUint(6), "y": idealUint(5)}, 230 want: idealUint(30), 231 }, 232 { 233 expr: `select (x * 0x5) from tbl`, 234 vctx: vctxType{"x": idealUint(6), "y": idealUint(5)}, 235 want: idealInt(30), 236 }, 237 { 238 expr: `select (x < y) from tbl`, 239 vctx: vctxType{"x": idealUint(6), "y": idealUint(5)}, 240 want: false, 241 }, 242 { 243 expr: `select (x <= y) from tbl`, 244 vctx: vctxType{"x": idealUint(6), "y": idealUint(5)}, 245 want: false, 246 }, 247 { 248 expr: `select (x > y) from tbl`, 249 vctx: vctxType{"x": idealUint(6), "y": idealUint(5)}, 250 want: true, 251 }, 252 { 253 expr: `select (x >= y) from tbl`, 254 vctx: vctxType{"x": idealUint(6), "y": idealUint(5)}, 255 want: true, 256 }, 257 { 258 expr: `select (x = y) from tbl`, 259 vctx: vctxType{"x": idealUint(6), "y": idealUint(5)}, 260 want: false, 261 }, 262 { 263 expr: `select (x != y) from tbl`, 264 vctx: vctxType{"x": idealUint(6), "y": idealUint(5)}, 265 want: true, 266 }, 267 // uint8 268 { 269 expr: "select (x + y) from tbl // uint8", 270 vctx: vctxType{"x": uint8(5), "y": uint8(2)}, 271 want: uint8(7), 272 }, 273 { 274 expr: "select (x - y) from tbl // uint8", 275 vctx: vctxType{"x": uint8(5), "y": uint8(2)}, 276 want: uint8(3), 277 }, 278 { 279 expr: "select (x * y) from tbl // uint8", 280 vctx: vctxType{"x": uint8(5), "y": uint8(2)}, 281 want: uint8(10), 282 }, 283 { 284 expr: "select (x / y) from tbl // uint8", 285 vctx: vctxType{"x": uint8(5), "y": uint8(2)}, 286 want: uint8(2), 287 }, 288 { 289 expr: "select (x < y) from tbl // uint8", 290 vctx: vctxType{"x": uint8(5), "y": uint8(2)}, 291 want: false, 292 }, 293 { 294 expr: "select (x <= y) from tbl // uint8", 295 vctx: vctxType{"x": uint8(5), "y": uint8(2)}, 296 want: false, 297 }, 298 { 299 expr: "select (x > y) from tbl // uint8", 300 vctx: vctxType{"x": uint8(5), "y": uint8(2)}, 301 want: true, 302 }, 303 { 304 expr: "select (x >= y) from tbl // uint8", 305 vctx: vctxType{"x": uint8(5), "y": uint8(2)}, 306 want: true, 307 }, 308 { 309 expr: "select (x = y) from tbl // uint8", 310 vctx: vctxType{"x": uint8(5), "y": uint8(2)}, 311 want: false, 312 }, 313 { 314 expr: "select (x != y) from tbl // uint8", 315 vctx: vctxType{"x": uint8(5), "y": uint8(2)}, 316 want: true, 317 }, 318 // uint16 319 { 320 expr: "select (x + y) from tbl // uint16", 321 vctx: vctxType{"x": uint16(5), "y": uint16(2)}, 322 want: uint16(7), 323 }, 324 { 325 expr: "select (x - y) from tbl // uint16", 326 vctx: vctxType{"x": uint16(5), "y": uint16(2)}, 327 want: uint16(3), 328 }, 329 { 330 expr: "select (x * y) from tbl // uint16", 331 vctx: vctxType{"x": uint16(5), "y": uint16(2)}, 332 want: uint16(10), 333 }, 334 { 335 expr: "select (x / y) from tbl // uint16", 336 vctx: vctxType{"x": uint16(5), "y": uint16(2)}, 337 want: uint16(2), 338 }, 339 { 340 expr: "select (x < y) from tbl // uint16", 341 vctx: vctxType{"x": uint16(5), "y": uint16(2)}, 342 want: false, 343 }, 344 { 345 expr: "select (x <= y) from tbl // uint16", 346 vctx: vctxType{"x": uint16(5), "y": uint16(2)}, 347 want: false, 348 }, 349 { 350 expr: "select (x > y) from tbl // uint16", 351 vctx: vctxType{"x": uint16(5), "y": uint16(2)}, 352 want: true, 353 }, 354 { 355 expr: "select (x >= y) from tbl // uint16", 356 vctx: vctxType{"x": uint16(5), "y": uint16(2)}, 357 want: true, 358 }, 359 { 360 expr: "select (x = y) from tbl // uint16", 361 vctx: vctxType{"x": uint16(5), "y": uint16(2)}, 362 want: false, 363 }, 364 { 365 expr: "select (x != y) from tbl // uint16", 366 vctx: vctxType{"x": uint16(5), "y": uint16(2)}, 367 want: true, 368 }, 369 // int32 370 { 371 expr: "select (x + y) from tbl // uint32", 372 vctx: vctxType{"x": uint32(5), "y": uint32(2)}, 373 want: uint32(7), 374 }, 375 { 376 expr: "select (x - y) from tbl // uint32", 377 vctx: vctxType{"x": uint32(5), "y": uint32(2)}, 378 want: uint32(3), 379 }, 380 { 381 expr: "select (x * y) from tbl // uint32", 382 vctx: vctxType{"x": uint32(5), "y": uint32(2)}, 383 want: uint32(10), 384 }, 385 { 386 expr: "select (x / y) from tbl // uint32", 387 vctx: vctxType{"x": uint32(5), "y": uint32(2)}, 388 want: uint32(2), 389 }, 390 { 391 expr: "select (x < y) from tbl // uint32", 392 vctx: vctxType{"x": uint32(5), "y": uint32(2)}, 393 want: false, 394 }, 395 { 396 expr: "select (x <= y) from tbl // uint32", 397 vctx: vctxType{"x": uint32(5), "y": uint32(2)}, 398 want: false, 399 }, 400 { 401 expr: "select (x > y) from tbl // uint32", 402 vctx: vctxType{"x": uint32(5), "y": uint32(2)}, 403 want: true, 404 }, 405 { 406 expr: "select (x >= y) from tbl // uint32", 407 vctx: vctxType{"x": uint32(5), "y": uint32(2)}, 408 want: true, 409 }, 410 { 411 expr: "select (x = y) from tbl // uint32", 412 vctx: vctxType{"x": uint32(5), "y": uint32(2)}, 413 want: false, 414 }, 415 { 416 expr: "select (x != y) from tbl // uint32", 417 vctx: vctxType{"x": uint32(5), "y": uint32(2)}, 418 want: true, 419 }, 420 // uint64 421 { 422 expr: "select (x + y) from tbl // uint64", 423 vctx: vctxType{"x": uint64(5), "y": uint64(2)}, 424 want: uint64(7), 425 }, 426 { 427 expr: "select (x - y) from tbl // uint64", 428 vctx: vctxType{"x": uint64(5), "y": uint64(2)}, 429 want: uint64(3), 430 }, 431 { 432 expr: "select (x * y) from tbl // uint64", 433 vctx: vctxType{"x": uint64(5), "y": uint64(2)}, 434 want: uint64(10), 435 }, 436 { 437 expr: "select (x / y) from tbl // uint64", 438 vctx: vctxType{"x": uint64(5), "y": uint64(2)}, 439 want: uint64(2), 440 }, 441 { 442 expr: "select (x < y) from tbl // uint64", 443 vctx: vctxType{"x": uint64(5), "y": uint64(2)}, 444 want: false, 445 }, 446 { 447 expr: "select (x <= y) from tbl // uint64", 448 vctx: vctxType{"x": uint64(5), "y": uint64(2)}, 449 want: false, 450 }, 451 { 452 expr: "select (x > y) from tbl // uint64", 453 vctx: vctxType{"x": uint64(5), "y": uint64(2)}, 454 want: true, 455 }, 456 { 457 expr: "select (x >= y) from tbl // uint64", 458 vctx: vctxType{"x": uint64(5), "y": uint64(2)}, 459 want: true, 460 }, 461 { 462 expr: "select (x = y) from tbl // uint64", 463 vctx: vctxType{"x": uint64(5), "y": uint64(2)}, 464 want: false, 465 }, 466 { 467 expr: "select (x != y) from tbl // uint64", 468 vctx: vctxType{"x": uint64(5), "y": uint64(2)}, 469 want: true, 470 }, 471 // int8 472 { 473 expr: "select (x + y) from tbl // int8", 474 vctx: vctxType{"x": int8(5), "y": int8(2)}, 475 want: int8(7), 476 }, 477 { 478 expr: "select (x - y) from tbl // int8", 479 vctx: vctxType{"x": int8(5), "y": int8(2)}, 480 want: int8(3), 481 }, 482 { 483 expr: "select (x * y) from tbl // int8", 484 vctx: vctxType{"x": int8(5), "y": int8(2)}, 485 want: int8(10), 486 }, 487 { 488 expr: "select (x / y) from tbl // int8", 489 vctx: vctxType{"x": int8(5), "y": int8(2)}, 490 want: int8(2), 491 }, 492 { 493 expr: "select (x < y) from tbl // int8", 494 vctx: vctxType{"x": int8(5), "y": int8(2)}, 495 want: false, 496 }, 497 { 498 expr: "select (x <= y) from tbl // int8", 499 vctx: vctxType{"x": int8(5), "y": int8(2)}, 500 want: false, 501 }, 502 { 503 expr: "select (x > y) from tbl // int8", 504 vctx: vctxType{"x": int8(5), "y": int8(2)}, 505 want: true, 506 }, 507 { 508 expr: "select (x >= y) from tbl // int8", 509 vctx: vctxType{"x": int8(5), "y": int8(2)}, 510 want: true, 511 }, 512 { 513 expr: "select (x = y) from tbl // int8", 514 vctx: vctxType{"x": int8(5), "y": int8(2)}, 515 want: false, 516 }, 517 { 518 expr: "select (x != y) from tbl // int8", 519 vctx: vctxType{"x": int8(5), "y": int8(2)}, 520 want: true, 521 }, 522 // int16 523 { 524 expr: "select (x + y) from tbl // int16", 525 vctx: vctxType{"x": int16(5), "y": int16(2)}, 526 want: int16(7), 527 }, 528 { 529 expr: "select (x - y) from tbl // int16", 530 vctx: vctxType{"x": int16(5), "y": int16(2)}, 531 want: int16(3), 532 }, 533 { 534 expr: "select (x * y) from tbl // int16", 535 vctx: vctxType{"x": int16(5), "y": int16(2)}, 536 want: int16(10), 537 }, 538 { 539 expr: "select (x / y) from tbl // int16", 540 vctx: vctxType{"x": int16(5), "y": int16(2)}, 541 want: int16(2), 542 }, 543 { 544 expr: "select (x < y) from tbl // int16", 545 vctx: vctxType{"x": int16(5), "y": int16(2)}, 546 want: false, 547 }, 548 { 549 expr: "select (x <= y) from tbl // int16", 550 vctx: vctxType{"x": int16(5), "y": int16(2)}, 551 want: false, 552 }, 553 { 554 expr: "select (x > y) from tbl // int16", 555 vctx: vctxType{"x": int16(5), "y": int16(2)}, 556 want: true, 557 }, 558 { 559 expr: "select (x >= y) from tbl // int16", 560 vctx: vctxType{"x": int16(5), "y": int16(2)}, 561 want: true, 562 }, 563 { 564 expr: "select (x = y) from tbl // int16", 565 vctx: vctxType{"x": int16(5), "y": int16(2)}, 566 want: false, 567 }, 568 { 569 expr: "select (x != y) from tbl // int16", 570 vctx: vctxType{"x": int16(5), "y": int16(2)}, 571 want: true, 572 }, 573 // int32 574 { 575 expr: "select (x + y) from tbl // int32", 576 vctx: vctxType{"x": int32(5), "y": int32(2)}, 577 want: int32(7), 578 }, 579 { 580 expr: "select (x - y) from tbl // int32", 581 vctx: vctxType{"x": int32(5), "y": int32(2)}, 582 want: int32(3), 583 }, 584 { 585 expr: "select (x * y) from tbl // int32", 586 vctx: vctxType{"x": int32(5), "y": int32(2)}, 587 want: int32(10), 588 }, 589 { 590 expr: "select (x / y) from tbl // int32", 591 vctx: vctxType{"x": int32(5), "y": int32(2)}, 592 want: int32(2), 593 }, 594 { 595 expr: "select (x < y) from tbl // int32", 596 vctx: vctxType{"x": int32(5), "y": int32(2)}, 597 want: false, 598 }, 599 { 600 expr: "select (x <= y) from tbl // int32", 601 vctx: vctxType{"x": int32(5), "y": int32(2)}, 602 want: false, 603 }, 604 { 605 expr: "select (x > y) from tbl // int32", 606 vctx: vctxType{"x": int32(5), "y": int32(2)}, 607 want: true, 608 }, 609 { 610 expr: "select (x >= y) from tbl // int32", 611 vctx: vctxType{"x": int32(5), "y": int32(2)}, 612 want: true, 613 }, 614 { 615 expr: "select (x = y) from tbl // int32", 616 vctx: vctxType{"x": int32(5), "y": int32(2)}, 617 want: false, 618 }, 619 { 620 expr: "select (x != y) from tbl // int32", 621 vctx: vctxType{"x": int32(5), "y": int32(2)}, 622 want: true, 623 }, 624 // int64 625 { 626 expr: "select (x + y) from tbl // int64", 627 vctx: vctxType{"x": int64(5), "y": int64(2)}, 628 want: int64(7), 629 }, 630 { 631 expr: "select (x - y) from tbl // int64", 632 vctx: vctxType{"x": int64(5), "y": int64(2)}, 633 want: int64(3), 634 }, 635 { 636 expr: "select (x * y) from tbl // int64", 637 vctx: vctxType{"x": int64(5), "y": int64(2)}, 638 want: int64(10), 639 }, 640 { 641 expr: "select (x / y) from tbl // int64", 642 vctx: vctxType{"x": int64(5), "y": int64(2)}, 643 want: int64(2), 644 }, 645 { 646 expr: "select (x < y) from tbl // int64", 647 vctx: vctxType{"x": int64(5), "y": int64(2)}, 648 want: false, 649 }, 650 { 651 expr: "select (x <= y) from tbl // int64", 652 vctx: vctxType{"x": int64(5), "y": int64(2)}, 653 want: false, 654 }, 655 { 656 expr: "select (x > y) from tbl // int64", 657 vctx: vctxType{"x": int64(5), "y": int64(2)}, 658 want: true, 659 }, 660 { 661 expr: "select (x >= y) from tbl // int64", 662 vctx: vctxType{"x": int64(5), "y": int64(2)}, 663 want: true, 664 }, 665 { 666 expr: "select (x = y) from tbl // int64", 667 vctx: vctxType{"x": int64(5), "y": int64(2)}, 668 want: false, 669 }, 670 { 671 expr: "select (x != y) from tbl // int64", 672 vctx: vctxType{"x": int64(5), "y": int64(2)}, 673 want: true, 674 }, 675 // float32 676 { 677 expr: "select (x + y) from tbl // float32", 678 vctx: vctxType{"x": float32(5), "y": float32(2)}, 679 want: float32(7), 680 }, 681 { 682 expr: "select (x - y) from tbl // float32", 683 vctx: vctxType{"x": float32(5), "y": float32(2)}, 684 want: float32(3), 685 }, 686 { 687 expr: "select (x * y) from tbl // float32", 688 vctx: vctxType{"x": float32(5), "y": float32(2)}, 689 want: float32(10), 690 }, 691 { 692 expr: "select (x / y) from tbl // float32", 693 vctx: vctxType{"x": float32(5), "y": float32(2)}, 694 want: float32(2.5), 695 }, 696 { 697 expr: "select (x < y) from tbl // float32", 698 vctx: vctxType{"x": float32(5), "y": float32(2)}, 699 want: false, 700 }, 701 { 702 expr: "select (x <= y) from tbl // float32", 703 vctx: vctxType{"x": float32(5), "y": float32(2)}, 704 want: false, 705 }, 706 { 707 expr: "select (x > y) from tbl // float32", 708 vctx: vctxType{"x": float32(5), "y": float32(2)}, 709 want: true, 710 }, 711 { 712 expr: "select (x >= y) from tbl // float32", 713 vctx: vctxType{"x": float32(5), "y": float32(2)}, 714 want: true, 715 }, 716 { 717 expr: "select (x = y) from tbl // float32", 718 vctx: vctxType{"x": float32(5), "y": float32(2)}, 719 want: false, 720 }, 721 { 722 expr: "select (x != y) from tbl // float32", 723 vctx: vctxType{"x": float32(5), "y": float32(2)}, 724 want: true, 725 }, 726 // float64 727 { 728 expr: "select (x + y) from tbl // float64", 729 vctx: vctxType{"x": float64(5), "y": float64(2)}, 730 want: float64(7), 731 }, 732 { 733 expr: "select (x - y) from tbl // float64", 734 vctx: vctxType{"x": float64(5), "y": float64(2)}, 735 want: float64(3), 736 }, 737 { 738 expr: "select (x * y) from tbl // float64", 739 vctx: vctxType{"x": float64(5), "y": float64(2)}, 740 want: float64(10), 741 }, 742 { 743 expr: "select (x / y) from tbl // float64", 744 vctx: vctxType{"x": float64(5), "y": float64(2)}, 745 want: float64(2.5), 746 }, 747 { 748 expr: "select (x < y) from tbl // float64", 749 vctx: vctxType{"x": float64(5), "y": float64(2)}, 750 want: false, 751 }, 752 { 753 expr: "select (x <= y) from tbl // float64", 754 vctx: vctxType{"x": float64(5), "y": float64(2)}, 755 want: false, 756 }, 757 { 758 expr: "select (x > y) from tbl // float64", 759 vctx: vctxType{"x": float64(5), "y": float64(2)}, 760 want: true, 761 }, 762 { 763 expr: "select (x >= y) from tbl // float64", 764 vctx: vctxType{"x": float64(5), "y": float64(2)}, 765 want: true, 766 }, 767 { 768 expr: "select (x = y) from tbl // float64", 769 vctx: vctxType{"x": float64(5), "y": float64(2)}, 770 want: false, 771 }, 772 { 773 expr: "select (x != y) from tbl // float64", 774 vctx: vctxType{"x": float64(5), "y": float64(2)}, 775 want: true, 776 }, 777 } { 778 t.Run(tc.expr, func(t *testing.T) { 779 stmt, err := sqlparser.Parse(tc.expr) 780 if err != nil { 781 t.Fatalf("could not parse %q: %v", tc.expr, err) 782 } 783 expr, err := newExprFrom(stmt.(*sqlparser.Select).SelectExprs[0].(*sqlparser.AliasedExpr).Expr, nil) 784 if err != nil { 785 t.Fatalf("could not generate expression: %v", err) 786 } 787 ectx := newExecCtx(nil, nil) 788 v, err := expr.eval(ectx, tc.vctx) 789 switch { 790 case err == nil && tc.err == nil: 791 // ok 792 case err == nil && tc.err != nil: 793 t.Fatalf("expected an error (got=nil): %v", tc.err) 794 case err != nil && tc.err == nil: 795 t.Fatalf("unexpected error: %v", err) 796 case err.Error() != tc.err.Error(): 797 t.Fatalf("invalid error.\ngot= %q\nwant=%q", err, tc.err) 798 } 799 if !reflect.DeepEqual(v, tc.want) { 800 t.Fatalf("invalid result.\ngot= %v (%T)\nwant=%v (%T)", v, v, tc.want, tc.want) 801 } 802 }) 803 } 804 } 805 806 func TestSelectColumns(t *testing.T) { 807 db, err := sql.Open("root", "../../testdata/simple.root") 808 if err != nil { 809 t.Fatal(err) 810 } 811 defer db.Close() 812 813 for _, tc := range []struct { 814 query string 815 cols []string 816 types []any 817 args []any 818 vals [][]any 819 }{ 820 { 821 query: `select one from tree`, 822 cols: []string{"one"}, 823 types: []any{int32(0)}, 824 vals: [][]any{ 825 {int32(1)}, 826 {int32(2)}, 827 {int32(3)}, 828 {int32(4)}, 829 }, 830 }, 831 { 832 query: `select (one) from tree`, 833 cols: []string{"one"}, 834 types: []any{int32(0)}, 835 vals: [][]any{ 836 {int32(1)}, 837 {int32(2)}, 838 {int32(3)}, 839 {int32(4)}, 840 }, 841 }, 842 { 843 query: `select (one, two) from tree`, 844 cols: []string{"one", "two"}, 845 types: []any{int32(0), 0.0}, 846 vals: [][]any{ 847 {int32(1), 1.1}, 848 {int32(2), 2.2}, 849 {int32(3), 3.3}, 850 {int32(4), 4.4}, 851 }, 852 }, 853 { 854 query: `select (one, (two)) from tree`, 855 cols: []string{"one", "two"}, 856 types: []any{int32(0), 0.0}, 857 vals: [][]any{ 858 {int32(1), 1.1}, 859 {int32(2), 2.2}, 860 {int32(3), 3.3}, 861 {int32(4), 4.4}, 862 }, 863 }, 864 { 865 query: `select (one, ((two))) from tree`, 866 cols: []string{"one", "two"}, 867 types: []any{int32(0), 0.0}, 868 vals: [][]any{ 869 {int32(1), 1.1}, 870 {int32(2), 2.2}, 871 {int32(3), 3.3}, 872 {int32(4), 4.4}, 873 }, 874 }, 875 { 876 query: `select (((one), ((two)))) from tree`, 877 cols: []string{"one", "two"}, 878 types: []any{int32(0), 0.0}, 879 vals: [][]any{ 880 {int32(1), 1.1}, 881 {int32(2), 2.2}, 882 {int32(3), 3.3}, 883 {int32(4), 4.4}, 884 }, 885 }, 886 { 887 query: `select three from tree`, 888 cols: []string{"three"}, 889 types: []any{""}, 890 vals: [][]any{ 891 {"uno"}, 892 {"dos"}, 893 {"tres"}, 894 {"quatro"}, 895 }, 896 }, 897 { 898 query: `select (one, two, three) from tree`, 899 cols: []string{"one", "two", "three"}, 900 types: []any{int32(0), 0.0, ""}, 901 vals: [][]any{ 902 {int32(1), 1.1, "uno"}, 903 {int32(2), 2.2, "dos"}, 904 {int32(3), 3.3, "tres"}, 905 {int32(4), 4.4, "quatro"}, 906 }, 907 }, 908 { 909 query: `select (?, two, ?) from tree`, 910 cols: []string{"", "two", ""}, 911 types: []any{"", 0.0, ""}, 912 args: []any{"one", "three"}, 913 vals: [][]any{ 914 {"one", 1.1, "three"}, 915 {"one", 2.2, "three"}, 916 {"one", 3.3, "three"}, 917 {"one", 4.4, "three"}, 918 }, 919 }, 920 { 921 query: `select (:v1, two, :v2) from tree`, 922 cols: []string{"", "two", ""}, 923 types: []any{"", 0.0, ""}, 924 args: []any{"one", "three"}, 925 vals: [][]any{ 926 {"one", 1.1, "three"}, 927 {"one", 2.2, "three"}, 928 {"one", 3.3, "three"}, 929 {"one", 4.4, "three"}, 930 }, 931 }, 932 { 933 query: `select (:v2, two, :v1) from tree`, 934 cols: []string{"", "two", ""}, 935 types: []any{"", 0.0, ""}, 936 args: []any{"three", "one"}, 937 vals: [][]any{ 938 {"one", 1.1, "three"}, 939 {"one", 2.2, "three"}, 940 {"one", 3.3, "three"}, 941 {"one", 4.4, "three"}, 942 }, 943 }, 944 { 945 query: `select (:v2, two+:v3, :v1) from tree`, 946 cols: []string{"", "", ""}, 947 types: []any{"", 0.0, ""}, 948 args: []any{"three", "one", 10}, 949 vals: [][]any{ 950 {"one", 11.1, "three"}, 951 {"one", 12.2, "three"}, 952 {"one", 13.3, "three"}, 953 {"one", 14.4, "three"}, 954 }, 955 }, 956 { 957 query: `select (one) from tree where (two > 3)`, 958 cols: []string{"one"}, 959 types: []any{int32(0)}, 960 vals: [][]any{ 961 {int32(3)}, 962 {int32(4)}, 963 }, 964 }, 965 { 966 query: `select (one) from tree where (3 <= two && two < 4)`, 967 cols: []string{"one"}, 968 types: []any{int32(0)}, 969 vals: [][]any{ 970 {int32(3)}, 971 }, 972 }, 973 { 974 query: `select (one, two) from tree where (three="quatro")`, 975 cols: []string{"one", "two"}, 976 types: []any{int32(0), 0.0}, 977 vals: [][]any{ 978 {int32(4), 4.4}, 979 }, 980 }, 981 { 982 query: `select (one, two, ?+:v2) from tree where (three="quatro")`, 983 cols: []string{"one", "two", ""}, 984 types: []any{int32(0), 0.0, uint64(0)}, 985 args: []any{idealUint(5), idealUint(10)}, 986 vals: [][]any{ 987 {int32(4), 4.4, uint64(15)}, 988 }, 989 }, 990 } { 991 t.Run(tc.query, func(t *testing.T) { 992 rows, err := db.Query(tc.query, tc.args...) 993 if err != nil { 994 t.Fatal(err) 995 } 996 defer rows.Close() 997 998 cols, err := rows.Columns() 999 if err != nil { 1000 t.Fatal(err) 1001 } 1002 1003 if got, want := cols, tc.cols; !reflect.DeepEqual(got, want) { 1004 t.Fatalf("invalid columns.\ngot= %q\nwant=%q", got, want) 1005 } 1006 1007 var got [][]any 1008 for rows.Next() { 1009 vars := make([]any, len(tc.types)) 1010 for i, v := range tc.types { 1011 vars[i] = reflect.New(reflect.TypeOf(v)).Interface() 1012 } 1013 err = rows.Scan(vars...) 1014 if err != nil { 1015 t.Fatal(err) 1016 } 1017 row := make([]any, len(vars)) 1018 for i, v := range vars { 1019 row[i] = reflect.Indirect(reflect.ValueOf(v)).Interface() 1020 } 1021 got = append(got, row) 1022 } 1023 1024 if got, want := got, tc.vals; !reflect.DeepEqual(got, want) { 1025 t.Fatalf("invalid values.\ngot= %v\nwant=%v\n", got, want) 1026 } 1027 }) 1028 } 1029 }