Skip to content

Conversation

@vim89
Copy link
Owner

@vim89 vim89 commented Sep 22, 2025

Summary

Changes Made

Core Implementation

  • AggAnyOps.scala (new): Provides .count and .countDistinct methods for Expr[T]
  • AggOps.scala (modified): Adds .countBy() and .countDistinctBy() methods for grouped queries
  • Dialect.scala (modified): Adds implicit conversion for AggAnyOps integration

API Features

  • Purchase.select.map(_.productId).countCOUNT(product_id)
  • Purchase.select.map(_.productId).countDistinctCOUNT(DISTINCT product_id)
  • Purchase.select.countBy(_.productId)COUNT(product_id)
  • Purchase.select.countDistinctBy(_.productId)COUNT(DISTINCT product_id)
  • Window function support: .countBy(_.col).over.partitionBy(...)
  • Proper NULL semantics: COUNT ignores NULL values, consistent with SQL standards

Test Coverage

  • DbCountOpsTests.scala (new): Basic COUNT operations, GROUP BY, filters, joins
  • DbCountOpsAdvancedTests.scala (new): Complex types, edge cases, Option handling
  • Cross-dialect testing for PostgreSQL, MySQL, SQLite, H2, MS SQL Server
  • Window function and CTE integration tests

Documentation

  • tutorial.md: Added COUNT examples after existing .size documentation
  • reference.md: Comprehensive API reference with SQL output examples
  • cheatsheet.md: Updated aggregate functions table and quick reference
  • Window functions: Added COUNT support documentation

Test Plan

  • Compiles successfully for Scala 2.13.12 and 3.6.2
  • All existing tests continue to pass
  • New COUNT operations work across all supported database dialects
  • Proper NULL handling verified with Option types
  • Window functions integrate correctly with existing patterns
  • Documentation examples validated against actual API

  Implements comprehensive COUNT operations for ScalaSQL addressing issue com-lihaoyi#95:

  Core Implementation:
  - Add AggAnyOps.scala with count/countDistinct methods for Expr[T]
  - Add countBy/countDistinctBy methods to AggOps for grouped queries
  - Add implicit conversion in Dialect for AggAnyOps integration

  API Features:
  - count/countDistinct: COUNT(expr) and COUNT(DISTINCT expr)
  - countBy/countDistinctBy: COUNT(column) and COUNT(DISTINCT column)
  - Full cross-dialect support (PostgreSQL, MySQL, SQLite, H2, MS SQL)
  - Proper NULL handling (COUNT ignores NULL values)
  - Window function support with OVER clauses
  - Integration with existing aggregation patterns

  Test Coverage:
  - Basic COUNT operations and edge cases
  - Option type handling with NULL database values
  - Complex type support (UUID, BigDecimal, DateTime, Boolean)
  - GROUP BY with COUNT aggregations
  - Window functions, filters, joins, and complex expressions
  - Cross-platform compatibility testing

  Documentation:
  - Updated tutorial.md with COUNT examples and patterns
  - Added comprehensive reference.md API documentation
  - Updated cheatsheet.md with COUNT operations
  - Added window function documentation for COUNT
@vim89 vim89 merged commit 6d466e3 into main Sep 22, 2025
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.

2 participants