sqlfluff-templater-dataform 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Alan Cruickshank
4
+ Copyright (c) 2024 Noriaki Hiraki
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
@@ -0,0 +1,44 @@
1
+ Metadata-Version: 2.1
2
+ Name: sqlfluff-templater-dataform
3
+ Version: 0.1.0
4
+ Summary: Lint your dataform project SQL
5
+ Author: hiracky16
6
+ License: MIT License
7
+
8
+ Copyright (c) 2018 Alan Cruickshank
9
+ Copyright (c) 2024 Noriaki Hiraki
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+
29
+ Project-URL: Repository, https://github.com/hiracky16/sqlfluff-templater-dataform.git
30
+ Keywords: sqlfluff,sql,linter,formatter,bigquery,dataform
31
+ Description-Content-Type: text/markdown
32
+ License-File: LICENSE.md
33
+ Requires-Dist: sqlfluff==3.1.1
34
+
35
+ # Dataform plugin for SQLFluff
36
+
37
+ This project is based on [dbt plugin for SQLFluff](https://github.com/hiracky16/sqlfluff/blob/main/plugins/sqlfluff-templater-dbt/) and is licensed under the MIT License.
38
+
39
+ This plugin works with [SQLFluff](https://pypi.org/project/sqlfluff/), the
40
+ SQL linter for humans, to correctly parse and compile SQL projects using
41
+ [Dataform](https://cloud.google.com/dataform).
42
+
43
+ For more details on how to use this plugin,
44
+ <!-- [see the documentation](). -->
@@ -0,0 +1,10 @@
1
+ # Dataform plugin for SQLFluff
2
+
3
+ This project is based on [dbt plugin for SQLFluff](https://github.com/hiracky16/sqlfluff/blob/main/plugins/sqlfluff-templater-dbt/) and is licensed under the MIT License.
4
+
5
+ This plugin works with [SQLFluff](https://pypi.org/project/sqlfluff/), the
6
+ SQL linter for humans, to correctly parse and compile SQL projects using
7
+ [Dataform](https://cloud.google.com/dataform).
8
+
9
+ For more details on how to use this plugin,
10
+ <!-- [see the documentation](). -->
@@ -0,0 +1,29 @@
1
+ [project]
2
+ name = "sqlfluff-templater-dataform"
3
+ version = "0.1.0"
4
+ description = "Lint your dataform project SQL"
5
+ readme = {file = "README.md", content-type = "text/markdown"}
6
+ authors = [
7
+ {name = "hiracky16"},
8
+ ]
9
+ license = {file = "LICENSE.md"}
10
+ keywords = [
11
+ "sqlfluff",
12
+ "sql",
13
+ "linter",
14
+ "formatter",
15
+ "bigquery",
16
+ "dataform",
17
+ ]
18
+ dependencies = [
19
+ "sqlfluff==3.1.1"
20
+ ]
21
+
22
+ [project.entry-points.sqlfluff]
23
+ sqlfluff_templater_dataform = "sqlfluff_templater_dataform"
24
+
25
+ [tool.setuptools.packages.find]
26
+ include = ["sqlfluff_templater_dataform"]
27
+
28
+ [project.urls]
29
+ Repository = "https://github.com/hiracky16/sqlfluff-templater-dataform.git"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,10 @@
1
+ """Defines the hook endpoints for the dataform templater plugin."""
2
+
3
+ from sqlfluff.core.plugin import hookimpl
4
+ from sqlfluff_templater_dataform.templater import DataformTemplater
5
+
6
+
7
+ @hookimpl
8
+ def get_templaters():
9
+ """Get templaters."""
10
+ return [DataformTemplater]
@@ -0,0 +1,196 @@
1
+ import logging
2
+ import os
3
+ import os.path
4
+ import re
5
+ from typing import (
6
+ Iterator,
7
+ List,
8
+ Optional,
9
+ )
10
+ from sqlfluff.core.templaters.base import RawTemplater, TemplatedFile, large_file_check, RawFileSlice, TemplatedFileSlice
11
+ from sqlfluff.cli.formatters import OutputStreamFormatter
12
+ from sqlfluff.core import FluffConfig
13
+ from sqlfluff.core.errors import SQLFluffSkipFile
14
+
15
+
16
+ # Instantiate the templater logger
17
+ templater_logger = logging.getLogger("sqlfluff.templater")
18
+
19
+ class UsedJSBlockError(SQLFluffSkipFile):
20
+ """ This package does not support dataform js block """
21
+ """ When js block used, skip linting a file."""
22
+ pass
23
+
24
+ class DataformTemplater(RawTemplater):
25
+ """A templater using dataform."""
26
+
27
+ name = "dataform"
28
+ sequential_fail_limit = 3
29
+ adapters = {}
30
+
31
+ def __init__(self, **kwargs):
32
+ self.sqlfluff_config = None
33
+ self.formatter = None
34
+ self.project_id = None
35
+ self.dataset_id = None
36
+ self.working_dir = os.getcwd()
37
+ self._sequential_fails = 0
38
+ super().__init__(**kwargs)
39
+
40
+ def sequence_files(
41
+ self, fnames: List[str], config=None, formatter=None
42
+ ) -> Iterator[str]:
43
+ self.sqlfluff_config = config
44
+ # sqlfluff_config がこの段階で入ってくるのでデフォルトの project_id と dataset_id をセット
45
+ self.project_id = self.sqlfluff_config.get_section(
46
+ (self.templater_selector, self.name, "project_id")
47
+ )
48
+ self.dataset_id = self.sqlfluff_config.get_section(
49
+ (self.templater_selector, self.name, "dataset_id")
50
+ )
51
+ return fnames
52
+
53
+ @large_file_check
54
+ def process(
55
+ self,
56
+ *,
57
+ fname: str,
58
+ in_str: Optional[str] = None,
59
+ config: Optional["FluffConfig"] = None,
60
+ formatter: Optional["OutputStreamFormatter"] = None,
61
+ ):
62
+ templater_logger.info(in_str)
63
+ if in_str and self.has_js_block(in_str):
64
+ raise UsedJSBlockError("JavaScript block is not supported.")
65
+
66
+ templated_sql, raw_slices, templated_slices = self.slice_sqlx_template(in_str)
67
+
68
+ return TemplatedFile(
69
+ source_str=in_str,
70
+ templated_str=templated_sql,
71
+ fname=fname,
72
+ sliced_file=templated_slices,
73
+ raw_sliced=raw_slices,
74
+ ), []
75
+
76
+ def has_js_block(self, sql: str) -> bool:
77
+ pattern = re.compile(r'js\s*\{(?:[^{}]|\{[^{}]*\})*\}', re.DOTALL)
78
+ return bool(pattern.search(sql))
79
+
80
+ def replace_blocks(self, in_str: str) -> str:
81
+ pattern = re.compile(r'config\s*\{(?:[^{}]|\{[^{}]*\})*\}', re.DOTALL)
82
+ return re.sub(pattern, '', in_str)
83
+
84
+ def replace_ref_with_bq_table(self, sql):
85
+ # スペースを含む ref 関数呼び出しに対応する正規表現
86
+ pattern = re.compile(r"\$\{\s*ref\(\s*'([^']+)'(?:\s*,\s*'([^']+)')?\s*\)\s*\}")
87
+ def ref_to_table(match):
88
+ if match.group(2):
89
+ dataset = match.group(1)
90
+ model_name = match.group(2)
91
+ else:
92
+ dataset = self.dataset_id
93
+ model_name = match.group(1)
94
+ return f"`{self.project_id}.{dataset}.{model_name}`"
95
+
96
+ return re.sub(pattern, ref_to_table, sql)
97
+
98
+ # SQLX をスライスして、RawFileSlice と TemplatedFileSlice を同時に返す関数
99
+ def slice_sqlx_template(self, sql: str) -> (str, List[RawFileSlice], List[TemplatedFileSlice]):
100
+ # config や js ブロックを改行に置換
101
+ replaced_sql = self.replace_blocks(sql)
102
+ # ref 関数をBigQueryテーブル名に置換
103
+ replaced_sql = self.replace_ref_with_bq_table(replaced_sql)
104
+
105
+ # SQLX の構造に対応する正規表現パターン
106
+ patterns = [
107
+ (r'config\s*\{(?:[^{}]|\{[^{}]*\})*\}', 'templated'), # config ブロック
108
+ # (r'js\s*\{(?:[^{}]|\{[^{}]*\})*\}', 'templated'), # js ブロック
109
+ (r'\$\{\s*ref\(\s*\'([^\']+)\'(?:\s*,\s*\'([^\']+)\')?\s*\)\s*\}', 'templated') # ref 関数
110
+ ]
111
+
112
+ raw_slices = [] # RawFileSlice のリスト
113
+ templated_slices = [] # TemplatedFileSlice のリスト
114
+ current_idx = 0
115
+ templated_idx = 0 # テンプレート後のインデックス
116
+ block_idx = 0
117
+
118
+ # SQLX 全体をスキャンしてスライスを作成
119
+ while current_idx < len(sql):
120
+ next_match = None
121
+ next_match_type = None
122
+
123
+ # 各パターンで最初にマッチする箇所を探す
124
+ for pattern, match_type in patterns:
125
+ match = re.search(pattern, sql[current_idx:])
126
+ if match:
127
+ match_start = current_idx + match.start()
128
+ if not next_match or match_start < next_match.start():
129
+ next_match = match
130
+ next_match_type = match_type
131
+
132
+ # マッチするものがない場合、残りはリテラルとして追加
133
+ if not next_match:
134
+ raw_slices.append(RawFileSlice(
135
+ raw=sql[current_idx:],
136
+ slice_type='literal',
137
+ source_idx=current_idx,
138
+ block_idx=block_idx
139
+ ))
140
+ templated_slices.append(TemplatedFileSlice(
141
+ slice_type='literal',
142
+ source_slice=slice(current_idx, len(sql)),
143
+ templated_slice=slice(templated_idx, templated_idx + len(sql) - current_idx)
144
+ ))
145
+ break
146
+
147
+ # リテラル部分を追加(マッチした部分の手前までの内容を追加)
148
+ if next_match.start() > 0:
149
+ raw_slices.append(RawFileSlice(
150
+ raw=sql[current_idx:next_match.start() + current_idx],
151
+ slice_type='literal',
152
+ source_idx=current_idx,
153
+ block_idx=block_idx
154
+ ))
155
+ templated_slices.append(TemplatedFileSlice(
156
+ slice_type='literal',
157
+ source_slice=slice(current_idx, next_match.start() + current_idx),
158
+ templated_slice=slice(templated_idx, templated_idx + next_match.start())
159
+ ))
160
+ templated_idx += next_match.start()
161
+ block_idx += 1
162
+
163
+ # `ref` 関数の置換を適用する
164
+ if next_match_type == 'templated' and r"ref(" in next_match.group(0):
165
+ ref_replaced = self.replace_ref_with_bq_table(next_match.group(0))
166
+ raw_slices.append(RawFileSlice(
167
+ raw=next_match.group(0),
168
+ slice_type='templated',
169
+ source_idx=current_idx + next_match.start(),
170
+ block_idx=block_idx
171
+ ))
172
+ templated_slices.append(TemplatedFileSlice(
173
+ slice_type=next_match_type,
174
+ source_slice=slice(current_idx + next_match.start(), current_idx + next_match.end()),
175
+ templated_slice=slice(templated_idx, templated_idx + len(ref_replaced))
176
+ ))
177
+ templated_idx += len(ref_replaced)
178
+ else:
179
+ raw_slices.append(RawFileSlice(
180
+ raw=next_match.group(0),
181
+ slice_type=next_match_type,
182
+ source_idx=current_idx + next_match.start(),
183
+ block_idx=block_idx
184
+ ))
185
+ templated_slices.append(TemplatedFileSlice(
186
+ slice_type=next_match_type,
187
+ source_slice=slice(current_idx + next_match.start(), current_idx + next_match.end()),
188
+ templated_slice=slice(templated_idx, templated_idx)
189
+ ))
190
+
191
+ # インデックスを次のマッチの終わりに移動
192
+ current_idx = current_idx + next_match.end()
193
+ block_idx += 1
194
+
195
+ # 置換済みのSQLと、スライス情報を返す
196
+ return replaced_sql, raw_slices, templated_slices
@@ -0,0 +1,44 @@
1
+ Metadata-Version: 2.1
2
+ Name: sqlfluff-templater-dataform
3
+ Version: 0.1.0
4
+ Summary: Lint your dataform project SQL
5
+ Author: hiracky16
6
+ License: MIT License
7
+
8
+ Copyright (c) 2018 Alan Cruickshank
9
+ Copyright (c) 2024 Noriaki Hiraki
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+
29
+ Project-URL: Repository, https://github.com/hiracky16/sqlfluff-templater-dataform.git
30
+ Keywords: sqlfluff,sql,linter,formatter,bigquery,dataform
31
+ Description-Content-Type: text/markdown
32
+ License-File: LICENSE.md
33
+ Requires-Dist: sqlfluff==3.1.1
34
+
35
+ # Dataform plugin for SQLFluff
36
+
37
+ This project is based on [dbt plugin for SQLFluff](https://github.com/hiracky16/sqlfluff/blob/main/plugins/sqlfluff-templater-dbt/) and is licensed under the MIT License.
38
+
39
+ This plugin works with [SQLFluff](https://pypi.org/project/sqlfluff/), the
40
+ SQL linter for humans, to correctly parse and compile SQL projects using
41
+ [Dataform](https://cloud.google.com/dataform).
42
+
43
+ For more details on how to use this plugin,
44
+ <!-- [see the documentation](). -->
@@ -0,0 +1,11 @@
1
+ LICENSE.md
2
+ README.md
3
+ pyproject.toml
4
+ sqlfluff_templater_dataform/__init__.py
5
+ sqlfluff_templater_dataform/templater.py
6
+ sqlfluff_templater_dataform.egg-info/PKG-INFO
7
+ sqlfluff_templater_dataform.egg-info/SOURCES.txt
8
+ sqlfluff_templater_dataform.egg-info/dependency_links.txt
9
+ sqlfluff_templater_dataform.egg-info/entry_points.txt
10
+ sqlfluff_templater_dataform.egg-info/requires.txt
11
+ sqlfluff_templater_dataform.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [sqlfluff]
2
+ sqlfluff_templater_dataform = sqlfluff_templater_dataform
@@ -0,0 +1 @@
1
+ sqlfluff_templater_dataform