This repository was archived by the owner on May 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
feat(memory): seed query_history with canonical NL-SQL pairs on index #1510
Merged
Merged
Changes from 1 commit
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| """Generate canonical NL-SQL seed pairs from an MDL manifest. | ||
|
|
||
| Pure functions — no LanceDB or embedding dependency. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| _NUMERIC_TYPES = { | ||
| "int", | ||
| "integer", | ||
| "bigint", | ||
| "smallint", | ||
| "tinyint", | ||
| "float", | ||
| "double", | ||
| "decimal", | ||
| "numeric", | ||
| "real", | ||
| "number", | ||
| } | ||
|
|
||
| SEED_TAG = "source:seed" | ||
|
|
||
|
|
||
| def generate_seed_queries(manifest: dict) -> list[dict]: | ||
| """Return a list of {"nl": ..., "sql": ...} seed pairs.""" | ||
| pairs: list[dict] = [] | ||
|
|
||
| for model in manifest.get("models", []): | ||
| pairs.extend(_model_seeds(model)) | ||
|
|
||
| for rel in manifest.get("relationships", []): | ||
| pair = _relationship_seed(rel) | ||
| if pair: | ||
| pairs.append(pair) | ||
|
|
||
| return pairs | ||
|
|
||
|
|
||
| def _model_seeds(model: dict) -> list[dict]: | ||
| name = model["name"] | ||
| columns = model.get("columns", []) | ||
| pairs = [] | ||
|
|
||
| # Template 1: basic listing | ||
| pairs.append( | ||
| { | ||
| "nl": f"List all {name}", | ||
| "sql": f"SELECT * FROM {name} LIMIT 100", | ||
| } | ||
| ) | ||
|
|
||
| # Find first numeric column (non-calculated) and first groupable column | ||
| numeric_col = None | ||
| group_col = None | ||
| for col in columns: | ||
| col_type = (col.get("type") or "").split("(")[0].lower().strip() | ||
| is_calc = col.get("isCalculated", False) | ||
| is_pk = col["name"] == model.get("primaryKey") | ||
|
|
||
| if col_type in _NUMERIC_TYPES and not is_calc and numeric_col is None: | ||
| numeric_col = col["name"] | ||
| elif ( | ||
| col_type not in _NUMERIC_TYPES | ||
| and not is_pk | ||
| and not is_calc | ||
| and group_col is None | ||
| ): | ||
| group_col = col["name"] | ||
|
|
||
| # Template 2a: simple aggregation | ||
| if numeric_col: | ||
| pairs.append( | ||
| { | ||
| "nl": f"Total {numeric_col} in {name}", | ||
| "sql": f"SELECT SUM({numeric_col}) FROM {name}", | ||
| } | ||
| ) | ||
|
|
||
| # Template 2b: grouped aggregation | ||
| if numeric_col and group_col: | ||
| pairs.append( | ||
| { | ||
| "nl": f"{numeric_col} by {group_col} in {name}", | ||
| "sql": f"SELECT {group_col}, SUM({numeric_col}) FROM {name} GROUP BY 1", | ||
| } | ||
| ) | ||
|
|
||
| return pairs | ||
|
|
||
|
|
||
| def _relationship_seed(rel: dict) -> dict | None: | ||
| models = rel.get("models", []) | ||
| condition = rel.get("condition", "") | ||
|
|
||
| if len(models) < 2 or not condition: | ||
| return None | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| left, right = models[0], models[1] | ||
|
|
||
| return { | ||
| "nl": f"{left} with {right} details", | ||
| "sql": f"SELECT * FROM {left} JOIN {right} ON {condition} LIMIT 100", | ||
| } | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.