# Copyright 2024 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# Preparation.
mysql> drop table if exists test.t;

# Build vector index on table with data on the stable layer
mysql> CREATE TABLE test.t (`v` vector(5) DEFAULT NULL, VECTOR INDEX idx_v_l2 USING HNSW ((VEC_L2_DISTANCE(v))), VECTOR INDEX idx_v_cos USING HNSW ((VEC_COSINE_DISTANCE(v))) );
mysql> INSERT INTO test.t VALUES ('[8.7, 5.7, 7.7, 9.8, 1.5]'),('[3.6, 9.7, 2.4, 6.6, 4.9]'),('[4.7, 4.9, 2.6, 5.2, 7.4]'),('[7.7, 6.7, 8.3, 7.8, 5.7]'),('[1.4, 4.5, 8.5, 7.7, 6.2]');
func> wait_table test t
mysql> alter table test.t compact tiflash replica;
mysql> select index_name,rows_stable_indexed,rows_stable_not_indexed from information_schema.tiflash_indexes where TIDB_DATABASE = 'test' AND TIDB_TABLE = 't';
+------------+---------------------+-------------------------+
| index_name | rows_stable_indexed | rows_stable_not_indexed |
+------------+---------------------+-------------------------+
| idx_v_l2   |                   5 |                       0 |
| idx_v_cos  |                   5 |                       0 |
+------------+---------------------+-------------------------+

mysql> set tidb_isolation_read_engines='tiflash';select VEC_DIMS(v) from test.t;
+-------------+
| VEC_DIMS(v) |
+-------------+
|           5 |
|           5 |
|           5 |
|           5 |
|           5 |
+-------------+

mysql> set tidb_isolation_read_engines='tiflash';select count(v),count(distinct v),min(v),max(v) from test.t;
+----------+-------------------+-----------------------+-----------------------+
| count(v) | count(distinct v) | min(v)                | max(v)                |
+----------+-------------------+-----------------------+-----------------------+
|        5 |                 5 | [1.4,4.5,8.5,7.7,6.2] | [8.7,5.7,7.7,9.8,1.5] |
+----------+-------------------+-----------------------+-----------------------+

mysql> set tidb_isolation_read_engines='tiflash';select v,count(1) from test.t group by v order by v;
+-----------------------+----------+
| v                     | count(1) |
+-----------------------+----------+
| [1.4,4.5,8.5,7.7,6.2] |        1 |
| [3.6,9.7,2.4,6.6,4.9] |        1 |
| [4.7,4.9,2.6,5.2,7.4] |        1 |
| [7.7,6.7,8.3,7.8,5.7] |        1 |
| [8.7,5.7,7.7,9.8,1.5] |        1 |
+-----------------------+----------+

# Cleanup
mysql> drop table if exists test.t
