Repository: ClickHouse/ClickHouse
File: tests/integration/test_storage_delta/test.py (Python)

Code before:
    )
    assert len(result.strip().split("\n")) == 1

    instance.query("SYSTEM FLUSH LOGS")
    scanned_files = int(
        instance.query(
            f"SELECT ProfileEvents['DeltaLakeScannedFiles'] FROM system.query_log "
            f"WHERE query_id = '{query_id}' AND type = 'QueryFinish'"
        )
    )

    assert scanned_files > 0
    assert full_scan_files == num_data_files

    # Check which shutdown path was hit by querying text_log
    first_check_hits = instance.query(
        f"SELECT count() FROM system.text_log WHERE query_id = '{query_id}' AND message LIKE '%shutdown detected at first check%'"
    ).strip()
    queue_check_hits = instance.query(
        f"SELECT count() FROM system.text_log WHERE query_id = '{query_id}' AND message LIKE '%shutdown detected after queue wait%'"
    ).strip()

    print(f"First shutdown check hits: {first_check_hits}")
    print(f"Queue shutdown check hits: {queue_check_hits}")

    assert first_check_hits or queue_check_hits

    assert 1 == int(instance.query(
        f"SELECT count() FROM system.text_log WHERE query_id = '{query_id}' AND message LIKE '%List batch size is 1/1, shutdown: true%'"
    ))


    # Early return should scan significantly fewer files
    # With s3_list_object_keys_size=1, queue pauses frequently forcing shutdown checks
    # Should stop very early after consuming just a few files
    assert scanned_files < full_scan_files, \
        f"Early return should scan fewer files: {scanned_files} >= {full_scan_files}"
    # 3 because:
    # we have async reader creation with 2 existing readers at a moment of time,
    # each calls next() and consumes 2 files from the scan.
    # It takes 1 file for the query to stop because of LIMIT 1.
    # But because scan is also asynchronous and continues once batch limit is not reached,
    # we get +1 scanned file.
    assert scanned_files == 3, \
        f"Early return should scan 3 files with LIMIT 1, but scanned {scanned_files}"




Review comment (suggestion):
`first_check_hits` / `queue_check_hits` are strings; if the query returns `"0"`, it is truthy in Python, so this assertion can pass even when both counts are zero. Convert to `int` and assert `> 0` for at least one of them (e.g., `assert int(first_check_hits) > 0 or int(queue_check_hits) > 0`).
```suggestion
    first_check_hits = int(first_check_hits)
    queue_check_hits = int(queue_check_hits)

    print(f"First shutdown check hits: {first_check_hits}")
    print(f"Queue shutdown check hits: {queue_check_hits}")

    assert first_check_hits > 0 or queue_check_hits > 0
```

Code after:
    )
    assert len(result.strip().split("\n")) == 1

    instance.query("SYSTEM FLUSH LOGS")
    scanned_files = int(
        instance.query(
            f"SELECT ProfileEvents['DeltaLakeScannedFiles'] FROM system.query_log "
            f"WHERE query_id = '{query_id}' AND type = 'QueryFinish'"
        )
    )

    assert scanned_files > 0
    assert full_scan_files == num_data_files

    # Check which shutdown path was hit by querying text_log
    first_check_hits = instance.query(
        f"SELECT count() FROM system.text_log WHERE query_id = '{query_id}' AND message LIKE '%shutdown detected at first check%'"
    ).strip()
    queue_check_hits = instance.query(
        f"SELECT count() FROM system.text_log WHERE query_id = '{query_id}' AND message LIKE '%shutdown detected after queue wait%'"
    ).strip()

    first_check_hits = int(first_check_hits)
    queue_check_hits = int(queue_check_hits)

    print(f"First shutdown check hits: {first_check_hits}")
    print(f"Queue shutdown check hits: {queue_check_hits}")

    assert first_check_hits > 0 or queue_check_hits > 0

    assert 1 == int(instance.query(
        f"SELECT count() FROM system.text_log WHERE query_id = '{query_id}' AND message LIKE '%List batch size is 1/1, shutdown: true%'"
    ))


    # Early return should scan significantly fewer files
    # With s3_list_object_keys_size=1, queue pauses frequently forcing shutdown checks
    # Should stop very early after consuming just a few files
    assert scanned_files < full_scan_files, \
        f"Early return should scan fewer files: {scanned_files} >= {full_scan_files}"
    # 3 because:
    # we have async reader creation with 2 existing readers at a moment of time,
    # each calls next() and consumes 2 files from the scan.
    # It takes 1 file for the query to stop because of LIMIT 1.
    # But because scan is also asynchronous and continues once batch limit is not reached,
    # we get +1 scanned file.
    assert scanned_files == 3, \
        f"Early return should scan 3 files with LIMIT 1, but scanned {scanned_files}"