@@ -1955,3 +1955,91 @@ def test_create_table_like():
19551955 # Clean up
19561956 db_replicator_runner .stop ()
19571957 binlog_replicator_runner .stop ()
1958+
1959+
1960+ def test_year_type ():
1961+ """
1962+ Test that MySQL YEAR type is properly converted to UInt16 in ClickHouse
1963+ and that year values are correctly handled.
1964+ """
1965+ config_file = CONFIG_FILE
1966+ cfg = config .Settings ()
1967+ cfg .load (config_file )
1968+ mysql_config = cfg .mysql
1969+ clickhouse_config = cfg .clickhouse
1970+ mysql = mysql_api .MySQLApi (
1971+ database = None ,
1972+ mysql_settings = mysql_config
1973+ )
1974+ ch = clickhouse_api .ClickhouseApi (
1975+ database = TEST_DB_NAME ,
1976+ clickhouse_settings = clickhouse_config
1977+ )
1978+
1979+ prepare_env (cfg , mysql , ch )
1980+
1981+ mysql .execute (f'''
1982+ CREATE TABLE `{ TEST_TABLE_NAME } ` (
1983+ id INT NOT NULL AUTO_INCREMENT,
1984+ year_field YEAR NOT NULL,
1985+ nullable_year YEAR,
1986+ PRIMARY KEY (id)
1987+ )
1988+ ''' )
1989+
1990+ # Insert test data with various year values
1991+ mysql .execute (f'''
1992+ INSERT INTO `{ TEST_TABLE_NAME } ` (year_field, nullable_year) VALUES
1993+ (2024, 2024),
1994+ (1901, NULL),
1995+ (2155, 2000),
1996+ (2000, 1999);
1997+ ''' , commit = True )
1998+
1999+ run_all_runner = RunAllRunner (cfg_file = config_file )
2000+ run_all_runner .run ()
2001+
2002+ assert_wait (lambda : TEST_DB_NAME in ch .get_databases ())
2003+ ch .execute_command (f'USE `{ TEST_DB_NAME } `' )
2004+ assert_wait (lambda : TEST_TABLE_NAME in ch .get_tables ())
2005+ assert_wait (lambda : len (ch .select (TEST_TABLE_NAME )) == 4 )
2006+
2007+ # Get the ClickHouse data
2008+ results = ch .select (TEST_TABLE_NAME )
2009+
2010+ # Verify the data
2011+ assert results [0 ]['year_field' ] == 2024
2012+ assert results [0 ]['nullable_year' ] == 2024
2013+ assert results [1 ]['year_field' ] == 1901
2014+ assert results [1 ]['nullable_year' ] is None
2015+ assert results [2 ]['year_field' ] == 2155
2016+ assert results [2 ]['nullable_year' ] == 2000
2017+ assert results [3 ]['year_field' ] == 2000
2018+ assert results [3 ]['nullable_year' ] == 1999
2019+
2020+ # Test realtime replication by adding more records
2021+ mysql .execute (f'''
2022+ INSERT INTO `{ TEST_TABLE_NAME } ` (year_field, nullable_year) VALUES
2023+ (2025, 2025),
2024+ (1999, NULL),
2025+ (2100, 2100);
2026+ ''' , commit = True )
2027+
2028+ # Wait for new records to be replicated
2029+ assert_wait (lambda : len (ch .select (TEST_TABLE_NAME )) == 7 )
2030+
2031+ # Verify the new records - include order by in the where clause
2032+ new_results = ch .select (TEST_TABLE_NAME , where = "year_field >= 2025 ORDER BY year_field ASC" )
2033+ assert len (new_results ) == 3
2034+
2035+ # Check specific values
2036+ assert new_results [0 ]['year_field' ] == 2025
2037+ assert new_results [0 ]['nullable_year' ] == 2025
2038+ assert new_results [1 ]['year_field' ] == 2100
2039+ assert new_results [1 ]['nullable_year' ] == 2100
2040+ assert new_results [2 ]['year_field' ] == 2155
2041+ assert new_results [2 ]['nullable_year' ] == 2000
2042+
2043+ run_all_runner .stop ()
2044+ assert_wait (lambda : 'stopping db_replicator' in read_logs (TEST_DB_NAME ))
2045+ assert ('Traceback' not in read_logs (TEST_DB_NAME ))
0 commit comments