Skip to content

Commit c2dc909

Browse files
committed
*: fix fix pushdown cast to datetime from float for tikv
Signed-off-by: xhe <xw897002528@gmail.com>
1 parent 0a7bdc9 commit c2dc909

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

‎pkg/executor/test/simpletest/simple_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,3 +819,29 @@ func TestSelectWhereInvalidDSTTime(t *testing.T) {
819819
"Warning 8179 Timestamp is not valid, since it is in Daylight Saving Time transition '{2025 3 30 2 30 0 0}' for time zone 'Europe/Amsterdam'",
820820
"Warning 8179 Timestamp is not valid, since it is in Daylight Saving Time transition '{2025 3 30 2 30 0 0}' for time zone 'Europe/Amsterdam'"))
821821
}
822+
823+
824+
// TestIssue44135 tests that CAST(float AS DATETIME) returns consistent results
825+
// See https://github.com/pingcap/tidb/issues/44135
826+
func TestIssue44135(t *testing.T) {
827+
store := testkit.CreateMockStore(t)
828+
tk := testkit.NewTestKit(t, store)
829+
tk.MustExec("use test")
830+
831+
// Case 1: With generated column
832+
tk.MustExec("DROP TABLE IF EXISTS t0")
833+
tk.MustExec("CREATE TABLE t0(`c0` float GENERATED ALWAYS AS (`c1`), `c1` float)")
834+
tk.MustExec("INSERT INTO t0(c1) VALUES (0.5822439)")
835+
result1 := tk.MustQuery("SELECT * FROM t0 WHERE (~ (CAST(c0 AS DATETIME)))")
836+
837+
// Case 2: Without generated column (base column only)
838+
tk.MustExec("DROP TABLE IF EXISTS t1")
839+
tk.MustExec("CREATE TABLE t1(`c1` float)")
840+
tk.MustExec("INSERT INTO t1(c1) VALUES (0.5822439)")
841+
result2 := tk.MustQuery("SELECT * FROM t1 WHERE (~ (CAST(c1 AS DATETIME)))")
842+
843+
// Both queries should return consistent results (either both return the row or both return empty)
844+
rows1 := result1.Rows()
845+
rows2 := result2.Rows()
846+
require.Equal(t, len(rows1), len(rows2), "CAST(float AS DATETIME) should return consistent results for generated column and base column")
847+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2024 PingCAP, Inc.
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 pushdowntest
16+
17+
import (
18+
"testing"
19+
20+
"github.com/pingcap/tidb/pkg/testkit"
21+
"github.com/pingcap/tidb/tests/realtikvtest"
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
// TestIssue44135WithTiKV tests that CAST(float AS DATETIME) returns consistent results
26+
// for both generated columns and base columns when running with real TiKV.
27+
// See https://github.com/pingcap/tidb/issues/44135
28+
func TestIssue44135WithTiKV(t *testing.T) {
29+
store := realtikvtest.CreateMockStoreAndSetup(t)
30+
tk := testkit.NewTestKit(t, store)
31+
tk.MustExec("use test")
32+
33+
// Case 1: With generated column
34+
tk.MustExec("DROP TABLE IF EXISTS t0")
35+
tk.MustExec("CREATE TABLE t0(`c0` float GENERATED ALWAYS AS (`c1`), `c1` float)")
36+
tk.MustExec("INSERT INTO t0(c1) VALUES (0.5822439)")
37+
result1 := tk.MustQuery("SELECT * FROM t0 WHERE (~ (CAST(c0 AS DATETIME)))")
38+
39+
// Case 2: Without generated column (base column only)
40+
tk.MustExec("DROP TABLE IF EXISTS t1")
41+
tk.MustExec("CREATE TABLE t1(`c1` float)")
42+
tk.MustExec("INSERT INTO t1(c1) VALUES (0.5822439)")
43+
result2 := tk.MustQuery("SELECT * FROM t1 WHERE (~ (CAST(c1 AS DATETIME)))")
44+
45+
// Both queries should return consistent results (either both return the row or both return empty)
46+
rows1 := result1.Rows()
47+
rows2 := result2.Rows()
48+
require.Equal(t, len(rows1), len(rows2), "CAST(float AS DATETIME) should return consistent results for generated column and base column")
49+
}

0 commit comments

Comments
 (0)