Skip to content

Add support for array values in WHERE clause of CQL queries #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

eliasjpr
Copy link
Contributor

This pull request introduces support for handling array values in SQL WHERE clauses, improves type handling in the CQL module, and enhances query-building functionality. The changes include updates to allow arrays in conditions, adjustments to type definitions, and modifications to validation and query-building logic.

Support for array values in WHERE clauses:

  • Added test cases to validate handling of array values in WHERE clauses, including single and multiple array conditions (spec/core/query_spec.cr).
  • Updated the where method to accept Hash values containing arrays, enabling more flexible query conditions (src/query.cr).

Type handling improvements:

  • Modified the type property in CQL::Column to support single types and arrays of types (src/column.cr).
  • Updated the validate! method in CQL::Column to handle both single values and arrays, ensuring proper validation for array-based conditions (src/column.cr).

Query-building enhancements:

  • Enhanced the build_condition_from_hash and get_expression methods to handle array values, enabling the generation of IN conditions in SQL queries (src/query.cr). [1] [2]
  • Simplified the build_condition_from_hash method by removing redundant logic and ensuring compatibility with array-based conditions (src/query.cr).

This commit enhances the CQL query functionality by allowing array values in the WHERE clause. It introduces tests to verify that queries can handle single and multiple array conditions correctly. Additionally, the column type validation is updated to accept both single values and arrays, ensuring robust handling of various input types in query conditions.
@eliasjpr eliasjpr requested a review from Copilot May 15, 2025 12:15
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces support for array values in SQL WHERE clauses within the CQL module, improves type handling for columns, and enhances query-building logic. The key changes include:

  • Updating method signatures in query handling to accept arrays.
  • Adjusting column type definitions and validation to support single values and arrays.
  • Adding new tests to verify proper handling of array-based conditions.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/query.cr Updates to where and condition-building methods to handle array values.
src/expression/expressions.cr Adjustments in the Expression::InCondition initialization to accept varying types.
src/column.cr Changes to column type declaration and validation logic to support arrays.
spec/core/query_spec.cr New test cases for verifying correct behavior with array values in selections.

raise Error.new "Expected column `#{name}` to be #{type}, but got #{value.class}"
def validate!(value : T | Array(T)) forall T
return if value.class == JSON::Any
return if value.class == T
Copy link
Preview

Copilot AI May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated validate! method only checks the class of the input value and does not iterate over array elements when an Array is provided, which may cause valid array inputs to be rejected. Consider iterating through the array and validating each element against the expected type.

Suggested change
return if value.class == T
if value.is_a?(Array)
value.each do |element|
unless element.is_a?(T)
raise Error.new "Expected all elements of column `#{name}` to be #{type}, but got #{element.class}"
end
end
return
end
return if value.is_a?(T)

Copilot uses AI. Check for mistakes.

@@ -726,12 +725,24 @@ module CQL
end

# Expects hash with String keys now
private def build_condition_from_hash(hash : Hash(Symbol | String, DB::Any))
private def build_condition_from_hash(hash : Hash(Symbol | String, T | Array(T))) forall T
Copy link
Preview

Copilot AI May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] There are two overloads of build_condition_from_hash with similar logic; consolidating them could reduce duplication and improve code clarity.

Copilot uses AI. Check for mistakes.

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