github.com/mithrandie/csvq@v1.18.1/lib/parser/ast_test.go (about) 1 package parser 2 3 import ( 4 "reflect" 5 "testing" 6 "time" 7 8 "github.com/mithrandie/ternary" 9 10 "github.com/mithrandie/csvq/lib/option" 11 "github.com/mithrandie/csvq/lib/value" 12 ) 13 14 func TestBaseExpr_Line(t *testing.T) { 15 e := BaseExpr{ 16 line: 3, 17 char: 5, 18 sourceFile: "source.sql", 19 } 20 21 expect := 3 22 if e.Line() != expect { 23 t.Errorf("line = %d, want %d for %#v", e.Line(), expect, e) 24 } 25 } 26 27 func TestBaseExpr_Char(t *testing.T) { 28 e := BaseExpr{ 29 line: 3, 30 char: 5, 31 sourceFile: "source.sql", 32 } 33 34 expect := 5 35 if e.Char() != expect { 36 t.Errorf("line = %d, want %d for %#v", e.Char(), expect, e) 37 } 38 } 39 40 func TestBaseExpr_SourceFile(t *testing.T) { 41 e := BaseExpr{ 42 line: 3, 43 char: 5, 44 sourceFile: "source.sql", 45 } 46 47 expect := "source.sql" 48 if e.SourceFile() != expect { 49 t.Errorf("line = %q, want %q for %#v", e.SourceFile(), expect, e) 50 } 51 } 52 53 func TestBaseExpr_HasParseInfo(t *testing.T) { 54 expr := &BaseExpr{} 55 if !expr.HasParseInfo() { 56 t.Errorf("has parse info = %t, want %t for %#v", expr.HasParseInfo(), true, expr) 57 } 58 59 queryExpr := NewNullValue() 60 if queryExpr.HasParseInfo() { 61 t.Errorf("has parse info = %t, want %t for %#v", expr.HasParseInfo(), false, queryExpr) 62 } 63 } 64 65 func TestPrimitiveType_String(t *testing.T) { 66 e := NewTernaryValueFromString("true") 67 expect := "TRUE" 68 if e.String() != expect { 69 t.Errorf("result = %q, want %q for %q ", e.String(), expect, e) 70 } 71 72 e = NewTernaryValue(ternary.FALSE) 73 expect = "FALSE" 74 if e.String() != expect { 75 t.Errorf("result = %q, want %q for %q ", e.String(), expect, e) 76 } 77 78 e = NewStringValue("str") 79 expect = "'str'" 80 if e.String() != expect { 81 t.Errorf("result = %q, want %q for %q ", e.String(), expect, e) 82 } 83 84 e = NewIntegerValue(1) 85 expect = "1" 86 if e.String() != expect { 87 t.Errorf("result = %q, want %q for %q ", e.String(), expect, e) 88 } 89 90 e = NewFloatValue(1.234) 91 expect = "1.234" 92 if e.String() != expect { 93 t.Errorf("result = %q, want %q for %q ", e.String(), expect, e) 94 } 95 96 e = NewNullValue() 97 expect = "NULL" 98 if e.String() != expect { 99 t.Errorf("result = %q, want %q for %q ", e.String(), expect, e) 100 } 101 } 102 103 func TestPrimitiveType_IsInteger(t *testing.T) { 104 e := NewDatetimeValue(time.Date(2012, 2, 4, 9, 18, 15, 0, time.Local)) 105 if e.IsInteger() != false { 106 t.Errorf("result = %t, want %t for %q ", e.IsInteger(), false, e) 107 } 108 109 e = NewIntegerValue(1) 110 if e.IsInteger() != true { 111 t.Errorf("result = %t, want %t for %q ", e.IsInteger(), true, e) 112 } 113 } 114 115 func TestPlaceHolder_String(t *testing.T) { 116 s := "?" 117 ordinal := 3 118 e := Placeholder{Literal: s, Ordinal: ordinal, Name: ""} 119 expect := "?{3}" 120 if e.String() != expect { 121 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 122 } 123 124 s = ":foo" 125 ordinal = 5 126 e = Placeholder{Literal: s, Ordinal: ordinal, Name: "foo"} 127 expect = ":foo" 128 if e.String() != expect { 129 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 130 } 131 } 132 133 func TestIdentifier_String(t *testing.T) { 134 s := "abcde" 135 e := Identifier{Literal: s} 136 if e.String() != s { 137 t.Errorf("string = %q, want %q for %#v", e.String(), s, e) 138 } 139 140 s = "abcde" 141 e = Identifier{Literal: s, Quoted: true} 142 if e.String() != option.QuoteIdentifier(s) { 143 t.Errorf("string = %q, want %q for %#v", e.String(), option.QuoteIdentifier(s), e) 144 } 145 } 146 147 func TestConstant_String(t *testing.T) { 148 e := Constant{ 149 Space: "math", 150 Name: "pi", 151 } 152 expect := "MATH::PI" 153 if e.String() != expect { 154 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 155 } 156 } 157 158 func TestFieldReference_String(t *testing.T) { 159 e := FieldReference{ 160 View: Identifier{Literal: "table1"}, 161 Column: Identifier{Literal: "column1"}, 162 } 163 expect := "table1.column1" 164 if e.String() != expect { 165 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 166 } 167 168 e = FieldReference{ 169 Column: Identifier{Literal: "column1"}, 170 } 171 expect = "column1" 172 if e.String() != expect { 173 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 174 } 175 } 176 177 func TestColumnNumber_String(t *testing.T) { 178 e := ColumnNumber{ 179 View: Identifier{Literal: "table1"}, 180 Number: value.NewInteger(3), 181 } 182 expect := "table1.3" 183 if e.String() != expect { 184 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 185 } 186 } 187 188 func TestParentheses_String(t *testing.T) { 189 s := "abcde" 190 e := Parentheses{Expr: NewStringValue(s)} 191 expect := "('abcde')" 192 if e.String() != expect { 193 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 194 } 195 } 196 197 func TestRowValue_String(t *testing.T) { 198 e := RowValue{ 199 Value: ValueList{ 200 Values: []QueryExpression{ 201 NewIntegerValueFromString("1"), 202 NewIntegerValueFromString("2"), 203 }, 204 }, 205 } 206 expect := "(1, 2)" 207 if e.String() != expect { 208 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 209 } 210 } 211 212 func TestValueList_String(t *testing.T) { 213 e := ValueList{ 214 Values: []QueryExpression{ 215 NewIntegerValueFromString("1"), 216 NewIntegerValueFromString("2"), 217 }, 218 } 219 expect := "(1, 2)" 220 if e.String() != expect { 221 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 222 } 223 } 224 225 func TestRowValueList_String(t *testing.T) { 226 e := RowValueList{ 227 RowValues: []QueryExpression{ 228 ValueList{ 229 Values: []QueryExpression{ 230 NewIntegerValueFromString("1"), 231 NewIntegerValueFromString("2"), 232 }, 233 }, 234 ValueList{ 235 Values: []QueryExpression{ 236 NewIntegerValueFromString("3"), 237 NewIntegerValueFromString("4"), 238 }, 239 }, 240 }, 241 } 242 expect := "((1, 2), (3, 4))" 243 if e.String() != expect { 244 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 245 } 246 } 247 248 func TestSelectQuery_IsForUpdate(t *testing.T) { 249 e := SelectQuery{ 250 SelectEntity: SelectEntity{ 251 SelectClause: SelectClause{ 252 Fields: []QueryExpression{Field{Object: Identifier{Literal: "column"}}}, 253 }, 254 }, 255 Context: Token{Token: UPDATE, Literal: "update"}, 256 } 257 if !e.IsForUpdate() { 258 t.Errorf("IsForUpdate() = %t, want %t for %#v", e.IsForUpdate(), true, e) 259 } 260 261 e = SelectQuery{ 262 SelectEntity: SelectEntity{ 263 SelectClause: SelectClause{ 264 Fields: []QueryExpression{Field{Object: Identifier{Literal: "column"}}}, 265 }, 266 }, 267 } 268 if e.IsForUpdate() { 269 t.Errorf("IsForUpdate() = %t, want %t for %#v", e.IsForUpdate(), false, e) 270 } 271 } 272 273 func TestSelectQuery_String(t *testing.T) { 274 e := SelectQuery{ 275 WithClause: WithClause{ 276 InlineTables: []QueryExpression{ 277 InlineTable{ 278 Name: Identifier{Literal: "ct"}, 279 Query: SelectQuery{ 280 SelectEntity: SelectEntity{ 281 SelectClause: SelectClause{ 282 Fields: []QueryExpression{ 283 Field{Object: NewIntegerValueFromString("1")}, 284 }, 285 }, 286 }, 287 }, 288 }, 289 }, 290 }, 291 SelectEntity: SelectEntity{ 292 SelectClause: SelectClause{ 293 Fields: []QueryExpression{Field{Object: Identifier{Literal: "column"}}}, 294 }, 295 FromClause: FromClause{ 296 Tables: []QueryExpression{ 297 Table{Object: Identifier{Literal: "table"}}, 298 }, 299 }, 300 }, 301 OrderByClause: OrderByClause{ 302 Items: []QueryExpression{ 303 OrderItem{ 304 Value: Identifier{Literal: "column"}, 305 }, 306 }, 307 }, 308 LimitClause: LimitClause{ 309 Type: Token{Token: LIMIT, Literal: "limit"}, 310 Value: NewIntegerValueFromString("10"), 311 OffsetClause: OffsetClause{ 312 Value: NewIntegerValueFromString("10"), 313 }, 314 }, 315 Context: Token{Token: UPDATE, Literal: "update"}, 316 } 317 expect := "WITH ct AS (SELECT 1) SELECT column FROM table ORDER BY column LIMIT 10 OFFSET 10 FOR UPDATE" 318 if e.String() != expect { 319 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 320 } 321 } 322 323 func TestSelectSet_String(t *testing.T) { 324 e := SelectSet{ 325 LHS: SelectEntity{ 326 SelectClause: SelectClause{ 327 Fields: []QueryExpression{Field{Object: NewIntegerValueFromString("1")}}, 328 }, 329 }, 330 Operator: Token{Token: UNION, Literal: "union"}, 331 All: Token{Token: ALL, Literal: "all"}, 332 RHS: SelectEntity{ 333 SelectClause: SelectClause{ 334 Fields: []QueryExpression{Field{Object: NewIntegerValueFromString("2")}}, 335 }, 336 }, 337 } 338 expect := "SELECT 1 UNION ALL SELECT 2" 339 if e.String() != expect { 340 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 341 } 342 } 343 344 func TestSelectEntity_String(t *testing.T) { 345 e := SelectEntity{ 346 SelectClause: SelectClause{ 347 Fields: []QueryExpression{Field{Object: Identifier{Literal: "column"}}}, 348 }, 349 IntoClause: IntoClause{ 350 Variables: []Variable{ 351 {Name: "var1"}, 352 {Name: "var2"}, 353 }, 354 }, 355 FromClause: FromClause{ 356 Tables: []QueryExpression{ 357 Table{Object: Identifier{Literal: "table"}}, 358 }, 359 }, 360 WhereClause: WhereClause{ 361 Filter: Comparison{ 362 LHS: Identifier{Literal: "column"}, 363 Operator: Token{Token: '>', Literal: ">"}, 364 RHS: NewIntegerValueFromString("1"), 365 }, 366 }, 367 GroupByClause: GroupByClause{ 368 Items: []QueryExpression{ 369 Identifier{Literal: "column1"}, 370 }, 371 }, 372 HavingClause: HavingClause{ 373 Filter: Comparison{ 374 LHS: Identifier{Literal: "column"}, 375 Operator: Token{Token: '>', Literal: ">"}, 376 RHS: NewIntegerValueFromString("1"), 377 }, 378 }, 379 } 380 381 expect := "SELECT column INTO @var1, @var2 FROM table WHERE column > 1 GROUP BY column1 HAVING column > 1" 382 if e.String() != expect { 383 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 384 } 385 } 386 387 func TestSelectClause_IsDistinct(t *testing.T) { 388 e := SelectClause{} 389 if e.IsDistinct() == true { 390 t.Errorf("distinct = %t, want %t for %#v", e.IsDistinct(), false, e) 391 } 392 393 e = SelectClause{Distinct: Token{Token: DISTINCT, Literal: "distinct"}} 394 if e.IsDistinct() == false { 395 t.Errorf("distinct = %t, want %t for %#v", e.IsDistinct(), true, e) 396 } 397 } 398 399 func TestSelectClause_String(t *testing.T) { 400 e := SelectClause{ 401 Distinct: Token{Token: DISTINCT, Literal: "distinct"}, 402 Fields: []QueryExpression{ 403 Field{ 404 Object: Identifier{Literal: "column1"}, 405 }, 406 Field{ 407 Object: Identifier{Literal: "column2"}, 408 As: Token{Token: AS, Literal: "as"}, 409 Alias: Identifier{Literal: "alias"}, 410 }, 411 }, 412 } 413 expect := "SELECT DISTINCT column1, column2 AS alias" 414 if e.String() != expect { 415 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 416 } 417 } 418 419 func TestIntoClause_String(t *testing.T) { 420 e := IntoClause{ 421 Variables: []Variable{ 422 {Name: "var1"}, 423 {Name: "var2"}, 424 }, 425 } 426 expect := "INTO @var1, @var2" 427 if e.String() != expect { 428 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 429 } 430 } 431 432 func TestFromClause_String(t *testing.T) { 433 e := FromClause{ 434 Tables: []QueryExpression{ 435 Table{Object: Identifier{Literal: "table1"}}, 436 Table{Object: Identifier{Literal: "table2"}}, 437 }, 438 } 439 expect := "FROM table1, table2" 440 if e.String() != expect { 441 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 442 } 443 } 444 445 func TestWhereClause_String(t *testing.T) { 446 e := WhereClause{ 447 Filter: Comparison{ 448 LHS: Identifier{Literal: "column"}, 449 Operator: Token{Token: '>', Literal: ">"}, 450 RHS: NewIntegerValueFromString("1"), 451 }, 452 } 453 expect := "WHERE column > 1" 454 if e.String() != expect { 455 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 456 } 457 } 458 459 func TestGroupByClause_String(t *testing.T) { 460 e := GroupByClause{ 461 Items: []QueryExpression{ 462 Identifier{Literal: "column1"}, 463 Identifier{Literal: "column2"}, 464 }, 465 } 466 expect := "GROUP BY column1, column2" 467 if e.String() != expect { 468 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 469 } 470 } 471 472 func TestHavingClause_String(t *testing.T) { 473 e := HavingClause{ 474 Filter: Comparison{ 475 LHS: Identifier{Literal: "column"}, 476 Operator: Token{Token: '>', Literal: ">"}, 477 RHS: NewIntegerValueFromString("1"), 478 }, 479 } 480 expect := "HAVING column > 1" 481 if e.String() != expect { 482 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 483 } 484 } 485 486 func TestOrderByClause_String(t *testing.T) { 487 e := OrderByClause{ 488 Items: []QueryExpression{ 489 OrderItem{ 490 Value: Identifier{Literal: "column1"}, 491 }, 492 OrderItem{ 493 Value: Identifier{Literal: "column2"}, 494 Direction: Token{Token: ASC, Literal: "asc"}, 495 }, 496 }, 497 } 498 expect := "ORDER BY column1, column2 ASC" 499 if e.String() != expect { 500 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 501 } 502 } 503 504 func TestLimitClause_String(t *testing.T) { 505 e := LimitClause{ 506 OffsetClause: OffsetClause{Value: NewIntegerValueFromString("10")}, 507 } 508 expect := "OFFSET 10" 509 if e.String() != expect { 510 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 511 } 512 513 e = LimitClause{ 514 Type: Token{Token: LIMIT, Literal: "limit"}, 515 Value: NewIntegerValueFromString("10"), 516 Unit: Token{Token: ROWS, Literal: "rows"}, 517 Restriction: Token{Token: TIES, Literal: "with ties"}, 518 OffsetClause: OffsetClause{Value: NewIntegerValueFromString("10")}, 519 } 520 expect = "LIMIT 10 ROWS WITH TIES OFFSET 10" 521 if e.String() != expect { 522 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 523 } 524 525 e = LimitClause{ 526 Type: Token{Token: FETCH, Literal: "fetch"}, 527 Position: Token{Token: NEXT, Literal: "next"}, 528 Value: NewIntegerValueFromString("10"), 529 Unit: Token{Token: ROWS, Literal: "rows"}, 530 Restriction: Token{Token: TIES, Literal: "with ties"}, 531 OffsetClause: OffsetClause{Value: NewIntegerValueFromString("10")}, 532 } 533 expect = "OFFSET 10 FETCH NEXT 10 ROWS WITH TIES" 534 if e.String() != expect { 535 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 536 } 537 } 538 539 func TestLimitClause_Percentage(t *testing.T) { 540 e := LimitClause{Type: Token{Token: LIMIT, Literal: "limit"}, Value: NewIntegerValue(10)} 541 if e.Percentage() { 542 t.Errorf("percentage = %t, want %t for %#v", e.Percentage(), false, e) 543 } 544 545 e = LimitClause{Type: Token{Token: LIMIT, Literal: "limit"}, Value: NewIntegerValue(10), Unit: Token{Token: PERCENT, Literal: "percent"}} 546 if !e.Percentage() { 547 t.Errorf("percentage = %t, want %t for %#v", e.Percentage(), true, e) 548 } 549 } 550 551 func TestLimitClause_WithTies(t *testing.T) { 552 e := LimitClause{Type: Token{Token: LIMIT, Literal: "limit"}, Value: NewIntegerValue(10)} 553 if e.WithTies() { 554 t.Errorf("with ties = %t, want %t for %#v", e.WithTies(), false, e) 555 } 556 557 e = LimitClause{Type: Token{Token: LIMIT, Literal: "limit"}, Value: NewIntegerValue(10), Restriction: Token{Token: TIES, Literal: "with ties"}} 558 if !e.WithTies() { 559 t.Errorf("with ties = %t, want %t for %#v", e.WithTies(), true, e) 560 } 561 } 562 563 func TestOffsetClause_String(t *testing.T) { 564 e := OffsetClause{Value: NewIntegerValueFromString("10")} 565 expect := "OFFSET 10" 566 if e.String() != expect { 567 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 568 } 569 570 e = OffsetClause{Value: NewIntegerValueFromString("10"), Unit: Token{Token: ROWS, Literal: "rows"}} 571 expect = "OFFSET 10 ROWS" 572 if e.String() != expect { 573 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 574 } 575 } 576 577 func TestWithClause_String(t *testing.T) { 578 e := WithClause{ 579 InlineTables: []QueryExpression{ 580 InlineTable{ 581 Name: Identifier{Literal: "alias1"}, 582 Query: SelectQuery{ 583 SelectEntity: SelectEntity{ 584 SelectClause: SelectClause{ 585 Fields: []QueryExpression{ 586 NewIntegerValueFromString("1"), 587 }, 588 }, 589 }, 590 }, 591 }, 592 InlineTable{ 593 Recursive: Token{Token: RECURSIVE, Literal: "recursive"}, 594 Name: Identifier{Literal: "alias2"}, 595 Query: SelectQuery{ 596 SelectEntity: SelectEntity{ 597 SelectClause: SelectClause{ 598 Fields: []QueryExpression{ 599 NewIntegerValueFromString("2"), 600 }, 601 }, 602 }, 603 }, 604 }, 605 }, 606 } 607 expect := "WITH alias1 AS (SELECT 1), RECURSIVE alias2 AS (SELECT 2)" 608 if e.String() != expect { 609 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 610 } 611 } 612 613 func TestInlineTable_String(t *testing.T) { 614 e := InlineTable{ 615 Recursive: Token{Token: RECURSIVE, Literal: "recursive"}, 616 Name: Identifier{Literal: "it"}, 617 Fields: []QueryExpression{ 618 Identifier{Literal: "column1"}, 619 }, 620 Query: SelectQuery{ 621 SelectEntity: SelectEntity{ 622 SelectClause: SelectClause{ 623 Fields: []QueryExpression{ 624 NewIntegerValueFromString("1"), 625 }, 626 }, 627 }, 628 }, 629 } 630 expect := "RECURSIVE it (column1) AS (SELECT 1)" 631 if e.String() != expect { 632 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 633 } 634 } 635 636 func TestInlineTable_IsRecursive(t *testing.T) { 637 e := InlineTable{ 638 Recursive: Token{Token: RECURSIVE, Literal: "recursive"}, 639 Name: Identifier{Literal: "alias"}, 640 Query: SelectQuery{ 641 SelectEntity: SelectEntity{ 642 SelectClause: SelectClause{ 643 Fields: []QueryExpression{ 644 NewIntegerValueFromString("1"), 645 }, 646 }, 647 }, 648 }, 649 } 650 if e.IsRecursive() != true { 651 t.Errorf("IsRecursive = %t, want %t for %#v", e.IsRecursive(), true, e) 652 } 653 654 e = InlineTable{ 655 Name: Identifier{Literal: "alias"}, 656 Query: SelectQuery{ 657 SelectEntity: SelectEntity{ 658 SelectClause: SelectClause{ 659 Fields: []QueryExpression{ 660 NewIntegerValueFromString("1"), 661 }, 662 }, 663 }, 664 }, 665 } 666 if e.IsRecursive() != false { 667 t.Errorf("IsRecursive = %t, want %t for %#v", e.IsRecursive(), false, e) 668 } 669 } 670 671 func TestSubquery_String(t *testing.T) { 672 e := Subquery{ 673 Query: SelectQuery{ 674 SelectEntity: SelectEntity{ 675 SelectClause: SelectClause{ 676 Fields: []QueryExpression{ 677 NewIntegerValueFromString("1"), 678 }, 679 }, 680 FromClause: FromClause{ 681 Tables: []QueryExpression{Dual{}}, 682 }, 683 }, 684 }, 685 } 686 expect := "(SELECT 1 FROM DUAL)" 687 if e.String() != expect { 688 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 689 } 690 } 691 692 func TestUrl_String(t *testing.T) { 693 e := Url{ 694 Raw: "https://example.com/foo.txt?q=p", 695 } 696 expect := "https://example.com/foo.txt?q=p" 697 698 if e.String() != expect { 699 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 700 } 701 } 702 703 func TestTableFunction_String(t *testing.T) { 704 e := TableFunction{ 705 Name: "file", 706 Args: []QueryExpression{ 707 NewStringValue("./foo.csv"), 708 }, 709 } 710 expect := "FILE::('./foo.csv')" 711 712 if e.String() != expect { 713 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 714 } 715 } 716 717 func TestFormatSpecifiedFunction_String(t *testing.T) { 718 e := FormatSpecifiedFunction{ 719 Type: Token{Token: FIXED, Literal: "fixed"}, 720 FormatElement: NewStringValue("[1, 2, 3]"), 721 Path: Identifier{Literal: "fixed_length.dat", Quoted: true}, 722 Args: []QueryExpression{NewStringValue("utf8")}, 723 } 724 expect := "FIXED('[1, 2, 3]', `fixed_length.dat`, 'utf8')" 725 if e.String() != expect { 726 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 727 } 728 729 e = FormatSpecifiedFunction{ 730 Type: Token{Token: FIXED, Literal: "fixed"}, 731 FormatElement: NewStringValue("[1, 2, 3]"), 732 Path: Identifier{Literal: "fixed_length.dat", Quoted: true}, 733 Args: nil, 734 } 735 expect = "FIXED('[1, 2, 3]', `fixed_length.dat`)" 736 if e.String() != expect { 737 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 738 } 739 740 e = FormatSpecifiedFunction{ 741 Type: Token{Token: LTSV, Literal: "ltsv"}, 742 Path: Identifier{Literal: "table.ltsv", Quoted: true}, 743 Args: []QueryExpression{NewStringValue("utf8")}, 744 } 745 expect = "LTSV(`table.ltsv`, 'utf8')" 746 if e.String() != expect { 747 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 748 } 749 } 750 751 func TestJsonQuery_String(t *testing.T) { 752 e := JsonQuery{ 753 JsonQuery: Token{Token: JSON_ROW, Literal: "json_array"}, 754 Query: NewStringValue("key"), 755 JsonText: NewStringValue("{\"key\":1}"), 756 } 757 expect := "JSON_ROW('key', '{\"key\":1}')" 758 if e.String() != expect { 759 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 760 } 761 } 762 763 func TestComparison_String(t *testing.T) { 764 e := Comparison{ 765 LHS: Identifier{Literal: "column"}, 766 Operator: Token{Token: '>', Literal: ">"}, 767 RHS: NewIntegerValueFromString("1"), 768 } 769 expect := "column > 1" 770 if e.String() != expect { 771 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 772 } 773 } 774 775 func TestIs_IsNegated(t *testing.T) { 776 e := Is{} 777 if e.IsNegated() == true { 778 t.Errorf("negation = %t, want %t for %#v", e.IsNegated(), false, e) 779 } 780 781 e = Is{Negation: Token{Token: NOT, Literal: "not"}} 782 if e.IsNegated() == false { 783 t.Errorf("negation = %t, want %t for %#v", e.IsNegated(), true, e) 784 } 785 } 786 787 func TestIs_String(t *testing.T) { 788 e := Is{ 789 LHS: Identifier{Literal: "column"}, 790 RHS: NewNullValue(), 791 Negation: Token{Token: NOT, Literal: "not"}, 792 } 793 expect := "column IS NOT NULL" 794 if e.String() != expect { 795 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 796 } 797 } 798 799 func TestBetween_IsNegated(t *testing.T) { 800 e := Between{} 801 if e.IsNegated() == true { 802 t.Errorf("negation = %t, want %t for %#v", e.IsNegated(), false, e) 803 } 804 805 e = Between{Negation: Token{Token: NOT, Literal: "not"}} 806 if e.IsNegated() == false { 807 t.Errorf("negation = %t, want %t for %#v", e.IsNegated(), true, e) 808 } 809 } 810 811 func TestBetween_String(t *testing.T) { 812 e := Between{ 813 LHS: Identifier{Literal: "column"}, 814 Low: NewIntegerValueFromString("-10"), 815 High: NewIntegerValueFromString("10"), 816 Negation: Token{Token: NOT, Literal: "not"}, 817 } 818 expect := "column NOT BETWEEN -10 AND 10" 819 if e.String() != expect { 820 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 821 } 822 } 823 824 func TestIn_IsNegated(t *testing.T) { 825 e := In{} 826 if e.IsNegated() == true { 827 t.Errorf("negation = %t, want %t for %#v", e.IsNegated(), false, e) 828 } 829 830 e = In{Negation: Token{Token: NOT, Literal: "not"}} 831 if e.IsNegated() == false { 832 t.Errorf("negation = %t, want %t for %#v", e.IsNegated(), true, e) 833 } 834 } 835 836 func TestIn_String(t *testing.T) { 837 e := In{ 838 LHS: Identifier{Literal: "column"}, 839 Values: RowValue{ 840 Value: ValueList{ 841 Values: []QueryExpression{ 842 NewIntegerValueFromString("1"), 843 NewIntegerValueFromString("2"), 844 }, 845 }, 846 }, 847 Negation: Token{Token: NOT, Literal: "not"}, 848 } 849 expect := "column NOT IN (1, 2)" 850 if e.String() != expect { 851 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 852 } 853 } 854 855 func TestAll_String(t *testing.T) { 856 e := All{ 857 LHS: RowValue{ 858 Value: ValueList{ 859 Values: []QueryExpression{ 860 Identifier{Literal: "column1"}, 861 Identifier{Literal: "column2"}, 862 }, 863 }, 864 }, 865 Operator: Token{Token: '>', Literal: ">"}, 866 Values: Subquery{ 867 Query: SelectQuery{ 868 SelectEntity: SelectEntity{ 869 SelectClause: SelectClause{ 870 Fields: []QueryExpression{ 871 NewIntegerValueFromString("1"), 872 }, 873 }, 874 FromClause: FromClause{ 875 Tables: []QueryExpression{Dual{}}, 876 }, 877 }, 878 }, 879 }, 880 } 881 expect := "(column1, column2) > ALL (SELECT 1 FROM DUAL)" 882 if e.String() != expect { 883 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 884 } 885 } 886 887 func TestAny_String(t *testing.T) { 888 e := Any{ 889 LHS: RowValue{ 890 Value: ValueList{ 891 Values: []QueryExpression{ 892 Identifier{Literal: "column1"}, 893 Identifier{Literal: "column2"}, 894 }, 895 }, 896 }, 897 Operator: Token{Token: '>', Literal: ">"}, 898 Values: Subquery{ 899 Query: SelectQuery{ 900 SelectEntity: SelectEntity{ 901 SelectClause: SelectClause{ 902 Fields: []QueryExpression{ 903 NewIntegerValueFromString("1"), 904 }, 905 }, 906 FromClause: FromClause{ 907 Tables: []QueryExpression{Dual{}}, 908 }, 909 }, 910 }, 911 }, 912 } 913 expect := "(column1, column2) > ANY (SELECT 1 FROM DUAL)" 914 if e.String() != expect { 915 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 916 } 917 } 918 919 func TestLike_IsNegated(t *testing.T) { 920 e := Like{} 921 if e.IsNegated() == true { 922 t.Errorf("negation = %t, want %t for %#v", e.IsNegated(), false, e) 923 } 924 925 e = Like{Negation: Token{Token: NOT, Literal: "not"}} 926 if e.IsNegated() == false { 927 t.Errorf("negation = %t, want %t for %#v", e.IsNegated(), true, e) 928 } 929 } 930 931 func TestLike_String(t *testing.T) { 932 e := Like{ 933 LHS: Identifier{Literal: "column"}, 934 Pattern: NewStringValue("pattern"), 935 Negation: Token{Token: NOT, Literal: "not"}, 936 } 937 expect := "column NOT LIKE 'pattern'" 938 if e.String() != expect { 939 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 940 } 941 } 942 943 func TestExists_String(t *testing.T) { 944 e := Exists{ 945 Query: Subquery{ 946 Query: SelectQuery{ 947 SelectEntity: SelectEntity{ 948 SelectClause: SelectClause{ 949 Fields: []QueryExpression{ 950 NewIntegerValueFromString("1"), 951 }, 952 }, 953 FromClause: FromClause{ 954 Tables: []QueryExpression{Dual{}}, 955 }, 956 }, 957 }, 958 }, 959 } 960 expect := "EXISTS (SELECT 1 FROM DUAL)" 961 if e.String() != expect { 962 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 963 } 964 } 965 966 func TestArithmetic_String(t *testing.T) { 967 e := Arithmetic{ 968 LHS: Identifier{Literal: "column"}, 969 Operator: Token{Token: '+', Literal: "+"}, 970 RHS: NewIntegerValueFromString("2"), 971 } 972 expect := "column + 2" 973 if e.String() != expect { 974 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 975 } 976 } 977 978 func TestUnaryArithmetic_String(t *testing.T) { 979 e := UnaryArithmetic{ 980 Operand: Identifier{Literal: "column"}, 981 Operator: Token{Token: '-', Literal: "-"}, 982 } 983 expect := "-column" 984 if e.String() != expect { 985 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 986 } 987 } 988 989 func TestLogic_String(t *testing.T) { 990 e := Logic{ 991 LHS: NewTernaryValueFromString("true"), 992 Operator: Token{Token: AND, Literal: "and"}, 993 RHS: NewTernaryValueFromString("false"), 994 } 995 expect := "TRUE AND FALSE" 996 if e.String() != expect { 997 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 998 } 999 } 1000 1001 func TestUnaryLogic_String(t *testing.T) { 1002 e := UnaryLogic{ 1003 Operator: Token{Token: NOT, Literal: "not"}, 1004 Operand: NewTernaryValueFromString("false"), 1005 } 1006 expect := "NOT FALSE" 1007 if e.String() != expect { 1008 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1009 } 1010 1011 e = UnaryLogic{ 1012 Operator: Token{Token: '!', Literal: "!"}, 1013 Operand: NewTernaryValueFromString("false"), 1014 } 1015 expect = "!FALSE" 1016 if e.String() != expect { 1017 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1018 } 1019 } 1020 1021 func TestConcat_String(t *testing.T) { 1022 e := Concat{ 1023 Items: []QueryExpression{ 1024 Identifier{Literal: "column"}, 1025 NewStringValue("a"), 1026 }, 1027 } 1028 expect := "column || 'a'" 1029 if e.String() != expect { 1030 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1031 } 1032 } 1033 1034 func TestFunction_String(t *testing.T) { 1035 e := Function{ 1036 Name: "sum", 1037 Args: []QueryExpression{ 1038 Identifier{Literal: "column"}, 1039 }, 1040 } 1041 expect := "SUM(column)" 1042 if e.String() != expect { 1043 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1044 } 1045 1046 e = Function{ 1047 Name: "substring", 1048 Args: []QueryExpression{ 1049 Identifier{Literal: "column"}, 1050 NewIntegerValue(2), 1051 NewIntegerValue(5), 1052 }, 1053 } 1054 expect = "SUBSTRING(column, 2, 5)" 1055 if e.String() != expect { 1056 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1057 } 1058 1059 e = Function{ 1060 Name: "substring", 1061 Args: []QueryExpression{ 1062 Identifier{Literal: "column"}, 1063 NewIntegerValue(2), 1064 }, 1065 From: Token{Token: FROM, Literal: "from"}, 1066 } 1067 expect = "SUBSTRING(column FROM 2)" 1068 if e.String() != expect { 1069 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1070 } 1071 1072 e = Function{ 1073 Name: "substring", 1074 Args: []QueryExpression{ 1075 Identifier{Literal: "column"}, 1076 NewIntegerValue(2), 1077 NewIntegerValue(5), 1078 }, 1079 From: Token{Token: FROM, Literal: "from"}, 1080 For: Token{Token: FOR, Literal: "for"}, 1081 } 1082 expect = "SUBSTRING(column FROM 2 FOR 5)" 1083 if e.String() != expect { 1084 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1085 } 1086 } 1087 1088 func TestAggregateFunction_String(t *testing.T) { 1089 e := AggregateFunction{ 1090 Name: "sum", 1091 Distinct: Token{Token: DISTINCT, Literal: "distinct"}, 1092 Args: []QueryExpression{ 1093 FieldReference{Column: Identifier{Literal: "column"}}, 1094 }, 1095 } 1096 expect := "SUM(DISTINCT column)" 1097 if e.String() != expect { 1098 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1099 } 1100 } 1101 1102 func TestAggregateFunction_IsDistinct(t *testing.T) { 1103 e := AggregateFunction{} 1104 if e.IsDistinct() == true { 1105 t.Errorf("distinct = %t, want %t for %#v", e.IsDistinct(), false, e) 1106 } 1107 1108 e = AggregateFunction{Distinct: Token{Token: DISTINCT, Literal: "distinct"}} 1109 if e.IsDistinct() == false { 1110 t.Errorf("distinct = %t, want %t for %#v", e.IsDistinct(), true, e) 1111 } 1112 } 1113 1114 func TestTable_String(t *testing.T) { 1115 e := Table{ 1116 Object: Identifier{Literal: "table"}, 1117 As: Token{Token: AS, Literal: "as"}, 1118 Alias: Identifier{Literal: "alias"}, 1119 } 1120 expect := "table AS alias" 1121 if e.String() != expect { 1122 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1123 } 1124 1125 e = Table{ 1126 Object: Identifier{Literal: "table"}, 1127 } 1128 expect = "table" 1129 if e.String() != expect { 1130 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1131 } 1132 1133 e = Table{ 1134 Lateral: Token{Token: LATERAL, Literal: "lateral"}, 1135 Object: Identifier{Literal: "table"}, 1136 } 1137 expect = "LATERAL table" 1138 if e.String() != expect { 1139 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1140 } 1141 } 1142 1143 func TestJoin_String(t *testing.T) { 1144 e := Join{ 1145 Table: Table{Object: Identifier{Literal: "table1"}}, 1146 JoinTable: Table{Object: Identifier{Literal: "table2"}}, 1147 Natural: Token{Token: NATURAL, Literal: "natural"}, 1148 } 1149 expect := "table1 NATURAL JOIN table2" 1150 if e.String() != expect { 1151 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1152 } 1153 1154 e = Join{ 1155 Table: Table{Object: Identifier{Literal: "table1"}}, 1156 JoinTable: Table{Object: Identifier{Literal: "table2"}}, 1157 JoinType: Token{Token: OUTER, Literal: "outer"}, 1158 Direction: Token{Token: LEFT, Literal: "left"}, 1159 Condition: JoinCondition{ 1160 Using: []QueryExpression{ 1161 Identifier{Literal: "column"}, 1162 }, 1163 }, 1164 } 1165 expect = "table1 LEFT OUTER JOIN table2 USING (column)" 1166 if e.String() != expect { 1167 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1168 } 1169 } 1170 1171 func TestJoinCondition_String(t *testing.T) { 1172 e := JoinCondition{ 1173 On: Comparison{ 1174 LHS: Identifier{Literal: "column"}, 1175 Operator: Token{Token: '>', Literal: ">"}, 1176 RHS: NewIntegerValueFromString("1"), 1177 }, 1178 } 1179 expect := "ON column > 1" 1180 if e.String() != expect { 1181 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1182 } 1183 1184 e = JoinCondition{ 1185 Using: []QueryExpression{ 1186 Identifier{Literal: "column1"}, 1187 Identifier{Literal: "column2"}, 1188 }, 1189 } 1190 expect = "USING (column1, column2)" 1191 if e.String() != expect { 1192 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1193 } 1194 } 1195 1196 func TestField_String(t *testing.T) { 1197 e := Field{ 1198 Object: Identifier{Literal: "column"}, 1199 As: Token{Token: AS, Literal: "as"}, 1200 Alias: Identifier{Literal: "alias"}, 1201 } 1202 expect := "column AS alias" 1203 if e.String() != expect { 1204 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1205 } 1206 1207 e = Field{ 1208 Object: Identifier{Literal: "column"}, 1209 } 1210 expect = "column" 1211 if e.String() != expect { 1212 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1213 } 1214 } 1215 1216 func TestField_Name(t *testing.T) { 1217 location, _ := time.LoadLocation("UTC") 1218 1219 e := Field{ 1220 Object: Identifier{Literal: "column"}, 1221 As: Token{Token: AS, Literal: "as"}, 1222 Alias: Identifier{Literal: "alias"}, 1223 } 1224 expect := "alias" 1225 if e.Name() != expect { 1226 t.Errorf("name = %q, want %q for %#v", e.Name(), expect, e) 1227 } 1228 1229 e = Field{ 1230 Object: Identifier{Literal: "column"}, 1231 } 1232 expect = "column" 1233 if e.Name() != expect { 1234 t.Errorf("name = %q, want %q for %#v", e.Name(), expect, e) 1235 } 1236 1237 e = Field{ 1238 Object: NewStringValue("foo"), 1239 } 1240 expect = "foo" 1241 if e.Name() != expect { 1242 t.Errorf("name = %q, want %q for %#v", e.Name(), expect, e) 1243 } 1244 1245 e = Field{ 1246 Object: NewDatetimeValueFromString("2012-01-01 00:00:00 +00:00", nil, location), 1247 } 1248 expect = "2012-01-01 00:00:00 +00:00" 1249 if e.Name() != expect { 1250 t.Errorf("name = %q, want %q for %#v", e.Name(), expect, e) 1251 } 1252 1253 e = Field{ 1254 Object: FieldReference{ 1255 View: Identifier{Literal: "tbl"}, 1256 Column: Identifier{Literal: "column1"}, 1257 }, 1258 } 1259 expect = "column1" 1260 if e.Name() != expect { 1261 t.Errorf("name = %q, want %q for %#v", e.Name(), expect, e) 1262 } 1263 } 1264 1265 func TestAllColumns_String(t *testing.T) { 1266 e := AllColumns{} 1267 if e.String() != "*" { 1268 t.Errorf("string = %q, want %q for %#v", e.String(), "*", e) 1269 } 1270 } 1271 1272 func TestDual_String(t *testing.T) { 1273 s := "DUAL" 1274 e := Dual{} 1275 if e.String() != s { 1276 t.Errorf("string = %q, want %q for %#v", e.String(), s, e) 1277 } 1278 } 1279 1280 func TestStdin_String(t *testing.T) { 1281 s := "STDIN" 1282 e := Stdin{} 1283 if e.String() != s { 1284 t.Errorf("string = %q, want %q for %#v", e.String(), s, e) 1285 } 1286 } 1287 1288 func TestOrderItem_String(t *testing.T) { 1289 e := OrderItem{ 1290 Value: Identifier{Literal: "column"}, 1291 Direction: Token{Token: DESC, Literal: "desc"}, 1292 } 1293 expect := "column DESC" 1294 if e.String() != expect { 1295 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1296 } 1297 1298 e = OrderItem{ 1299 Value: Identifier{Literal: "column"}, 1300 } 1301 expect = "column" 1302 if e.String() != expect { 1303 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1304 } 1305 1306 e = OrderItem{ 1307 Value: Identifier{Literal: "column"}, 1308 NullsPosition: Token{Token: FIRST, Literal: "first"}, 1309 } 1310 expect = "column NULLS FIRST" 1311 if e.String() != expect { 1312 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1313 } 1314 } 1315 1316 func TestCase_String(t *testing.T) { 1317 e := CaseExpr{ 1318 Value: Identifier{Literal: "column"}, 1319 When: []QueryExpression{ 1320 CaseExprWhen{ 1321 Condition: NewIntegerValueFromString("1"), 1322 Result: NewStringValue("A"), 1323 }, 1324 CaseExprWhen{ 1325 Condition: NewIntegerValueFromString("2"), 1326 Result: NewStringValue("B"), 1327 }, 1328 }, 1329 Else: CaseExprElse{Result: NewStringValue("C")}, 1330 } 1331 expect := "CASE column WHEN 1 THEN 'A' WHEN 2 THEN 'B' ELSE 'C' END" 1332 if e.String() != expect { 1333 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1334 } 1335 1336 e = CaseExpr{ 1337 When: []QueryExpression{ 1338 CaseExprWhen{ 1339 Condition: Comparison{ 1340 LHS: Identifier{Literal: "column"}, 1341 Operator: Token{Token: '>', Literal: ">"}, 1342 RHS: NewIntegerValueFromString("1"), 1343 }, 1344 Result: NewStringValue("A"), 1345 }, 1346 }, 1347 } 1348 expect = "CASE WHEN column > 1 THEN 'A' END" 1349 if e.String() != expect { 1350 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1351 } 1352 } 1353 1354 func TestCaseWhen_String(t *testing.T) { 1355 e := CaseExprWhen{ 1356 Condition: Comparison{ 1357 LHS: Identifier{Literal: "column"}, 1358 Operator: Token{Token: '>', Literal: ">"}, 1359 RHS: NewIntegerValueFromString("1"), 1360 }, 1361 Result: NewStringValue("abcde"), 1362 } 1363 expect := "WHEN column > 1 THEN 'abcde'" 1364 if e.String() != expect { 1365 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1366 } 1367 } 1368 1369 func TestCaseElse_String(t *testing.T) { 1370 e := CaseExprElse{Result: NewStringValue("abcde")} 1371 expect := "ELSE 'abcde'" 1372 if e.String() != expect { 1373 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1374 } 1375 } 1376 1377 func TestListFunction_String(t *testing.T) { 1378 e := ListFunction{ 1379 Name: "listagg", 1380 Distinct: Token{Token: DISTINCT, Literal: "distinct"}, 1381 Args: []QueryExpression{ 1382 Identifier{Literal: "column1"}, 1383 NewStringValue(","), 1384 }, 1385 OrderBy: OrderByClause{ 1386 Items: []QueryExpression{Identifier{Literal: "column1"}}, 1387 }, 1388 } 1389 expect := "LISTAGG(DISTINCT column1, ',') WITHIN GROUP (ORDER BY column1)" 1390 if e.String() != expect { 1391 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1392 } 1393 1394 e = ListFunction{ 1395 Name: "listagg", 1396 Distinct: Token{Token: DISTINCT, Literal: "distinct"}, 1397 Args: []QueryExpression{ 1398 Identifier{Literal: "column1"}, 1399 NewStringValue(","), 1400 }, 1401 } 1402 expect = "LISTAGG(DISTINCT column1, ',')" 1403 if e.String() != expect { 1404 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1405 } 1406 } 1407 1408 func TestListFunction_IsDistinct(t *testing.T) { 1409 e := ListFunction{} 1410 if e.IsDistinct() == true { 1411 t.Errorf("distinct = %t, want %t for %#v", e.IsDistinct(), false, e) 1412 } 1413 1414 e = ListFunction{Distinct: Token{Token: DISTINCT, Literal: "distinct"}} 1415 if e.IsDistinct() == false { 1416 t.Errorf("distinct = %t, want %t for %#v", e.IsDistinct(), true, e) 1417 } 1418 } 1419 1420 func TestAnalyticFunction_String(t *testing.T) { 1421 e := AnalyticFunction{ 1422 Name: "avg", 1423 Distinct: Token{Token: DISTINCT, Literal: "distinct"}, 1424 Args: []QueryExpression{ 1425 Identifier{Literal: "column4"}, 1426 }, 1427 IgnoreType: Token{Token: NULLS, Literal: "nulls"}, 1428 AnalyticClause: AnalyticClause{ 1429 PartitionClause: PartitionClause{ 1430 Values: []QueryExpression{ 1431 Identifier{Literal: "column1"}, 1432 Identifier{Literal: "column2"}, 1433 }, 1434 }, 1435 OrderByClause: OrderByClause{ 1436 Items: []QueryExpression{ 1437 OrderItem{Value: Identifier{Literal: "column3"}}, 1438 }, 1439 }, 1440 }, 1441 } 1442 expect := "AVG(DISTINCT column4 IGNORE NULLS) OVER (PARTITION BY column1, column2 ORDER BY column3)" 1443 if e.String() != expect { 1444 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1445 } 1446 } 1447 1448 func TestAnalyticFunction_IsDistinct(t *testing.T) { 1449 e := AnalyticFunction{} 1450 if e.IsDistinct() == true { 1451 t.Errorf("distinct = %t, want %t for %#v", e.IsDistinct(), false, e) 1452 } 1453 1454 e = AnalyticFunction{Distinct: Token{Token: DISTINCT, Literal: "distinct"}} 1455 if e.IsDistinct() == false { 1456 t.Errorf("distinct = %t, want %t for %#v", e.IsDistinct(), true, e) 1457 } 1458 } 1459 1460 func TestAnalyticFunction_IgnoreNulls(t *testing.T) { 1461 e := AnalyticFunction{} 1462 if e.IgnoreNulls() == true { 1463 t.Errorf("IgnoreNulls() = %t, want %t for %#v", e.IgnoreNulls(), false, e) 1464 } 1465 1466 e = AnalyticFunction{IgnoreType: Token{Token: NULLS, Literal: "nulls"}} 1467 if e.IgnoreNulls() == false { 1468 t.Errorf("IgnoreNulls() = %t, want %t for %#v", e.IgnoreNulls(), true, e) 1469 } 1470 } 1471 1472 func TestAnalyticClause_String(t *testing.T) { 1473 e := AnalyticClause{ 1474 PartitionClause: PartitionClause{ 1475 Values: []QueryExpression{ 1476 Identifier{Literal: "column1"}, 1477 Identifier{Literal: "column2"}, 1478 }, 1479 }, 1480 OrderByClause: OrderByClause{ 1481 Items: []QueryExpression{ 1482 OrderItem{Value: Identifier{Literal: "column3"}}, 1483 }, 1484 }, 1485 WindowingClause: WindowingClause{ 1486 FrameLow: WindowFramePosition{ 1487 Direction: Token{Token: CURRENT, Literal: "current"}, 1488 }, 1489 }, 1490 } 1491 expect := "PARTITION BY column1, column2 ORDER BY column3 ROWS CURRENT ROW" 1492 if e.String() != expect { 1493 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1494 } 1495 } 1496 1497 func TestAnalyticClause_PartitionValues(t *testing.T) { 1498 e := AnalyticClause{ 1499 PartitionClause: PartitionClause{ 1500 Values: []QueryExpression{ 1501 Identifier{Literal: "column1"}, 1502 Identifier{Literal: "column2"}, 1503 }, 1504 }, 1505 } 1506 expect := []QueryExpression{ 1507 Identifier{Literal: "column1"}, 1508 Identifier{Literal: "column2"}, 1509 } 1510 if !reflect.DeepEqual(e.PartitionValues(), expect) { 1511 t.Errorf("partition values = %q, want %q for %#v", e.PartitionValues(), expect, e) 1512 } 1513 1514 e = AnalyticClause{} 1515 expect = []QueryExpression(nil) 1516 if !reflect.DeepEqual(e.PartitionValues(), expect) { 1517 t.Errorf("partition values = %q, want %q for %#v", e.PartitionValues(), expect, e) 1518 } 1519 } 1520 1521 func TestPartition_String(t *testing.T) { 1522 e := PartitionClause{ 1523 Values: []QueryExpression{ 1524 Identifier{Literal: "column1"}, 1525 Identifier{Literal: "column2"}, 1526 }, 1527 } 1528 expect := "PARTITION BY column1, column2" 1529 if e.String() != expect { 1530 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1531 } 1532 } 1533 1534 func TestWindowingClause_String(t *testing.T) { 1535 e := WindowingClause{ 1536 FrameLow: WindowFramePosition{ 1537 Direction: Token{Token: CURRENT, Literal: "current"}, 1538 }, 1539 } 1540 expect := "ROWS CURRENT ROW" 1541 if e.String() != expect { 1542 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1543 } 1544 1545 e = WindowingClause{ 1546 FrameLow: WindowFramePosition{ 1547 Direction: Token{Token: PRECEDING, Literal: "preceding"}, 1548 Offset: 1, 1549 }, 1550 FrameHigh: WindowFramePosition{ 1551 Direction: Token{Token: FOLLOWING, Literal: "following"}, 1552 Unbounded: Token{Token: UNBOUNDED, Literal: "unbounded"}, 1553 }, 1554 } 1555 expect = "ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING" 1556 if e.String() != expect { 1557 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1558 } 1559 } 1560 1561 func TestVariable_String(t *testing.T) { 1562 e := Variable{ 1563 Name: "var", 1564 } 1565 expect := "@var" 1566 if e.String() != expect { 1567 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1568 } 1569 } 1570 1571 func TestVariableSubstitution_String(t *testing.T) { 1572 e := VariableSubstitution{ 1573 Variable: Variable{ 1574 Name: "var", 1575 }, 1576 Value: NewIntegerValueFromString("1"), 1577 } 1578 expect := "@var := 1" 1579 if e.String() != expect { 1580 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1581 } 1582 } 1583 1584 func TestEnvironmentVariable_String(t *testing.T) { 1585 e := EnvironmentVariable{ 1586 Name: "envvar", 1587 } 1588 expect := "@%envvar" 1589 if e.String() != expect { 1590 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1591 } 1592 1593 e = EnvironmentVariable{ 1594 Name: "envvar", 1595 Quoted: true, 1596 } 1597 expect = "@%`envvar`" 1598 if e.String() != expect { 1599 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1600 } 1601 } 1602 1603 func TestRuntimeInformation_String(t *testing.T) { 1604 e := RuntimeInformation{ 1605 Name: "ri", 1606 } 1607 expect := "@#RI" 1608 if e.String() != expect { 1609 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1610 } 1611 } 1612 1613 func TestFlag_String(t *testing.T) { 1614 e := Flag{ 1615 Name: "flag", 1616 } 1617 expect := "@@FLAG" 1618 if e.String() != expect { 1619 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1620 } 1621 } 1622 1623 func TestCursorStatus_String(t *testing.T) { 1624 e := CursorStatus{ 1625 Cursor: Identifier{Literal: "cur"}, 1626 Negation: Token{Token: NOT, Literal: "not"}, 1627 Type: Token{Token: RANGE, Literal: "range"}, 1628 } 1629 expect := "CURSOR cur IS NOT IN RANGE" 1630 if e.String() != expect { 1631 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1632 } 1633 1634 e = CursorStatus{ 1635 Cursor: Identifier{Literal: "cur"}, 1636 Type: Token{Token: OPEN, Literal: "open"}, 1637 } 1638 expect = "CURSOR cur IS OPEN" 1639 if e.String() != expect { 1640 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1641 } 1642 } 1643 1644 func TestCursorAttrebute_String(t *testing.T) { 1645 e := CursorAttrebute{ 1646 Cursor: Identifier{Literal: "cur"}, 1647 Attrebute: Token{Token: COUNT, Literal: "count"}, 1648 } 1649 expect := "CURSOR cur COUNT" 1650 if e.String() != expect { 1651 t.Errorf("string = %q, want %q for %#v", e.String(), expect, e) 1652 } 1653 }