|
| 1 | +# AST 仕様(暫定) |
| 2 | + |
| 3 | +このドキュメントは、`spec/apache-calcite-Parser.js` が出力する AST の最小仕様をまとめたものです。 |
| 4 | +現状は実装の読みやすさ優先の軽量ノード構成です。 |
| 5 | + |
| 6 | +## 共通ルール |
| 7 | +- すべてのノードは `type` フィールドを持つ |
| 8 | +- 省略可能要素は `null` または未設定 |
| 9 | +- リストは配列で表現 |
| 10 | + |
| 11 | +## ルート |
| 12 | +- `SqlStmtList` |
| 13 | + - `statements`: ステートメント配列 |
| 14 | +- `SqlStmtEof` |
| 15 | + - `stmt` |
| 16 | +- `SqlExpressionEof` |
| 17 | + - `expr` |
| 18 | + |
| 19 | +## クエリ |
| 20 | +- `SqlSelect` |
| 21 | + - `hints`: `TableHints | null` |
| 22 | + - `stream`: `boolean` |
| 23 | + - `setQuantifier`: `"ALL" | "DISTINCT" | null` |
| 24 | + - `selectItems`: `AddSelectItem[]` |
| 25 | + - `from`: `FromClause | null` |
| 26 | + - `where`: `Where | null` |
| 27 | + - `groupBy`: `GroupBy | null` |
| 28 | + - `having`: `Having | null` |
| 29 | + - `window`: `Window | null` |
| 30 | + - `qualify`: `Qualify | null` |
| 31 | +- `QueryOrExpr` |
| 32 | + - `withList`, `leaf`, `setOps` |
| 33 | +- `OrderedQueryOrExpr` |
| 34 | + - `query`, `orderByLimitOpt` |
| 35 | + |
| 36 | +## 句 |
| 37 | +- `FromClause` |
| 38 | + - `first`: `TableRef` |
| 39 | + - `joins`: (`JoinTable` | `CommaJoin` | `ApplyJoin`)[] |
| 40 | +- `JoinTable` |
| 41 | + - `natural`: `boolean` |
| 42 | + - `joinType`: `string` (e.g. `"JOIN"`, `"LEFT JOIN"`) |
| 43 | + - `table`: `TableRef` |
| 44 | + - `condition`: `On | Using | null` |
| 45 | +- `CommaJoin` |
| 46 | + - `table` |
| 47 | +- `Where` |
| 48 | + - `expr` |
| 49 | +- `GroupBy` |
| 50 | + - `set`: `"DISTINCT" | "ALL" | null` |
| 51 | + - `list`: `GroupingElementList` |
| 52 | +- `Having` |
| 53 | + - `expr` |
| 54 | +- `Window` |
| 55 | + - `items`: `AddWindowSpec[]` |
| 56 | +- `Qualify` |
| 57 | + - `expr` |
| 58 | +- `OrderBy` |
| 59 | + - `list`: `OrderItemList` |
| 60 | +- `LimitClause` |
| 61 | + - `value`, `offset` |
| 62 | +- `OffsetClause` |
| 63 | + - `value`, `rows` |
| 64 | +- `FetchClause` |
| 65 | + - `mode`, `value`, `rows` |
| 66 | + |
| 67 | +## テーブル参照 |
| 68 | +- `TableRef` |
| 69 | + - `base`, `pivot`, `unpivot`, `matchRecognize`, `alias`, `columns`, `tablesample` |
| 70 | +- `TableName` |
| 71 | + - `name`: `CompoundTableIdentifier` |
| 72 | +- `Subquery` |
| 73 | + - `query`: `OrderedQueryOrExpr` |
| 74 | +- `Unnest` |
| 75 | + - `items`: `Expression[]` |
| 76 | + - `withOrdinality`: `boolean` |
| 77 | +- `TableFunctionCall` |
| 78 | + - `call`: `NamedRoutineCall` |
| 79 | +- `Pivot` |
| 80 | + - `aggs`: `AddPivotAgg[]` |
| 81 | + - `axis`: `SimpleIdentifierOrList` |
| 82 | + - `values`: `AddPivotValue[]` |
| 83 | +- `Unpivot` |
| 84 | + - `nulls`: `"INCLUDE" | "EXCLUDE" | null` |
| 85 | + - `columns`: `SimpleIdentifierOrList` |
| 86 | + - `axis`: `SimpleIdentifierOrList` |
| 87 | + - `values`: `AddUnpivotValue[]` |
| 88 | +- `MatchRecognize` |
| 89 | + - `partitionBy`, `orderBy`, `measures`, `rowsPerMatch`, `afterMatchSkip` |
| 90 | + - `pattern`, `anchorStart`, `anchorEnd`, `within`, `subsets`, `define` |
| 91 | + |
| 92 | +## 式 |
| 93 | +- `Expression2b`(内部表現) |
| 94 | + - `prefixes`, `base`, `extensions` |
| 95 | +- `BinaryExpression` |
| 96 | + - `operator`, `left`, `right` |
| 97 | +- `Literal` |
| 98 | + - `value` |
| 99 | +- `StringLiteral` |
| 100 | + - `value` |
| 101 | +- `NumericLiteral` |
| 102 | + - `value` |
| 103 | +- `Identifier` |
| 104 | + - `name` |
| 105 | +- `CompoundIdentifier` |
| 106 | + - `parts` |
| 107 | + |
| 108 | +## 関数 |
| 109 | +- `NamedFunctionCall` |
| 110 | + - `name`, `args`, `orderBy`, `withinGroup`, `nullTreatment`, `filter`, `over` |
| 111 | +- `BuiltinFunctionCall` |
| 112 | + - `kind` ごとにフィールドが異なる(各関数ノード参照) |
| 113 | +- `MatchRecognizeFunctionCall` |
| 114 | + |
| 115 | +## DML/DDL |
| 116 | +- `SqlInsert` / `SqlDelete` / `SqlUpdate` / `SqlMerge` |
| 117 | +- `SqlSetOption` / `SqlAlter` / `SqlExplain` / `SqlDescribe` |
| 118 | + |
| 119 | +## 例 |
| 120 | +```json |
| 121 | +{ |
| 122 | + "type": "SqlStmtList", |
| 123 | + "statements": [ |
| 124 | + { |
| 125 | + "type": "SqlSelect", |
| 126 | + "selectItems": [ |
| 127 | + { "type": "AddSelectItem", "expr": { "type": "SelectExpression", "star": true } } |
| 128 | + ], |
| 129 | + "from": { |
| 130 | + "type": "FromClause", |
| 131 | + "first": { "type": "TableRef", "base": { "type": "TableName", "name": { "type": "CompoundTableIdentifier" } } }, |
| 132 | + "joins": [] |
| 133 | + } |
| 134 | + } |
| 135 | + ] |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## TODO |
| 140 | +- `Expression*` 系の正規化 |
| 141 | +- `BuiltinFunctionCall` の各関数ノードの仕様を追加 |
| 142 | +- テストで AST 形を固定 |
0 commit comments