...

Source file src/cloud.google.com/go/bigquery/internal/query/order_test.go

Documentation: cloud.google.com/go/bigquery/internal/query

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package query
    16  
    17  import "testing"
    18  
    19  func TestDetectOrderedResults(t *testing.T) {
    20  	testCases := []struct {
    21  		query     string
    22  		isOrdered bool
    23  	}{
    24  		{query: "SELECT 1 AS num FROM a ORDER BY num", isOrdered: true},
    25  		{query: "SELECT 1 AS num FROM a", isOrdered: false},
    26  		{query: "SELECT 1 AS num FROM (SELECT x FROM b ORDER BY x)", isOrdered: false},
    27  		{query: "SELECT x AS num FROM (SELECT x FROM b ORDER BY x) a inner join b on b.x = a.x ORDER BY x", isOrdered: true},
    28  		{query: "SELECT SUM(1) FROM a", isOrdered: false},
    29  		{query: "SELECT COUNT(a.x) FROM a ORDER BY b", isOrdered: true},
    30  		{query: "SELECT FOO(a.x, a.y) FROM a ORDER BY a", isOrdered: true},
    31  		// Invalid queries
    32  		{query: "SELECT 1 AS num FROM a (", isOrdered: false},
    33  		{query: "SELECT 1 AS num FROM a )", isOrdered: false},
    34  		// Script statements
    35  		{query: "CALL foo()", isOrdered: false},
    36  		{query: "DECLARE x INT64", isOrdered: false},
    37  		{query: "SET x = 4;", isOrdered: false},
    38  	}
    39  
    40  	for _, tc := range testCases {
    41  		got := HasOrderedResults(tc.query)
    42  		if got != tc.isOrdered {
    43  			if tc.isOrdered {
    44  				t.Fatalf("expected query `%s` to be ordered", tc.query)
    45  			} else {
    46  				t.Fatalf("expected query `%s` to not be ordered", tc.query)
    47  			}
    48  
    49  		}
    50  	}
    51  }
    52  

View as plain text