forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: support float approximation in roachtest query comparison utils
Before this change unoptimized query oracle tests would compare results using simple string comparison. However, due to floating point precision limitations, it's possible for results with floating point to diverge during the course of normal computation. This results in test failures that are difficult to reproduce or determine whether they are expected behavior. This change utilizes existing floating point comparison functions used by logic tests to match float values only to a specific precision. Like the logic tests, we also have special handling for floats and decimals under the s390x architecture (see cockroachdb#63244). In order to avoid costly comparisons, we only check floating point precision if the naiive string comparison approach fails and there are float or decimal types in the result. Epic: None Fixes: cockroachdb#95665 Release note: None
- Loading branch information
1 parent
3ee36a1
commit 7c1d19e
Showing
6 changed files
with
251 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright 2023 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 tests | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
) | ||
|
||
// TestUnsortedMatricesDiff is a unit test for the | ||
// unsortedMatricesDiffWithFloatComp() and unsortedMatricesDiff() utility | ||
// functions. | ||
func TestUnsortedMatricesDiff(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
tcs := []struct { | ||
name string | ||
colTypes []string | ||
t1, t2 [][]string | ||
exactMatch bool | ||
approxMatch bool | ||
}{ | ||
{ | ||
name: "float exact match", | ||
colTypes: []string{"FLOAT8"}, | ||
t1: [][]string{{"1.2345678901234567"}}, | ||
t2: [][]string{{"1.2345678901234567"}}, | ||
exactMatch: true, | ||
}, | ||
{ | ||
name: "float approx match", | ||
colTypes: []string{"FLOAT8"}, | ||
t1: [][]string{{"1.2345678901234563"}}, | ||
t2: [][]string{{"1.2345678901234564"}}, | ||
exactMatch: false, | ||
approxMatch: true, | ||
}, | ||
{ | ||
name: "float no match", | ||
colTypes: []string{"FLOAT8"}, | ||
t1: [][]string{{"1.234567890123"}}, | ||
t2: [][]string{{"1.234567890124"}}, | ||
exactMatch: false, | ||
approxMatch: false, | ||
}, | ||
{ | ||
name: "multi float approx match", | ||
colTypes: []string{"FLOAT8", "FLOAT8"}, | ||
t1: [][]string{{"1.2345678901234567", "1.2345678901234567"}}, | ||
t2: [][]string{{"1.2345678901234567", "1.2345678901234568"}}, | ||
exactMatch: false, | ||
approxMatch: true, | ||
}, | ||
{ | ||
name: "string no match", | ||
colTypes: []string{"STRING"}, | ||
t1: [][]string{{"hello"}}, | ||
t2: [][]string{{"world"}}, | ||
exactMatch: false, | ||
approxMatch: false, | ||
}, | ||
{ | ||
name: "mixed types match", | ||
colTypes: []string{"STRING", "FLOAT8"}, | ||
t1: [][]string{{"hello", "1.2345678901234567"}}, | ||
t2: [][]string{{"hello", "1.2345678901234567"}}, | ||
exactMatch: true, | ||
}, | ||
{ | ||
name: "mixed types float approx match", | ||
colTypes: []string{"STRING", "FLOAT8"}, | ||
t1: [][]string{{"hello", "1.23456789012345678"}}, | ||
t2: [][]string{{"hello", "1.23456789012345679"}}, | ||
exactMatch: false, | ||
approxMatch: true, | ||
}, | ||
{ | ||
name: "mixed types no match", | ||
colTypes: []string{"STRING", "FLOAT8"}, | ||
t1: [][]string{{"hello", "1.2345678901234567"}}, | ||
t2: [][]string{{"world", "1.2345678901234567"}}, | ||
exactMatch: false, | ||
approxMatch: false, | ||
}, | ||
{ | ||
name: "different col count", | ||
colTypes: []string{"STRING"}, | ||
t1: [][]string{{"hello", "1.2345678901234567"}}, | ||
t2: [][]string{{"world", "1.2345678901234567"}}, | ||
exactMatch: false, | ||
approxMatch: false, | ||
}, | ||
{ | ||
name: "different row count", | ||
colTypes: []string{"STRING", "FLOAT8"}, | ||
t1: [][]string{{"hello", "1.2345678901234567"}, {"aloha", "2.345"}}, | ||
t2: [][]string{{"world", "1.2345678901234567"}}, | ||
exactMatch: false, | ||
approxMatch: false, | ||
}, | ||
{ | ||
name: "multi row unsorted", | ||
colTypes: []string{"STRING", "FLOAT8"}, | ||
t1: [][]string{{"hello", "1.2345678901234567"}, {"world", "1.2345678901234560"}}, | ||
t2: [][]string{{"world", "1.2345678901234560"}, {"hello", "1.2345678901234567"}}, | ||
exactMatch: true, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
match := unsortedMatricesDiff(tc.t1, tc.t2) | ||
if tc.exactMatch && match != "" { | ||
t.Fatalf("unsortedMatricesDiff: expected exact match, got diff: %s", match) | ||
} else if !tc.exactMatch && match == "" { | ||
t.Fatalf("unsortedMatricesDiff: expected no exact match, got no diff") | ||
} | ||
|
||
var err error | ||
match, err = unsortedMatricesDiffWithFloatComp(tc.t1, tc.t2, tc.colTypes) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if tc.exactMatch && match != "" { | ||
t.Fatalf("unsortedMatricesDiffWithFloatComp: expected exact match, got diff: %s", match) | ||
} else if !tc.exactMatch && tc.approxMatch && match != "" { | ||
t.Fatalf("unsortedMatricesDiffWithFloatComp: expected approx match, got diff: %s", match) | ||
} else if !tc.exactMatch && !tc.approxMatch && match == "" { | ||
t.Fatalf("unsortedMatricesDiffWithFloatComp: expected no approx match, got no diff") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters