‹ 首页

bigquery-sql

@signalpilot-labs · 收录于 5 天前 · 上游提交 6 天前

BigQuery-specific SQL patterns: UNNEST for array expansion, STRUCT, ARRAY_AGG, DATE_DIFF/DATE_ADD, backtick-quoted table references, EXCEPT/REPLACE in SELECT, approximate aggregation, partitioned and wildcard tables.

适合你,如果经常用 BigQuery 写复杂 SQL 查询

/ 通过 npx 安装 校验哈希
npx oh-my-skill add signalpilot-labs/signalpilot/bigquery-sql
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- signalpilot-labs/signalpilot/bigquery-sql
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify signalpilot-labs/signalpilot/bigquery-sql
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
473GitHub stars
~919上下文体积 · 单文件
索引托管

怎么用

商店整理自技能原文 · 版本 436a4c4 · 表述以原文为准
它做什么

Claude 会使用 BigQuery 专属的 SQL 写法,比如用 UNNEST 展开数组、STRUCT 和 ARRAY_AGG 处理结构体与数组、DATE_DIFF 等日期函数、反引号引用表名、SELECT EXCEPT/REPLACE 排除或替换列、近似聚合、分区表过滤、通配符表查询等。

什么时候触发

当你询问 BigQuery SQL 的写法、查询优化或表操作时自动触发,例如要求“写一个 BigQuery 查询”或“如何 UNNEST 数组”。

装好后可以这样说
Claude 会给出包含 UNNEST 的示例。
技能原文 SKILL.md作者撰写 · Apache-2.0 · 436a4c4

BigQuery SQL Skill

1. Table References - Always Backtick-Quote
-- Full table reference
SELECT * FROM `project.dataset.table`;

-- Can omit project if using the default project
SELECT * FROM `dataset.table`;
2. Array Expansion - Use UNNEST
-- Explode an array column to rows
SELECT id, item
FROM `project.dataset.table`,
UNNEST(array_col) AS item;

-- UNNEST with offset (position)
SELECT id, item, pos
FROM `project.dataset.table`,
UNNEST(array_col) AS item WITH OFFSET AS pos;

-- UNNEST a literal array
SELECT * FROM UNNEST([1, 2, 3]) AS num;
3. Date Functions
-- Add/subtract time
DATE_ADD(order_date, INTERVAL 7 DAY)
DATE_ADD(CURRENT_DATE(), INTERVAL -1 MONTH)

-- Difference between dates
DATE_DIFF(end_date, start_date, DAY)
DATE_DIFF(end_date, start_date, MONTH)

-- Truncate to period
DATE_TRUNC(event_date, MONTH)
TIMESTAMP_TRUNC(event_ts, HOUR)

-- Current date/time
CURRENT_DATE()
CURRENT_TIMESTAMP()
4. SELECT EXCEPT and REPLACE
-- All columns except one
SELECT * EXCEPT (col_to_remove) FROM `dataset.table`;

-- Replace a column value inline
SELECT * REPLACE (UPPER(name) AS name) FROM `dataset.table`;
5. STRUCT and ARRAY_AGG
-- Create a STRUCT
SELECT STRUCT(id, name) AS person FROM `dataset.table`;

-- Aggregate rows into an array
SELECT department, ARRAY_AGG(employee_name) AS employees
FROM `dataset.employees`
GROUP BY department;

-- Aggregate into array of structs
SELECT ARRAY_AGG(STRUCT(id, name)) AS records FROM `dataset.table`;
6. Approximate Aggregation (for large tables)
-- Approximate distinct count (faster for large tables)
APPROX_COUNT_DISTINCT(user_id)

-- Approximate quantiles
APPROX_QUANTILES(value, 100)[OFFSET(50)]  -- median
7. Partitioned Tables

When querying partitioned tables, always filter on the partition column to avoid full-table scans:

-- Partition on _PARTITIONDATE (pseudo-column)
WHERE _PARTITIONDATE >= '2024-01-01'

-- Partition on a date column
WHERE event_date BETWEEN '2024-01-01' AND '2024-12-31'
8. Wildcard Tables (date-sharded)
-- Query all date-sharded tables matching a prefix
SELECT * FROM `project.dataset.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20240101' AND '20241231';
9. String Functions
REGEXP_EXTRACT(col, r'pattern')          -- extract first match
REGEXP_REPLACE(col, r'pattern', 'repl')  -- replace matches
SPLIT(col, ',')[SAFE_OFFSET(0)]          -- split, access by index
TRIM(col) / LTRIM(col) / RTRIM(col)
FORMAT('%s-%d', str_col, int_col)        -- printf-style formatting
10. Common Anti-Patterns to Avoid
  • Do NOT use = NULL - use IS NULL
  • Do NOT forget to filter partitioned tables - costs money
  • Do NOT use COUNT(DISTINCT ...) on huge tables - use APPROX_COUNT_DISTINCT
  • Always backtick-quote table names with dots in them
11. Benchmark Patterns
  • STRING_AGG: Use STRING_AGG(col, ',' ORDER BY col) for string aggregation (not GROUP_CONCAT).
  • SAFE_DIVIDE / SAFE_CAST: Use to avoid division-by-zero errors and cast failures.
  • IF / IIF: BigQuery supports IF(condition, true_val, false_val) - often cleaner than CASE WHEN for simple conditions.
  • GENERATE_DATE_ARRAY / GENERATE_TIMESTAMP_ARRAY: For date spine generation.
  • Numeric precision: BigQuery's FLOAT64 can lose precision. Use NUMERIC type or ROUND() only when the question asks for it.
  • INFORMATION_SCHEMA: SELECT * FROM dataset.INFORMATION_SCHEMA.COLUMNS for metadata queries - useful when schema_overview is insufficient.
12. Spider2 BigQuery Patterns
  • Default project: spider2-public-data. Table references: spider2-public-data.{dataset}.{table}
  • StackOverflow tags: Stored as pipe-delimited strings in tags column (e.g., |python|python-2.7|). To filter for Python 2 specific questions (excluding Python 3): ```sql WHERE REGEXP_CONTAINS(tags, r'python-2') AND NOT REGEXP_CONTAINS(tags, r'python-3') ```
  • Date columns: Many BQ tables store dates as TIMESTAMP or DATE. Always check the actual type with describe_table.
  • Large tables: Use partition filters and LIMIT during exploration. Avoid SELECT * on tables with >1M rows.
按 Apache-2.0 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。