mirror of https://github.com/buster-so/buster.git
hotfix on sql dialect
This commit is contained in:
parent
4464abfc22
commit
523a6921b1
|
@ -398,42 +398,7 @@ Once all TODO list items are addressed and submitted for review, the system will
|
|||
|
||||
<sql_best_practices>
|
||||
- Current SQL Dialect Guidance:
|
||||
- **Date/Time Functions (PostgreSQL/Supabase)**:
|
||||
- **\`DATE_TRUNC\`**: Prefer \`DATE_TRUNC('day', column)\`, \`DATE_TRUNC('week', column)\`, \`DATE_TRUNC('month', column)\`, etc., for grouping time series data. Note that \`'week'\` starts on Monday.
|
||||
- **\`EXTRACT\`**: \`EXTRACT(DOW FROM column)\` (0=Sun), \`EXTRACT(ISODOW FROM column)\` (1=Mon), \`EXTRACT(WEEK FROM column)\`, \`EXTRACT(EPOCH FROM column)\` (Unix timestamp).
|
||||
- **Intervals**: Use \`INTERVAL '1 day'\`, \`INTERVAL '1 month'\`, etc.
|
||||
- **Current Date/Time**: \`CURRENT_DATE\`, \`CURRENT_TIMESTAMP\`, \`NOW()\`.
|
||||
- **Performance Tips**:
|
||||
- Use indexes on date columns: \`CREATE INDEX ON table (DATE_TRUNC('day', date_col))\` for frequent grouping
|
||||
- Prefer \`EXISTS\` over \`IN\` for subqueries with large result sets
|
||||
- Use \`LIMIT\` rather than fetching all rows when possible
|
||||
- For time ranges, use \`column >= start_date AND column < end_date\` (avoids timezone issues)
|
||||
- **String Handling**: Use \`ILIKE\` for case-insensitive matching, \`SIMILAR TO\` for regex patterns
|
||||
- **Aggregations**: Use \`FILTER (WHERE condition)\` for conditional aggregations instead of \`CASE WHEN\`
|
||||
- **JSON Operations**: PostgreSQL excels at JSON - use \`->\`, \`->>\`, \`@>\`, \`?\` operators for JSON queries
|
||||
- **Window Functions**: Powerful for analytics - \`LAG()\`, \`LEAD()\`, \`FIRST_VALUE()\`, \`PERCENT_RANK()\`
|
||||
- **Date Spine Example**: For complete time series, use \`generate_series()\`:
|
||||
\`\`\`sql
|
||||
WITH date_spine AS (
|
||||
SELECT generate_series(
|
||||
DATE_TRUNC('month', CURRENT_DATE - INTERVAL '11 months'),
|
||||
DATE_TRUNC('month', CURRENT_DATE),
|
||||
INTERVAL '1 month'
|
||||
)::date AS period_date
|
||||
)
|
||||
SELECT
|
||||
ds.period_date,
|
||||
COALESCE(SUM(t.amount), 0) AS total_amount
|
||||
FROM date_spine ds
|
||||
LEFT JOIN schema.transactions t ON DATE_TRUNC('month', t.date) = ds.period_date
|
||||
GROUP BY ds.period_date
|
||||
ORDER BY ds.period_date;
|
||||
\`\`\`
|
||||
- **Common Gotchas**:
|
||||
- \`NOW()\` returns timestamp with timezone, \`CURRENT_TIMESTAMP\` is standard SQL
|
||||
- String concatenation: Use \`||\` not \`+\`
|
||||
- \`NULL\` comparisons: Use \`IS NULL\`/\`IS NOT NULL\`, never \`= NULL\`
|
||||
- Performance: Ensure date/timestamp columns used in \`WHERE\` or \`JOIN\` clauses are indexed. Consider functional indexes on \`DATE_TRUNC\` or \`EXTRACT\` expressions if filtering/grouping by them frequently.
|
||||
${params.sqlDialectGuidance}
|
||||
- Keep Queries Simple: Strive for simplicity and clarity in your SQL. Adhere as closely as possible to the user's direct request without overcomplicating the logic or making unnecessary assumptions.
|
||||
- Default Time Range: If the user does not specify a time range for analysis, default to the last 12 months from the current date. Clearly state this assumption if making it.
|
||||
- Avoid Bold Assumptions: Do not make complex or bold assumptions about the user's intent or the underlying data. If the request is highly ambiguous beyond a reasonable time frame assumption, indicate this limitation in your final response.
|
||||
|
|
Loading…
Reference in New Issue