tinybird 5.0.3__tar.gz → 5.0.3.dev0__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.
Files changed (131) hide show
  1. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/PKG-INFO +3 -11
  2. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/pyproject.toml +5 -5
  3. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/sql_template.py +8 -13
  4. tinybird-5.0.3.dev0/tinybird/sql_template_fmt.py +296 -0
  5. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/__cli__.py +2 -2
  6. tinybird-5.0.3.dev0/tinybird/tb/modules/datafile/build_datasource.py +409 -0
  7. tinybird-5.0.3.dev0/tinybird/tb/modules/datafile/pipe_checker.py +568 -0
  8. tinybird-5.0.3.dev0/tinybird/tb/modules/datafile/playground.py +1060 -0
  9. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/local_common.py +2 -1
  10. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tornado_template.py +2 -2
  11. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird.egg-info/PKG-INFO +3 -11
  12. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird.egg-info/SOURCES.txt +3 -0
  13. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird.egg-info/requires.txt +2 -3
  14. tinybird-5.0.3/tinybird/sql_template_fmt.py +0 -514
  15. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/setup.cfg +0 -0
  16. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/__cli__.py +0 -0
  17. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/ch_utils/constants.py +0 -0
  18. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/ch_utils/engine.py +0 -0
  19. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/check_pypi.py +0 -0
  20. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/client.py +0 -0
  21. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/config.py +0 -0
  22. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/context.py +0 -0
  23. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/datafile/common.py +0 -0
  24. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/datafile/exceptions.py +0 -0
  25. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/datafile/parse_connection.py +0 -0
  26. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/datafile/parse_datasource.py +0 -0
  27. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/datafile/parse_pipe.py +0 -0
  28. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/datatypes.py +0 -0
  29. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/feedback_manager.py +0 -0
  30. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/git_settings.py +0 -0
  31. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/iterating/__init__.py +0 -0
  32. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/iterating/data_branch_modes.py +0 -0
  33. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/prompts.py +0 -0
  34. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/service_datasources.py +0 -0
  35. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/sql.py +0 -0
  36. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/sql_toolset.py +0 -0
  37. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/syncasync.py +0 -0
  38. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/check_pypi.py +0 -0
  39. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/cli.py +0 -0
  40. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/client.py +0 -0
  41. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/config.py +0 -0
  42. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/branch.py +0 -0
  43. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/build.py +0 -0
  44. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/build_common.py +0 -0
  45. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/cicd.py +0 -0
  46. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/cli.py +0 -0
  47. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/common.py +0 -0
  48. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/config.py +0 -0
  49. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/connection.py +0 -0
  50. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/connection_dynamodb.py +0 -0
  51. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/connection_kafka.py +0 -0
  52. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/connection_s3.py +0 -0
  53. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/copy.py +0 -0
  54. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/create.py +0 -0
  55. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/datafile/build.py +0 -0
  56. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/datafile/build_common.py +0 -0
  57. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/datafile/build_pipe.py +0 -0
  58. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/datafile/diff.py +0 -0
  59. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/datafile/fixture.py +0 -0
  60. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/datafile/format_common.py +0 -0
  61. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/datafile/format_connection.py +0 -0
  62. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/datafile/format_datasource.py +0 -0
  63. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/datafile/format_pipe.py +0 -0
  64. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/datafile/pull.py +0 -0
  65. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/datasource.py +0 -0
  66. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/deployment.py +0 -0
  67. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/deployment_common.py +0 -0
  68. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/deprecations.py +0 -0
  69. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/endpoint.py +0 -0
  70. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/exceptions.py +0 -0
  71. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/feedback_manager.py +0 -0
  72. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/fmt.py +0 -0
  73. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/info.py +0 -0
  74. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/infra.py +0 -0
  75. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/job.py +0 -0
  76. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/job_common.py +0 -0
  77. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/llm.py +0 -0
  78. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/llm_utils.py +0 -0
  79. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/local.py +0 -0
  80. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/local_logs.py +0 -0
  81. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/login.py +0 -0
  82. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/login_common.py +0 -0
  83. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/logout.py +0 -0
  84. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/logs.py +0 -0
  85. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/materialization.py +0 -0
  86. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/open.py +0 -0
  87. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/pipe.py +0 -0
  88. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/preview.py +0 -0
  89. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/project.py +0 -0
  90. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/project_commands.py +0 -0
  91. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/py_project.py +0 -0
  92. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/query_output.py +0 -0
  93. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/regions.py +0 -0
  94. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/secret.py +0 -0
  95. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/secret_common.py +0 -0
  96. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/sink.py +0 -0
  97. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/table.py +0 -0
  98. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/telemetry.py +0 -0
  99. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/test.py +0 -0
  100. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/test_common.py +0 -0
  101. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/tinyunit/tinyunit.py +0 -0
  102. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/token.py +0 -0
  103. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/ts_project.py +0 -0
  104. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/watch.py +0 -0
  105. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/workspace.py +0 -0
  106. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb/modules/workspace_members.py +0 -0
  107. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli.py +0 -0
  108. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/auth.py +0 -0
  109. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/branch.py +0 -0
  110. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/cicd.py +0 -0
  111. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/cli.py +0 -0
  112. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/common.py +0 -0
  113. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/config.py +0 -0
  114. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/connection.py +0 -0
  115. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/datasource.py +0 -0
  116. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/exceptions.py +0 -0
  117. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/fmt.py +0 -0
  118. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/job.py +0 -0
  119. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/pipe.py +0 -0
  120. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/regions.py +0 -0
  121. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/tag.py +0 -0
  122. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/telemetry.py +0 -0
  123. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/test.py +0 -0
  124. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/tinyunit/tinyunit.py +0 -0
  125. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/workspace.py +0 -0
  126. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/tb_cli_modules/workspace_members.py +0 -0
  127. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/utils/__init__.py +0 -0
  128. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird/utils/bools.py +0 -0
  129. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird.egg-info/dependency_links.txt +0 -0
  130. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird.egg-info/entry_points.txt +0 -0
  131. {tinybird-5.0.3 → tinybird-5.0.3.dev0}/tinybird.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: tinybird
3
- Version: 5.0.3
3
+ Version: 5.0.3.dev0
4
4
  Summary: Tinybird Command Line Tool
5
5
  Home-page: https://www.tinybird.co/docs/forward/commands
6
6
  Author: Tinybird
@@ -9,7 +9,6 @@ Requires-Python: >=3.11, <3.14
9
9
  Description-Content-Type: text/x-rst
10
10
  Requires-Dist: aiofiles==24.1.0
11
11
  Requires-Dist: boto3
12
- Requires-Dist: certifi>=2026.10.19
13
12
  Requires-Dist: click<8.2,>=8.1.6
14
13
  Requires-Dist: clickhouse-toolset==0.34.dev0
15
14
  Requires-Dist: colorama==0.4.6
@@ -25,8 +24,8 @@ Requires-Dist: pydantic~=2.11.7
25
24
  Requires-Dist: pyperclip==1.9.0
26
25
  Requires-Dist: pyyaml<6.1,>=6.0
27
26
  Requires-Dist: requests<3,>=2.28.1
28
- Requires-Dist: shandy-sqlfmt==0.29.0
29
- Requires-Dist: shandy-sqlfmt[jinjafmt]==0.29.0
27
+ Requires-Dist: shandy-sqlfmt==0.11.1
28
+ Requires-Dist: shandy-sqlfmt[jinjafmt]==0.11.1
30
29
  Requires-Dist: toposort==1.10
31
30
  Requires-Dist: tornado~=6.5.5
32
31
  Requires-Dist: urllib3<2,>=1.26.14
@@ -53,12 +52,6 @@ The Tinybird command-line tool allows you to use all the Tinybird functionality
53
52
  Changelog
54
53
  ----------
55
54
 
56
- 5.0.3
57
- *******
58
-
59
- - `Changed` The CLI requires ``certifi>=2026.10.19``, which no longer trusts the compromised eMudhra intermediate CAs.
60
- - `Changed` Upgrade the ``shandy-sqlfmt`` SQL formatter from 0.11.1 to 0.29.0. ``tb fmt`` keeps its existing output.
61
-
62
55
  5.0.2
63
56
  *******
64
57
 
@@ -67,7 +60,6 @@ Changelog
67
60
  5.0.1
68
61
  *******
69
62
 
70
- - `Fixed` SQL template ``backend_hint()`` and ``max_threads()`` directives so line breaks cannot escape their SQL comments.
71
63
  - `Fixed` Git branches whose name starts with a digit (for example ``3-clean-up-tokens``) work with ``tb build`` and ``tb deploy``. Cloud branches keep the leading digit; Tinybird Local workspaces use a ``branch_`` prefix.
72
64
  - `Fixed` ``tb branch clear <name>`` also finds the ``branch_``-prefixed name that ``tb branch create <name>`` generates. If the name matches more than one Branch, use the Branch ID.
73
65
 
@@ -71,6 +71,7 @@ dependencies = [
71
71
  "orjson==3.11.9",
72
72
  "packaging~=26.3",
73
73
  "passlib==1.7.1",
74
+ "posthog==7.28.0",
74
75
  "pre-commit==4.6.2",
75
76
  "prometheus_client==0.26.0",
76
77
  "prompt_toolkit==3.0.48",
@@ -98,8 +99,8 @@ dependencies = [
98
99
  "requests~=2.32.5",
99
100
  "sentry-sdk==2.69.2",
100
101
  "setproctitle==1.3.3",
101
- "shandy-sqlfmt==0.29.0",
102
- "shandy-sqlfmt[jinjafmt]==0.29.0",
102
+ "shandy-sqlfmt==0.11.1",
103
+ "shandy-sqlfmt[jinjafmt]==0.11.1",
103
104
  "sqlparse==0.4.3",
104
105
  "starlette==1.0.1",
105
106
  "streaming-form-data==1.20.0",
@@ -107,7 +108,7 @@ dependencies = [
107
108
  "tabulate==0.8.10",
108
109
  "thefuzz==0.22.1",
109
110
  "tinybird-python-sdk[ai]==0.3.7",
110
- "tinybird-toolset==2.8.0",
111
+ "tinybird-toolset==2.7.4",
111
112
  "toposort==1.10",
112
113
  "tornado==6.5.5",
113
114
  "urllib3~=2.8",
@@ -154,7 +155,7 @@ lint = [
154
155
  "flake8~=7.4",
155
156
  "flake8-async~=26.8",
156
157
  "mypy==2.3.1",
157
- "ruff==0.17.0",
158
+ "ruff==0.16.10",
158
159
  "types-Markdown",
159
160
  "types-PyYAML",
160
161
  "types-aiofiles==24.1.0.20241221",
@@ -209,7 +210,6 @@ members = [
209
210
  "data_connector",
210
211
  "gatherer",
211
212
  "platform-api",
212
- "songbird",
213
213
  "tinybird_orchestrator",
214
214
  ]
215
215
 
@@ -1389,6 +1389,10 @@ _namespace = {
1389
1389
  "__doc__": {},
1390
1390
  "__name__": {},
1391
1391
  "__package__": {},
1392
+ "open": None,
1393
+ "close": None,
1394
+ "print": None,
1395
+ "input": None,
1392
1396
  }
1393
1397
 
1394
1398
 
@@ -1403,14 +1407,6 @@ reserved_vars.discard("id")
1403
1407
  error_vars = ["error", "custom_error"]
1404
1408
 
1405
1409
 
1406
- _SQL_COMMENT_UNSAFE_CHARS = re.compile(r"[\x00-\x1f\x7f\u2028\u2029]")
1407
-
1408
-
1409
- def _sql_comment_safe(value: Any) -> str:
1410
- """Make a value safe to put in a `--` SQL comment: a line break would end the comment."""
1411
- return _SQL_COMMENT_UNSAFE_CHARS.sub(" ", str(value))
1412
-
1413
-
1414
1410
  def generate(self, **kwargs) -> Tuple[str, TemplateExecutionResults]:
1415
1411
  """Generate this template with the given arguments."""
1416
1412
  namespace = {}
@@ -1451,17 +1447,16 @@ def generate(self, **kwargs) -> Tuple[str, TemplateExecutionResults]:
1451
1447
 
1452
1448
  def set_max_threads(x):
1453
1449
  try:
1454
- max_threads = int(x)
1450
+ template_execution_results["max_threads"] = int(x)
1451
+ return Expression(f"-- max_threads {x}\n")
1455
1452
  except Exception:
1456
- return Expression("-- max_threads: wrong argument\n")
1457
- template_execution_results["max_threads"] = max_threads
1458
- return Expression(f"-- max_threads {max_threads}\n")
1453
+ return Expression(f"-- max_threads: wrong argument {x}\n")
1459
1454
 
1460
1455
  def set_backend_hint(hint):
1461
1456
  template_execution_results["backend_hint"] = str(hint)
1462
1457
  if hint is None or hint is False:
1463
1458
  template_execution_results["backend_hint"] = None
1464
- return Expression(f"-- backend_hint {_sql_comment_safe(hint)}\n")
1459
+ return Expression(f"-- backend_hint {hint}\n")
1465
1460
 
1466
1461
  def set_cache_ttl(ttl_expression):
1467
1462
  valid_ttl_expressions = ("5s", "1m", "5m", "30m", "1h")
@@ -0,0 +1,296 @@
1
+ import logging
2
+ from dataclasses import dataclass
3
+ from functools import partial
4
+ from types import MethodType
5
+ from typing import Any, Dict, List, Optional
6
+
7
+ from sqlfmt import actions, api
8
+ from sqlfmt.analyzer import Analyzer, Rule, group
9
+ from sqlfmt.comment import Comment
10
+ from sqlfmt.dialect import ClickHouse
11
+ from sqlfmt.exception import SqlfmtBracketError
12
+ from sqlfmt.jinjafmt import JinjaFormatter, JinjaTag
13
+ from sqlfmt.line import Line
14
+ from sqlfmt.mode import Mode
15
+ from sqlfmt.node import Node, get_previous_token
16
+ from sqlfmt.node_manager import NodeManager
17
+ from sqlfmt.token import Token, TokenType
18
+
19
+ # This class extends and monkey patches https://github.com/tconbeer/sqlfm
20
+ INDENT = " " * 4
21
+ # This is the default value in tb fmt --diff, let's use the same
22
+ DEFAULT_FMT_LINE_LENGTH = 100
23
+
24
+
25
+ @dataclass
26
+ class TBLine(Line):
27
+ @property
28
+ def prefix(self) -> str:
29
+ """
30
+ Returns the whitespace to be printed at the start of this Line for
31
+ proper indentation.
32
+
33
+ Tinybird => This is overriden from the base Line because we want SQL inside a template to be indented
34
+ https://github.com/tconbeer/sqlfmt/blob/c11775b92d8a45f0e91d871b81a88a894d620bec/src/sqlfmt/line.py#L92
35
+ """
36
+ return INDENT * (self.depth[0] + self.depth[1])
37
+
38
+
39
+ def from_nodes(
40
+ cls,
41
+ previous_node: Optional[Node],
42
+ nodes: List[Node],
43
+ comments: List[Comment],
44
+ ) -> "Line":
45
+ """
46
+ Creates and returns a new line from a list of Nodes. Useful for line
47
+ splitting and merging.
48
+
49
+ Tinybird => Monkey patched to use `TBLine` and our own indentation logic.
50
+ """
51
+ if nodes:
52
+ line = TBLine(
53
+ previous_node=previous_node,
54
+ nodes=nodes,
55
+ comments=comments,
56
+ formatting_disabled=nodes[0].formatting_disabled or nodes[-1].formatting_disabled,
57
+ )
58
+ else:
59
+ line = TBLine(
60
+ previous_node=previous_node,
61
+ nodes=nodes,
62
+ comments=comments,
63
+ formatting_disabled=previous_node.formatting_disabled if previous_node else False,
64
+ )
65
+
66
+ return line
67
+
68
+
69
+ def _format_jinja_node(self, node: Node, max_length: int) -> bool:
70
+ """
71
+ Format a single jinja tag. No-ops for nodes that
72
+ are not jinja. Returns True if the node was blackened
73
+ """
74
+ if node.is_jinja:
75
+ formatter = JinjaFormatter(TBMode())
76
+ tag = JinjaTag.from_string(node.value, node.depth[0] + node.depth[1])
77
+ if formatter.use_black:
78
+ # Monkey patching black to not normalize strings
79
+ old_format_str = formatter.code_formatter.black.format_str
80
+
81
+ def format_str(string, mode=None):
82
+ black_mode = formatter.code_formatter.black.Mode(
83
+ line_length=mode.line_length, string_normalization=False
84
+ )
85
+ return old_format_str(string, mode=black_mode)
86
+
87
+ # ugly way to monkeypatch the above function only once
88
+ if "add_formatter_only_once" not in formatter.code_formatter.PY_RESERVED_WORDS:
89
+ formatter.code_formatter.black.format_str = format_str
90
+ formatter.code_formatter.PY_RESERVED_WORDS.append("add_formatter_only_once")
91
+
92
+ if tag.code and formatter.use_black:
93
+ tag.code, tag.is_blackened = formatter.code_formatter.format_string(
94
+ tag.code,
95
+ max_length=tag.max_code_length(max_length),
96
+ )
97
+
98
+ if "{%" in node.value:
99
+ parts = tag.code.split("\n")
100
+ prefix = INDENT * (node.depth[0] + node.depth[1])
101
+ if len(parts) > 1:
102
+ tag.code = "\n".join([f"{prefix if i != 0 else ''}{part}" for i, part in enumerate(parts)])
103
+
104
+ node.value = str(tag)
105
+
106
+ return tag.is_blackened
107
+
108
+ return False
109
+
110
+
111
+ # Some monkey patching
112
+ Line.from_nodes = MethodType(from_nodes, Line)
113
+ JinjaFormatter._format_jinja_node = MethodType(_format_jinja_node, JinjaFormatter)
114
+
115
+
116
+ class TinybirdNodeManager(NodeManager):
117
+ def __init__(self, case_sensitive_names: bool, lower_keywords: bool) -> None:
118
+ super().__init__(case_sensitive_names)
119
+ self.lower_keywords = lower_keywords
120
+
121
+ def create_node(self, token: Token, previous_node: Optional[Node]) -> Node:
122
+ try:
123
+ return super().create_node(token, previous_node)
124
+ except SqlfmtBracketError:
125
+ # we already have tb check and the toolset to check the SQL, no need for this extra validation
126
+ prev_token, extra_whitespace = get_previous_token(previous_node)
127
+ prefix = self.whitespace(token, prev_token, extra_whitespace)
128
+ value = self.standardize_value(token)
129
+ return Node(
130
+ token=token,
131
+ previous_node=previous_node,
132
+ prefix=prefix,
133
+ value=value,
134
+ formatting_disabled=False,
135
+ )
136
+
137
+ def raise_on_mismatched_bracket(self, token: Token, last_bracket: Node) -> None:
138
+ # we already have tb check and the toolset to check the SQL, no need for this extra validation
139
+ pass
140
+
141
+ def standardize_value(self, token: Token) -> str:
142
+ """
143
+ Tinybird => Patched to not lower keywords
144
+ https://github.com/tconbeer/sqlfmt/blob/c11775b92d8a45f0e91d871b81a88a894d620bec/src/sqlfmt/node_manager.py#L215
145
+ """
146
+ if self.lower_keywords:
147
+ return super().standardize_value(token)
148
+
149
+ if token.type in (
150
+ TokenType.UNTERM_KEYWORD,
151
+ TokenType.STATEMENT_START,
152
+ TokenType.STATEMENT_END,
153
+ TokenType.WORD_OPERATOR,
154
+ TokenType.ON,
155
+ TokenType.BOOLEAN_OPERATOR,
156
+ TokenType.SET_OPERATOR,
157
+ ):
158
+ return " ".join(token.token.split())
159
+ return token.token
160
+
161
+
162
+ class TinybirdDialect(ClickHouse):
163
+ """
164
+ This is an extension of the base rules.
165
+
166
+ We might need to override the base `word_operator` and `unterm_keyword` rules with some custom ClickHouse terms.
167
+
168
+ For now we are just overriding the end blocks for `if` and `for` to work with Tornado templates.
169
+
170
+ https://github.com/tconbeer/sqlfmt/blob/c11775b92d8a45f0e91d871b81a88a894d620bec/src/sqlfmt/dialect.py#L55
171
+ """
172
+
173
+ def __init__(self, lower_keywords: bool = False) -> None:
174
+ super().__init__()
175
+ self.lower_keywords = lower_keywords
176
+
177
+ override_rules = {
178
+ "main": [
179
+ Rule(
180
+ name="quoted_name",
181
+ priority=200,
182
+ pattern=group(
183
+ # tripled single quotes (optionally raw/bytes)
184
+ r"(rb?|b|br)?'''.*?'''",
185
+ # tripled double quotes
186
+ r'(rb?|b|br)?""".*?"""',
187
+ # possibly escaped double quotes
188
+ r'(rb?|b|br|u&|@)?"([^"\\]*(\\.[^"\\]*|""[^"\\]*)*)"',
189
+ # possibly escaped single quotes
190
+ r"(rb?|b|br|u&|x)?'([^'\\]*(\\.[^'\\]*|''[^'\\]*)*)'",
191
+ r"\$\w*\$[^$]*?\$\w*\$", # pg dollar-delimited strings
192
+ # possibly escaped backtick
193
+ r"`([^`\\]*(\\.[^`\\]*)*)`",
194
+ r"((?<!\\)\$[\w$]+)", # Words starting with $ for .incl files
195
+ ),
196
+ action=partial(actions.add_node_to_buffer, token_type=TokenType.QUOTED_NAME),
197
+ ),
198
+ Rule(
199
+ name="number",
200
+ priority=700,
201
+ pattern=group(
202
+ r"-?0x[0-9a-f][0-9a-f_]*(\.[0-9a-f_]*)?(p[+-]?\d[\d_]*)?",
203
+ r"-?0b[01][01_]*",
204
+ r"-?(\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(e[+-]?\d[\d_]*)?",
205
+ ),
206
+ action=partial(actions.add_node_to_buffer, token_type=TokenType.NUMBER),
207
+ ),
208
+ ],
209
+ "jinja": [
210
+ Rule(
211
+ name="jinja_if_block_end",
212
+ priority=203,
213
+ # Accept both Tornado-style {% end %} and {% end if %}
214
+ pattern=group(r"\{%-?\s*end(\s+if)?\s*-?%\}"),
215
+ action=actions.raise_sqlfmt_bracket_error,
216
+ ),
217
+ Rule(
218
+ name="jinja_for_block_end",
219
+ priority=211,
220
+ # Accept both Tornado-style {% end %} and {% end for %}
221
+ pattern=group(r"\{%-?\s*end(\s+for)?\s*-?%\}"),
222
+ action=actions.raise_sqlfmt_bracket_error,
223
+ ),
224
+ ],
225
+ }
226
+
227
+ for section, rules in override_rules.items():
228
+ for rule in rules:
229
+ for rr in self.RULES[section]:
230
+ if rr.name == rule.name:
231
+ self.RULES[section].remove(rr)
232
+ self.RULES[section].append(rule)
233
+ break
234
+
235
+ def initialize_analyzer(self, line_length: int) -> Analyzer:
236
+ """
237
+ Creates and returns an analyzer that uses the Dialect's rules for lexing
238
+ Custom NodeManager for Tinybird
239
+ """
240
+ return Analyzer(
241
+ line_length=line_length,
242
+ rules=self.get_rules(),
243
+ node_manager=TinybirdNodeManager(self.case_sensitive_names, self.lower_keywords),
244
+ )
245
+
246
+
247
+ @dataclass
248
+ class TBMode(Mode):
249
+ lower_keywords: bool = False
250
+
251
+ def __post_init__(self) -> None:
252
+ """
253
+ Tinybird => Overriden to use `TinybirdDialect`
254
+ https://github.com/tconbeer/sqlfmt/blob/c11775b92d8a45f0e91d871b81a88a894d620bec/src/sqlfmt/mode.py#L31
255
+ """
256
+ self.dialect = TinybirdDialect(self.lower_keywords)
257
+
258
+
259
+ def _calc_str(self) -> str:
260
+ if self.is_multiline:
261
+ return self.token.token + "\n"
262
+ marker, comment_text = self._comment_parts()
263
+ if comment_text == "":
264
+ return marker + "\n"
265
+ return marker + " " + comment_text + "\n"
266
+
267
+
268
+ Comment._calc_str = property(_calc_str)
269
+
270
+
271
+ def format_sql_template(
272
+ sql: str,
273
+ line_length: Optional[int] = None,
274
+ lower_keywords: bool = False,
275
+ resource_name: Optional[str] = None,
276
+ resource_source: Optional[str] = None,
277
+ ) -> str:
278
+ try:
279
+ # https://github.com/tconbeer/sqlfmt/blob/c11775b92d8a45f0e91d871b81a88a894d620bec/src/sqlfmt/mode.py#L16-L29
280
+ config: Dict[str, Any] = {
281
+ "line_length": line_length or DEFAULT_FMT_LINE_LENGTH,
282
+ "lower_keywords": lower_keywords,
283
+ }
284
+
285
+ mode = TBMode(**config)
286
+ sql = sql.strip()
287
+ return (
288
+ "%\n" + api.format_string(sql[1:], mode=mode).strip()
289
+ if sql[0] == "%"
290
+ else api.format_string(sql, mode=mode).strip()
291
+ )
292
+ except Exception as e:
293
+ resource_info = f" in '{resource_name}'" if resource_name else ""
294
+ source_info = f" ({resource_source})" if resource_source else ""
295
+ logging.warning(f"sqlfmt error{resource_info}{source_info}: {str(e)}")
296
+ return sql
@@ -4,5 +4,5 @@ __description__ = 'Tinybird Command Line Tool'
4
4
  __url__ = 'https://www.tinybird.co/docs/forward/commands'
5
5
  __author__ = 'Tinybird'
6
6
  __author_email__ = 'support@tinybird.co'
7
- __version__ = '5.0.3'
8
- __revision__ = 'ef090149'
7
+ __version__ = '5.0.3.dev0'
8
+ __revision__ = '74fe6e30'