-
Notifications
You must be signed in to change notification settings - Fork 1
Feat/redisvl integration tests #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,8 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| import dataclasses | ||
| from dataclasses import dataclass | ||
|
|
||
| import sqlglot | ||
| from sqlglot import exp | ||
|
|
@@ -15,6 +16,9 @@ class AggregationSpec: | |
| function: str | ||
| field: str | None = None | ||
| alias: str | None = None | ||
| extra_args: list[str] = dataclasses.field( | ||
| default_factory=list | ||
| ) # For reducers like QUANTILE | ||
|
|
||
|
|
||
| @dataclass | ||
|
|
@@ -49,14 +53,14 @@ class ParsedQuery: | |
| """Result of parsing a SQL query.""" | ||
|
|
||
| index: str = "" | ||
| fields: list[str] = field(default_factory=list) | ||
| conditions: list[Condition] = field(default_factory=list) | ||
| fields: list[str] = dataclasses.field(default_factory=list) | ||
| conditions: list[Condition] = dataclasses.field(default_factory=list) | ||
| boolean_operator: str = "AND" | ||
| aggregations: list[AggregationSpec] = field(default_factory=list) | ||
| computed_fields: list[ComputedField] = field(default_factory=list) | ||
| aggregations: list[AggregationSpec] = dataclasses.field(default_factory=list) | ||
| computed_fields: list[ComputedField] = dataclasses.field(default_factory=list) | ||
| vector_search: VectorSearchSpec | None = None | ||
| groupby_fields: list[str] = field(default_factory=list) | ||
| orderby_fields: list[tuple[str, str]] = field( | ||
| groupby_fields: list[str] = dataclasses.field(default_factory=list) | ||
| orderby_fields: list[tuple[str, str]] = dataclasses.field( | ||
| default_factory=list | ||
| ) # (field, ASC|DESC) | ||
| limit: int | None = None | ||
|
|
@@ -150,9 +154,28 @@ def _process_select_expression_inner( | |
| result.fields.append(expression.name) | ||
| elif isinstance(expression, exp.Star): | ||
| result.fields.append("*") | ||
| elif isinstance(expression, (exp.Count, exp.Sum, exp.Avg, exp.Min, exp.Max)): | ||
| elif isinstance( | ||
| expression, | ||
| ( | ||
| exp.Count, | ||
| exp.Sum, | ||
| exp.Avg, | ||
| exp.Min, | ||
| exp.Max, | ||
| exp.Stddev, | ||
| exp.Variance, | ||
| exp.FirstValue, | ||
| exp.ArrayAgg, | ||
| ), | ||
| ): | ||
| # Aggregation function | ||
| # Map sqlglot function names to Redis reducer names | ||
| func_name = expression.key.upper() | ||
| redis_func_map = { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add these to enable all available reducers from redis |
||
| "FIRSTVALUE": "FIRST_VALUE", | ||
| "ARRAYAGG": "TOLIST", | ||
| } | ||
| func_name = redis_func_map.get(func_name, func_name) | ||
| field_name = None | ||
| # Get the field being aggregated (if any) | ||
| if expression.this: | ||
|
|
@@ -184,10 +207,34 @@ def _process_select_expression_inner( | |
| # - Distance: L2/Euclidean distance | ||
| # - CosineDistance: cosine_distance() function | ||
| self._process_vector_distance(expression, result, alias) | ||
| elif isinstance(expression, exp.Quantile): | ||
| # QUANTILE(field, quantile_value) -> REDUCE QUANTILE 2 @field quantile_value | ||
| field_name = None | ||
| if expression.this and isinstance(expression.this, exp.Column): | ||
| field_name = expression.this.name | ||
| quantile_value = None | ||
| if expression.args.get("quantile"): | ||
| quantile_value = str(expression.args["quantile"].this) | ||
| extra_args = [quantile_value] if quantile_value else [] | ||
| result.aggregations.append( | ||
| AggregationSpec( | ||
| function="QUANTILE", | ||
| field=field_name, | ||
| alias=alias, | ||
| extra_args=extra_args, | ||
| ) | ||
| ) | ||
| elif isinstance(expression, exp.Anonymous): | ||
| # Custom function call (e.g., vector_distance) - check before exp.Func | ||
| # since Anonymous is a subclass of Func | ||
| func_name = expression.name.lower() | ||
| # Redis-specific reducer functions that sqlglot doesn't recognize | ||
| redis_reducers = { | ||
| "count_distinct", | ||
| "count_distinctish", | ||
| "quantile", | ||
| "random_sample", | ||
| } | ||
| if func_name == "vector_distance": | ||
| # Extract the vector field name from first argument | ||
| if expression.expressions: | ||
|
|
@@ -198,6 +245,26 @@ def _process_select_expression_inner( | |
| field=field_name, | ||
| alias=alias or func_name, | ||
| ) | ||
| elif func_name in redis_reducers: | ||
| # Redis-specific reducer functions | ||
| field_name = None | ||
| reducer_extra_args: list[str] = [] | ||
| if expression.expressions: | ||
| first_arg = expression.expressions[0] | ||
| if isinstance(first_arg, exp.Column): | ||
| field_name = first_arg.name | ||
| # Extract additional arguments (e.g., quantile value for QUANTILE) | ||
| for arg in expression.expressions[1:]: | ||
| if isinstance(arg, exp.Literal): | ||
| reducer_extra_args.append(str(arg.this)) | ||
| result.aggregations.append( | ||
| AggregationSpec( | ||
| function=func_name.upper(), | ||
| field=field_name, | ||
| alias=alias, | ||
| extra_args=reducer_extra_args, | ||
| ) | ||
| ) | ||
| else: | ||
| # Other custom functions - treat as computed field | ||
| expr_str = expression.sql() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -189,11 +189,17 @@ def _build_search( | |
| args.append("2") | ||
| params["vector"] = None # Placeholder for vector bytes | ||
|
|
||
| # RETURN clause | ||
| if parsed.fields and parsed.fields != ["*"]: | ||
| # RETURN clause - include vector distance alias if present | ||
| return_fields = list(parsed.fields) if parsed.fields else [] | ||
| if analyzed.vector_search and analyzed.vector_search.alias: | ||
| # Add vector distance alias to return fields (like VectorQuery with return_score=True) | ||
| if analyzed.vector_search.alias not in return_fields: | ||
| return_fields.append(analyzed.vector_search.alias) | ||
|
|
||
| if return_fields and return_fields != ["*"]: | ||
| args.append("RETURN") | ||
| args.append(str(len(parsed.fields))) | ||
| args.extend(parsed.fields) | ||
| args.append(str(len(return_fields))) | ||
| args.extend(return_fields) | ||
|
|
||
| # SORTBY | ||
| if parsed.orderby_fields: | ||
|
|
@@ -251,8 +257,15 @@ def _build_aggregate( | |
| for agg in analyzed.aggregations: | ||
| args.append("REDUCE") | ||
| args.append(agg.function.upper()) | ||
| if agg.field: | ||
| args.extend(["1", f"@{agg.field}"]) | ||
| # COUNT always takes 0 arguments in Redis | ||
| if agg.function.upper() == "COUNT": | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. count reducer had a bug because unlike the rest of the reducers it takes 0 args |
||
| args.append("0") | ||
| elif agg.field: | ||
| # Calculate nargs: 1 for field + number of extra args | ||
| nargs = 1 + len(agg.extra_args) | ||
| args.append(str(nargs)) | ||
| args.append(f"@{agg.field}") | ||
| args.extend(agg.extra_args) | ||
| else: | ||
| args.append("0") | ||
| if agg.alias: | ||
|
|
@@ -263,8 +276,15 @@ def _build_aggregate( | |
| for agg in analyzed.aggregations: | ||
| args.append("REDUCE") | ||
| args.append(agg.function.upper()) | ||
| if agg.field: | ||
| args.extend(["1", f"@{agg.field}"]) | ||
| # COUNT always takes 0 arguments in Redis | ||
| if agg.function.upper() == "COUNT": | ||
| args.append("0") | ||
| elif agg.field: | ||
| # Calculate nargs: 1 for field + number of extra args | ||
| nargs = 1 + len(agg.extra_args) | ||
| args.append(str(nargs)) | ||
| args.append(f"@{agg.field}") | ||
| args.extend(agg.extra_args) | ||
| else: | ||
| args.append("0") | ||
| # Always provide an alias | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quantile takes 2 arguments instead of 1 like the others