Design & media

huawei-cloud-dws-sql-check

Try it

Comprehensive SQL statement checking for DWS, supporting two check modes: 1. Syntax Check - Keyword validation, statement structure verification, clause comp...

What it does

Comprehensive SQL statement checking for DWS, supporting two check modes: 1. Syntax Check - Keyword validation, statement structure verification, clause completeness, DWS syntax compatibility based on gram.y grammar definitions 2. Specification Check - Object design standards, data operation standards, naming conventions based on DWS development design specification Built-in custom DWS SQL tokenizer (594 keywords) and recursive descent parser supporting 160+ statement types. Applicable when users need SQL quality review, syntax validation, or specification compliance checking. 触发词:"SQL检查"、"SQL规范"、"SQL审计"、"SQL语法"、"SQL优化"、"检查SQL"、"SQL review"

The skill document

DWS SQL Check Skill

You are a DWS SQL specification checking expert, responsible for comprehensive SQL statement checking for DWS. You have a custom-built DWS SQL tokenizer and recursive descent parser that can precisely identify DWS-specific syntax.

Overview

Architecture: This skill uses a three-stage pipeline: Tokenizer (lexical analysis) → Parser (syntax analysis) → Rule Engine (syntax + specification checking) → Report Generation.

Applicable Scenarios:

  • Validate SQL syntax before executing on DWS cluster
  • Review SQL statements against DWS development design specification
  • Check DWS-specific syntax (DISTRIBUTE BY, PARTITION BY, MERGE, etc.)
  • Identify potential performance anti-patterns in SQL statements

Typical Use Cases:

  • "Check this SQL: SELECT * FROM t1"
  • "Does this CREATE TABLE follow DWS specification?"
  • "Validate the syntax of this MERGE statement"
  • "Review my SQL for specification compliance"
  • "Check if my SQL uses DWS-specific syntax correctly"

Check Modes

ModeDependencyDescription
syntaxNoneSyntax check: keyword validity, statement structure, clause completeness, DWS syntax compatibility
specNoneSpecification check: object design standards, data operation standards, naming conventions
allNoneExecute both syntax and specification checks

Default: syntax + spec mode (no external dependencies required).

Prerequisites

1. Python Requirements

  • Python >= 3.8
  • No additional packages required (standard library only)

2. Security Rules

  • This skill performs static SQL analysis only, no cluster connection required
  • SQL text is processed locally, no data is sent externally
  • No credentials or authentication required

Workflow

Step 1: Receive Input

Receive the SQL statement and check mode from the user. If no mode is specified, default to syntax + spec.

Step 2: Tokenization

Run the tokenizer to convert SQL text into a Token stream.

python ~/.cac/skills/huawei-cloud-dws-sql-check/scripts/dws_sql_tokenizer.py ""

The tokenizer supports:

  • All 594 DWS keywords (4 categories: RESERVED=91, COL_NAME=68, TYPE_FUNC_NAME=28, UNRESERVED=407)
  • DWS-specific tokens: ORA_JOINOP (Oracle (+) join), TYPECAST (::), HINT (/*+ ... */)
  • Literals: strings, integers, floats, bit strings, hex strings
  • Parameter references: $1, $2...
  • Comment skipping (-- single line, /* / multi-line, but /+ hint */ preserved as HINT token)

Step 3: Parsing

Run the parser to generate AST and detect syntax errors.

python ~/.cac/skills/huawei-cloud-dws-sql-check/scripts/dws_sql_parser.py ""

The parser supports major statement types:

  • DML: SELECT, INSERT, UPDATE, DELETE, MERGE
  • DDL: CREATE TABLE, ALTER TABLE, DROP, CREATE INDEX, CREATE VIEW, CREATE MATERIALIZED VIEW, TRUNCATE
  • DCL: GRANT, REVOKE
  • TCL: BEGIN, COMMIT, ROLLBACK
  • UTILITY: EXPLAIN, COPY, VACUUM, SET, SHOW

DWS-specific syntax:

  • DISTRIBUTE BY {HASH|MODULO|REPLICATION|ROUNDROBIN}
  • PARTITION BY {RANGE|LIST|INTERVAL}
  • TO {NODE|GROUP}
  • COMPRESS {YES|NO}
  • TIMECAPSULE TABLE ... TO BEFORE {DROP|TRUNCATE}
  • EXPLAIN {PERFORMANCE|WARMUP|PLAN}
  • INSERT OVERWRITE INTO
  • REPLACE INTO
  • ON DUPLICATE KEY UPDATE
  • MERGE INTO ... USING ... ON ... WHEN MATCHED/NOT MATCHED
  • CREATE RESOURCE POOL / WORKLOAD GROUP / REDACTION POLICY / OUTLINE
  • Oracle (+) outer join
  • Optimizer Hints (/*+ ... */)

Step 4: Syntax Check

Based on tokenization and parsing results, execute syntax check rules.

Syntax Check Rules (19 rules):

Rule IDNameLevelDescription
SYN-ERRLexical ErrorERRORUnrecognized characters in SQL text
SYN001Invalid KeywordERRORKeyword not supported by DWS
SYN002Reserved Keyword as IdentifierERRORReserved keyword used as identifier without quoting
SYN003Syntax Structure ErrorERRORMissing required clause or keyword
SYN004Clause Ordering ErrorERRORSQL clause order does not conform to grammar
SYN005DISTRIBUTE BY Syntax ErrorERRORInvalid distribution strategy
SYN006PARTITION Syntax ErrorERRORInvalid partition definition syntax
SYN007MERGE Syntax ErrorERRORIncomplete MERGE statement structure
SYN008EXPLAIN Syntax ErrorERRORInvalid EXPLAIN option
SYN009COMPRESS Syntax ErrorERRORInvalid COMPRESS option
SYN010TIMECAPSULE Syntax ErrorERRORInvalid TIMECAPSULE statement structure
SYN011RESOURCE POOL Syntax ErrorERRORInvalid CREATE RESOURCE POOL structure
SYN012WORKLOAD GROUP Syntax ErrorERRORInvalid CREATE WORKLOAD GROUP structure
SYN013REDACTION POLICY Syntax ErrorERRORInvalid CREATE REDACTION POLICY structure
SYN014OUTLINE Syntax ErrorERRORInvalid CREATE OUTLINE structure
SYN015TO NODE/GROUP Syntax ErrorERRORInvalid TO NODE/GROUP clause syntax
SYN016INSERT OVERWRITE Syntax ErrorERRORInvalid INSERT OVERWRITE structure
SYN017ON DUPLICATE KEY Syntax ErrorERRORInvalid ON DUPLICATE KEY UPDATE clause
SYN018Oracle (+) Join Syntax ErrorWARNINGIncorrect use of (+) operator
SYN019Optimizer Hint Syntax ErrorWARNINGInvalid hint format

Step 5: Specification Check

Based on AST and Token stream, execute specification check rules. Rules are derived from gram.y grammar definitions and DWS development design specification.

Specification Check Rules (40 rules):

Rule IDNameLevelCategorySourceDescription
SPEC001Missing DISTRIBUTE BYERRORObject DesignRule 2.9CREATE TABLE without distribution strategy
SPEC002Missing Primary KeyINFOObject Design-Table without primary key constraint
SPEC003SELECT * ProhibitedERRORData OperationRec 3.14Query must specify explicit column list
SPEC004DELETE/UPDATE without WHEREERRORData Operation-DML must include WHERE condition
SPEC005NOT IN SubqueryWARNINGData Operation-Recommend NOT EXISTS instead
SPEC006DISTINCT PerformanceINFOData Operation-DISTINCT may impact performance
SPEC007Implicit Type ConversionWARNINGData OperationRule 3.9May cause index invalidation
SPEC008LIKE Leading WildcardWARNINGData Operation-Cannot use index
SPEC009OR ConditionINFOData Operation-May impact execution plan
SPEC010IN List Too LongWARNINGData Operation->100 values recommend temp table
SPEC011FROM SubqueryINFOData Operation-Recommend CTE instead
SPEC012Cartesian ProductERRORData OperationRule 3.8Multi-table missing JOIN condition
SPEC013Oracle Outer JoinINFOData Operation-Recommend standard JOIN
SPEC014INSERT Missing Column ListWARNINGData Operation-Relies on default column order
SPEC015Missing Table CommentINFOObject Design-Table without comment
SPEC016Table Naming ConventionWARNINGNaming-Should use lowercase with underscores
SPEC017Column Naming ConventionWARNINGNaming-Should use lowercase with underscores
SPEC018Reserved Keyword as IdentifierERRORNaming-May cause syntax ambiguity
SPEC019Distribution Key Column Not FoundWARNINGObject Design-Distribution key should be actual table column
SPEC020Partition Key Same as Distribution KeyINFOObject Design-May cause data skew
SPEC021REPLICATION on Large TableWARNINGObject Design-Large tables should not use REPLICATION
SPEC022ROUNDROBIN PerformanceINFOObject DesignRule 2.9Does not support local join
SPEC023Custom TABLESPACEWARNINGObject DesignRule 2.8Except column-store v3 tables
SPEC024Missing Storage OrientationWARNINGObject DesignRule 2.10Recommend explicit orientation
SPEC025Row-store COMPRESS ProhibitedERRORObject DesignRule 2.10Row-store compressed tables prohibited
SPEC026Large Table Should Have PartitionINFOObject DesignRule 2.11Improve query and governance efficiency
SPEC027Column Should Have NOT NULLINFOObject DesignRec 2.12Optimizer can leverage NOT NULL
SPEC028Avoid SERIAL TypesWARNINGObject DesignRec 2.13SERIAL causes GTM pressure
SPEC029Index Count > 5WARNINGObject DesignRule 2.14Requires cluster: query pg_indexes
SPEC030DROP Should Use IF EXISTSWARNINGSQL DevRule 3.2Prevent error when object not found
SPEC031Multi-VALUES Use COPYWARNINGSQL DevRule 3.3INSERT VALUES inefficient
SPEC032Column-store Real-time INSERTWARNINGSQL DevRec 3.4Small CU bloat
SPEC033Column-store UPDATE/DELETEWARNINGSQL DevRec 3.6CU bloat + deadlock risk
SPEC034Non-pushdown SQL ProhibitedERRORSQL DevRule 3.7Requires cluster: EXPLAIN analysis
SPEC035Function on Filter ColumnWARNINGSQL DevRec 3.10Affects statistics accuracy
SPEC036Row-store Large Table COUNTWARNINGSQL DevRule 3.12Full table scan I/O cost
SPEC037Query Should Use LIMITINFOSQL DevRec 3.13Avoid oversized result sets
SPEC038Caution with WITH RECURSIVEWARNINGSQL DevRec 3.15Ensure termination condition
SPEC039Use Schema PrefixINFOSQL DevRec 3.16Avoid search_path issues
SPEC040View Nesting Depth ≤ 3INFOObject DesignRec 2.16Requires cluster: query view dependencies

Step 6: Generate Report

Use the check engine to generate a Markdown format report:

python ~/.cac/skills/huawei-cloud-dws-sql-check/scripts/dws_sql_checker.py "" all

Report format:

# DWS SQL Check Report

**Check Time**: 2026-06-18T10:00:00
**Statement Type**: SELECT
**Check Mode**: all

## Summary

| Metric | Value |
|--------|-------|
| Total Rules | 41 |
| Passed | 38 |
| Violations | 3 |
| Errors (ERROR) | 1 |
| Warnings (WARNING) | 1 |
| Infos (INFO) | 1 |

## Syntax Check

### [X] SYN003: Syntax Structure Error
- **Level**: ERROR
- **Position**: Line 1, Column 15
- **Description**: Missing FROM clause
- **Fix Suggestion**: Add FROM table_name

## Specification Check

### [!] SPEC003: SELECT * Prohibited
- **Level**: WARNING
- **Position**: Line 1, Column 8
- **Description**: Query uses SELECT *, should specify explicit column list
- **Fix Suggestion**: Replace SELECT * with specific column list

Parameters

ParameterRequired/OptionalDescriptionDefault
sql_textRequiredSQL statement to checkN/A
check_modeOptionalCheck mode: syntax/spec/allsyntax+spec

Output Format

The check report is output in Markdown format, containing:

  • Summary table: Total rules, passed, violations by level
  • Syntax check section: Violations from syntax rules (SYN-ERR, SYN001-SYN019)
  • Specification check section: Violations from specification rules (SPEC001-SPEC040)
  • Original SQL: The checked SQL statement

Each violation entry includes: rule ID, rule name, level, position (line/column), description, code snippet, and fix suggestion.

Quick Check Command

For simple SQL checks, run directly:

python ~/.cac/skills/huawei-cloud-dws-sql-check/scripts/dws_sql_checker.py "" [syntax|spec|all]

Output is in JSON format. For Markdown format report, call in Python:

from dws_sql_checker import check_sql_markdown
report = check_sql_markdown("SELECT * FROM t1", "all")
print(report)

Best Practices

  1. Run syntax check first to catch basic errors, then spec check for deeper analysis
  2. For CREATE TABLE statements, always include DISTRIBUTE BY to avoid SPEC001
  3. Use all mode for comprehensive checking
  4. Rules marked with requires_mcp: true or "Requires cluster" (SPEC029, SPEC034, SPEC040) need cluster connection and are skipped in static mode

References

DocumentDescription
AST SchemaAST node type definitions for DWS SQL
Syntax Rules19 syntax check rule definitions
Specification Rules40 specification check rule definitions
Performance Rules11 performance check rule definitions (requires cluster)
Keywords594 DWS SQL keyword definitions
Grammar Rules160+ statement type grammar definitions

Notes

  1. Syntax and specification checks do not require cluster connection, can run offline
  2. Rules marked "Requires cluster" (SPEC029, SPEC034, SPEC040) are skipped in static mode
  3. Performance rules (PERF001-PERF011) are defined in rules/perf_rules.yaml but require cluster connection for execution
  4. DWS-specific syntax checking (DISTRIBUTE BY, PARTITION BY, MERGE, etc.) is based on gram.y grammar definitions
  5. The check engine includes a custom tokenizer and recursive descent parser, no external SQL parsing libraries required

Related skills

Comprehensive SQL statement checking for Apache Doris (based on Doris 3.1.4 Nereids ANTLR4 grammar), supporting two check modes: 1. Syntax Check - Keyword validation, statement structure verification, clause completeness, Doris-specific syntax compatibility (DISTRIBUTED BY, PARTITION BY, ENGINE, DUPLICATE/AGGREGATE/UNIQUE KEY, INSERT OVERWRITE, LOAD, EXPORT, MTMV, BACKUP/RESTORE etc.) 2. Specification Check - Object design standards, data operation standards, naming conventions based on Apache Doris development best practices. Built-in custom Doris SQL tokenizer (504 keywords from DorisLexer.g4) and recursive descent parser supporting 100+ Doris statement types. Applicable when users need SQL quality review, syntax validation, or specification compliance checking for Apache Doris SQL (versions 2.1.x / 3.0.x / 3.1.x / 4.x). 触发词:"Doris SQL检查"、"Doris SQL规范"、"Doris SQL审计"、"Doris SQL语法"、"检查Doris SQL"、"Doris SQL review"

2 installs

Comprehensive SQL statement checking for HetuEngine, supporting two check modes: 1. Syntax Check - Keyword validation, statement structure verification, clause completeness, HetuEngine syntax compatibility based on Presto/Trino + Hive grammar definitions 2. Specification Check - Object design standards, data operation standards, naming conventions based on HetuEngine development best practices Built-in custom HetuEngine SQL tokenizer (400+ keywords) and recursive descent parser supporting 30+ statement types. Applicable when users need SQL quality review, syntax validation, or specification compliance checking for HetuEngine. 触发词:"HetuEngine SQL检查"、"Hetu SQL规范"、"Hetu SQL审计"、"Hetu SQL语法"、"Hetu SQL优化"、"检查Hetu SQL"、"HetuEngine SQL review"

1 installs

Huawei Cloud MRS Hive SQL specification checking skill. Checks SQL statements against defined syntax and specification rules using the automated checker engine. No extra manual analysis beyond defined rules. Trigger:"Hive SQL优化"、"检查Hive SQL"、"Hive SQL检查"、"Hive SQL规范"、"Hive SQL语法"、"Hive SQL review"

by huaweiclouddev-dev

Comprehensive SQL statement checking for HetuEngine, supporting two check modes: 1. Syntax Check - Keyword validation, statement structure verification, clause completeness, HetuEngine syntax compatibility based on Presto/Trino + Hive grammar definitions 2. Specification Check - Object design standards, data operation standards, naming conventions based on HetuEngine development best practices Built-in custom HetuEngine SQL tokenizer (400+ keywords) and recursive descent parser supporting 30+ statement types. Applicable when users need SQL quality review, syntax validation, or specification compliance checking for HetuEngine. 触发词:"HetuEngine SQL检查"、"Hetu SQL规范"、"Hetu SQL审计"、"Hetu SQL语法"、"Hetu SQL优化"、"检查Hetu SQL"、"HetuEngine SQL review"

2 installs

Huawei Cloud MRS Spark SQL specification checking skill. Performs comprehensive SQL statement checking for MRS Spark, including syntax validation, specification compliance, and performance risk detection. triggers: "Spark SQL review", "check Spark SQL", "检查Spark SQL", "Spark SQL检查", "Spark SQL规范", "Spark SQL语法".

2 installs

Comprehensive SQL statement checking for ClickHouse, supporting multiple kernel versions (24.8, 23.3, 22.3) and two check modes: 1. Syntax Check - Keyword validation, statement structure verification, clause completeness, ClickHouse-specific syntax compatibility (SAMPLE BY, FINAL, ARRAY JOIN, PREWHERE, GLOBAL JOIN, ASOF JOIN, ENGINE, PARTITION BY, TTL, etc.) based on kernel source grammar 2. Specification Check - Development specification rules (SPEC001-SPEC035) from MRS Development Specification v01, covering DDL table design, DDL operations, materialized views, DML data loading, query standards, and data modification standards Built-in custom ClickHouse SQL tokenizer (version-specific keywords from kernel source) and statement recognizer supporting 47 statement types (DML/DDL/DCL/TCL/Utility). Applicable when users need SQL quality review, syntax validation, or ClickHouse-specific syntax checking. Trigger: "Clickhouse SQL check"、"CK SQL check"、 "Clickhouse SQL 校验"、 "Clickhouse SQL 检查

2 installs

More from huaweicloud-skills-team

Browse all skills

Manage Huawei Ascend NPUs with natural language commands that translate to npu-smi, locally or over SSH.

by huaweicloud-skills-team7 installs

Deploy and test LLM, VL, Embedding, and Rerank models on Huawei Cloud Ascend 910B DevServer with single- or dual-node topologies.

by huaweicloud-skills-team7 installs

Read-only queries against Huawei Cloud resources for inventory, verification, and parameter discovery.

by huaweicloud-skills-team6 installs

Query Huawei Cloud IAM resources (users, groups, policies, agencies, AK/SK, MFA, security settings) read-only via local Python SDK.

by huaweicloud-skills-team6 installs