Skip to content

Commit a12d9b5

Browse files
authored
Merge pull request #18 from DeepLcom/fix/sql-dialect
Fix/sql dialect
2 parents e324303 + eaca4f5 commit a12d9b5

11 files changed

Lines changed: 43 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
### Changed
1717

1818

19+
## [0.3.1]
20+
21+
### Fixed
22+
- MockTable classes now have a `_sql_dialect` attribute that is used with `sglglot` for more reliable dialect conversions
23+
24+
1925
## [0.3.0]
2026

2127
**Full Changelog**: https://github.com/DeepLcom/sql-mock/compare/v0.2.0...v0.3.0
@@ -47,7 +53,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4753
## [0.1.2] - 2023-10-26
4854
Initial version.
4955

50-
[Unreleased]: https://github.com/DeepLcom/sql-mock/compare/v0.3.0...HEAD
56+
[Unreleased]: https://github.com/DeepLcom/sql-mock/compare/v0.3.1...HEAD
57+
[0.3.1]: https://github.com/DeepLcom/sql-mock/releases/tag/v0.3.1
5158
[0.3.0]: https://github.com/DeepLcom/sql-mock/releases/tag/v0.3.0
5259
[0.2.0]: https://github.com/DeepLcom/sql-mock/releases/tag/v0.2.0
5360
[0.1.2]: https://github.com/DeepLcom/sql-mock/releases/tag/v0.1.2

docs/genindex.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,12 @@ <h1 id="index">Index</h1>
115115
<h2 id="_">_</h2>
116116
<table style="width: 100%" class="indextable genindextable"><tr>
117117
<td style="width: 33%; vertical-align: top;"><ul>
118-
<li><a href="sql_mock.html#sql_mock.table_mocks.BaseMockTable._sql_mock_data">_sql_mock_data (sql_mock.table_mocks.BaseMockTable attribute)</a>
118+
<li><a href="sql_mock.html#sql_mock.table_mocks.BaseMockTable._sql_dialect">_sql_dialect (sql_mock.table_mocks.BaseMockTable attribute)</a>
119119
</li>
120120
</ul></td>
121121
<td style="width: 33%; vertical-align: top;"><ul>
122+
<li><a href="sql_mock.html#sql_mock.table_mocks.BaseMockTable._sql_mock_data">_sql_mock_data (sql_mock.table_mocks.BaseMockTable attribute)</a>
123+
</li>
122124
<li><a href="sql_mock.html#sql_mock.table_mocks.BaseMockTable._sql_mock_meta">_sql_mock_meta (sql_mock.table_mocks.BaseMockTable attribute)</a>
123125
</li>
124126
</ul></td>

docs/objects.inv

9 Bytes
Binary file not shown.

docs/searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/sitemap.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?xml version='1.0' encoding='utf-8'?>
2-
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://deeplcom.github.io/sql-mock/en/faq.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/getting_started/installation.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/getting_started/quickstart.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/index.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/modules.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/robots.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/sql_mock.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/sql_mock.bigquery.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/sql_mock.clickhouse.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/bigquery/examples.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/bigquery/index.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/bigquery/settings.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/clickhouse/examples.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/clickhouse/index.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/clickhouse/settings.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/defining_table_mocks.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/examples.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/result_assertion.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/usage/your_sql_query_to_test.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/genindex.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/py-modindex.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/search.html</loc></url></urlset>
2+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://deeplcom.github.io/sql-mock/en/index.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/modules.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/sql_mock.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/sql_mock.bigquery.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/sql_mock.clickhouse.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/genindex.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/py-modindex.html</loc></url><url><loc>https://deeplcom.github.io/sql-mock/en/search.html</loc></url></urlset>

docs/sql_mock.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,17 @@ <h2>Submodules<a class="headerlink" href="#submodules" title="Link to this headi
338338
</dl>
339339
</dd></dl>
340340

341+
<dl class="py attribute">
342+
<dt class="sig sig-object py" id="sql_mock.table_mocks.BaseMockTable._sql_dialect">
343+
<span class="sig-name descname"><span class="pre">_sql_dialect</span></span><a class="headerlink" href="#sql_mock.table_mocks.BaseMockTable._sql_dialect" title="Link to this definition"></a></dt>
344+
<dd><p>The sql dialect that the mock model uses. It will be leveraged by sqlglot.</p>
345+
<dl class="field-list simple">
346+
<dt class="field-odd">Type<span class="colon">:</span></dt>
347+
<dd class="field-odd"><p>str</p>
348+
</dd>
349+
</dl>
350+
</dd></dl>
351+
341352
<dl class="py method">
342353
<dt class="sig sig-object py" id="sql_mock.table_mocks.BaseMockTable.as_sql_input">
343354
<span class="sig-name descname"><span class="pre">as_sql_input</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sql_mock.table_mocks.BaseMockTable.as_sql_input" title="Link to this definition"></a></dt>
@@ -529,14 +540,15 @@ <h2>Submodules<a class="headerlink" href="#submodules" title="Link to this headi
529540

530541
<dl class="py function">
531542
<dt class="sig sig-object py" id="sql_mock.table_mocks.select_from_cte">
532-
<span class="sig-prename descclassname"><span class="pre">sql_mock.table_mocks.</span></span><span class="sig-name descname"><span class="pre">select_from_cte</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">query</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cte_name</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sql_mock.table_mocks.select_from_cte" title="Link to this definition"></a></dt>
543+
<span class="sig-prename descclassname"><span class="pre">sql_mock.table_mocks.</span></span><span class="sig-name descname"><span class="pre">select_from_cte</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">query</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cte_name</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sql_dialect</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sql_mock.table_mocks.select_from_cte" title="Link to this definition"></a></dt>
533544
<dd><p>If selecting from a CTE, we need to replace the the final SELECT statement
534545
with a SELECT * FROM select_cte</p>
535546
<dl class="field-list simple">
536547
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
537548
<dd class="field-odd"><ul class="simple">
538549
<li><p><strong>query</strong> (<em>str</em>) – Original SQL query</p></li>
539550
<li><p><strong>cte_name</strong> (<em>str</em>) – Name of the CTE to select from</p></li>
551+
<li><p><strong>sql_dialect</strong> (<em>str</em>) – The sql dialect to use for generating the query</p></li>
540552
</ul>
541553
</dd>
542554
</dl>

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
[tool.poetry]
33
name = "sql-mock"
4-
version = "0.3.0"
4+
version = "0.3.1"
55
description = "Simplify the testing of SQL data models and queries by allowing users to mock input data and create tests for various scenarios. It provides a consistent and convenient way to test the execution of your query without the need to process a massive amount of data."
66
repository = "https://github.com/DeepLcom/sql-mock"
77
readme = "README.md"

src/sql_mock/bigquery/table_mocks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66

77
class BigQueryMockTable(BaseMockTable):
8+
_sql_dialect = "bigquery"
9+
810
def __init__(
911
self,
1012
*args,

src/sql_mock/clickhouse/table_mocks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66

77
class ClickHouseTableMock(BaseMockTable):
8+
_sql_dialect = "clickhouse"
9+
810
def __init__(
911
self,
1012
*args,

src/sql_mock/table_mocks.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ def replace_original_table_references(query: str, mock_tables: list["BaseMockTab
2828
return query
2929

3030

31-
def select_from_cte(query: str, cte_name: str):
31+
def select_from_cte(query: str, cte_name: str, sql_dialect: str):
3232
"""
3333
If selecting from a CTE, we need to replace the the final SELECT statement
3434
with a SELECT * FROM select_cte
3535
3636
Args:
3737
query (str): Original SQL query
3838
cte_name (str): Name of the CTE to select from
39+
sql_dialect (str): The sql dialect to use for generating the query
3940
"""
4041
ast = sqlglot.parse_one(query)
4142

@@ -52,7 +53,7 @@ def select_from_cte(query: str, cte_name: str):
5253
col.pop()
5354

5455
# Change the final select statement to SELECT * FROM <cte_name>
55-
adjusted_query = ast.select("*").from_(cte_name).sql(pretty=True)
56+
adjusted_query = ast.select("*").from_(cte_name).sql(pretty=True, dialect=sql_dialect)
5657
return adjusted_query
5758

5859

@@ -131,10 +132,12 @@ class BaseMockTable:
131132
Attributes:
132133
_sql_mock_data (SQLMockData): A class that stores data which is for processing. This is automatcially created on instantiation.
133134
_sql_mock_meta (MockTableMeta): A class attribute to store table metadata. It is created using the `table_meta` decorator.
135+
_sql_dialect (str): The sql dialect that the mock model uses. It will be leveraged by sqlglot.
134136
"""
135137

136138
_sql_mock_meta: MockTableMeta = None
137139
_sql_mock_data: SQLMockData = None
140+
_sql_dialect: str = None
138141

139142
def __init__(self, data: list[dict] = None) -> None:
140143
"""
@@ -213,7 +216,7 @@ def _generate_query(
213216
result_query = self._sql_mock_data.rendered_query
214217

215218
if cte_to_select is not None:
216-
result_query = select_from_cte(result_query, cte_to_select)
219+
result_query = select_from_cte(result_query, cte_to_select, sql_dialect=self._sql_dialect)
217220
final_columns_to_select = "*"
218221
else:
219222
final_columns_to_select = ",\n".join(

0 commit comments

Comments
 (0)