Skip to content

feat: implement CONDITIONAL_TRUE_EVENT and CONDITIONAL_CHANGE_EVENT window functions#25

Merged
sivchari merged 1 commit intomainfrom
feature/conditional-event-functions
Feb 6, 2026
Merged

feat: implement CONDITIONAL_TRUE_EVENT and CONDITIONAL_CHANGE_EVENT window functions#25
sivchari merged 1 commit intomainfrom
feature/conditional-event-functions

Conversation

@sivchari
Copy link
Copy Markdown
Owner

@sivchari sivchari commented Feb 6, 2026

概要

Phase 8 の最終機能として、Snowflake 互換の Window Functions CONDITIONAL_TRUE_EVENTCONDITIONAL_CHANGE_EVENT を実装しました。

変更内容

新規ファイル

  • engine/src/functions/window.rs (392 行)
    • CONDITIONAL_TRUE_EVENT: 条件が TRUE のときカウンタをインクリメント
    • CONDITIONAL_CHANGE_EVENT: 値が前の行から変化したときカウンタをインクリメント
    • DataFusion の WindowUDFImpl および PartitionEvaluator トレイトを実装
    • 複数のデータ型に対応 (Int, Float, String, Boolean, Date/Time)

変更ファイル

  • engine/src/functions/mod.rs

    • window モジュールの export 追加
    • ドキュメント更新
  • engine/src/executor.rs

    • Window UDF の登録 (register_udwf)
    • 統合テスト 2 件追加

技術詳細

DataFusion API 対応

  • DataFusion 45 の新しい API に対応
    • PartitionEvaluatorArgs の引数追加
    • WindowUDFFieldArgs の使用
  • evaluate_all() メソッドでパーティション全体を一括処理
  • supports_bounded_execution() = false で最適なパフォーマンス

機能詳細

CONDITIONAL_TRUE_EVENT

SELECT CONDITIONAL_TRUE_EVENT(active) OVER (ORDER BY id) FROM table;
  • boolean 条件が TRUE になるたびにカウンタをインクリメント
  • NULL は FALSE として扱う
  • カウンタは 0 から開始し、各パーティション内で独立

CONDITIONAL_CHANGE_EVENT

SELECT CONDITIONAL_CHANGE_EVENT(status) OVER (ORDER BY id) FROM table;
  • 値が前の行から変化したときにカウンタをインクリメント
  • 最初の行は 0 (比較対象なし)
  • NULL と非 NULL の間の変化も検出
  • 複数のデータ型に対応

テスト結果

テスト種別 件数 結果
Rust ユニットテスト 199 ✅ Pass
Server 統合テスト 7 ✅ Pass
CI チェック (fmt) - ✅ Pass
CI チェック (clippy) - ✅ Pass

追加テスト

  • window.rs: 5 ユニットテスト
    • test_conditional_true_event_basic
    • test_conditional_true_event_with_nulls
    • test_conditional_change_event_basic
    • test_conditional_change_event_with_nulls
    • test_conditional_change_event_strings
  • executor.rs: 2 統合テスト
    • test_conditional_true_event
    • test_conditional_change_event

Phase 8 完了

Phase 8 の全ての Window Functions が実装完了:

  • ✅ FIRST_VALUE / LAST_VALUE (Phase 5 で実装済み)
  • ✅ NTH_VALUE (Phase 5 で実装済み)
  • ✅ LEAD / LAG (既存実装)
  • ✅ NTILE (Phase 5 で実装済み)
  • ✅ PERCENT_RANK / CUME_DIST (Phase 8)
  • ✅ RATIO_TO_REPORT (Phase 8)
  • ✅ CONDITIONAL_TRUE_EVENT (この PR)
  • ✅ CONDITIONAL_CHANGE_EVENT (この PR)

チェックリスト

  • コンパイル成功
  • 全テストパス
  • フォーマットチェックパス
  • Clippy チェックパス
  • ドキュメント更新
  • ユニットテスト追加
  • 統合テスト追加

関連

…indow functions

## Summary
Implemented Snowflake-compatible window functions CONDITIONAL_TRUE_EVENT and CONDITIONAL_CHANGE_EVENT using DataFusion's WindowUDFImpl and PartitionEvaluator traits.

## Changes
- Add engine/src/functions/window.rs
  - CONDITIONAL_TRUE_EVENT: increments counter when condition is TRUE
  - CONDITIONAL_CHANGE_EVENT: increments counter when value changes
  - WindowUDFImpl and PartitionEvaluator implementations
  - Support for multiple data types (Int, Float, String, Boolean, Date/Time)

- Update engine/src/functions/mod.rs
  - Add window module export
  - Update documentation

- Update engine/src/executor.rs
  - Register Window UDFs (register_udwf)
  - Add integration tests for both functions

## Technical Details
- Uses DataFusion 45 API (PartitionEvaluatorArgs, WindowUDFFieldArgs)
- Implements evaluate_all() for full partition processing
- supports_bounded_execution() = false for optimal performance
- Handles NULL values correctly
- Array value comparison across types

## Tests
- Unit tests: 5 tests in window.rs
- Integration tests: 2 tests in executor.rs
- All tests passing (199 unit + 7 integration)

## Completes Phase 8
All Phase 8 window functions now implemented:
- FIRST_VALUE/LAST_VALUE (Phase 5)
- NTH_VALUE (Phase 5)
- LEAD/LAG (existing)
- NTILE (Phase 5)
- PERCENT_RANK/CUME_DIST (Phase 8)
- RATIO_TO_REPORT (Phase 8)
- CONDITIONAL_TRUE_EVENT (this PR)
- CONDITIONAL_CHANGE_EVENT (this PR)
@sivchari sivchari merged commit 37ddfaa into main Feb 6, 2026
1 check passed
@sivchari sivchari deleted the feature/conditional-event-functions branch February 6, 2026 02:42
@github-actions github-actions bot mentioned this pull request Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant