relationalai 0.13.0.dev0__py3-none-any.whl → 0.13.1__py3-none-any.whl

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 (837) hide show
  1. frontend/debugger/dist/.gitignore +2 -0
  2. frontend/debugger/dist/assets/favicon-Dy0ZgA6N.png +0 -0
  3. frontend/debugger/dist/assets/index-Cssla-O7.js +208 -0
  4. frontend/debugger/dist/assets/index-DlHsYx1V.css +9 -0
  5. frontend/debugger/dist/index.html +17 -0
  6. relationalai/__init__.py +256 -1
  7. relationalai/clients/__init__.py +18 -0
  8. relationalai/clients/client.py +912 -0
  9. relationalai/clients/config.py +673 -0
  10. relationalai/clients/direct_access_client.py +118 -0
  11. relationalai/clients/exec_txn_poller.py +91 -0
  12. relationalai/clients/hash_util.py +31 -0
  13. relationalai/clients/local.py +571 -0
  14. relationalai/clients/profile_polling.py +73 -0
  15. relationalai/clients/resources/__init__.py +8 -0
  16. relationalai/clients/resources/azure/azure.py +477 -0
  17. relationalai/clients/resources/snowflake/__init__.py +20 -0
  18. relationalai/clients/resources/snowflake/cli_resources.py +87 -0
  19. relationalai/clients/resources/snowflake/direct_access_resources.py +694 -0
  20. relationalai/clients/resources/snowflake/engine_state_handlers.py +309 -0
  21. relationalai/clients/resources/snowflake/error_handlers.py +199 -0
  22. relationalai/clients/resources/snowflake/export_procedure.py.jinja +249 -0
  23. relationalai/clients/resources/snowflake/resources_factory.py +99 -0
  24. relationalai/clients/resources/snowflake/snowflake.py +3190 -0
  25. relationalai/clients/resources/snowflake/use_index_poller.py +1019 -0
  26. relationalai/clients/resources/snowflake/use_index_resources.py +188 -0
  27. relationalai/clients/resources/snowflake/util.py +387 -0
  28. relationalai/clients/result_helpers.py +420 -0
  29. relationalai/clients/types.py +113 -0
  30. relationalai/clients/util.py +356 -0
  31. relationalai/debugging.py +389 -0
  32. relationalai/dsl.py +1749 -0
  33. relationalai/early_access/builder/__init__.py +30 -0
  34. relationalai/early_access/builder/builder/__init__.py +35 -0
  35. relationalai/early_access/builder/snowflake/__init__.py +12 -0
  36. relationalai/early_access/builder/std/__init__.py +25 -0
  37. relationalai/early_access/builder/std/decimals/__init__.py +12 -0
  38. relationalai/early_access/builder/std/integers/__init__.py +12 -0
  39. relationalai/early_access/builder/std/math/__init__.py +12 -0
  40. relationalai/early_access/builder/std/strings/__init__.py +14 -0
  41. relationalai/early_access/devtools/__init__.py +12 -0
  42. relationalai/early_access/devtools/benchmark_lqp/__init__.py +12 -0
  43. relationalai/early_access/devtools/extract_lqp/__init__.py +12 -0
  44. relationalai/early_access/dsl/adapters/orm/adapter_qb.py +427 -0
  45. relationalai/early_access/dsl/adapters/orm/parser.py +636 -0
  46. relationalai/early_access/dsl/adapters/owl/adapter.py +176 -0
  47. relationalai/early_access/dsl/adapters/owl/parser.py +160 -0
  48. relationalai/early_access/dsl/bindings/common.py +402 -0
  49. relationalai/early_access/dsl/bindings/csv.py +170 -0
  50. relationalai/early_access/dsl/bindings/legacy/binding_models.py +143 -0
  51. relationalai/early_access/dsl/bindings/snowflake.py +64 -0
  52. relationalai/early_access/dsl/codegen/binder.py +411 -0
  53. relationalai/early_access/dsl/codegen/common.py +79 -0
  54. relationalai/early_access/dsl/codegen/helpers.py +23 -0
  55. relationalai/early_access/dsl/codegen/relations.py +700 -0
  56. relationalai/early_access/dsl/codegen/weaver.py +417 -0
  57. relationalai/early_access/dsl/core/builders/__init__.py +47 -0
  58. relationalai/early_access/dsl/core/builders/logic.py +19 -0
  59. relationalai/early_access/dsl/core/builders/scalar_constraint.py +11 -0
  60. relationalai/early_access/dsl/core/constraints/predicate/atomic.py +455 -0
  61. relationalai/early_access/dsl/core/constraints/predicate/universal.py +73 -0
  62. relationalai/early_access/dsl/core/constraints/scalar.py +310 -0
  63. relationalai/early_access/dsl/core/context.py +13 -0
  64. relationalai/early_access/dsl/core/cset.py +132 -0
  65. relationalai/early_access/dsl/core/exprs/__init__.py +116 -0
  66. relationalai/early_access/dsl/core/exprs/relational.py +18 -0
  67. relationalai/early_access/dsl/core/exprs/scalar.py +412 -0
  68. relationalai/early_access/dsl/core/instances.py +44 -0
  69. relationalai/early_access/dsl/core/logic/__init__.py +193 -0
  70. relationalai/early_access/dsl/core/logic/aggregation.py +98 -0
  71. relationalai/early_access/dsl/core/logic/exists.py +223 -0
  72. relationalai/early_access/dsl/core/logic/helper.py +163 -0
  73. relationalai/early_access/dsl/core/namespaces.py +32 -0
  74. relationalai/early_access/dsl/core/relations.py +276 -0
  75. relationalai/early_access/dsl/core/rules.py +112 -0
  76. relationalai/early_access/dsl/core/std/__init__.py +45 -0
  77. relationalai/early_access/dsl/core/temporal/recall.py +6 -0
  78. relationalai/early_access/dsl/core/types/__init__.py +270 -0
  79. relationalai/early_access/dsl/core/types/concepts.py +128 -0
  80. relationalai/early_access/dsl/core/types/constrained/__init__.py +267 -0
  81. relationalai/early_access/dsl/core/types/constrained/nominal.py +143 -0
  82. relationalai/early_access/dsl/core/types/constrained/subtype.py +124 -0
  83. relationalai/early_access/dsl/core/types/standard.py +92 -0
  84. relationalai/early_access/dsl/core/types/unconstrained.py +50 -0
  85. relationalai/early_access/dsl/core/types/variables.py +203 -0
  86. relationalai/early_access/dsl/ir/compiler.py +318 -0
  87. relationalai/early_access/dsl/ir/executor.py +260 -0
  88. relationalai/early_access/dsl/ontologies/constraints.py +88 -0
  89. relationalai/early_access/dsl/ontologies/export.py +30 -0
  90. relationalai/early_access/dsl/ontologies/models.py +453 -0
  91. relationalai/early_access/dsl/ontologies/python_printer.py +303 -0
  92. relationalai/early_access/dsl/ontologies/readings.py +60 -0
  93. relationalai/early_access/dsl/ontologies/relationships.py +322 -0
  94. relationalai/early_access/dsl/ontologies/roles.py +87 -0
  95. relationalai/early_access/dsl/ontologies/subtyping.py +55 -0
  96. relationalai/early_access/dsl/orm/constraints.py +438 -0
  97. relationalai/early_access/dsl/orm/measures/dimensions.py +200 -0
  98. relationalai/early_access/dsl/orm/measures/initializer.py +16 -0
  99. relationalai/early_access/dsl/orm/measures/measure_rules.py +275 -0
  100. relationalai/early_access/dsl/orm/measures/measures.py +299 -0
  101. relationalai/early_access/dsl/orm/measures/role_exprs.py +268 -0
  102. relationalai/early_access/dsl/orm/models.py +256 -0
  103. relationalai/early_access/dsl/orm/object_oriented_printer.py +344 -0
  104. relationalai/early_access/dsl/orm/printer.py +469 -0
  105. relationalai/early_access/dsl/orm/reasoners.py +480 -0
  106. relationalai/early_access/dsl/orm/relations.py +19 -0
  107. relationalai/early_access/dsl/orm/relationships.py +251 -0
  108. relationalai/early_access/dsl/orm/types.py +42 -0
  109. relationalai/early_access/dsl/orm/utils.py +79 -0
  110. relationalai/early_access/dsl/orm/verb.py +204 -0
  111. relationalai/early_access/dsl/physical_metadata/tables.py +133 -0
  112. relationalai/early_access/dsl/relations.py +170 -0
  113. relationalai/early_access/dsl/rulesets.py +69 -0
  114. relationalai/early_access/dsl/schemas/__init__.py +450 -0
  115. relationalai/early_access/dsl/schemas/builder.py +48 -0
  116. relationalai/early_access/dsl/schemas/comp_names.py +51 -0
  117. relationalai/early_access/dsl/schemas/components.py +203 -0
  118. relationalai/early_access/dsl/schemas/contexts.py +156 -0
  119. relationalai/early_access/dsl/schemas/exprs.py +89 -0
  120. relationalai/early_access/dsl/schemas/fragments.py +464 -0
  121. relationalai/early_access/dsl/serialization.py +79 -0
  122. relationalai/early_access/dsl/serialize/exporter.py +163 -0
  123. relationalai/early_access/dsl/snow/api.py +105 -0
  124. relationalai/early_access/dsl/snow/common.py +76 -0
  125. relationalai/early_access/dsl/state_mgmt/__init__.py +129 -0
  126. relationalai/early_access/dsl/state_mgmt/state_charts.py +125 -0
  127. relationalai/early_access/dsl/state_mgmt/transitions.py +130 -0
  128. relationalai/early_access/dsl/types/__init__.py +40 -0
  129. relationalai/early_access/dsl/types/concepts.py +12 -0
  130. relationalai/early_access/dsl/types/entities.py +135 -0
  131. relationalai/early_access/dsl/types/values.py +17 -0
  132. relationalai/early_access/dsl/utils.py +102 -0
  133. relationalai/early_access/graphs/__init__.py +13 -0
  134. relationalai/early_access/lqp/__init__.py +12 -0
  135. relationalai/early_access/lqp/compiler/__init__.py +12 -0
  136. relationalai/early_access/lqp/constructors/__init__.py +18 -0
  137. relationalai/early_access/lqp/executor/__init__.py +12 -0
  138. relationalai/early_access/lqp/ir/__init__.py +12 -0
  139. relationalai/early_access/lqp/passes/__init__.py +12 -0
  140. relationalai/early_access/lqp/pragmas/__init__.py +12 -0
  141. relationalai/early_access/lqp/primitives/__init__.py +12 -0
  142. relationalai/early_access/lqp/types/__init__.py +12 -0
  143. relationalai/early_access/lqp/utils/__init__.py +12 -0
  144. relationalai/early_access/lqp/validators/__init__.py +12 -0
  145. relationalai/early_access/metamodel/__init__.py +58 -0
  146. relationalai/early_access/metamodel/builtins/__init__.py +12 -0
  147. relationalai/early_access/metamodel/compiler/__init__.py +12 -0
  148. relationalai/early_access/metamodel/dependency/__init__.py +12 -0
  149. relationalai/early_access/metamodel/factory/__init__.py +17 -0
  150. relationalai/early_access/metamodel/helpers/__init__.py +12 -0
  151. relationalai/early_access/metamodel/ir/__init__.py +14 -0
  152. relationalai/early_access/metamodel/rewrite/__init__.py +7 -0
  153. relationalai/early_access/metamodel/typer/__init__.py +3 -0
  154. relationalai/early_access/metamodel/typer/typer/__init__.py +12 -0
  155. relationalai/early_access/metamodel/types/__init__.py +15 -0
  156. relationalai/early_access/metamodel/util/__init__.py +15 -0
  157. relationalai/early_access/metamodel/visitor/__init__.py +12 -0
  158. relationalai/early_access/rel/__init__.py +12 -0
  159. relationalai/early_access/rel/executor/__init__.py +12 -0
  160. relationalai/early_access/rel/rel_utils/__init__.py +12 -0
  161. relationalai/early_access/rel/rewrite/__init__.py +7 -0
  162. relationalai/early_access/solvers/__init__.py +19 -0
  163. relationalai/early_access/sql/__init__.py +11 -0
  164. relationalai/early_access/sql/executor/__init__.py +3 -0
  165. relationalai/early_access/sql/rewrite/__init__.py +3 -0
  166. relationalai/early_access/tests/logging/__init__.py +12 -0
  167. relationalai/early_access/tests/test_snapshot_base/__init__.py +12 -0
  168. relationalai/early_access/tests/utils/__init__.py +12 -0
  169. relationalai/environments/__init__.py +35 -0
  170. relationalai/environments/base.py +381 -0
  171. relationalai/environments/colab.py +14 -0
  172. relationalai/environments/generic.py +71 -0
  173. relationalai/environments/ipython.py +68 -0
  174. relationalai/environments/jupyter.py +9 -0
  175. relationalai/environments/snowbook.py +169 -0
  176. relationalai/errors.py +2496 -0
  177. relationalai/experimental/SF.py +38 -0
  178. relationalai/experimental/inspect.py +47 -0
  179. relationalai/experimental/pathfinder/__init__.py +158 -0
  180. relationalai/experimental/pathfinder/api.py +160 -0
  181. relationalai/experimental/pathfinder/automaton.py +584 -0
  182. relationalai/experimental/pathfinder/bridge.py +226 -0
  183. relationalai/experimental/pathfinder/compiler.py +416 -0
  184. relationalai/experimental/pathfinder/datalog.py +214 -0
  185. relationalai/experimental/pathfinder/diagnostics.py +56 -0
  186. relationalai/experimental/pathfinder/filter.py +236 -0
  187. relationalai/experimental/pathfinder/glushkov.py +439 -0
  188. relationalai/experimental/pathfinder/options.py +265 -0
  189. relationalai/experimental/pathfinder/pathfinder-v0.7.0.rel +1951 -0
  190. relationalai/experimental/pathfinder/rpq.py +344 -0
  191. relationalai/experimental/pathfinder/transition.py +200 -0
  192. relationalai/experimental/pathfinder/utils.py +26 -0
  193. relationalai/experimental/paths/README.md +107 -0
  194. relationalai/experimental/paths/api.py +143 -0
  195. relationalai/experimental/paths/benchmarks/grid_graph.py +37 -0
  196. relationalai/experimental/paths/code_organization.md +2 -0
  197. relationalai/experimental/paths/examples/Movies.ipynb +16328 -0
  198. relationalai/experimental/paths/examples/basic_example.py +40 -0
  199. relationalai/experimental/paths/examples/minimal_engine_warmup.py +3 -0
  200. relationalai/experimental/paths/examples/movie_example.py +77 -0
  201. relationalai/experimental/paths/examples/movies_data/actedin.csv +193 -0
  202. relationalai/experimental/paths/examples/movies_data/directed.csv +45 -0
  203. relationalai/experimental/paths/examples/movies_data/follows.csv +7 -0
  204. relationalai/experimental/paths/examples/movies_data/movies.csv +39 -0
  205. relationalai/experimental/paths/examples/movies_data/person.csv +134 -0
  206. relationalai/experimental/paths/examples/movies_data/produced.csv +16 -0
  207. relationalai/experimental/paths/examples/movies_data/ratings.csv +10 -0
  208. relationalai/experimental/paths/examples/movies_data/wrote.csv +11 -0
  209. relationalai/experimental/paths/examples/paths_benchmark.py +115 -0
  210. relationalai/experimental/paths/examples/paths_example.py +116 -0
  211. relationalai/experimental/paths/examples/pattern_to_automaton.py +28 -0
  212. relationalai/experimental/paths/find_paths_via_automaton.py +85 -0
  213. relationalai/experimental/paths/graph.py +185 -0
  214. relationalai/experimental/paths/path_algorithms/find_paths.py +280 -0
  215. relationalai/experimental/paths/path_algorithms/one_sided_ball_repetition.py +26 -0
  216. relationalai/experimental/paths/path_algorithms/one_sided_ball_upto.py +111 -0
  217. relationalai/experimental/paths/path_algorithms/single.py +59 -0
  218. relationalai/experimental/paths/path_algorithms/two_sided_balls_repetition.py +39 -0
  219. relationalai/experimental/paths/path_algorithms/two_sided_balls_upto.py +103 -0
  220. relationalai/experimental/paths/path_algorithms/usp-old.py +130 -0
  221. relationalai/experimental/paths/path_algorithms/usp-tuple.py +183 -0
  222. relationalai/experimental/paths/path_algorithms/usp.py +150 -0
  223. relationalai/experimental/paths/product_graph.py +93 -0
  224. relationalai/experimental/paths/rpq/automaton.py +584 -0
  225. relationalai/experimental/paths/rpq/diagnostics.py +56 -0
  226. relationalai/experimental/paths/rpq/rpq.py +378 -0
  227. relationalai/experimental/paths/tests/tests_limit_sp_max_length.py +90 -0
  228. relationalai/experimental/paths/tests/tests_limit_sp_multiple.py +119 -0
  229. relationalai/experimental/paths/tests/tests_limit_sp_single.py +104 -0
  230. relationalai/experimental/paths/tests/tests_limit_walks_multiple.py +113 -0
  231. relationalai/experimental/paths/tests/tests_limit_walks_single.py +149 -0
  232. relationalai/experimental/paths/tests/tests_one_sided_ball_repetition_multiple.py +70 -0
  233. relationalai/experimental/paths/tests/tests_one_sided_ball_repetition_single.py +64 -0
  234. relationalai/experimental/paths/tests/tests_one_sided_ball_upto_multiple.py +115 -0
  235. relationalai/experimental/paths/tests/tests_one_sided_ball_upto_single.py +75 -0
  236. relationalai/experimental/paths/tests/tests_single_paths.py +152 -0
  237. relationalai/experimental/paths/tests/tests_single_walks.py +208 -0
  238. relationalai/experimental/paths/tests/tests_single_walks_undirected.py +297 -0
  239. relationalai/experimental/paths/tests/tests_two_sided_balls_repetition_multiple.py +107 -0
  240. relationalai/experimental/paths/tests/tests_two_sided_balls_repetition_single.py +76 -0
  241. relationalai/experimental/paths/tests/tests_two_sided_balls_upto_multiple.py +76 -0
  242. relationalai/experimental/paths/tests/tests_two_sided_balls_upto_single.py +110 -0
  243. relationalai/experimental/paths/tests/tests_usp_nsp_multiple.py +229 -0
  244. relationalai/experimental/paths/tests/tests_usp_nsp_single.py +108 -0
  245. relationalai/experimental/paths/tree_agg.py +168 -0
  246. relationalai/experimental/paths/utilities/iterators.py +27 -0
  247. relationalai/experimental/paths/utilities/prefix_sum.py +91 -0
  248. relationalai/experimental/solvers.py +1087 -0
  249. relationalai/loaders/csv.py +195 -0
  250. relationalai/loaders/loader.py +177 -0
  251. relationalai/loaders/types.py +23 -0
  252. relationalai/rel_emitter.py +373 -0
  253. relationalai/rel_utils.py +185 -0
  254. relationalai/semantics/__init__.py +22 -146
  255. relationalai/semantics/designs/query_builder/identify_by.md +106 -0
  256. relationalai/semantics/devtools/benchmark_lqp.py +535 -0
  257. relationalai/semantics/devtools/compilation_manager.py +294 -0
  258. relationalai/semantics/devtools/extract_lqp.py +110 -0
  259. relationalai/semantics/internal/internal.py +3785 -0
  260. relationalai/semantics/internal/snowflake.py +325 -0
  261. relationalai/semantics/lqp/README.md +34 -0
  262. relationalai/semantics/lqp/builtins.py +16 -0
  263. relationalai/semantics/lqp/compiler.py +22 -0
  264. relationalai/semantics/lqp/constructors.py +68 -0
  265. relationalai/semantics/lqp/executor.py +469 -0
  266. relationalai/semantics/lqp/intrinsics.py +24 -0
  267. relationalai/semantics/lqp/model2lqp.py +877 -0
  268. relationalai/semantics/lqp/passes.py +680 -0
  269. relationalai/semantics/lqp/primitives.py +252 -0
  270. relationalai/semantics/lqp/result_helpers.py +202 -0
  271. relationalai/semantics/lqp/rewrite/annotate_constraints.py +57 -0
  272. relationalai/semantics/lqp/rewrite/cdc.py +216 -0
  273. relationalai/semantics/lqp/rewrite/extract_common.py +338 -0
  274. relationalai/semantics/lqp/rewrite/extract_keys.py +512 -0
  275. relationalai/semantics/lqp/rewrite/function_annotations.py +114 -0
  276. relationalai/semantics/lqp/rewrite/functional_dependencies.py +314 -0
  277. relationalai/semantics/lqp/rewrite/quantify_vars.py +296 -0
  278. relationalai/semantics/lqp/rewrite/splinter.py +76 -0
  279. relationalai/semantics/lqp/types.py +101 -0
  280. relationalai/semantics/lqp/utils.py +160 -0
  281. relationalai/semantics/lqp/validators.py +57 -0
  282. relationalai/semantics/metamodel/__init__.py +40 -6
  283. relationalai/semantics/metamodel/builtins.py +771 -205
  284. relationalai/semantics/metamodel/compiler.py +133 -0
  285. relationalai/semantics/metamodel/dependency.py +862 -0
  286. relationalai/semantics/metamodel/executor.py +61 -0
  287. relationalai/semantics/metamodel/factory.py +287 -0
  288. relationalai/semantics/metamodel/helpers.py +361 -0
  289. relationalai/semantics/metamodel/rewrite/discharge_constraints.py +39 -0
  290. relationalai/semantics/metamodel/rewrite/dnf_union_splitter.py +210 -0
  291. relationalai/semantics/metamodel/rewrite/extract_nested_logicals.py +78 -0
  292. relationalai/semantics/metamodel/rewrite/flatten.py +554 -0
  293. relationalai/semantics/metamodel/rewrite/format_outputs.py +165 -0
  294. relationalai/semantics/metamodel/typer/checker.py +353 -0
  295. relationalai/semantics/metamodel/typer/typer.py +1395 -0
  296. relationalai/semantics/metamodel/util.py +506 -0
  297. relationalai/semantics/reasoners/__init__.py +10 -0
  298. relationalai/semantics/reasoners/graph/README.md +620 -0
  299. relationalai/semantics/reasoners/graph/__init__.py +37 -0
  300. relationalai/semantics/reasoners/graph/core.py +9019 -0
  301. relationalai/semantics/reasoners/graph/design/beyond_demand_transform.md +797 -0
  302. relationalai/semantics/reasoners/graph/tests/README.md +21 -0
  303. relationalai/semantics/reasoners/optimization/__init__.py +68 -0
  304. relationalai/semantics/reasoners/optimization/common.py +88 -0
  305. relationalai/semantics/reasoners/optimization/solvers_dev.py +568 -0
  306. relationalai/semantics/reasoners/optimization/solvers_pb.py +1414 -0
  307. relationalai/semantics/rel/builtins.py +40 -0
  308. relationalai/semantics/rel/compiler.py +989 -0
  309. relationalai/semantics/rel/executor.py +362 -0
  310. relationalai/semantics/rel/rel.py +482 -0
  311. relationalai/semantics/rel/rel_utils.py +276 -0
  312. relationalai/semantics/snowflake/__init__.py +3 -0
  313. relationalai/semantics/sql/compiler.py +2503 -0
  314. relationalai/semantics/sql/executor/duck_db.py +52 -0
  315. relationalai/semantics/sql/executor/result_helpers.py +64 -0
  316. relationalai/semantics/sql/executor/snowflake.py +149 -0
  317. relationalai/semantics/sql/rewrite/denormalize.py +222 -0
  318. relationalai/semantics/sql/rewrite/double_negation.py +49 -0
  319. relationalai/semantics/sql/rewrite/recursive_union.py +127 -0
  320. relationalai/semantics/sql/rewrite/sort_output_query.py +246 -0
  321. relationalai/semantics/sql/sql.py +504 -0
  322. relationalai/semantics/std/__init__.py +40 -60
  323. relationalai/semantics/std/constraints.py +43 -37
  324. relationalai/semantics/std/datetime.py +135 -246
  325. relationalai/semantics/std/decimals.py +52 -45
  326. relationalai/semantics/std/floats.py +5 -13
  327. relationalai/semantics/std/integers.py +11 -26
  328. relationalai/semantics/std/math.py +112 -183
  329. relationalai/semantics/std/pragmas.py +11 -0
  330. relationalai/semantics/std/re.py +62 -80
  331. relationalai/semantics/std/std.py +14 -0
  332. relationalai/semantics/std/strings.py +60 -117
  333. relationalai/semantics/tests/test_snapshot_abstract.py +143 -0
  334. relationalai/semantics/tests/test_snapshot_base.py +9 -0
  335. relationalai/semantics/tests/utils.py +46 -0
  336. relationalai/std/__init__.py +70 -0
  337. relationalai/tools/cli.py +1936 -0
  338. relationalai/tools/cli_controls.py +1826 -0
  339. relationalai/tools/cli_helpers.py +398 -0
  340. relationalai/tools/debugger.py +183 -289
  341. relationalai/tools/debugger_client.py +109 -0
  342. relationalai/tools/debugger_server.py +302 -0
  343. relationalai/tools/dev.py +685 -0
  344. relationalai/tools/notes +7 -0
  345. relationalai/tools/qb_debugger.py +425 -0
  346. relationalai/util/clean_up_databases.py +95 -0
  347. relationalai/util/format.py +106 -48
  348. relationalai/util/list_databases.py +9 -0
  349. relationalai/util/otel_configuration.py +26 -0
  350. relationalai/util/otel_handler.py +484 -0
  351. relationalai/util/snowflake_handler.py +88 -0
  352. relationalai/util/span_format_test.py +43 -0
  353. relationalai/util/span_tracker.py +207 -0
  354. relationalai/util/spans_file_handler.py +72 -0
  355. relationalai/util/tracing_handler.py +34 -0
  356. relationalai-0.13.1.dist-info/METADATA +74 -0
  357. relationalai-0.13.1.dist-info/RECORD +459 -0
  358. relationalai-0.13.1.dist-info/WHEEL +4 -0
  359. relationalai-0.13.1.dist-info/entry_points.txt +3 -0
  360. relationalai-0.13.1.dist-info/licenses/LICENSE +202 -0
  361. relationalai_test_util/__init__.py +4 -0
  362. relationalai_test_util/fixtures.py +233 -0
  363. relationalai_test_util/snapshot.py +252 -0
  364. relationalai_test_util/traceback.py +118 -0
  365. relationalai/config/__init__.py +0 -56
  366. relationalai/config/config.py +0 -289
  367. relationalai/config/config_fields.py +0 -86
  368. relationalai/config/connections/__init__.py +0 -46
  369. relationalai/config/connections/base.py +0 -23
  370. relationalai/config/connections/duckdb.py +0 -29
  371. relationalai/config/connections/snowflake.py +0 -243
  372. relationalai/config/external/__init__.py +0 -17
  373. relationalai/config/external/dbt_converter.py +0 -101
  374. relationalai/config/external/dbt_models.py +0 -93
  375. relationalai/config/external/snowflake_converter.py +0 -41
  376. relationalai/config/external/snowflake_models.py +0 -85
  377. relationalai/config/external/utils.py +0 -19
  378. relationalai/semantics/backends/lqp/annotations.py +0 -11
  379. relationalai/semantics/backends/sql/sql_compiler.py +0 -327
  380. relationalai/semantics/frontend/base.py +0 -1707
  381. relationalai/semantics/frontend/core.py +0 -179
  382. relationalai/semantics/frontend/front_compiler.py +0 -1313
  383. relationalai/semantics/frontend/pprint.py +0 -408
  384. relationalai/semantics/metamodel/metamodel.py +0 -437
  385. relationalai/semantics/metamodel/metamodel_analyzer.py +0 -519
  386. relationalai/semantics/metamodel/metamodel_compiler.py +0 -0
  387. relationalai/semantics/metamodel/pprint.py +0 -412
  388. relationalai/semantics/metamodel/rewriter.py +0 -266
  389. relationalai/semantics/metamodel/typer.py +0 -1378
  390. relationalai/semantics/std/aggregates.py +0 -149
  391. relationalai/semantics/std/common.py +0 -44
  392. relationalai/semantics/std/numbers.py +0 -86
  393. relationalai/shims/executor.py +0 -147
  394. relationalai/shims/helpers.py +0 -126
  395. relationalai/shims/hoister.py +0 -221
  396. relationalai/shims/mm2v0.py +0 -1290
  397. relationalai/tools/cli/__init__.py +0 -6
  398. relationalai/tools/cli/cli.py +0 -90
  399. relationalai/tools/cli/components/__init__.py +0 -5
  400. relationalai/tools/cli/components/progress_reader.py +0 -1524
  401. relationalai/tools/cli/components/utils.py +0 -58
  402. relationalai/tools/cli/config_template.py +0 -45
  403. relationalai/tools/cli/dev.py +0 -19
  404. relationalai/tools/typer_debugger.py +0 -93
  405. relationalai/util/dataclasses.py +0 -43
  406. relationalai/util/docutils.py +0 -40
  407. relationalai/util/error.py +0 -199
  408. relationalai/util/naming.py +0 -145
  409. relationalai/util/python.py +0 -35
  410. relationalai/util/runtime.py +0 -156
  411. relationalai/util/schema.py +0 -197
  412. relationalai/util/source.py +0 -185
  413. relationalai/util/structures.py +0 -163
  414. relationalai/util/tracing.py +0 -261
  415. relationalai-0.13.0.dev0.dist-info/METADATA +0 -46
  416. relationalai-0.13.0.dev0.dist-info/RECORD +0 -488
  417. relationalai-0.13.0.dev0.dist-info/WHEEL +0 -5
  418. relationalai-0.13.0.dev0.dist-info/entry_points.txt +0 -3
  419. relationalai-0.13.0.dev0.dist-info/top_level.txt +0 -2
  420. v0/relationalai/__init__.py +0 -216
  421. v0/relationalai/clients/__init__.py +0 -5
  422. v0/relationalai/clients/azure.py +0 -477
  423. v0/relationalai/clients/client.py +0 -912
  424. v0/relationalai/clients/config.py +0 -673
  425. v0/relationalai/clients/direct_access_client.py +0 -118
  426. v0/relationalai/clients/hash_util.py +0 -31
  427. v0/relationalai/clients/local.py +0 -571
  428. v0/relationalai/clients/profile_polling.py +0 -73
  429. v0/relationalai/clients/result_helpers.py +0 -420
  430. v0/relationalai/clients/snowflake.py +0 -3869
  431. v0/relationalai/clients/types.py +0 -113
  432. v0/relationalai/clients/use_index_poller.py +0 -980
  433. v0/relationalai/clients/util.py +0 -356
  434. v0/relationalai/debugging.py +0 -389
  435. v0/relationalai/dsl.py +0 -1749
  436. v0/relationalai/early_access/builder/__init__.py +0 -30
  437. v0/relationalai/early_access/builder/builder/__init__.py +0 -35
  438. v0/relationalai/early_access/builder/snowflake/__init__.py +0 -12
  439. v0/relationalai/early_access/builder/std/__init__.py +0 -25
  440. v0/relationalai/early_access/builder/std/decimals/__init__.py +0 -12
  441. v0/relationalai/early_access/builder/std/integers/__init__.py +0 -12
  442. v0/relationalai/early_access/builder/std/math/__init__.py +0 -12
  443. v0/relationalai/early_access/builder/std/strings/__init__.py +0 -14
  444. v0/relationalai/early_access/devtools/__init__.py +0 -12
  445. v0/relationalai/early_access/devtools/benchmark_lqp/__init__.py +0 -12
  446. v0/relationalai/early_access/devtools/extract_lqp/__init__.py +0 -12
  447. v0/relationalai/early_access/dsl/adapters/orm/adapter_qb.py +0 -427
  448. v0/relationalai/early_access/dsl/adapters/orm/parser.py +0 -636
  449. v0/relationalai/early_access/dsl/adapters/owl/adapter.py +0 -176
  450. v0/relationalai/early_access/dsl/adapters/owl/parser.py +0 -160
  451. v0/relationalai/early_access/dsl/bindings/common.py +0 -402
  452. v0/relationalai/early_access/dsl/bindings/csv.py +0 -170
  453. v0/relationalai/early_access/dsl/bindings/legacy/binding_models.py +0 -143
  454. v0/relationalai/early_access/dsl/bindings/snowflake.py +0 -64
  455. v0/relationalai/early_access/dsl/codegen/binder.py +0 -411
  456. v0/relationalai/early_access/dsl/codegen/common.py +0 -79
  457. v0/relationalai/early_access/dsl/codegen/helpers.py +0 -23
  458. v0/relationalai/early_access/dsl/codegen/relations.py +0 -700
  459. v0/relationalai/early_access/dsl/codegen/weaver.py +0 -417
  460. v0/relationalai/early_access/dsl/core/builders/__init__.py +0 -47
  461. v0/relationalai/early_access/dsl/core/builders/logic.py +0 -19
  462. v0/relationalai/early_access/dsl/core/builders/scalar_constraint.py +0 -11
  463. v0/relationalai/early_access/dsl/core/constraints/predicate/atomic.py +0 -455
  464. v0/relationalai/early_access/dsl/core/constraints/predicate/universal.py +0 -73
  465. v0/relationalai/early_access/dsl/core/constraints/scalar.py +0 -310
  466. v0/relationalai/early_access/dsl/core/context.py +0 -13
  467. v0/relationalai/early_access/dsl/core/cset.py +0 -132
  468. v0/relationalai/early_access/dsl/core/exprs/__init__.py +0 -116
  469. v0/relationalai/early_access/dsl/core/exprs/relational.py +0 -18
  470. v0/relationalai/early_access/dsl/core/exprs/scalar.py +0 -412
  471. v0/relationalai/early_access/dsl/core/instances.py +0 -44
  472. v0/relationalai/early_access/dsl/core/logic/__init__.py +0 -193
  473. v0/relationalai/early_access/dsl/core/logic/aggregation.py +0 -98
  474. v0/relationalai/early_access/dsl/core/logic/exists.py +0 -223
  475. v0/relationalai/early_access/dsl/core/logic/helper.py +0 -163
  476. v0/relationalai/early_access/dsl/core/namespaces.py +0 -32
  477. v0/relationalai/early_access/dsl/core/relations.py +0 -276
  478. v0/relationalai/early_access/dsl/core/rules.py +0 -112
  479. v0/relationalai/early_access/dsl/core/std/__init__.py +0 -45
  480. v0/relationalai/early_access/dsl/core/temporal/recall.py +0 -6
  481. v0/relationalai/early_access/dsl/core/types/__init__.py +0 -270
  482. v0/relationalai/early_access/dsl/core/types/concepts.py +0 -128
  483. v0/relationalai/early_access/dsl/core/types/constrained/__init__.py +0 -267
  484. v0/relationalai/early_access/dsl/core/types/constrained/nominal.py +0 -143
  485. v0/relationalai/early_access/dsl/core/types/constrained/subtype.py +0 -124
  486. v0/relationalai/early_access/dsl/core/types/standard.py +0 -92
  487. v0/relationalai/early_access/dsl/core/types/unconstrained.py +0 -50
  488. v0/relationalai/early_access/dsl/core/types/variables.py +0 -203
  489. v0/relationalai/early_access/dsl/ir/compiler.py +0 -318
  490. v0/relationalai/early_access/dsl/ir/executor.py +0 -260
  491. v0/relationalai/early_access/dsl/ontologies/constraints.py +0 -88
  492. v0/relationalai/early_access/dsl/ontologies/export.py +0 -30
  493. v0/relationalai/early_access/dsl/ontologies/models.py +0 -453
  494. v0/relationalai/early_access/dsl/ontologies/python_printer.py +0 -303
  495. v0/relationalai/early_access/dsl/ontologies/readings.py +0 -60
  496. v0/relationalai/early_access/dsl/ontologies/relationships.py +0 -322
  497. v0/relationalai/early_access/dsl/ontologies/roles.py +0 -87
  498. v0/relationalai/early_access/dsl/ontologies/subtyping.py +0 -55
  499. v0/relationalai/early_access/dsl/orm/constraints.py +0 -438
  500. v0/relationalai/early_access/dsl/orm/measures/dimensions.py +0 -200
  501. v0/relationalai/early_access/dsl/orm/measures/initializer.py +0 -16
  502. v0/relationalai/early_access/dsl/orm/measures/measure_rules.py +0 -275
  503. v0/relationalai/early_access/dsl/orm/measures/measures.py +0 -299
  504. v0/relationalai/early_access/dsl/orm/measures/role_exprs.py +0 -268
  505. v0/relationalai/early_access/dsl/orm/models.py +0 -256
  506. v0/relationalai/early_access/dsl/orm/object_oriented_printer.py +0 -344
  507. v0/relationalai/early_access/dsl/orm/printer.py +0 -469
  508. v0/relationalai/early_access/dsl/orm/reasoners.py +0 -480
  509. v0/relationalai/early_access/dsl/orm/relations.py +0 -19
  510. v0/relationalai/early_access/dsl/orm/relationships.py +0 -251
  511. v0/relationalai/early_access/dsl/orm/types.py +0 -42
  512. v0/relationalai/early_access/dsl/orm/utils.py +0 -79
  513. v0/relationalai/early_access/dsl/orm/verb.py +0 -204
  514. v0/relationalai/early_access/dsl/physical_metadata/tables.py +0 -133
  515. v0/relationalai/early_access/dsl/relations.py +0 -170
  516. v0/relationalai/early_access/dsl/rulesets.py +0 -69
  517. v0/relationalai/early_access/dsl/schemas/__init__.py +0 -450
  518. v0/relationalai/early_access/dsl/schemas/builder.py +0 -48
  519. v0/relationalai/early_access/dsl/schemas/comp_names.py +0 -51
  520. v0/relationalai/early_access/dsl/schemas/components.py +0 -203
  521. v0/relationalai/early_access/dsl/schemas/contexts.py +0 -156
  522. v0/relationalai/early_access/dsl/schemas/exprs.py +0 -89
  523. v0/relationalai/early_access/dsl/schemas/fragments.py +0 -464
  524. v0/relationalai/early_access/dsl/serialization.py +0 -79
  525. v0/relationalai/early_access/dsl/serialize/exporter.py +0 -163
  526. v0/relationalai/early_access/dsl/snow/api.py +0 -104
  527. v0/relationalai/early_access/dsl/snow/common.py +0 -76
  528. v0/relationalai/early_access/dsl/state_mgmt/__init__.py +0 -129
  529. v0/relationalai/early_access/dsl/state_mgmt/state_charts.py +0 -125
  530. v0/relationalai/early_access/dsl/state_mgmt/transitions.py +0 -130
  531. v0/relationalai/early_access/dsl/types/__init__.py +0 -40
  532. v0/relationalai/early_access/dsl/types/concepts.py +0 -12
  533. v0/relationalai/early_access/dsl/types/entities.py +0 -135
  534. v0/relationalai/early_access/dsl/types/values.py +0 -17
  535. v0/relationalai/early_access/dsl/utils.py +0 -102
  536. v0/relationalai/early_access/graphs/__init__.py +0 -13
  537. v0/relationalai/early_access/lqp/__init__.py +0 -12
  538. v0/relationalai/early_access/lqp/compiler/__init__.py +0 -12
  539. v0/relationalai/early_access/lqp/constructors/__init__.py +0 -18
  540. v0/relationalai/early_access/lqp/executor/__init__.py +0 -12
  541. v0/relationalai/early_access/lqp/ir/__init__.py +0 -12
  542. v0/relationalai/early_access/lqp/passes/__init__.py +0 -12
  543. v0/relationalai/early_access/lqp/pragmas/__init__.py +0 -12
  544. v0/relationalai/early_access/lqp/primitives/__init__.py +0 -12
  545. v0/relationalai/early_access/lqp/types/__init__.py +0 -12
  546. v0/relationalai/early_access/lqp/utils/__init__.py +0 -12
  547. v0/relationalai/early_access/lqp/validators/__init__.py +0 -12
  548. v0/relationalai/early_access/metamodel/__init__.py +0 -58
  549. v0/relationalai/early_access/metamodel/builtins/__init__.py +0 -12
  550. v0/relationalai/early_access/metamodel/compiler/__init__.py +0 -12
  551. v0/relationalai/early_access/metamodel/dependency/__init__.py +0 -12
  552. v0/relationalai/early_access/metamodel/factory/__init__.py +0 -17
  553. v0/relationalai/early_access/metamodel/helpers/__init__.py +0 -12
  554. v0/relationalai/early_access/metamodel/ir/__init__.py +0 -14
  555. v0/relationalai/early_access/metamodel/rewrite/__init__.py +0 -7
  556. v0/relationalai/early_access/metamodel/typer/__init__.py +0 -3
  557. v0/relationalai/early_access/metamodel/typer/typer/__init__.py +0 -12
  558. v0/relationalai/early_access/metamodel/types/__init__.py +0 -15
  559. v0/relationalai/early_access/metamodel/util/__init__.py +0 -15
  560. v0/relationalai/early_access/metamodel/visitor/__init__.py +0 -12
  561. v0/relationalai/early_access/rel/__init__.py +0 -12
  562. v0/relationalai/early_access/rel/executor/__init__.py +0 -12
  563. v0/relationalai/early_access/rel/rel_utils/__init__.py +0 -12
  564. v0/relationalai/early_access/rel/rewrite/__init__.py +0 -7
  565. v0/relationalai/early_access/solvers/__init__.py +0 -19
  566. v0/relationalai/early_access/sql/__init__.py +0 -11
  567. v0/relationalai/early_access/sql/executor/__init__.py +0 -3
  568. v0/relationalai/early_access/sql/rewrite/__init__.py +0 -3
  569. v0/relationalai/early_access/tests/logging/__init__.py +0 -12
  570. v0/relationalai/early_access/tests/test_snapshot_base/__init__.py +0 -12
  571. v0/relationalai/early_access/tests/utils/__init__.py +0 -12
  572. v0/relationalai/environments/__init__.py +0 -35
  573. v0/relationalai/environments/base.py +0 -381
  574. v0/relationalai/environments/colab.py +0 -14
  575. v0/relationalai/environments/generic.py +0 -71
  576. v0/relationalai/environments/ipython.py +0 -68
  577. v0/relationalai/environments/jupyter.py +0 -9
  578. v0/relationalai/environments/snowbook.py +0 -169
  579. v0/relationalai/errors.py +0 -2455
  580. v0/relationalai/experimental/SF.py +0 -38
  581. v0/relationalai/experimental/inspect.py +0 -47
  582. v0/relationalai/experimental/pathfinder/__init__.py +0 -158
  583. v0/relationalai/experimental/pathfinder/api.py +0 -160
  584. v0/relationalai/experimental/pathfinder/automaton.py +0 -584
  585. v0/relationalai/experimental/pathfinder/bridge.py +0 -226
  586. v0/relationalai/experimental/pathfinder/compiler.py +0 -416
  587. v0/relationalai/experimental/pathfinder/datalog.py +0 -214
  588. v0/relationalai/experimental/pathfinder/diagnostics.py +0 -56
  589. v0/relationalai/experimental/pathfinder/filter.py +0 -236
  590. v0/relationalai/experimental/pathfinder/glushkov.py +0 -439
  591. v0/relationalai/experimental/pathfinder/options.py +0 -265
  592. v0/relationalai/experimental/pathfinder/rpq.py +0 -344
  593. v0/relationalai/experimental/pathfinder/transition.py +0 -200
  594. v0/relationalai/experimental/pathfinder/utils.py +0 -26
  595. v0/relationalai/experimental/paths/api.py +0 -143
  596. v0/relationalai/experimental/paths/benchmarks/grid_graph.py +0 -37
  597. v0/relationalai/experimental/paths/examples/basic_example.py +0 -40
  598. v0/relationalai/experimental/paths/examples/minimal_engine_warmup.py +0 -3
  599. v0/relationalai/experimental/paths/examples/movie_example.py +0 -77
  600. v0/relationalai/experimental/paths/examples/paths_benchmark.py +0 -115
  601. v0/relationalai/experimental/paths/examples/paths_example.py +0 -116
  602. v0/relationalai/experimental/paths/examples/pattern_to_automaton.py +0 -28
  603. v0/relationalai/experimental/paths/find_paths_via_automaton.py +0 -85
  604. v0/relationalai/experimental/paths/graph.py +0 -185
  605. v0/relationalai/experimental/paths/path_algorithms/find_paths.py +0 -280
  606. v0/relationalai/experimental/paths/path_algorithms/one_sided_ball_repetition.py +0 -26
  607. v0/relationalai/experimental/paths/path_algorithms/one_sided_ball_upto.py +0 -111
  608. v0/relationalai/experimental/paths/path_algorithms/single.py +0 -59
  609. v0/relationalai/experimental/paths/path_algorithms/two_sided_balls_repetition.py +0 -39
  610. v0/relationalai/experimental/paths/path_algorithms/two_sided_balls_upto.py +0 -103
  611. v0/relationalai/experimental/paths/path_algorithms/usp-old.py +0 -130
  612. v0/relationalai/experimental/paths/path_algorithms/usp-tuple.py +0 -183
  613. v0/relationalai/experimental/paths/path_algorithms/usp.py +0 -150
  614. v0/relationalai/experimental/paths/product_graph.py +0 -93
  615. v0/relationalai/experimental/paths/rpq/automaton.py +0 -584
  616. v0/relationalai/experimental/paths/rpq/diagnostics.py +0 -56
  617. v0/relationalai/experimental/paths/rpq/rpq.py +0 -378
  618. v0/relationalai/experimental/paths/tests/tests_limit_sp_max_length.py +0 -90
  619. v0/relationalai/experimental/paths/tests/tests_limit_sp_multiple.py +0 -119
  620. v0/relationalai/experimental/paths/tests/tests_limit_sp_single.py +0 -104
  621. v0/relationalai/experimental/paths/tests/tests_limit_walks_multiple.py +0 -113
  622. v0/relationalai/experimental/paths/tests/tests_limit_walks_single.py +0 -149
  623. v0/relationalai/experimental/paths/tests/tests_one_sided_ball_repetition_multiple.py +0 -70
  624. v0/relationalai/experimental/paths/tests/tests_one_sided_ball_repetition_single.py +0 -64
  625. v0/relationalai/experimental/paths/tests/tests_one_sided_ball_upto_multiple.py +0 -115
  626. v0/relationalai/experimental/paths/tests/tests_one_sided_ball_upto_single.py +0 -75
  627. v0/relationalai/experimental/paths/tests/tests_single_paths.py +0 -152
  628. v0/relationalai/experimental/paths/tests/tests_single_walks.py +0 -208
  629. v0/relationalai/experimental/paths/tests/tests_single_walks_undirected.py +0 -297
  630. v0/relationalai/experimental/paths/tests/tests_two_sided_balls_repetition_multiple.py +0 -107
  631. v0/relationalai/experimental/paths/tests/tests_two_sided_balls_repetition_single.py +0 -76
  632. v0/relationalai/experimental/paths/tests/tests_two_sided_balls_upto_multiple.py +0 -76
  633. v0/relationalai/experimental/paths/tests/tests_two_sided_balls_upto_single.py +0 -110
  634. v0/relationalai/experimental/paths/tests/tests_usp_nsp_multiple.py +0 -229
  635. v0/relationalai/experimental/paths/tests/tests_usp_nsp_single.py +0 -108
  636. v0/relationalai/experimental/paths/tree_agg.py +0 -168
  637. v0/relationalai/experimental/paths/utilities/iterators.py +0 -27
  638. v0/relationalai/experimental/paths/utilities/prefix_sum.py +0 -91
  639. v0/relationalai/experimental/solvers.py +0 -1087
  640. v0/relationalai/loaders/csv.py +0 -195
  641. v0/relationalai/loaders/loader.py +0 -177
  642. v0/relationalai/loaders/types.py +0 -23
  643. v0/relationalai/rel_emitter.py +0 -373
  644. v0/relationalai/rel_utils.py +0 -185
  645. v0/relationalai/semantics/__init__.py +0 -29
  646. v0/relationalai/semantics/devtools/benchmark_lqp.py +0 -536
  647. v0/relationalai/semantics/devtools/compilation_manager.py +0 -294
  648. v0/relationalai/semantics/devtools/extract_lqp.py +0 -110
  649. v0/relationalai/semantics/internal/internal.py +0 -3785
  650. v0/relationalai/semantics/internal/snowflake.py +0 -324
  651. v0/relationalai/semantics/lqp/builtins.py +0 -16
  652. v0/relationalai/semantics/lqp/compiler.py +0 -22
  653. v0/relationalai/semantics/lqp/constructors.py +0 -68
  654. v0/relationalai/semantics/lqp/executor.py +0 -469
  655. v0/relationalai/semantics/lqp/intrinsics.py +0 -24
  656. v0/relationalai/semantics/lqp/model2lqp.py +0 -839
  657. v0/relationalai/semantics/lqp/passes.py +0 -680
  658. v0/relationalai/semantics/lqp/primitives.py +0 -252
  659. v0/relationalai/semantics/lqp/result_helpers.py +0 -202
  660. v0/relationalai/semantics/lqp/rewrite/annotate_constraints.py +0 -57
  661. v0/relationalai/semantics/lqp/rewrite/cdc.py +0 -216
  662. v0/relationalai/semantics/lqp/rewrite/extract_common.py +0 -338
  663. v0/relationalai/semantics/lqp/rewrite/extract_keys.py +0 -449
  664. v0/relationalai/semantics/lqp/rewrite/function_annotations.py +0 -114
  665. v0/relationalai/semantics/lqp/rewrite/functional_dependencies.py +0 -314
  666. v0/relationalai/semantics/lqp/rewrite/quantify_vars.py +0 -296
  667. v0/relationalai/semantics/lqp/rewrite/splinter.py +0 -76
  668. v0/relationalai/semantics/lqp/types.py +0 -101
  669. v0/relationalai/semantics/lqp/utils.py +0 -160
  670. v0/relationalai/semantics/lqp/validators.py +0 -57
  671. v0/relationalai/semantics/metamodel/__init__.py +0 -40
  672. v0/relationalai/semantics/metamodel/builtins.py +0 -774
  673. v0/relationalai/semantics/metamodel/compiler.py +0 -133
  674. v0/relationalai/semantics/metamodel/dependency.py +0 -862
  675. v0/relationalai/semantics/metamodel/executor.py +0 -61
  676. v0/relationalai/semantics/metamodel/factory.py +0 -287
  677. v0/relationalai/semantics/metamodel/helpers.py +0 -361
  678. v0/relationalai/semantics/metamodel/rewrite/discharge_constraints.py +0 -39
  679. v0/relationalai/semantics/metamodel/rewrite/dnf_union_splitter.py +0 -210
  680. v0/relationalai/semantics/metamodel/rewrite/extract_nested_logicals.py +0 -78
  681. v0/relationalai/semantics/metamodel/rewrite/flatten.py +0 -549
  682. v0/relationalai/semantics/metamodel/rewrite/format_outputs.py +0 -165
  683. v0/relationalai/semantics/metamodel/typer/checker.py +0 -353
  684. v0/relationalai/semantics/metamodel/typer/typer.py +0 -1395
  685. v0/relationalai/semantics/metamodel/util.py +0 -505
  686. v0/relationalai/semantics/reasoners/__init__.py +0 -10
  687. v0/relationalai/semantics/reasoners/graph/__init__.py +0 -37
  688. v0/relationalai/semantics/reasoners/graph/core.py +0 -9020
  689. v0/relationalai/semantics/reasoners/optimization/__init__.py +0 -68
  690. v0/relationalai/semantics/reasoners/optimization/common.py +0 -88
  691. v0/relationalai/semantics/reasoners/optimization/solvers_dev.py +0 -568
  692. v0/relationalai/semantics/reasoners/optimization/solvers_pb.py +0 -1163
  693. v0/relationalai/semantics/rel/builtins.py +0 -40
  694. v0/relationalai/semantics/rel/compiler.py +0 -989
  695. v0/relationalai/semantics/rel/executor.py +0 -359
  696. v0/relationalai/semantics/rel/rel.py +0 -482
  697. v0/relationalai/semantics/rel/rel_utils.py +0 -276
  698. v0/relationalai/semantics/snowflake/__init__.py +0 -3
  699. v0/relationalai/semantics/sql/compiler.py +0 -2503
  700. v0/relationalai/semantics/sql/executor/duck_db.py +0 -52
  701. v0/relationalai/semantics/sql/executor/result_helpers.py +0 -64
  702. v0/relationalai/semantics/sql/executor/snowflake.py +0 -145
  703. v0/relationalai/semantics/sql/rewrite/denormalize.py +0 -222
  704. v0/relationalai/semantics/sql/rewrite/double_negation.py +0 -49
  705. v0/relationalai/semantics/sql/rewrite/recursive_union.py +0 -127
  706. v0/relationalai/semantics/sql/rewrite/sort_output_query.py +0 -246
  707. v0/relationalai/semantics/sql/sql.py +0 -504
  708. v0/relationalai/semantics/std/__init__.py +0 -54
  709. v0/relationalai/semantics/std/constraints.py +0 -43
  710. v0/relationalai/semantics/std/datetime.py +0 -363
  711. v0/relationalai/semantics/std/decimals.py +0 -62
  712. v0/relationalai/semantics/std/floats.py +0 -7
  713. v0/relationalai/semantics/std/integers.py +0 -22
  714. v0/relationalai/semantics/std/math.py +0 -141
  715. v0/relationalai/semantics/std/pragmas.py +0 -11
  716. v0/relationalai/semantics/std/re.py +0 -83
  717. v0/relationalai/semantics/std/std.py +0 -14
  718. v0/relationalai/semantics/std/strings.py +0 -63
  719. v0/relationalai/semantics/tests/__init__.py +0 -0
  720. v0/relationalai/semantics/tests/test_snapshot_abstract.py +0 -143
  721. v0/relationalai/semantics/tests/test_snapshot_base.py +0 -9
  722. v0/relationalai/semantics/tests/utils.py +0 -46
  723. v0/relationalai/std/__init__.py +0 -70
  724. v0/relationalai/tools/__init__.py +0 -0
  725. v0/relationalai/tools/cli.py +0 -1940
  726. v0/relationalai/tools/cli_controls.py +0 -1826
  727. v0/relationalai/tools/cli_helpers.py +0 -390
  728. v0/relationalai/tools/debugger.py +0 -183
  729. v0/relationalai/tools/debugger_client.py +0 -109
  730. v0/relationalai/tools/debugger_server.py +0 -302
  731. v0/relationalai/tools/dev.py +0 -685
  732. v0/relationalai/tools/qb_debugger.py +0 -425
  733. v0/relationalai/util/clean_up_databases.py +0 -95
  734. v0/relationalai/util/format.py +0 -123
  735. v0/relationalai/util/list_databases.py +0 -9
  736. v0/relationalai/util/otel_configuration.py +0 -25
  737. v0/relationalai/util/otel_handler.py +0 -484
  738. v0/relationalai/util/snowflake_handler.py +0 -88
  739. v0/relationalai/util/span_format_test.py +0 -43
  740. v0/relationalai/util/span_tracker.py +0 -207
  741. v0/relationalai/util/spans_file_handler.py +0 -72
  742. v0/relationalai/util/tracing_handler.py +0 -34
  743. /relationalai/{semantics/frontend → analysis}/__init__.py +0 -0
  744. {v0/relationalai → relationalai}/analysis/mechanistic.py +0 -0
  745. {v0/relationalai → relationalai}/analysis/whynot.py +0 -0
  746. /relationalai/{shims → auth}/__init__.py +0 -0
  747. {v0/relationalai → relationalai}/auth/jwt_generator.py +0 -0
  748. {v0/relationalai → relationalai}/auth/oauth_callback_server.py +0 -0
  749. {v0/relationalai → relationalai}/auth/token_handler.py +0 -0
  750. {v0/relationalai → relationalai}/auth/util.py +0 -0
  751. {v0/relationalai/clients → relationalai/clients/resources/snowflake}/cache_store.py +0 -0
  752. {v0/relationalai → relationalai}/compiler.py +0 -0
  753. {v0/relationalai → relationalai}/dependencies.py +0 -0
  754. {v0/relationalai → relationalai}/docutils.py +0 -0
  755. {v0/relationalai/analysis → relationalai/early_access}/__init__.py +0 -0
  756. {v0/relationalai → relationalai}/early_access/dsl/__init__.py +0 -0
  757. {v0/relationalai/auth → relationalai/early_access/dsl/adapters}/__init__.py +0 -0
  758. {v0/relationalai/early_access → relationalai/early_access/dsl/adapters/orm}/__init__.py +0 -0
  759. {v0/relationalai → relationalai}/early_access/dsl/adapters/orm/model.py +0 -0
  760. {v0/relationalai/early_access/dsl/adapters → relationalai/early_access/dsl/adapters/owl}/__init__.py +0 -0
  761. {v0/relationalai → relationalai}/early_access/dsl/adapters/owl/model.py +0 -0
  762. {v0/relationalai/early_access/dsl/adapters/orm → relationalai/early_access/dsl/bindings}/__init__.py +0 -0
  763. {v0/relationalai/early_access/dsl/adapters/owl → relationalai/early_access/dsl/bindings/legacy}/__init__.py +0 -0
  764. {v0/relationalai/early_access/dsl/bindings → relationalai/early_access/dsl/codegen}/__init__.py +0 -0
  765. {v0/relationalai → relationalai}/early_access/dsl/constants.py +0 -0
  766. {v0/relationalai → relationalai}/early_access/dsl/core/__init__.py +0 -0
  767. {v0/relationalai → relationalai}/early_access/dsl/core/constraints/__init__.py +0 -0
  768. {v0/relationalai → relationalai}/early_access/dsl/core/constraints/predicate/__init__.py +0 -0
  769. {v0/relationalai → relationalai}/early_access/dsl/core/stack.py +0 -0
  770. {v0/relationalai/early_access/dsl/bindings/legacy → relationalai/early_access/dsl/core/temporal}/__init__.py +0 -0
  771. {v0/relationalai → relationalai}/early_access/dsl/core/utils.py +0 -0
  772. {v0/relationalai/early_access/dsl/codegen → relationalai/early_access/dsl/ir}/__init__.py +0 -0
  773. {v0/relationalai/early_access/dsl/core/temporal → relationalai/early_access/dsl/ontologies}/__init__.py +0 -0
  774. {v0/relationalai → relationalai}/early_access/dsl/ontologies/raw_source.py +0 -0
  775. {v0/relationalai/early_access/dsl/ir → relationalai/early_access/dsl/orm}/__init__.py +0 -0
  776. {v0/relationalai/early_access/dsl/ontologies → relationalai/early_access/dsl/orm/measures}/__init__.py +0 -0
  777. {v0/relationalai → relationalai}/early_access/dsl/orm/reasoner_errors.py +0 -0
  778. {v0/relationalai/early_access/dsl/orm → relationalai/early_access/dsl/physical_metadata}/__init__.py +0 -0
  779. {v0/relationalai/early_access/dsl/orm/measures → relationalai/early_access/dsl/serialize}/__init__.py +0 -0
  780. {v0/relationalai → relationalai}/early_access/dsl/serialize/binding_model.py +0 -0
  781. {v0/relationalai → relationalai}/early_access/dsl/serialize/model.py +0 -0
  782. {v0/relationalai/early_access/dsl/physical_metadata → relationalai/early_access/dsl/snow}/__init__.py +0 -0
  783. {v0/relationalai → relationalai}/early_access/tests/__init__.py +0 -0
  784. {v0/relationalai → relationalai}/environments/ci.py +0 -0
  785. {v0/relationalai → relationalai}/environments/hex.py +0 -0
  786. {v0/relationalai → relationalai}/environments/terminal.py +0 -0
  787. {v0/relationalai → relationalai}/experimental/__init__.py +0 -0
  788. {v0/relationalai → relationalai}/experimental/graphs.py +0 -0
  789. {v0/relationalai → relationalai}/experimental/paths/__init__.py +0 -0
  790. {v0/relationalai → relationalai}/experimental/paths/benchmarks/__init__.py +0 -0
  791. {v0/relationalai → relationalai}/experimental/paths/path_algorithms/__init__.py +0 -0
  792. {v0/relationalai → relationalai}/experimental/paths/rpq/__init__.py +0 -0
  793. {v0/relationalai → relationalai}/experimental/paths/rpq/filter.py +0 -0
  794. {v0/relationalai → relationalai}/experimental/paths/rpq/glushkov.py +0 -0
  795. {v0/relationalai → relationalai}/experimental/paths/rpq/transition.py +0 -0
  796. {v0/relationalai → relationalai}/experimental/paths/utilities/__init__.py +0 -0
  797. {v0/relationalai → relationalai}/experimental/paths/utilities/utilities.py +0 -0
  798. {v0/relationalai/early_access/dsl/serialize → relationalai/loaders}/__init__.py +0 -0
  799. {v0/relationalai → relationalai}/metagen.py +0 -0
  800. {v0/relationalai → relationalai}/metamodel.py +0 -0
  801. {v0/relationalai → relationalai}/rel.py +0 -0
  802. {v0/relationalai → relationalai}/semantics/devtools/__init__.py +0 -0
  803. {v0/relationalai → relationalai}/semantics/internal/__init__.py +0 -0
  804. {v0/relationalai → relationalai}/semantics/internal/annotations.py +0 -0
  805. {v0/relationalai → relationalai}/semantics/lqp/__init__.py +0 -0
  806. {v0/relationalai → relationalai}/semantics/lqp/ir.py +0 -0
  807. {v0/relationalai → relationalai}/semantics/lqp/pragmas.py +0 -0
  808. {v0/relationalai → relationalai}/semantics/lqp/rewrite/__init__.py +0 -0
  809. {v0/relationalai → relationalai}/semantics/metamodel/dataflow.py +0 -0
  810. {v0/relationalai → relationalai}/semantics/metamodel/ir.py +0 -0
  811. {v0/relationalai → relationalai}/semantics/metamodel/rewrite/__init__.py +0 -0
  812. {v0/relationalai → relationalai}/semantics/metamodel/typer/__init__.py +0 -0
  813. {v0/relationalai → relationalai}/semantics/metamodel/types.py +0 -0
  814. {v0/relationalai → relationalai}/semantics/metamodel/visitor.py +0 -0
  815. {v0/relationalai → relationalai}/semantics/reasoners/experimental/__init__.py +0 -0
  816. {v0/relationalai → relationalai}/semantics/rel/__init__.py +0 -0
  817. {v0/relationalai → relationalai}/semantics/sql/__init__.py +0 -0
  818. {v0/relationalai → relationalai}/semantics/sql/executor/__init__.py +0 -0
  819. {v0/relationalai → relationalai}/semantics/sql/rewrite/__init__.py +0 -0
  820. {v0/relationalai/early_access/dsl/snow → relationalai/semantics/tests}/__init__.py +0 -0
  821. {v0/relationalai → relationalai}/semantics/tests/logging.py +0 -0
  822. {v0/relationalai → relationalai}/std/aggregates.py +0 -0
  823. {v0/relationalai → relationalai}/std/dates.py +0 -0
  824. {v0/relationalai → relationalai}/std/graphs.py +0 -0
  825. {v0/relationalai → relationalai}/std/inspect.py +0 -0
  826. {v0/relationalai → relationalai}/std/math.py +0 -0
  827. {v0/relationalai → relationalai}/std/re.py +0 -0
  828. {v0/relationalai → relationalai}/std/strings.py +0 -0
  829. {v0/relationalai/loaders → relationalai/tools}/__init__.py +0 -0
  830. {v0/relationalai → relationalai}/tools/cleanup_snapshots.py +0 -0
  831. {v0/relationalai → relationalai}/tools/constants.py +0 -0
  832. {v0/relationalai → relationalai}/tools/query_utils.py +0 -0
  833. {v0/relationalai → relationalai}/tools/snapshot_viewer.py +0 -0
  834. {v0/relationalai → relationalai}/util/__init__.py +0 -0
  835. {v0/relationalai → relationalai}/util/constants.py +0 -0
  836. {v0/relationalai → relationalai}/util/graph.py +0 -0
  837. {v0/relationalai → relationalai}/util/timeout.py +0 -0
@@ -0,0 +1,3190 @@
1
+ # pyright: reportUnusedExpression=false
2
+ from __future__ import annotations
3
+ import base64
4
+ import importlib.resources
5
+ import io
6
+ import re
7
+ import json
8
+ import time
9
+ import textwrap
10
+ import ast
11
+ import uuid
12
+ import warnings
13
+ import atexit
14
+ import hashlib
15
+ from dataclasses import dataclass
16
+
17
+ from ....auth.token_handler import TokenHandler
18
+ from relationalai.clients.exec_txn_poller import ExecTxnPoller, query_complete_message
19
+ import snowflake.snowpark
20
+
21
+ from ....rel_utils import sanitize_identifier, to_fqn_relation_name
22
+ from ....tools.constants import FIELD_PLACEHOLDER, SNOWFLAKE_AUTHS, USE_GRAPH_INDEX, DEFAULT_QUERY_TIMEOUT_MINS, WAIT_FOR_STREAM_SYNC, Generation
23
+ from .... import std
24
+ from collections import defaultdict
25
+ import requests
26
+ import snowflake.connector
27
+ import pyarrow as pa
28
+
29
+ from snowflake.snowpark import Session
30
+ from snowflake.snowpark.context import get_active_session
31
+ from ... import result_helpers
32
+ from .... import debugging
33
+ from typing import Any, Dict, Iterable, Tuple, List, Literal, cast
34
+
35
+ from pandas import DataFrame
36
+
37
+ from ....tools.cli_controls import Spinner
38
+ from ...types import AvailableModel, EngineState, Import, ImportSource, ImportSourceTable, ImportsStatus, SourceInfo, TransactionAsyncResponse
39
+ from ...config import Config
40
+ from ...client import Client, ExportParams, ProviderBase, ResourcesBase
41
+ from ...util import IdentityParser, escape_for_f_string, get_pyrel_version, get_with_retries, poll_with_specified_overhead, safe_json_loads, sanitize_module_name, scrub_exception, wrap_with_request_id, normalize_datetime
42
+ from .util import (
43
+ collect_error_messages,
44
+ process_jinja_template,
45
+ type_to_sql,
46
+ type_to_snowpark,
47
+ sanitize_user_name as _sanitize_user_name,
48
+ normalize_params,
49
+ format_sproc_name,
50
+ is_azure_url,
51
+ is_container_runtime,
52
+ imports_to_dicts,
53
+ txn_list_to_dicts,
54
+ decrypt_artifact,
55
+ )
56
+ from ....environments import runtime_env, HexEnvironment, SnowbookEnvironment
57
+ from .... import dsl, rel, metamodel as m
58
+ from ....errors import EngineProvisioningFailed, EngineNameValidationException, Errors, GuardRailsException, InvalidAliasError, InvalidEngineSizeError, InvalidSourceTypeWarning, RAIException, HexSessionException, SnowflakeChangeTrackingNotEnabledException, SnowflakeDatabaseException, SnowflakeImportMissingException, SnowflakeInvalidSource, SnowflakeMissingConfigValuesException, SnowflakeProxyAPIDeprecationWarning, SnowflakeProxySourceError, ModelNotFoundException, UnknownSourceWarning, RowsDroppedFromTargetTableWarning, QueryTimeoutExceededException
59
+ from concurrent.futures import ThreadPoolExecutor
60
+ from datetime import datetime, timedelta
61
+ from snowflake.snowpark.types import StringType, StructField, StructType
62
+ # Import error handlers and constants
63
+ from .error_handlers import (
64
+ ErrorHandler,
65
+ DuoSecurityErrorHandler,
66
+ AppMissingErrorHandler,
67
+ DatabaseErrorsHandler,
68
+ EngineErrorsHandler,
69
+ ServiceNotStartedErrorHandler,
70
+ TransactionAbortedErrorHandler,
71
+ )
72
+ # Import engine state handlers
73
+ from .engine_state_handlers import (
74
+ EngineStateHandler,
75
+ EngineContext,
76
+ SyncPendingStateHandler,
77
+ SyncSuspendedStateHandler,
78
+ SyncReadyStateHandler,
79
+ SyncGoneStateHandler,
80
+ SyncMissingEngineHandler,
81
+ AsyncPendingStateHandler,
82
+ AsyncSuspendedStateHandler,
83
+ AsyncReadyStateHandler,
84
+ AsyncGoneStateHandler,
85
+ AsyncMissingEngineHandler,
86
+ )
87
+
88
+
89
+ #--------------------------------------------------
90
+ # Constants
91
+ #--------------------------------------------------
92
+
93
+ VALID_POOL_STATUS = ["ACTIVE", "IDLE", "SUSPENDED"]
94
+ # transaction list and get return different fields (duration vs timings)
95
+ LIST_TXN_SQL_FIELDS = ["id", "database_name", "engine_name", "state", "abort_reason", "read_only","created_by", "created_on", "finished_at", "duration"]
96
+ GET_TXN_SQL_FIELDS = ["id", "database", "engine", "state", "abort_reason", "read_only","created_by", "created_on", "finished_at", "timings"]
97
+ VALID_ENGINE_STATES = ["READY", "PENDING"]
98
+
99
+ # Cloud-specific engine sizes
100
+ INTERNAL_ENGINE_SIZES = ["XS", "S", "M", "L"]
101
+ ENGINE_SIZES_AWS = ["HIGHMEM_X64_S", "HIGHMEM_X64_M", "HIGHMEM_X64_L"]
102
+ ENGINE_SIZES_AZURE = ["HIGHMEM_X64_S", "HIGHMEM_X64_M", "HIGHMEM_X64_SL"]
103
+ # Note: ENGINE_ERRORS, ENGINE_NOT_READY_MSGS, DATABASE_ERRORS moved to util.py
104
+ PYREL_ROOT_DB = 'pyrel_root_db'
105
+
106
+ TERMINAL_TXN_STATES = ["COMPLETED", "ABORTED"]
107
+
108
+ TXN_ABORT_REASON_TIMEOUT = "transaction timeout"
109
+ GUARDRAILS_ABORT_REASON = "guard rail violation"
110
+
111
+ PRINT_TXN_PROGRESS_FLAG = "print_txn_progress"
112
+
113
+ #--------------------------------------------------
114
+ # Helpers
115
+ #--------------------------------------------------
116
+
117
+ def should_print_txn_progress(config) -> bool:
118
+ return bool(config.get(PRINT_TXN_PROGRESS_FLAG, False))
119
+
120
+ #--------------------------------------------------
121
+ # Resources
122
+ #--------------------------------------------------
123
+
124
+ APP_NAME = "___RAI_APP___"
125
+
126
+ @dataclass
127
+ class ExecContext:
128
+ """Execution context for SQL queries, containing all parameters needed for execution and retry."""
129
+ code: str
130
+ params: List[Any] | None = None
131
+ raw: bool = False
132
+ help: bool = True
133
+ skip_engine_db_error_retry: bool = False
134
+
135
+ def re_execute(self, resources: 'Resources') -> Any:
136
+ """Re-execute this context's query using the provided resources instance."""
137
+ return resources._exec(
138
+ code=self.code,
139
+ params=self.params,
140
+ raw=self.raw,
141
+ help=self.help,
142
+ skip_engine_db_error_retry=self.skip_engine_db_error_retry
143
+ )
144
+
145
+
146
+ @dataclass
147
+ class TxnCreationResult:
148
+ """Result of creating a transaction via _create_v2_txn.
149
+
150
+ This standardizes the response format between different implementations
151
+ (SQL stored procedure vs HTTP direct access).
152
+ """
153
+ txn_id: str
154
+ state: str
155
+ artifact_info: Dict[str, Dict] # Populated if fast-path (state is COMPLETED/ABORTED)
156
+
157
+
158
+ class Resources(ResourcesBase):
159
+ def __init__(
160
+ self,
161
+ profile: str | None = None,
162
+ config: Config | None = None,
163
+ connection: Session | None = None,
164
+ dry_run: bool = False,
165
+ reset_session: bool = False,
166
+ generation: Generation | None = None,
167
+ language: str = "rel", # Accepted for backward compatibility, but not stored in base class
168
+ ):
169
+ super().__init__(profile, config=config)
170
+ self._token_handler: TokenHandler | None = None
171
+ self._session = connection
172
+ self.generation = generation
173
+ if self._session is None and not dry_run:
174
+ try:
175
+ # we may still be constructing the config, so this can fail now,
176
+ # if so we'll create later
177
+ self._session = self.get_sf_session(reset_session)
178
+ except Exception:
179
+ pass
180
+ self._pending_transactions: list[str] = []
181
+ self._ns_cache = {}
182
+ # self.sources contains fully qualified Snowflake table/view names
183
+ self.sources: set[str] = set()
184
+ self._sproc_models = None
185
+ # Store language for backward compatibility (used by child classes for use_index polling)
186
+ self.language = language
187
+ # Register error and state handlers
188
+ self._register_handlers()
189
+ # Register atexit callback to cancel pending transactions
190
+ atexit.register(self.cancel_pending_transactions)
191
+
192
+ #--------------------------------------------------
193
+ # Initialization & Properties
194
+ #--------------------------------------------------
195
+
196
+ def _register_handlers(self) -> None:
197
+ """Register error and engine state handlers for processing."""
198
+ # Register base handlers using getter methods that subclasses can override
199
+ # Use defensive copying to ensure each instance has its own handler lists
200
+ # and prevent cross-instance contamination from subclass mutations
201
+ self._error_handlers = list(self._get_error_handlers())
202
+ self._sync_engine_state_handlers = list(self._get_engine_state_handlers(is_async=False))
203
+ self._async_engine_state_handlers = list(self._get_engine_state_handlers(is_async=True))
204
+
205
+ def _get_error_handlers(self) -> list[ErrorHandler]:
206
+ """Get list of error handlers. Subclasses can override to add custom handlers.
207
+
208
+ Returns:
209
+ List of error handlers for standard error processing using Strategy Pattern.
210
+
211
+ Example:
212
+ def _get_error_handlers(self) -> list[ErrorHandler]:
213
+ # Get base handlers
214
+ handlers = super()._get_error_handlers()
215
+ # Add custom handler
216
+ handlers.append(MyCustomErrorHandler())
217
+ return handlers
218
+ """
219
+ return [
220
+ DuoSecurityErrorHandler(),
221
+ AppMissingErrorHandler(),
222
+ DatabaseErrorsHandler(),
223
+ EngineErrorsHandler(),
224
+ ServiceNotStartedErrorHandler(),
225
+ TransactionAbortedErrorHandler(),
226
+ ]
227
+
228
+ def _get_engine_state_handlers(self, is_async: bool = False) -> list[EngineStateHandler]:
229
+ """Get list of engine state handlers. Subclasses can override.
230
+
231
+ Args:
232
+ is_async: If True, returns async handlers; if False, returns sync handlers.
233
+
234
+ Returns:
235
+ List of engine state handlers for processing engine states.
236
+
237
+ Example:
238
+ def _get_engine_state_handlers(self, is_async: bool = False) -> list[EngineStateHandler]:
239
+ # Get base handlers
240
+ handlers = super()._get_engine_state_handlers(is_async)
241
+ # Add custom handler
242
+ handlers.append(MyCustomStateHandler())
243
+ return handlers
244
+ """
245
+ if is_async:
246
+ return [
247
+ AsyncPendingStateHandler(),
248
+ AsyncSuspendedStateHandler(),
249
+ AsyncReadyStateHandler(),
250
+ AsyncGoneStateHandler(),
251
+ AsyncMissingEngineHandler(),
252
+ ]
253
+ else:
254
+ return [
255
+ SyncPendingStateHandler(),
256
+ SyncSuspendedStateHandler(),
257
+ SyncReadyStateHandler(),
258
+ SyncGoneStateHandler(),
259
+ SyncMissingEngineHandler(),
260
+ ]
261
+
262
+ @property
263
+ def token_handler(self) -> TokenHandler:
264
+ if not self._token_handler:
265
+ self._token_handler = TokenHandler.from_config(self.config)
266
+ return self._token_handler
267
+
268
+ def reset(self):
269
+ """Reset the session."""
270
+ self._session = None
271
+
272
+ #--------------------------------------------------
273
+ # Session Management
274
+ #--------------------------------------------------
275
+
276
+ def is_erp_running(self, app_name: str) -> bool:
277
+ """Check if the ERP is running. The app.service_status() returns single row/column containing an array of JSON service status objects."""
278
+ query = f"CALL {app_name}.app.service_status();"
279
+ try:
280
+ result = self._exec(query)
281
+ # The result is a list of dictionaries, each with a "STATUS" key
282
+ # The column name containing the result is "SERVICE_STATUS"
283
+ services_status = json.loads(result[0]["SERVICE_STATUS"])
284
+ # Find the dictionary with "name" of "main" and check if its "status" is "READY"
285
+ for service in services_status:
286
+ if service.get("name") == "main" and service.get("status") == "READY":
287
+ return True
288
+ return False
289
+ except Exception:
290
+ return False
291
+
292
+ def get_sf_session(self, reset_session: bool = False):
293
+ if self._session:
294
+ return self._session
295
+
296
+ if isinstance(runtime_env, HexEnvironment):
297
+ raise HexSessionException()
298
+ if isinstance(runtime_env, SnowbookEnvironment):
299
+ return get_active_session()
300
+ else:
301
+ # if there's already been a session created, try using that
302
+ # if reset_session is true always try to get the new session
303
+ if not reset_session:
304
+ try:
305
+ return get_active_session()
306
+ except Exception:
307
+ pass
308
+
309
+ # otherwise, create a new session
310
+ missing_keys = []
311
+ connection_parameters = {}
312
+
313
+ authenticator = self.config.get('authenticator', None)
314
+ passcode = self.config.get("passcode", "")
315
+ private_key_file = self.config.get("private_key_file", "")
316
+
317
+ # If the authenticator is not set, we need to set it based on the provided parameters
318
+ if authenticator is None:
319
+ if private_key_file != "":
320
+ authenticator = "snowflake_jwt"
321
+ elif passcode != "":
322
+ authenticator = "username_password_mfa"
323
+ else:
324
+ authenticator = "snowflake"
325
+ # set the default authenticator in the config so we can skip it when we check for missing keys
326
+ self.config.set("authenticator", authenticator)
327
+
328
+ if authenticator in SNOWFLAKE_AUTHS:
329
+ required_keys = {
330
+ key for key, value in SNOWFLAKE_AUTHS[authenticator].items() if value.get("required", True)
331
+ }
332
+ for key in required_keys:
333
+ if self.config.get(key, None) is None:
334
+ default = SNOWFLAKE_AUTHS[authenticator][key].get("value", None)
335
+ if default is None or default == FIELD_PLACEHOLDER:
336
+ # No default value and no value in the config, add to missing keys
337
+ missing_keys.append(key)
338
+ else:
339
+ # Set the default value in the config from the auth defaults
340
+ self.config.set(key, default)
341
+ if missing_keys:
342
+ profile = getattr(self.config, 'profile', None)
343
+ config_file_path = getattr(self.config, 'file_path', None)
344
+ raise SnowflakeMissingConfigValuesException(missing_keys, profile, config_file_path)
345
+ for key in SNOWFLAKE_AUTHS[authenticator]:
346
+ connection_parameters[key] = self.config.get(key, None)
347
+ else:
348
+ raise ValueError(f'Authenticator "{authenticator}" not supported')
349
+
350
+ return self._build_snowflake_session(connection_parameters)
351
+
352
+ def _build_snowflake_session(self, connection_parameters: Dict[str, Any]) -> Session:
353
+ try:
354
+ tmp = {
355
+ "client_session_keep_alive": True,
356
+ "client_session_keep_alive_heartbeat_frequency": 60 * 5,
357
+ }
358
+ tmp.update(connection_parameters)
359
+ connection_parameters = tmp
360
+ # authenticator programmatic access token needs to be upper cased to work...
361
+ connection_parameters["authenticator"] = connection_parameters["authenticator"].upper()
362
+ if "authenticator" in connection_parameters and connection_parameters["authenticator"] == "OAUTH_AUTHORIZATION_CODE":
363
+ # we are replicating OAUTH_AUTHORIZATION_CODE by first retrieving the token
364
+ # and then authenticating with the token via the OAUTH authenticator
365
+ connection_parameters["token"] = self.token_handler.get_session_login_token()
366
+ connection_parameters["authenticator"] = "OAUTH"
367
+ return Session.builder.configs(connection_parameters).create()
368
+ except snowflake.connector.errors.Error as e:
369
+ raise SnowflakeDatabaseException(e)
370
+ except Exception as e:
371
+ raise e
372
+
373
+ #--------------------------------------------------
374
+ # Core Execution Methods
375
+ #--------------------------------------------------
376
+
377
+ def _exec_sql(self, code: str, params: List[Any] | None, raw=False):
378
+ """
379
+ Lowest-level SQL execution method.
380
+
381
+ Directly executes SQL via the Snowflake session. This is the foundation
382
+ for all other execution methods. It:
383
+ - Replaces APP_NAME placeholder with actual app name
384
+ - Executes SQL with optional parameters
385
+ - Returns either raw session results or collected results
386
+
387
+ Args:
388
+ code: SQL code to execute (may contain APP_NAME placeholder)
389
+ params: Optional SQL parameters
390
+ raw: If True, return raw session results; if False, collect results
391
+
392
+ Returns:
393
+ Raw session results if raw=True, otherwise collected results
394
+ """
395
+ assert self._session is not None
396
+ sess_results = self._session.sql(
397
+ code.replace(APP_NAME, self.get_app_name()),
398
+ params
399
+ )
400
+ if raw:
401
+ return sess_results
402
+ return sess_results.collect()
403
+
404
+ def _exec(
405
+ self,
406
+ code: str,
407
+ params: List[Any] | Any | None = None,
408
+ raw: bool = False,
409
+ help: bool = True,
410
+ skip_engine_db_error_retry: bool = False
411
+ ) -> Any:
412
+ """
413
+ Mid-level SQL execution method with error handling.
414
+
415
+ This is the primary method for executing SQL queries. It wraps _exec_sql
416
+ with comprehensive error handling and parameter normalization. Used
417
+ extensively throughout the codebase for direct SQL operations like:
418
+ - SHOW commands (warehouses, databases, etc.)
419
+ - CALL statements to RAI app stored procedures
420
+ - Transaction management queries
421
+
422
+ The error handling flow:
423
+ 1. Normalizes parameters and creates execution context
424
+ 2. Calls _exec_sql to execute the query
425
+ 3. On error, uses standard error handling (Strategy Pattern), which subclasses
426
+ can influence via `_get_error_handlers()` or by overriding `_handle_standard_exec_errors()`
427
+
428
+ Args:
429
+ code: SQL code to execute
430
+ params: Optional SQL parameters (normalized to list if needed)
431
+ raw: If True, return raw session results; if False, collect results
432
+ help: If True, enable error handling; if False, raise errors immediately
433
+ skip_engine_db_error_retry: If True, skip use_index retry logic in error handlers
434
+
435
+ Returns:
436
+ Query results (collected or raw depending on 'raw' parameter)
437
+ """
438
+ # print(f"\n--- sql---\n{code}\n--- end sql---\n")
439
+ # Ensure session is initialized
440
+ if not self._session:
441
+ self._session = self.get_sf_session()
442
+
443
+ # Normalize parameters
444
+ normalized_params = normalize_params(params)
445
+
446
+ # Create execution context
447
+ ctx = ExecContext(
448
+ code=code,
449
+ params=normalized_params,
450
+ raw=raw,
451
+ help=help,
452
+ skip_engine_db_error_retry=skip_engine_db_error_retry
453
+ )
454
+
455
+ # Execute SQL
456
+ try:
457
+ return self._exec_sql(ctx.code, ctx.params, raw=ctx.raw)
458
+ except Exception as e:
459
+ if not ctx.help:
460
+ raise e
461
+
462
+ # Handle standard errors
463
+ result = self._handle_standard_exec_errors(e, ctx)
464
+ if result is not None:
465
+ return result
466
+
467
+ #--------------------------------------------------
468
+ # Error Handling
469
+ #--------------------------------------------------
470
+
471
+ def _handle_standard_exec_errors(self, e: Exception, ctx: ExecContext) -> Any | None:
472
+ """
473
+ Handle standard Snowflake/RAI errors using Strategy Pattern.
474
+
475
+ Each error type has a dedicated handler class that encapsulates
476
+ the detection logic and exception creation. Handlers are processed
477
+ in order until one matches and handles the error.
478
+ """
479
+ message = str(e).lower()
480
+
481
+ # Try each handler in order until one matches
482
+ for handler in self._error_handlers:
483
+ if handler.matches(e, message, ctx, self):
484
+ result = handler.handle(e, ctx, self)
485
+ if result is not None:
486
+ return result
487
+ return # Handler raised exception, we're done
488
+
489
+ # Fallback: transform to RAIException
490
+ raise RAIException(str(e))
491
+
492
+ #--------------------------------------------------
493
+ # Feature Detection & Configuration
494
+ #--------------------------------------------------
495
+
496
+ def is_direct_access_enabled(self) -> bool:
497
+ try:
498
+ feature_enabled = self._exec(
499
+ f"call {APP_NAME}.APP.DIRECT_INGRESS_ENABLED();"
500
+ )
501
+ if not feature_enabled:
502
+ return False
503
+
504
+ # Even if the feature is enabled, customers still need to reactivate ERP to ensure the endpoint is available.
505
+ endpoint = self._exec(
506
+ f"call {APP_NAME}.APP.SERVICE_ENDPOINT(true);"
507
+ )
508
+ if not endpoint or endpoint[0][0] is None:
509
+ return False
510
+
511
+ return feature_enabled[0][0]
512
+ except Exception as e:
513
+ raise Exception(f"Unable to determine if direct access is enabled. Details error: {e}") from e
514
+
515
+
516
+ def is_account_flag_set(self, flag: str) -> bool:
517
+ results = self._exec(
518
+ f"SHOW PARAMETERS LIKE '%{flag}%' IN ACCOUNT;"
519
+ )
520
+ if not results:
521
+ return False
522
+ return results[0]["value"] == "true"
523
+
524
+ #--------------------------------------------------
525
+ # Databases
526
+ #--------------------------------------------------
527
+
528
+ def get_database(self, database: str):
529
+ try:
530
+ results = self._exec(
531
+ f"call {APP_NAME}.api.get_database('{database}');"
532
+ )
533
+ except Exception as e:
534
+ messages = collect_error_messages(e)
535
+ if any("database does not exist" in msg for msg in messages):
536
+ return None
537
+ raise e
538
+
539
+ if not results:
540
+ return None
541
+ db = results[0]
542
+ if not db:
543
+ return None
544
+ return {
545
+ "id": db["ID"],
546
+ "name": db["NAME"],
547
+ "created_by": db["CREATED_BY"],
548
+ "created_on": db["CREATED_ON"],
549
+ "deleted_by": db["DELETED_BY"],
550
+ "deleted_on": db["DELETED_ON"],
551
+ "state": db["STATE"],
552
+ }
553
+
554
+ def get_installed_packages(self, database: str) -> Dict | None:
555
+ query = f"call {APP_NAME}.api.get_installed_package_versions('{database}');"
556
+ try:
557
+ results = self._exec(query)
558
+ except Exception as e:
559
+ messages = collect_error_messages(e)
560
+ if any("database does not exist" in msg for msg in messages):
561
+ return None
562
+ # fallback to None for old sql-lib versions
563
+ if any("unknown user-defined function" in msg for msg in messages):
564
+ return None
565
+ raise e
566
+
567
+ if not results:
568
+ return None
569
+
570
+ row = results[0]
571
+ if not row:
572
+ return None
573
+
574
+ return safe_json_loads(row["PACKAGE_VERSIONS"])
575
+
576
+ #--------------------------------------------------
577
+ # Engines
578
+ #--------------------------------------------------
579
+
580
+ def _prepare_engine_params(
581
+ self,
582
+ name: str | None,
583
+ size: str | None,
584
+ use_default_size: bool = False
585
+ ) -> tuple[str, str | None]:
586
+ """
587
+ Prepare engine parameters by resolving and validating name and size.
588
+
589
+ Args:
590
+ name: Engine name (None to use default)
591
+ size: Engine size (None to use config or default)
592
+ use_default_size: If True and size is None, use get_default_engine_size()
593
+
594
+ Returns:
595
+ Tuple of (engine_name, engine_size)
596
+
597
+ Raises:
598
+ EngineNameValidationException: If engine name is invalid
599
+ Exception: If engine size is invalid
600
+ """
601
+ from relationalai.tools.cli_helpers import validate_engine_name
602
+
603
+ engine_name = name or self.get_default_engine_name()
604
+
605
+ # Resolve engine size
606
+ if size:
607
+ engine_size = size
608
+ else:
609
+ if use_default_size:
610
+ engine_size = self.config.get_default_engine_size()
611
+ else:
612
+ engine_size = self.config.get("engine_size", None)
613
+
614
+ # Validate engine size
615
+ if engine_size:
616
+ is_size_valid, sizes = self.validate_engine_size(engine_size)
617
+ if not is_size_valid:
618
+ error_msg = f"Invalid engine size '{engine_size}'. Valid sizes are: {', '.join(sizes)}"
619
+ if use_default_size:
620
+ error_msg = f"Invalid engine size in config: '{engine_size}'. Valid sizes are: {', '.join(sizes)}"
621
+ raise Exception(error_msg)
622
+
623
+ # Validate engine name
624
+ is_name_valid, _ = validate_engine_name(engine_name)
625
+ if not is_name_valid:
626
+ raise EngineNameValidationException(engine_name)
627
+
628
+ return engine_name, engine_size
629
+
630
+ def _get_state_handler(self, state: str | None, handlers: list[EngineStateHandler]) -> EngineStateHandler:
631
+ """Find the appropriate state handler for the given state."""
632
+ for handler in handlers:
633
+ if handler.handles_state(state):
634
+ return handler
635
+ # Fallback to missing engine handler if no match
636
+ return handlers[-1] # Last handler should be MissingEngineHandler
637
+
638
+ def _process_engine_state(
639
+ self,
640
+ engine: EngineState | Dict[str, Any] | None,
641
+ context: EngineContext,
642
+ handlers: list[EngineStateHandler],
643
+ set_active_on_success: bool = False
644
+ ) -> EngineState | Dict[str, Any] | None:
645
+ """
646
+ Process engine state using appropriate state handler.
647
+
648
+ Args:
649
+ engine: Current engine state (or None if missing)
650
+ context: Engine context for state handling
651
+ handlers: List of state handlers to use (sync or async)
652
+ set_active_on_success: If True, set engine as active when handler returns engine
653
+
654
+ Returns:
655
+ Engine state after processing, or None if engine needs to be created
656
+ """
657
+ # Find and execute appropriate state handler
658
+ state = engine["state"] if engine else None
659
+ handler = self._get_state_handler(state, handlers)
660
+ engine = handler.handle(engine, context, self)
661
+
662
+ # If handler returned None and we didn't start with None state, engine needs to be created
663
+ # (e.g., GONE state deleted the engine, so we need to create a new one)
664
+ if not engine and state is not None:
665
+ handler = self._get_state_handler(None, handlers)
666
+ handler.handle(None, context, self)
667
+ elif set_active_on_success:
668
+ # Cast to EngineState for type safety (handlers return EngineDict which is compatible)
669
+ self._set_active_engine(cast(EngineState, engine))
670
+
671
+ return engine
672
+
673
+ def _handle_engine_creation_errors(self, error: Exception, engine_name: str, preserve_rai_exception: bool = False) -> None:
674
+ """
675
+ Handle errors during engine creation using error handlers.
676
+
677
+ Args:
678
+ error: The exception that occurred
679
+ engine_name: Name of the engine being created
680
+ preserve_rai_exception: If True, re-raise RAIException without wrapping
681
+
682
+ Raises:
683
+ RAIException: If preserve_rai_exception is True and error is RAIException
684
+ EngineProvisioningFailed: If error is not handled by error handlers
685
+ """
686
+ # Preserve RAIException passthrough if requested (for async mode)
687
+ if preserve_rai_exception and isinstance(error, RAIException):
688
+ raise error
689
+
690
+ # Check if this is a known error type that should be handled by error handlers
691
+ message = str(error).lower()
692
+ handled = False
693
+ # Engine creation isn't tied to a specific SQL ExecContext; pass a context that
694
+ # disables use_index retry behavior (and any future ctx-dependent handlers).
695
+ ctx = ExecContext(code="", help=True, skip_engine_db_error_retry=True)
696
+ for handler in self._error_handlers:
697
+ if handler.matches(error, message, ctx, self):
698
+ handler.handle(error, ctx, self)
699
+ handled = True
700
+ break # Handler raised exception, we're done
701
+
702
+ # If not handled by error handlers, wrap in EngineProvisioningFailed
703
+ if not handled:
704
+ raise EngineProvisioningFailed(engine_name, error) from error
705
+
706
+ def validate_engine_size(self, size: str) -> Tuple[bool, List[str]]:
707
+ if size is not None:
708
+ sizes = self.get_engine_sizes()
709
+ if size not in sizes:
710
+ return False, sizes
711
+ return True, []
712
+
713
+ def get_engine_sizes(self, cloud_provider: str|None=None):
714
+ sizes = []
715
+ if cloud_provider is None:
716
+ cloud_provider = self.get_cloud_provider()
717
+ if cloud_provider == 'azure':
718
+ sizes = ENGINE_SIZES_AZURE
719
+ else:
720
+ sizes = ENGINE_SIZES_AWS
721
+ if self.config.show_all_engine_sizes():
722
+ return INTERNAL_ENGINE_SIZES + sizes
723
+ else:
724
+ return sizes
725
+
726
+ def list_engines(self, state: str | None = None):
727
+ where_clause = f"WHERE STATUS = '{state.upper()}'" if state else ""
728
+ statement = f"SELECT NAME, ID, SIZE, STATUS, CREATED_BY, CREATED_ON, UPDATED_ON FROM {APP_NAME}.api.engines {where_clause} ORDER BY NAME ASC;"
729
+ results = self._exec(statement)
730
+ if not results:
731
+ return []
732
+ return [
733
+ {
734
+ "name": row["NAME"],
735
+ "id": row["ID"],
736
+ "size": row["SIZE"],
737
+ "state": row["STATUS"], # callers are expecting 'state'
738
+ "created_by": row["CREATED_BY"],
739
+ "created_on": row["CREATED_ON"],
740
+ "updated_on": row["UPDATED_ON"],
741
+ }
742
+ for row in results
743
+ ]
744
+
745
+ def get_engine(self, name: str):
746
+ results = self._exec(
747
+ f"SELECT NAME, ID, SIZE, STATUS, CREATED_BY, CREATED_ON, UPDATED_ON, VERSION, AUTO_SUSPEND_MINS, SUSPENDS_AT FROM {APP_NAME}.api.engines WHERE NAME='{name}';"
748
+ )
749
+ if not results:
750
+ return None
751
+ engine = results[0]
752
+ if not engine:
753
+ return None
754
+ engine_state: EngineState = {
755
+ "name": engine["NAME"],
756
+ "id": engine["ID"],
757
+ "size": engine["SIZE"],
758
+ "state": engine["STATUS"], # callers are expecting 'state'
759
+ "created_by": engine["CREATED_BY"],
760
+ "created_on": engine["CREATED_ON"],
761
+ "updated_on": engine["UPDATED_ON"],
762
+ "version": engine["VERSION"],
763
+ "auto_suspend": engine["AUTO_SUSPEND_MINS"],
764
+ "suspends_at": engine["SUSPENDS_AT"]
765
+ }
766
+ return engine_state
767
+
768
+ def get_default_engine_name(self) -> str:
769
+ if self.config.get("engine_name", None) is not None:
770
+ profile = self.config.profile
771
+ raise InvalidAliasError(f"""
772
+ 'engine_name' is not a valid config option.
773
+ If you meant to use a specific engine, use 'engine' instead.
774
+ Otherwise, remove it from your '{profile}' configuration profile.
775
+ """)
776
+ engine = self.config.get("engine", None)
777
+ if not engine and self.config.get("user", None):
778
+ engine = _sanitize_user_name(str(self.config.get("user")))
779
+ if not engine:
780
+ engine = self.get_user_based_engine_name()
781
+ self.config.set("engine", engine)
782
+ return engine
783
+
784
+ def is_valid_engine_state(self, name:str):
785
+ return name in VALID_ENGINE_STATES
786
+
787
+ def _create_engine(
788
+ self,
789
+ name: str,
790
+ size: str | None = None,
791
+ auto_suspend_mins: int | None= None,
792
+ is_async: bool = False,
793
+ headers: Dict | None = None,
794
+ ):
795
+ api = "create_engine_async" if is_async else "create_engine"
796
+ if size is None:
797
+ size = self.config.get_default_engine_size()
798
+ # if auto_suspend_mins is None, get the default value from the config
799
+ if auto_suspend_mins is None:
800
+ auto_suspend_mins = self.config.get_default_auto_suspend_mins()
801
+ try:
802
+ headers = debugging.gen_current_propagation_headers()
803
+ with debugging.span(api, name=name, size=size, auto_suspend_mins=auto_suspend_mins):
804
+ # check in case the config default is missing
805
+ if auto_suspend_mins is None:
806
+ self._exec(f"call {APP_NAME}.api.{api}('{name}', '{size}', null, {headers});")
807
+ else:
808
+ self._exec(f"call {APP_NAME}.api.{api}('{name}', '{size}', PARSE_JSON('{{\"auto_suspend_mins\": {auto_suspend_mins}}}'), {headers});")
809
+ except Exception as e:
810
+ raise EngineProvisioningFailed(name, e) from e
811
+
812
+ def create_engine(self, name:str, size:str|None=None, auto_suspend_mins:int|None=None, headers: Dict | None = None):
813
+ self._create_engine(name, size, auto_suspend_mins, headers=headers)
814
+
815
+ def create_engine_async(self, name:str, size:str|None=None, auto_suspend_mins:int|None=None):
816
+ self._create_engine(name, size, auto_suspend_mins, True)
817
+
818
+ def delete_engine(self, name:str, force:bool = False, headers: Dict | None = None):
819
+ request_headers = debugging.add_current_propagation_headers(headers)
820
+ self._exec(f"call {APP_NAME}.api.delete_engine('{name}', {force},{request_headers});")
821
+
822
+ def suspend_engine(self, name:str):
823
+ self._exec(f"call {APP_NAME}.api.suspend_engine('{name}');")
824
+
825
+ def resume_engine(self, name:str, headers: Dict | None = None) -> Dict:
826
+ request_headers = debugging.add_current_propagation_headers(headers)
827
+ self._exec(f"call {APP_NAME}.api.resume_engine('{name}',{request_headers});")
828
+ # returning empty dict to match the expected return type
829
+ return {}
830
+
831
+ def resume_engine_async(self, name:str, headers: Dict | None = None) -> Dict:
832
+ if headers is None:
833
+ headers = {}
834
+ self._exec(f"call {APP_NAME}.api.resume_engine_async('{name}',{headers});")
835
+ # returning empty dict to match the expected return type
836
+ return {}
837
+
838
+ def alter_engine_pool(self, size:str|None=None, mins:int|None=None, maxs:int|None=None):
839
+ """Alter engine pool node limits for Snowflake."""
840
+ self._exec(f"call {APP_NAME}.api.alter_engine_pool_node_limits('{size}', {mins}, {maxs});")
841
+
842
+ #--------------------------------------------------
843
+ # Graphs
844
+ #--------------------------------------------------
845
+
846
+ def list_graphs(self) -> List[AvailableModel]:
847
+ with debugging.span("list_models"):
848
+ query = textwrap.dedent(f"""
849
+ SELECT NAME, ID, CREATED_BY, CREATED_ON, STATE, DELETED_BY, DELETED_ON
850
+ FROM {APP_NAME}.api.databases
851
+ WHERE state <> 'DELETED'
852
+ ORDER BY NAME ASC;
853
+ """)
854
+ results = self._exec(query)
855
+ if not results:
856
+ return []
857
+ return [
858
+ {
859
+ "name": row["NAME"],
860
+ "id": row["ID"],
861
+ "created_by": row["CREATED_BY"],
862
+ "created_on": row["CREATED_ON"],
863
+ "state": row["STATE"],
864
+ "deleted_by": row["DELETED_BY"],
865
+ "deleted_on": row["DELETED_ON"],
866
+ }
867
+ for row in results
868
+ ]
869
+
870
+ def get_graph(self, name: str):
871
+ res = self.get_database(name)
872
+ if res and res.get("state") != "DELETED":
873
+ return res
874
+
875
+ def create_graph(self, name: str):
876
+ with debugging.span("create_model", name=name):
877
+ self._exec(f"call {APP_NAME}.api.create_database('{name}', false, {debugging.gen_current_propagation_headers()});")
878
+
879
+ def delete_graph(self, name:str, force=False, language:str="rel"):
880
+ prop_hdrs = debugging.gen_current_propagation_headers()
881
+ if self.config.get("use_graph_index", USE_GRAPH_INDEX):
882
+ keep_database = not force and self.config.get("reuse_model", True)
883
+ with debugging.span("release_index", name=name, keep_database=keep_database, language=language):
884
+ #TODO add headers to release_index
885
+ response = self._exec(f"call {APP_NAME}.api.release_index('{name}', OBJECT_CONSTRUCT('keep_database', {keep_database}, 'language', '{language}', 'user_agent', '{get_pyrel_version(self.generation)}'));")
886
+ if response:
887
+ result = next(iter(response))
888
+ obj = json.loads(result["RELEASE_INDEX"])
889
+ error = obj.get('error', None)
890
+ if error and "Model database not found" not in error:
891
+ raise Exception(f"Error releasing index: {error}")
892
+ else:
893
+ raise Exception("There was no response from the release index call.")
894
+ else:
895
+ with debugging.span("delete_model", name=name):
896
+ self._exec(f"call {APP_NAME}.api.delete_database('{name}', false, {prop_hdrs});")
897
+
898
+ def clone_graph(self, target_name:str, source_name:str, nowait_durable=True, force=False):
899
+ if force and self.get_graph(target_name):
900
+ self.delete_graph(target_name)
901
+ with debugging.span("clone_model", target_name=target_name, source_name=source_name):
902
+ # not a mistake: the clone_database argument order is indeed target then source:
903
+ headers = debugging.gen_current_propagation_headers()
904
+ self._exec(f"call {APP_NAME}.api.clone_database('{target_name}', '{source_name}', {nowait_durable}, {headers});")
905
+
906
+ def _poll_use_index(
907
+ self,
908
+ app_name: str,
909
+ sources: Iterable[str],
910
+ model: str,
911
+ engine_name: str,
912
+ engine_size: str | None = None,
913
+ program_span_id: str | None = None,
914
+ headers: Dict | None = None,
915
+ ) -> None:
916
+ """
917
+ Poll use_index to prepare indices for the given sources.
918
+
919
+ This is an optional interface method. Base Resources provides a no-op implementation.
920
+ UseIndexResources and DirectAccessResources override this to provide actual polling.
921
+
922
+ Returns:
923
+ None for base implementation. Child classes may return poller results.
924
+ """
925
+ return None
926
+
927
+ def maybe_poll_use_index(
928
+ self,
929
+ app_name: str,
930
+ sources: Iterable[str],
931
+ model: str,
932
+ engine_name: str,
933
+ engine_size: str | None = None,
934
+ program_span_id: str | None = None,
935
+ headers: Dict | None = None,
936
+ ) -> None:
937
+ """
938
+ Only call _poll_use_index if there are sources to process.
939
+
940
+ This is an optional interface method. Base Resources provides a no-op implementation.
941
+ UseIndexResources and DirectAccessResources override this to provide actual polling with caching.
942
+
943
+ Returns:
944
+ None for base implementation. Child classes may return poller results.
945
+ """
946
+ return None
947
+
948
+ #--------------------------------------------------
949
+ # Models
950
+ #--------------------------------------------------
951
+
952
+ def list_models(self, database: str, engine: str):
953
+ pass
954
+
955
+ def create_models(self, database: str, engine: str | None, models:List[Tuple[str, str]]) -> List[Any]:
956
+ rel_code = self.create_models_code(models)
957
+ self.exec_raw(database, engine, rel_code, readonly=False)
958
+ # TODO: handle SPCS errors once they're figured out
959
+ return []
960
+
961
+ def delete_model(self, database:str, engine:str | None, name:str):
962
+ self.exec_raw(database, engine, f"def delete[:rel, :catalog, :model, \"{name}\"]: rel[:catalog, :model, \"{name}\"]", readonly=False)
963
+
964
+ def create_models_code(self, models:List[Tuple[str, str]]) -> str:
965
+ lines = []
966
+ for (name, code) in models:
967
+ name = name.replace("\"", "\\\"")
968
+ assert "\"\"\"\"\"\"\"" not in code, "Code literals must use fewer than 7 quotes."
969
+
970
+ lines.append(textwrap.dedent(f"""
971
+ def delete[:rel, :catalog, :model, "{name}"]: rel[:catalog, :model, "{name}"]
972
+ def insert[:rel, :catalog, :model, "{name}"]: raw\"\"\"\"\"\"\"
973
+ """) + code + "\n\"\"\"\"\"\"\"")
974
+ rel_code = "\n\n".join(lines)
975
+ return rel_code
976
+
977
+ #--------------------------------------------------
978
+ # Exports
979
+ #--------------------------------------------------
980
+
981
+ def list_exports(self, database: str, engine: str):
982
+ return []
983
+
984
+
985
+ def get_export_code(self, params: ExportParams, all_installs):
986
+ sql_inputs = ", ".join([f"{name} {type_to_sql(type)}" for (name, _, type) in params.inputs])
987
+ input_names = [name for (name, *_) in params.inputs]
988
+ has_return_hint = params.out_fields and isinstance(params.out_fields[0], tuple)
989
+ if has_return_hint:
990
+ sql_out = ", ".join([f"\"{name}\" {type_to_sql(type)}" for (name, type) in params.out_fields])
991
+ sql_out_names = ", ".join([f"('{name}', '{type_to_sql(type)}')" for (ix, (name, type)) in enumerate(params.out_fields)])
992
+ py_outs = ", ".join([f"StructField(\"{name}\", {type_to_snowpark(type)})" for (name, type) in params.out_fields])
993
+ else:
994
+ sql_out = ""
995
+ sql_out_names = ", ".join([f"'{name}'" for name in params.out_fields])
996
+ py_outs = ", ".join([f"StructField(\"{name}\", {type_to_snowpark(str)})" for name in params.out_fields])
997
+ py_inputs = ", ".join([name for (name, *_) in params.inputs])
998
+ safe_rel = escape_for_f_string(params.code).strip()
999
+ clean_inputs = []
1000
+ for (name, var, type) in params.inputs:
1001
+ if type is str:
1002
+ clean_inputs.append(f"{name} = '\"' + escape({name}) + '\"'")
1003
+ # Replace `var` with `name` and keep the following non-word character unchanged
1004
+ pattern = re.compile(re.escape(var) + r'(\W)')
1005
+ value = format_sproc_name(name, type)
1006
+ safe_rel = re.sub(pattern, rf"{{{value}}}\1", safe_rel)
1007
+ if py_inputs:
1008
+ py_inputs = f", {py_inputs}"
1009
+ clean_inputs = ("\n").join(clean_inputs)
1010
+ file = "export_procedure.py.jinja"
1011
+ with importlib.resources.open_text(
1012
+ "relationalai.clients.resources.snowflake", file
1013
+ ) as f:
1014
+ template = f.read()
1015
+ def quote(s: str, f = False) -> str:
1016
+ return '"' + s + '"' if not f else 'f"' + s + '"'
1017
+
1018
+ wait_for_stream_sync = self.config.get("wait_for_stream_sync", WAIT_FOR_STREAM_SYNC)
1019
+ # 1. Check the sources for staled sources
1020
+ # 2. Get the object references for the sources
1021
+ # TODO: this could be optimized to do it in the run time of the stored procedure
1022
+ # instead of doing it here. It will make it more reliable when sources are
1023
+ # modified after the stored procedure is created.
1024
+ checked_sources = self._check_source_updates(self.sources)
1025
+ source_obj_references = self._get_source_references(checked_sources)
1026
+
1027
+ # Escape double quotes in the source object references
1028
+ escaped_source_obj_references = [source.replace('"', '\\"') for source in source_obj_references]
1029
+ escaped_proc_database = params.proc_database.replace('"', '\\"')
1030
+
1031
+ normalized_func_name = IdentityParser(params.func_name).identity
1032
+ assert normalized_func_name is not None, "Function name must be set"
1033
+ skip_invalid_data = params.skip_invalid_data
1034
+ python_code = process_jinja_template(
1035
+ template,
1036
+ func_name=quote(normalized_func_name),
1037
+ database=quote(params.root_database),
1038
+ proc_database=quote(escaped_proc_database),
1039
+ engine=quote(params.engine),
1040
+ rel_code=quote(safe_rel, f=True),
1041
+ APP_NAME=quote(APP_NAME),
1042
+ input_names=input_names,
1043
+ outputs=sql_out,
1044
+ sql_out_names=sql_out_names,
1045
+ clean_inputs=clean_inputs,
1046
+ py_inputs=py_inputs,
1047
+ py_outs=py_outs,
1048
+ skip_invalid_data=skip_invalid_data,
1049
+ source_references=", ".join(escaped_source_obj_references),
1050
+ install_code=all_installs.replace("\\", "\\\\").replace("\n", "\\n"),
1051
+ has_return_hint=has_return_hint,
1052
+ wait_for_stream_sync=wait_for_stream_sync,
1053
+ ).strip()
1054
+ return_clause = f"TABLE({sql_out})" if sql_out else "STRING"
1055
+ destination_input = "" if sql_out else "save_as_table STRING DEFAULT NULL,"
1056
+ module_name = sanitize_module_name(normalized_func_name)
1057
+ stage = f"@{self.get_app_name()}.app_state.stored_proc_code_stage"
1058
+ file_loc = f"{stage}/{module_name}.py"
1059
+ python_code = python_code.replace(APP_NAME, self.get_app_name())
1060
+
1061
+ hash = hashlib.sha256()
1062
+ hash.update(python_code.encode('utf-8'))
1063
+ code_hash = hash.hexdigest()
1064
+ print(code_hash)
1065
+
1066
+ sql_code = textwrap.dedent(f"""
1067
+ CREATE OR REPLACE PROCEDURE {normalized_func_name}({sql_inputs}{sql_inputs and ',' or ''} {destination_input} engine STRING DEFAULT NULL)
1068
+ RETURNS {return_clause}
1069
+ LANGUAGE PYTHON
1070
+ RUNTIME_VERSION = '3.10'
1071
+ IMPORTS = ('{file_loc}')
1072
+ PACKAGES = ('snowflake-snowpark-python')
1073
+ HANDLER = 'checked_handle'
1074
+ EXECUTE AS CALLER
1075
+ AS
1076
+ $$
1077
+ import {module_name}
1078
+ import inspect, hashlib, os, sys
1079
+ def checked_handle(*args, **kwargs):
1080
+ import_dir = sys._xoptions["snowflake_import_directory"]
1081
+ wheel_path = os.path.join(import_dir, '{module_name}.py')
1082
+ h = hashlib.sha256()
1083
+ with open(wheel_path, 'rb') as f:
1084
+ for chunk in iter(lambda: f.read(1<<20), b''):
1085
+ h.update(chunk)
1086
+ code_hash = h.hexdigest()
1087
+ if code_hash != '{code_hash}':
1088
+ raise RuntimeError("Code hash mismatch. The code has been modified since it was uploaded.")
1089
+ # Call the handle function with the provided arguments
1090
+ return {module_name}.handle(*args, **kwargs)
1091
+
1092
+ $$;
1093
+ """)
1094
+ # print(f"\n--- python---\n{python_code}\n--- end python---\n")
1095
+ # This check helps catch invalid code early and for dry runs:
1096
+ try:
1097
+ ast.parse(python_code)
1098
+ except SyntaxError:
1099
+ raise ValueError(f"Internal error: invalid Python code generated:\n{python_code}")
1100
+ return (sql_code, python_code, file_loc)
1101
+
1102
+ def get_sproc_models(self, params: ExportParams):
1103
+ if self._sproc_models is not None:
1104
+ return self._sproc_models
1105
+
1106
+ with debugging.span("get_sproc_models"):
1107
+ code = """
1108
+ def output(name, model):
1109
+ rel(:catalog, :model, name, model)
1110
+ and not starts_with(name, "rel/")
1111
+ and not starts_with(name, "pkg/rel")
1112
+ and not starts_with(name, "pkg/std")
1113
+ and starts_with(name, "pkg/")
1114
+ """
1115
+ res = self.exec_raw(params.model_database, params.engine, code, readonly=True, nowait_durable=True)
1116
+ df, errors = result_helpers.format_results(res, None, ["name", "model"])
1117
+ models = []
1118
+ for row in df.itertuples():
1119
+ models.append((row.name, row.model))
1120
+ self._sproc_models = models
1121
+ return models
1122
+
1123
+ def create_export(self, params: ExportParams):
1124
+ with debugging.span("create_export") as span:
1125
+ if params.dry_run:
1126
+ (sql_code, python_code, file_loc) = self.get_export_code(params, params.install_code)
1127
+ span["sql"] = sql_code
1128
+ return
1129
+
1130
+ start = time.perf_counter()
1131
+ use_graph_index = self.config.get("use_graph_index", USE_GRAPH_INDEX)
1132
+ # for the non graph index case we need to create the cloned proc database
1133
+ if not use_graph_index:
1134
+ raise RAIException(
1135
+ "To ensure permissions are properly accounted for, stored procedures require using the graph index. "
1136
+ "Set use_graph_index=True in your config to proceed."
1137
+ )
1138
+
1139
+ models = self.get_sproc_models(params)
1140
+ lib_installs = self.create_models_code(models)
1141
+ all_installs = lib_installs + "\n\n" + params.install_code
1142
+
1143
+ (sql_code, python_code, file_loc) = self.get_export_code(params, all_installs)
1144
+
1145
+ span["sql"] = sql_code
1146
+ assert self._session
1147
+
1148
+ with debugging.span("upload_sproc_code"):
1149
+ code_bytes = python_code.encode('utf-8')
1150
+ code_stream = io.BytesIO(code_bytes)
1151
+ self._session.file.put_stream(code_stream, file_loc, auto_compress=False, overwrite=True)
1152
+
1153
+ with debugging.span("sql_install"):
1154
+ self._exec(sql_code)
1155
+
1156
+ debugging.time("export", time.perf_counter() - start, DataFrame(), code=sql_code.replace(APP_NAME, self.get_app_name()))
1157
+
1158
+
1159
+ def create_export_table(self, database: str, engine: str, table: str, relation: str, columns: Dict[str, str], code: str, refresh: str|None=None):
1160
+ print("Snowflake doesn't support creating export tables yet. Try creating the table manually first.")
1161
+ pass
1162
+
1163
+ def delete_export(self, database: str, engine: str, name: str):
1164
+ pass
1165
+
1166
+ #--------------------------------------------------
1167
+ # Imports
1168
+ #--------------------------------------------------
1169
+
1170
+
1171
+ def change_stream_status(self, stream_id: str, model:str, suspend: bool):
1172
+ if stream_id and model:
1173
+ if suspend:
1174
+ self._exec(f"CALL {APP_NAME}.api.suspend_data_stream('{stream_id}', '{model}');")
1175
+ else:
1176
+ self._exec(f"CALL {APP_NAME}.api.resume_data_stream('{stream_id}', '{model}');")
1177
+
1178
+ def change_imports_status(self, suspend: bool):
1179
+ if suspend:
1180
+ self._exec(f"CALL {APP_NAME}.app.suspend_cdc();")
1181
+ else:
1182
+ self._exec(f"CALL {APP_NAME}.app.resume_cdc();")
1183
+
1184
+ def get_imports_status(self) -> ImportsStatus|None:
1185
+ # NOTE: We expect there to only ever be one result?
1186
+ results = self._exec(f"CALL {APP_NAME}.app.cdc_status();")
1187
+ if results:
1188
+ result = next(iter(results))
1189
+ engine = result['CDC_ENGINE_NAME']
1190
+ engine_status = result['CDC_ENGINE_STATUS']
1191
+ engine_size = result['CDC_ENGINE_SIZE']
1192
+ task_status = result['CDC_TASK_STATUS']
1193
+ info = result['CDC_TASK_INFO']
1194
+ enabled = result['CDC_ENABLED']
1195
+ return {"engine": engine, "engine_size": engine_size, "engine_status": engine_status, "status": task_status, "enabled": enabled, "info": info }
1196
+ return None
1197
+
1198
+ def set_imports_engine_size(self, size:str):
1199
+ try:
1200
+ self._exec(f"CALL {APP_NAME}.app.alter_cdc_engine_size('{size}');")
1201
+ except Exception as e:
1202
+ raise e
1203
+
1204
+ def list_imports(
1205
+ self,
1206
+ id:str|None = None,
1207
+ name:str|None = None,
1208
+ model:str|None = None,
1209
+ status:str|None = None,
1210
+ creator:str|None = None,
1211
+ ) -> list[Import]:
1212
+ where = []
1213
+ if id and isinstance(id, str):
1214
+ where.append(f"LOWER(ID) = '{id.lower()}'")
1215
+ if name and isinstance(name, str):
1216
+ where.append(f"LOWER(FQ_OBJECT_NAME) = '{name.lower()}'")
1217
+ if model and isinstance(model, str):
1218
+ where.append(f"LOWER(RAI_DATABASE) = '{model.lower()}'")
1219
+ if creator and isinstance(creator, str):
1220
+ where.append(f"LOWER(CREATED_BY) = '{creator.lower()}'")
1221
+ if status and isinstance(status, str):
1222
+ where.append(f"LOWER(batch_status) = '{status.lower()}'")
1223
+ where_clause = " AND ".join(where)
1224
+
1225
+ # This is roughly inspired by the native app code because we don't have a way to
1226
+ # get the status of multiple streams at once and doing them individually is way
1227
+ # too slow. We use window functions to get the status of the stream and the batch
1228
+ # details.
1229
+ statement = f"""
1230
+ SELECT
1231
+ ID,
1232
+ RAI_DATABASE,
1233
+ FQ_OBJECT_NAME,
1234
+ CREATED_AT,
1235
+ CREATED_BY,
1236
+ CASE
1237
+ WHEN nextBatch.quarantined > 0 THEN 'quarantined'
1238
+ ELSE nextBatch.status
1239
+ END as batch_status,
1240
+ nextBatch.processing_errors,
1241
+ nextBatch.batches
1242
+ FROM {APP_NAME}.api.data_streams as ds
1243
+ LEFT JOIN (
1244
+ SELECT DISTINCT
1245
+ data_stream_id,
1246
+ -- Get status from the progress record using window functions
1247
+ FIRST_VALUE(status) OVER (
1248
+ PARTITION BY data_stream_id
1249
+ ORDER BY
1250
+ CASE WHEN unloaded IS NOT NULL THEN 1 ELSE 0 END DESC,
1251
+ unloaded ASC
1252
+ ) as status,
1253
+ -- Get batch_details from the same record
1254
+ FIRST_VALUE(batch_details) OVER (
1255
+ PARTITION BY data_stream_id
1256
+ ORDER BY
1257
+ CASE WHEN unloaded IS NOT NULL THEN 1 ELSE 0 END DESC,
1258
+ unloaded ASC
1259
+ ) as batch_details,
1260
+ -- Aggregate the other fields
1261
+ FIRST_VALUE(processing_details:processingErrors) OVER (
1262
+ PARTITION BY data_stream_id
1263
+ ORDER BY
1264
+ CASE WHEN unloaded IS NOT NULL THEN 1 ELSE 0 END DESC,
1265
+ unloaded ASC
1266
+ ) as processing_errors,
1267
+ MIN(unloaded) OVER (PARTITION BY data_stream_id) as unloaded,
1268
+ COUNT(*) OVER (PARTITION BY data_stream_id) as batches,
1269
+ COUNT_IF(status = 'quarantined') OVER (PARTITION BY data_stream_id) as quarantined
1270
+ FROM {APP_NAME}.api.data_stream_batches
1271
+ ) nextBatch
1272
+ ON ds.id = nextBatch.data_stream_id
1273
+ {f"where {where_clause}" if where_clause else ""}
1274
+ ORDER BY FQ_OBJECT_NAME ASC;
1275
+ """
1276
+ results = self._exec(statement)
1277
+ items = []
1278
+ if results:
1279
+ for stream in results:
1280
+ (id, db, name, created_at, created_by, status, processing_errors, batches) = stream
1281
+ if status and isinstance(status, str):
1282
+ status = status.upper()
1283
+ if processing_errors:
1284
+ if status in ["QUARANTINED", "PENDING"]:
1285
+ start = processing_errors.rfind("Error")
1286
+ if start != -1:
1287
+ processing_errors = processing_errors[start:-1]
1288
+ else:
1289
+ processing_errors = None
1290
+ items.append(cast(Import, {
1291
+ "id": id,
1292
+ "model": db,
1293
+ "name": name,
1294
+ "created": created_at,
1295
+ "creator": created_by,
1296
+ "status": status.upper() if status else None,
1297
+ "errors": processing_errors if processing_errors != "[]" else None,
1298
+ "batches": f"{batches}" if batches else "",
1299
+ }))
1300
+ return items
1301
+
1302
+ def poll_imports(self, sources:List[str], model:str):
1303
+ source_set = self._create_source_set(sources)
1304
+ def check_imports():
1305
+ imports = [
1306
+ import_
1307
+ for import_ in self.list_imports(model=model)
1308
+ if import_["name"] in source_set
1309
+ ]
1310
+ # loop through printing status for each in the format (index): (name) - (status)
1311
+ statuses = [import_["status"] for import_ in imports]
1312
+ if all(status == "LOADED" for status in statuses):
1313
+ return True
1314
+ if any(status == "QUARANTINED" for status in statuses):
1315
+ failed_imports = [import_["name"] for import_ in imports if import_["status"] == "QUARANTINED"]
1316
+ raise RAIException("Imports failed:" + ", ".join(failed_imports)) from None
1317
+ # this check is necessary in case some of the tables are empty;
1318
+ # such tables may be synced even though their status is None:
1319
+ def synced(import_):
1320
+ if import_["status"] == "LOADED":
1321
+ return True
1322
+ if import_["status"] is None:
1323
+ import_full_status = self.get_import_stream(import_["name"], model)
1324
+ if import_full_status and import_full_status[0]["data_sync_status"] == "SYNCED":
1325
+ return True
1326
+ return False
1327
+ if all(synced(import_) for import_ in imports):
1328
+ return True
1329
+ poll_with_specified_overhead(check_imports, overhead_rate=0.1, max_delay=10)
1330
+
1331
+ def _create_source_set(self, sources: List[str]) -> set:
1332
+ return {
1333
+ source.upper() if not IdentityParser(source).has_double_quoted_identifier else IdentityParser(source).identity
1334
+ for source in sources
1335
+ }
1336
+
1337
+ def get_import_stream(self, name:str|None, model:str|None):
1338
+ results = self._exec(f"CALL {APP_NAME}.api.get_data_stream('{name}', '{model}');")
1339
+ if not results:
1340
+ return None
1341
+ return imports_to_dicts(results)
1342
+
1343
+ def create_import_stream(self, source:ImportSource, model:str, rate = 1, options: dict|None = None):
1344
+ assert isinstance(source, ImportSourceTable), "Snowflake integration only supports loading from SF Tables. Try loading your data as a table via the Snowflake interface first."
1345
+ object = source.fqn
1346
+
1347
+ # Parse only to the schema level
1348
+ schemaParser = IdentityParser(f"{source.database}.{source.schema}")
1349
+
1350
+ if object.lower() in [x["name"].lower() for x in self.list_imports(model=model)]:
1351
+ return
1352
+
1353
+ query = f"SHOW OBJECTS LIKE '{source.table}' IN {schemaParser.identity}"
1354
+
1355
+ info = self._exec(query)
1356
+ if not info:
1357
+ raise ValueError(f"Object {source.table} not found in schema {schemaParser.identity}")
1358
+ else:
1359
+ data = info[0]
1360
+ if not data:
1361
+ raise ValueError(f"Object {source.table} not found in {schemaParser.identity}")
1362
+ # (time, name, db_name, schema_name, kind, *rest)
1363
+ kind = data["kind"]
1364
+
1365
+ relation_name = to_fqn_relation_name(object)
1366
+
1367
+ command = f"""call {APP_NAME}.api.create_data_stream(
1368
+ {APP_NAME}.api.object_reference('{kind}', '{object}'),
1369
+ '{model}',
1370
+ '{relation_name}');"""
1371
+
1372
+ def create_stream(tracking_just_changed=False):
1373
+ try:
1374
+ self._exec(command)
1375
+ except Exception as e:
1376
+ messages = collect_error_messages(e)
1377
+ if any("ensure that change_tracking is enabled on the source object" in msg for msg in messages):
1378
+ if self.config.get("ensure_change_tracking", False) and not tracking_just_changed:
1379
+ try:
1380
+ self._exec(f"ALTER {kind} {object} SET CHANGE_TRACKING = TRUE;")
1381
+ create_stream(tracking_just_changed=True)
1382
+ except Exception:
1383
+ pass
1384
+ else:
1385
+ print("\n")
1386
+ exception = SnowflakeChangeTrackingNotEnabledException((object, kind))
1387
+ raise exception from None
1388
+ elif any("database does not exist" in msg for msg in messages):
1389
+ print("\n")
1390
+ raise ModelNotFoundException(model) from None
1391
+ raise e
1392
+
1393
+ create_stream()
1394
+
1395
+ def create_import_snapshot(self, source:ImportSource, model:str, options: dict|None = None):
1396
+ raise Exception("Snowflake integration doesn't support snapshot imports yet")
1397
+
1398
+ def delete_import(self, import_name:str, model:str, force = False):
1399
+ engine = self.get_default_engine_name()
1400
+ rel_name = to_fqn_relation_name(import_name)
1401
+ try:
1402
+ self._exec(f"""call {APP_NAME}.api.delete_data_stream(
1403
+ '{import_name}',
1404
+ '{model}'
1405
+ );""")
1406
+ except RAIException as err:
1407
+ if "streams do not exist" not in str(err) or not force:
1408
+ raise
1409
+
1410
+ # if force is true, we delete the leftover relation to free up the name (in case the user re-creates the stream)
1411
+ if force:
1412
+ self.exec_raw(model, engine, f"""
1413
+ declare ::{rel_name}
1414
+ def delete[:\"{rel_name}\"]: {{ {rel_name} }}
1415
+ """, readonly=False, bypass_index=True)
1416
+
1417
+ #--------------------------------------------------
1418
+ # Exec Async
1419
+ #--------------------------------------------------
1420
+
1421
+ def _check_exec_async_status(self, txn_id: str, headers: Dict | None = None):
1422
+ """Check whether the given transaction has completed."""
1423
+ if headers is None:
1424
+ headers = {}
1425
+
1426
+ with debugging.span("check_status"):
1427
+ response = self._exec(f"CALL {APP_NAME}.api.get_transaction('{txn_id}',{headers});")
1428
+ assert response, f"No results from get_transaction('{txn_id}')"
1429
+
1430
+ response_row = next(iter(response)).asDict()
1431
+ status: str = response_row['STATE']
1432
+
1433
+ # remove the transaction from the pending list if it's completed or aborted
1434
+ if status in ["COMPLETED", "ABORTED"]:
1435
+ if txn_id in self._pending_transactions:
1436
+ self._pending_transactions.remove(txn_id)
1437
+
1438
+ if status == "ABORTED":
1439
+ if response_row.get("ABORT_REASON", "") == TXN_ABORT_REASON_TIMEOUT:
1440
+ config_file_path = getattr(self.config, 'file_path', None)
1441
+ # todo: use the timeout returned alongside the transaction as soon as it's exposed
1442
+ timeout_mins = int(self.config.get("query_timeout_mins", DEFAULT_QUERY_TIMEOUT_MINS) or DEFAULT_QUERY_TIMEOUT_MINS)
1443
+ raise QueryTimeoutExceededException(
1444
+ timeout_mins=timeout_mins,
1445
+ query_id=txn_id,
1446
+ config_file_path=config_file_path,
1447
+ )
1448
+ elif response_row.get("ABORT_REASON", "") == GUARDRAILS_ABORT_REASON:
1449
+ raise GuardRailsException()
1450
+
1451
+ # @TODO: Find some way to tunnel the ABORT_REASON out. Azure doesn't have this, but it's handy
1452
+ return status == "COMPLETED" or status == "ABORTED"
1453
+
1454
+
1455
+ def _list_exec_async_artifacts(self, txn_id: str, headers: Dict | None = None) -> Dict[str, Dict]:
1456
+ """Grab the list of artifacts produced in the transaction and the URLs to retrieve their contents."""
1457
+ if headers is None:
1458
+ headers = {}
1459
+ with debugging.span("list_results"):
1460
+ response = self._exec(
1461
+ f"CALL {APP_NAME}.api.get_own_transaction_artifacts('{txn_id}',{headers});"
1462
+ )
1463
+ assert response, f"No results from get_own_transaction_artifacts('{txn_id}')"
1464
+ return {row["FILENAME"]: row for row in response}
1465
+
1466
+ def _fetch_exec_async_artifacts(
1467
+ self, artifact_info: Dict[str, Dict[str, Any]]
1468
+ ) -> Dict[str, Any]:
1469
+ """Grab the contents of the given artifacts from SF in parallel using threads."""
1470
+
1471
+ with requests.Session() as session:
1472
+ def _fetch_data(name_info):
1473
+ filename, metadata = name_info
1474
+
1475
+ try:
1476
+ # Extract the presigned URL and encryption material from metadata
1477
+ url_key = self.get_url_key(metadata)
1478
+ presigned_url = metadata[url_key]
1479
+ encryption_material = metadata["ENCRYPTION_MATERIAL"]
1480
+
1481
+ response = get_with_retries(session, presigned_url, config=self.config)
1482
+ response.raise_for_status() # Throw if something goes wrong
1483
+
1484
+ decrypted = self._maybe_decrypt(response.content, encryption_material)
1485
+ return (filename, decrypted)
1486
+
1487
+ except requests.RequestException as e:
1488
+ raise scrub_exception(wrap_with_request_id(e))
1489
+
1490
+ # Create a list of tuples for the map function
1491
+ name_info_pairs = list(artifact_info.items())
1492
+
1493
+ with ThreadPoolExecutor(max_workers=5) as executor:
1494
+ results = executor.map(_fetch_data, name_info_pairs)
1495
+
1496
+ return {name: data for (name, data) in results}
1497
+
1498
+ def _maybe_decrypt(self, content: bytes, encryption_material: str) -> bytes:
1499
+ # Decrypt if encryption material is present
1500
+ if encryption_material:
1501
+ # if there's no padding, the initial file was empty
1502
+ if len(content) == 0:
1503
+ return b""
1504
+
1505
+ return decrypt_artifact(content, encryption_material)
1506
+
1507
+ # otherwise, return content directly
1508
+ return content
1509
+
1510
+ def _parse_exec_async_results(self, arrow_files: List[Tuple[str, bytes]]):
1511
+ """Mimics the logic in _parse_arrow_results of railib/api.py#L303 without requiring a wrapping multipart form."""
1512
+ results = []
1513
+
1514
+ for file_name, file_content in arrow_files:
1515
+ with pa.ipc.open_stream(file_content) as reader:
1516
+ schema = reader.schema
1517
+ batches = [batch for batch in reader]
1518
+ table = pa.Table.from_batches(batches=batches, schema=schema)
1519
+ results.append({"relationId": file_name, "table": table})
1520
+
1521
+ return results
1522
+
1523
+ def _download_results(
1524
+ self, artifact_info: Dict[str, Dict], txn_id: str, state: str
1525
+ ) -> TransactionAsyncResponse:
1526
+ with debugging.span("download_results"):
1527
+ # Fetch artifacts
1528
+ artifacts = self._fetch_exec_async_artifacts(artifact_info)
1529
+
1530
+ # Directly use meta_json as it is fetched
1531
+ meta_json_bytes = artifacts["metadata.json"]
1532
+
1533
+ # Decode the bytes and parse the JSON
1534
+ meta_json_str = meta_json_bytes.decode('utf-8')
1535
+ meta_json = json.loads(meta_json_str) # Parse the JSON string
1536
+
1537
+ # Use the metadata to map arrow files to the relations they contain
1538
+ try:
1539
+ arrow_files_to_relations = {
1540
+ artifact["filename"]: artifact["relationId"]
1541
+ for artifact in meta_json
1542
+ }
1543
+ except KeyError:
1544
+ # TODO: Remove this fallback mechanism later once several engine versions are updated
1545
+ arrow_files_to_relations = {
1546
+ f"{ix}.arrow": artifact["relationId"]
1547
+ for ix, artifact in enumerate(meta_json)
1548
+ }
1549
+
1550
+ # Hydrate the arrow files into tables
1551
+ results = self._parse_exec_async_results(
1552
+ [
1553
+ (arrow_files_to_relations[name], content)
1554
+ for name, content in artifacts.items()
1555
+ if name.endswith(".arrow")
1556
+ ]
1557
+ )
1558
+
1559
+ # Create and return the response
1560
+ rsp = TransactionAsyncResponse()
1561
+ rsp.transaction = {
1562
+ "id": txn_id,
1563
+ "state": state,
1564
+ "response_format_version": None,
1565
+ }
1566
+ rsp.metadata = meta_json
1567
+ rsp.problems = artifacts.get(
1568
+ "problems.json"
1569
+ ) # Safely access possible missing keys
1570
+ rsp.results = results
1571
+ return rsp
1572
+
1573
+ def get_transaction_problems(self, txn_id: str) -> List[Dict[str, Any]]:
1574
+ with debugging.span("get_own_transaction_problems"):
1575
+ response = self._exec(
1576
+ f"select * from table({APP_NAME}.api.get_own_transaction_problems('{txn_id}'));"
1577
+ )
1578
+ if not response:
1579
+ return []
1580
+ return response
1581
+
1582
+ def get_url_key(self, metadata) -> str:
1583
+ # In Azure, there is only one type of URL, which is used for both internal and
1584
+ # external access; always use that one
1585
+ if is_azure_url(metadata['PRESIGNED_URL']):
1586
+ return 'PRESIGNED_URL'
1587
+
1588
+ configured = self.config.get("download_url_type", None)
1589
+ if configured == "internal":
1590
+ return 'PRESIGNED_URL_AP'
1591
+ elif configured == "external":
1592
+ return "PRESIGNED_URL"
1593
+
1594
+ if is_container_runtime():
1595
+ return 'PRESIGNED_URL_AP'
1596
+
1597
+ return 'PRESIGNED_URL'
1598
+
1599
+ def _exec_rai_app(
1600
+ self,
1601
+ database: str,
1602
+ engine: str | None,
1603
+ raw_code: str,
1604
+ inputs: Dict,
1605
+ readonly=True,
1606
+ nowait_durable=False,
1607
+ request_headers: Dict | None = None,
1608
+ bypass_index=False,
1609
+ language: str = "rel",
1610
+ query_timeout_mins: int | None = None,
1611
+ ):
1612
+ """
1613
+ High-level method to execute RAI app stored procedures.
1614
+
1615
+ Builds and executes SQL to call the RAI app's exec_async_v2 stored procedure.
1616
+ This method handles the SQL string construction for two different formats:
1617
+ 1. New format (with graph index): Uses object payload with parameterized query
1618
+ 2. Legacy format: Uses positional parameters
1619
+
1620
+ The choice between formats depends on the use_graph_index configuration.
1621
+ The new format allows the stored procedure to hash the model and username
1622
+ to determine the database, while the legacy format uses the passed database directly.
1623
+
1624
+ This method is called by _exec_async_v2 to create transactions. It skips
1625
+ use_index retry logic (skip_engine_db_error_retry=True) because that
1626
+ is handled at a higher level by exec_raw/exec_lqp.
1627
+
1628
+ Args:
1629
+ database: Database/model name
1630
+ engine: Engine name (optional)
1631
+ raw_code: Code to execute (REL, LQP, or SQL)
1632
+ inputs: Input parameters for the query
1633
+ readonly: Whether the transaction is read-only
1634
+ nowait_durable: Whether to wait for durable writes
1635
+ request_headers: Optional HTTP headers
1636
+ bypass_index: Whether to bypass graph index setup
1637
+ language: Query language ("rel" or "lqp")
1638
+ query_timeout_mins: Optional query timeout in minutes
1639
+
1640
+ Returns:
1641
+ Response from the stored procedure call (transaction creation result)
1642
+
1643
+ Raises:
1644
+ Exception: If transaction creation fails
1645
+ """
1646
+ assert language == "rel" or language == "lqp", "Only 'rel' and 'lqp' languages are supported"
1647
+ if query_timeout_mins is None and (timeout_value := self.config.get("query_timeout_mins", DEFAULT_QUERY_TIMEOUT_MINS)) is not None:
1648
+ query_timeout_mins = int(timeout_value)
1649
+ # Depending on the shape of the input, the behavior of exec_async_v2 changes.
1650
+ # When using the new format (with an object), the function retrieves the
1651
+ # 'rai' database by hashing the model and username. In contrast, the
1652
+ # current version directly uses the passed database value.
1653
+ # Therefore, we must use the original exec_async_v2 when not using the
1654
+ # graph index to ensure the correct database is utilized.
1655
+ use_graph_index = self.config.get("use_graph_index", USE_GRAPH_INDEX)
1656
+ if use_graph_index and not bypass_index:
1657
+ payload = {
1658
+ 'database': database,
1659
+ 'engine': engine,
1660
+ 'inputs': inputs,
1661
+ 'readonly': readonly,
1662
+ 'nowait_durable': nowait_durable,
1663
+ 'language': language,
1664
+ 'headers': request_headers
1665
+ }
1666
+ if query_timeout_mins is not None:
1667
+ payload["timeout_mins"] = query_timeout_mins
1668
+ sql_string = f"CALL {APP_NAME}.api.exec_async_v2(?, {payload});"
1669
+ else:
1670
+ if query_timeout_mins is not None:
1671
+ sql_string = f"CALL {APP_NAME}.api.exec_async_v2('{database}','{engine}', ?, {inputs}, {readonly}, {nowait_durable}, '{language}', {query_timeout_mins}, {request_headers});"
1672
+ else:
1673
+ sql_string = f"CALL {APP_NAME}.api.exec_async_v2('{database}','{engine}', ?, {inputs}, {readonly}, {nowait_durable}, '{language}', {request_headers});"
1674
+ # Don't let exec setup GI on failure, exec_raw and exec_lqp will do that and add the correct headers.
1675
+ response = self._exec(
1676
+ sql_string,
1677
+ raw_code,
1678
+ skip_engine_db_error_retry=True,
1679
+ )
1680
+ if not response:
1681
+ raise Exception("Failed to create transaction")
1682
+ return response
1683
+
1684
+ def _create_v2_txn(
1685
+ self,
1686
+ database: str,
1687
+ engine: str | None,
1688
+ raw_code: str,
1689
+ inputs: Dict,
1690
+ headers: Dict[str, str],
1691
+ readonly: bool,
1692
+ nowait_durable: bool,
1693
+ bypass_index: bool,
1694
+ language: str,
1695
+ query_timeout_mins: int | None,
1696
+ ) -> TxnCreationResult:
1697
+ """
1698
+ Create a transaction and return the result.
1699
+
1700
+ This method handles calling the RAI app stored procedure to create a transaction
1701
+ and parses the response into a standardized TxnCreationResult format.
1702
+
1703
+ This method can be overridden by subclasses (e.g., DirectAccessResources)
1704
+ to use different transport mechanisms (HTTP instead of SQL).
1705
+
1706
+ Args:
1707
+ database: Database/model name
1708
+ engine: Engine name (optional)
1709
+ raw_code: Code to execute (REL, LQP, or SQL)
1710
+ inputs: Input parameters for the query
1711
+ headers: HTTP headers (must be prepared by caller)
1712
+ readonly: Whether the transaction is read-only
1713
+ nowait_durable: Whether to wait for durable writes
1714
+ bypass_index: Whether to bypass graph index setup
1715
+ language: Query language ("rel" or "lqp")
1716
+ query_timeout_mins: Optional query timeout in minutes
1717
+
1718
+ Returns:
1719
+ TxnCreationResult containing txn_id, state, and artifact_info
1720
+ """
1721
+ response = self._exec_rai_app(
1722
+ database=database,
1723
+ engine=engine,
1724
+ raw_code=raw_code,
1725
+ inputs=inputs,
1726
+ readonly=readonly,
1727
+ nowait_durable=nowait_durable,
1728
+ request_headers=headers,
1729
+ bypass_index=bypass_index,
1730
+ language=language,
1731
+ query_timeout_mins=query_timeout_mins,
1732
+ )
1733
+
1734
+ rows = list(iter(response))
1735
+
1736
+ # process the first row since txn_id and state are the same for all rows
1737
+ first_row = rows[0]
1738
+ txn_id = first_row['ID']
1739
+ state = first_row['STATE']
1740
+
1741
+ # Build artifact_info if transaction completed immediately (fast path)
1742
+ artifact_info: Dict[str, Dict] = {}
1743
+ if state in ["COMPLETED", "ABORTED"]:
1744
+ for row in rows:
1745
+ filename = row['FILENAME']
1746
+ artifact_info[filename] = row
1747
+
1748
+ return TxnCreationResult(txn_id=txn_id, state=state, artifact_info=artifact_info)
1749
+
1750
+ def _exec_async_v2(
1751
+ self,
1752
+ database: str,
1753
+ engine: str | None,
1754
+ raw_code: str,
1755
+ inputs: Dict | None = None,
1756
+ readonly=True,
1757
+ nowait_durable=False,
1758
+ headers: Dict | None = None,
1759
+ bypass_index=False,
1760
+ language: str = "rel",
1761
+ query_timeout_mins: int | None = None,
1762
+ gi_setup_skipped: bool = False,
1763
+ ):
1764
+ """
1765
+ High-level async execution method with transaction polling and artifact management.
1766
+
1767
+ This is the core method for executing queries asynchronously. It:
1768
+ 1. Creates a transaction by calling _create_v2_txn
1769
+ 2. Handles two execution paths:
1770
+ - Fast path: Transaction completes immediately (COMPLETED/ABORTED)
1771
+ - Slow path: Transaction is pending, requires polling until completion
1772
+ 3. Manages pending transactions list
1773
+ 4. Downloads and returns query results/artifacts
1774
+
1775
+ This method is called by _execute_code (base implementation), and calls the
1776
+ following methods that can be overridden by child classes (e.g.,
1777
+ DirectAccessResources uses HTTP instead):
1778
+ - _create_v2_txn
1779
+ - _check_exec_async_status
1780
+ - _list_exec_async_artifacts
1781
+ - _download_results
1782
+
1783
+ Args:
1784
+ database: Database/model name
1785
+ engine: Engine name (optional)
1786
+ raw_code: Code to execute (REL, LQP, or SQL)
1787
+ inputs: Input parameters for the query
1788
+ readonly: Whether the transaction is read-only
1789
+ nowait_durable: Whether to wait for durable writes
1790
+ headers: Optional HTTP headers
1791
+ bypass_index: Whether to bypass graph index setup
1792
+ language: Query language ("rel" or "lqp")
1793
+ query_timeout_mins: Optional query timeout in minutes
1794
+ gi_setup_skipped: Whether graph index setup was skipped (for retry logic)
1795
+
1796
+ Returns:
1797
+ Query results (downloaded artifacts)
1798
+ """
1799
+ if inputs is None:
1800
+ inputs = {}
1801
+ request_headers = debugging.add_current_propagation_headers(headers)
1802
+ query_attrs_dict = json.loads(request_headers.get("X-Query-Attributes", "{}"))
1803
+
1804
+ with debugging.span("transaction", **query_attrs_dict) as txn_span:
1805
+ txn_start_time = time.time()
1806
+ with debugging.span("create_v2", **query_attrs_dict) as create_span:
1807
+ # Prepare headers for transaction creation
1808
+ request_headers['user-agent'] = get_pyrel_version(self.generation)
1809
+ request_headers['gi_setup_skipped'] = str(gi_setup_skipped)
1810
+ request_headers['pyrel_program_id'] = debugging.get_program_span_id() or ""
1811
+
1812
+ # Create the transaction
1813
+ result = self._create_v2_txn(
1814
+ database=database,
1815
+ engine=engine,
1816
+ raw_code=raw_code,
1817
+ inputs=inputs,
1818
+ headers=request_headers,
1819
+ readonly=readonly,
1820
+ nowait_durable=nowait_durable,
1821
+ bypass_index=bypass_index,
1822
+ language=language,
1823
+ query_timeout_mins=query_timeout_mins,
1824
+ )
1825
+
1826
+ txn_id = result.txn_id
1827
+ state = result.state
1828
+
1829
+ txn_span["txn_id"] = txn_id
1830
+ create_span["txn_id"] = txn_id
1831
+ debugging.event("transaction_created", txn_span, txn_id=txn_id)
1832
+
1833
+ print_txn_progress = should_print_txn_progress(self.config)
1834
+
1835
+ # fast path: transaction already finished
1836
+ if state in ["COMPLETED", "ABORTED"]:
1837
+ txn_end_time = time.time()
1838
+ if txn_id in self._pending_transactions:
1839
+ self._pending_transactions.remove(txn_id)
1840
+
1841
+ artifact_info = result.artifact_info
1842
+
1843
+ txn_duration = txn_end_time - txn_start_time
1844
+ if print_txn_progress:
1845
+ print(
1846
+ query_complete_message(txn_id, txn_duration, status_header=True)
1847
+ )
1848
+
1849
+ # Slow path: transaction not done yet; start polling
1850
+ else:
1851
+ self._pending_transactions.append(txn_id)
1852
+ # Use the interactive poller for transaction status
1853
+ with debugging.span("wait", txn_id=txn_id):
1854
+ if print_txn_progress:
1855
+ poller = ExecTxnPoller(resource=self, txn_id=txn_id, headers=request_headers, txn_start_time=txn_start_time)
1856
+ poller.poll()
1857
+ else:
1858
+ poll_with_specified_overhead(
1859
+ lambda: self._check_exec_async_status(txn_id, headers=request_headers), 0.1
1860
+ )
1861
+ artifact_info = self._list_exec_async_artifacts(txn_id, headers=request_headers)
1862
+
1863
+ with debugging.span("fetch"):
1864
+ return self._download_results(artifact_info, txn_id, state)
1865
+
1866
+ def get_user_based_engine_name(self):
1867
+ if not self._session:
1868
+ self._session = self.get_sf_session()
1869
+ user_table = self._session.sql("select current_user()").collect()
1870
+ user = user_table[0][0]
1871
+ assert isinstance(user, str), f"current_user() must return a string, not {type(user)}"
1872
+ return _sanitize_user_name(user)
1873
+
1874
+ def is_engine_ready(self, engine_name: str):
1875
+ engine = self.get_engine(engine_name)
1876
+ return engine and engine["state"] == "READY"
1877
+
1878
+ def auto_create_engine(self, name: str | None = None, size: str | None = None, headers: Dict | None = None):
1879
+ """Synchronously create/ensure an engine is ready, blocking until ready."""
1880
+ with debugging.span("auto_create_engine", active=self._active_engine) as span:
1881
+ active = self._get_active_engine()
1882
+ if active:
1883
+ return active
1884
+
1885
+ # Resolve and validate parameters
1886
+ engine_name, engine_size = self._prepare_engine_params(name, size)
1887
+
1888
+ try:
1889
+ # Get current engine state
1890
+ engine = self.get_engine(engine_name)
1891
+ if engine:
1892
+ span.update(cast(dict, engine))
1893
+
1894
+ # Create context for state handling
1895
+ context = EngineContext(
1896
+ engine_name=engine_name,
1897
+ engine_size=engine_size,
1898
+ headers=headers,
1899
+ requested_size=size,
1900
+ span=span,
1901
+ )
1902
+
1903
+ # Process engine state using sync handlers
1904
+ self._process_engine_state(engine, context, self._sync_engine_state_handlers)
1905
+
1906
+ except Exception as e:
1907
+ self._handle_engine_creation_errors(e, engine_name)
1908
+
1909
+ return engine_name
1910
+
1911
+ def auto_create_engine_async(self, name: str | None = None):
1912
+ """Asynchronously create/ensure an engine, returns immediately."""
1913
+ active = self._get_active_engine()
1914
+ if active and (active == name or name is None):
1915
+ return active
1916
+
1917
+ with Spinner(
1918
+ "Checking engine status",
1919
+ leading_newline=True,
1920
+ ) as spinner:
1921
+ with debugging.span("auto_create_engine_async", active=self._active_engine):
1922
+ # Resolve and validate parameters (use_default_size=True for async)
1923
+ engine_name, engine_size = self._prepare_engine_params(name, None, use_default_size=True)
1924
+
1925
+ try:
1926
+ # Get current engine state
1927
+ engine = self.get_engine(engine_name)
1928
+
1929
+ # Create context for state handling
1930
+ context = EngineContext(
1931
+ engine_name=engine_name,
1932
+ engine_size=engine_size,
1933
+ headers=None,
1934
+ requested_size=None,
1935
+ spinner=spinner,
1936
+ )
1937
+
1938
+ # Process engine state using async handlers
1939
+ self._process_engine_state(engine, context, self._async_engine_state_handlers, set_active_on_success=True)
1940
+
1941
+ except Exception as e:
1942
+ spinner.update_messages({
1943
+ "finished_message": f"Failed to create engine {engine_name}",
1944
+ })
1945
+ self._handle_engine_creation_errors(e, engine_name, preserve_rai_exception=True)
1946
+
1947
+ return engine_name
1948
+
1949
+ #--------------------------------------------------
1950
+ # Exec
1951
+ #--------------------------------------------------
1952
+
1953
+ def _execute_code(
1954
+ self,
1955
+ database: str,
1956
+ engine: str | None,
1957
+ raw_code: str,
1958
+ inputs: Dict | None,
1959
+ readonly: bool,
1960
+ nowait_durable: bool,
1961
+ headers: Dict | None,
1962
+ bypass_index: bool,
1963
+ language: str,
1964
+ query_timeout_mins: int | None,
1965
+ ) -> Any:
1966
+ """
1967
+ Template method for code execution - can be overridden by child classes.
1968
+
1969
+ This is a template method that provides a hook for child classes to add
1970
+ execution logic (like retry mechanisms). The base implementation simply
1971
+ calls _exec_async_v2 directly.
1972
+
1973
+ UseIndexResources overrides this method to use _exec_with_gi_retry, which
1974
+ adds automatic use_index polling on engine/database errors.
1975
+
1976
+ This method is called by exec_lqp() and exec_raw() to provide a single
1977
+ execution point that can be customized per resource class.
1978
+
1979
+ Args:
1980
+ database: Database/model name
1981
+ engine: Engine name (optional)
1982
+ raw_code: Code to execute (already processed/encoded)
1983
+ inputs: Input parameters for the query
1984
+ readonly: Whether the transaction is read-only
1985
+ nowait_durable: Whether to wait for durable writes
1986
+ headers: Optional HTTP headers
1987
+ bypass_index: Whether to bypass graph index setup
1988
+ language: Query language ("rel" or "lqp")
1989
+ query_timeout_mins: Optional query timeout in minutes
1990
+
1991
+ Returns:
1992
+ Query results
1993
+ """
1994
+ return self._exec_async_v2(
1995
+ database, engine, raw_code, inputs, readonly, nowait_durable,
1996
+ headers=headers, bypass_index=bypass_index, language=language,
1997
+ query_timeout_mins=query_timeout_mins, gi_setup_skipped=True,
1998
+ )
1999
+
2000
+ def exec_lqp(
2001
+ self,
2002
+ database: str,
2003
+ engine: str | None,
2004
+ raw_code: bytes,
2005
+ readonly=True,
2006
+ *,
2007
+ inputs: Dict | None = None,
2008
+ nowait_durable=False,
2009
+ headers: Dict | None = None,
2010
+ bypass_index=False,
2011
+ query_timeout_mins: int | None = None,
2012
+ ):
2013
+ """Execute LQP code."""
2014
+ raw_code_b64 = base64.b64encode(raw_code).decode("utf-8")
2015
+ return self._execute_code(
2016
+ database, engine, raw_code_b64, inputs, readonly, nowait_durable,
2017
+ headers, bypass_index, 'lqp', query_timeout_mins
2018
+ )
2019
+
2020
+ def exec_raw(
2021
+ self,
2022
+ database: str,
2023
+ engine: str | None,
2024
+ raw_code: str,
2025
+ readonly=True,
2026
+ *,
2027
+ inputs: Dict | None = None,
2028
+ nowait_durable=False,
2029
+ headers: Dict | None = None,
2030
+ bypass_index=False,
2031
+ query_timeout_mins: int | None = None,
2032
+ ):
2033
+ """Execute raw code."""
2034
+ raw_code = raw_code.replace("'", "\\'")
2035
+ return self._execute_code(
2036
+ database, engine, raw_code, inputs, readonly, nowait_durable,
2037
+ headers, bypass_index, 'rel', query_timeout_mins
2038
+ )
2039
+
2040
+
2041
+ def format_results(self, results, task:m.Task|None=None) -> Tuple[DataFrame, List[Any]]:
2042
+ return result_helpers.format_results(results, task)
2043
+
2044
+ #--------------------------------------------------
2045
+ # Exec format
2046
+ #--------------------------------------------------
2047
+
2048
+ def exec_format(
2049
+ self,
2050
+ database: str,
2051
+ engine: str,
2052
+ raw_code: str,
2053
+ cols: List[str],
2054
+ format: str,
2055
+ inputs: Dict | None = None,
2056
+ readonly=True,
2057
+ nowait_durable=False,
2058
+ skip_invalid_data=False,
2059
+ headers: Dict | None = None,
2060
+ query_timeout_mins: int | None = None,
2061
+ ):
2062
+ if inputs is None:
2063
+ inputs = {}
2064
+ if headers is None:
2065
+ headers = {}
2066
+ if 'user-agent' not in headers:
2067
+ headers['user-agent'] = get_pyrel_version(self.generation)
2068
+ if query_timeout_mins is None and (timeout_value := self.config.get("query_timeout_mins", DEFAULT_QUERY_TIMEOUT_MINS)) is not None:
2069
+ query_timeout_mins = int(timeout_value)
2070
+ # TODO: add headers
2071
+ start = time.perf_counter()
2072
+ output_table = "out" + str(uuid.uuid4()).replace("-", "_")
2073
+ temp_table = f"temp_{output_table}"
2074
+ use_graph_index = self.config.get("use_graph_index", USE_GRAPH_INDEX)
2075
+ txn_id = None
2076
+ rejected_rows = None
2077
+ col_names_map = None
2078
+ artifacts = None
2079
+ assert self._session
2080
+ temp = self._session.createDataFrame([], StructType([StructField(name, StringType()) for name in cols]))
2081
+ with debugging.span("transaction") as txn_span:
2082
+ try:
2083
+ # In the graph index case we need to use the new exec_into_table proc as it obfuscates the db name
2084
+ with debugging.span("exec_format"):
2085
+ if use_graph_index:
2086
+ # we do not provide a default value for query_timeout_mins so that we can control the default on app level
2087
+ if query_timeout_mins is not None:
2088
+ res = self._exec(f"call {APP_NAME}.api.exec_into_table(?, ?, ?, ?, ?, NULL, ?, {headers}, ?, ?);", [database, engine, raw_code, output_table, readonly, nowait_durable, skip_invalid_data, query_timeout_mins])
2089
+ else:
2090
+ res = self._exec(f"call {APP_NAME}.api.exec_into_table(?, ?, ?, ?, ?, NULL, ?, {headers}, ?);", [database, engine, raw_code, output_table, readonly, nowait_durable, skip_invalid_data])
2091
+ txn_id = json.loads(res[0]["EXEC_INTO_TABLE"])["rai_transaction_id"]
2092
+ rejected_rows = json.loads(res[0]["EXEC_INTO_TABLE"]).get("rejected_rows", [])
2093
+ rejected_rows_count = json.loads(res[0]["EXEC_INTO_TABLE"]).get("rejected_rows_count", 0)
2094
+ else:
2095
+ if query_timeout_mins is not None:
2096
+ res = self._exec(f"call {APP_NAME}.api.exec_into(?, ?, ?, ?, ?, {inputs}, ?, {headers}, ?, ?);", [database, engine, raw_code, output_table, readonly, nowait_durable, skip_invalid_data, query_timeout_mins])
2097
+ else:
2098
+ res = self._exec(f"call {APP_NAME}.api.exec_into(?, ?, ?, ?, ?, {inputs}, ?, {headers}, ?);", [database, engine, raw_code, output_table, readonly, nowait_durable, skip_invalid_data])
2099
+ txn_id = json.loads(res[0]["EXEC_INTO"])["rai_transaction_id"]
2100
+ rejected_rows = json.loads(res[0]["EXEC_INTO"]).get("rejected_rows", [])
2101
+ rejected_rows_count = json.loads(res[0]["EXEC_INTO"]).get("rejected_rows_count", 0)
2102
+ debugging.event("transaction_created", txn_span, txn_id=txn_id)
2103
+ debugging.time("exec_format", time.perf_counter() - start, DataFrame())
2104
+
2105
+ with debugging.span("temp_table_swap", txn_id=txn_id):
2106
+ out_sample = self._exec(f"select * from {APP_NAME}.results.{output_table} limit 1;")
2107
+ if out_sample:
2108
+ keys = set([k.lower() for k in out_sample[0].as_dict().keys()])
2109
+ col_names_map = {}
2110
+ for ix, name in enumerate(cols):
2111
+ col_key = f"col{ix:03}"
2112
+ if col_key in keys:
2113
+ col_names_map[col_key] = IdentityParser(name).identity
2114
+ else:
2115
+ col_names_map[col_key] = name
2116
+
2117
+ names = ", ".join([
2118
+ f"{col_key} as {alias}" if col_key in keys else f"NULL as {alias}"
2119
+ for col_key, alias in col_names_map.items()
2120
+ ])
2121
+ self._exec(f"CREATE TEMPORARY TABLE {APP_NAME}.results.{temp_table} AS SELECT {names} FROM {APP_NAME}.results.{output_table};")
2122
+ self._exec(f"call {APP_NAME}.api.drop_result_table(?)", [output_table])
2123
+ temp = cast(snowflake.snowpark.DataFrame, self._exec(f"select * from {APP_NAME}.results.{temp_table}", raw=True))
2124
+ if rejected_rows:
2125
+ debugging.warn(RowsDroppedFromTargetTableWarning(rejected_rows, rejected_rows_count, col_names_map))
2126
+ except Exception as e:
2127
+ messages = collect_error_messages(e)
2128
+ if any("no columns returned" in msg or "columns of results could not be determined" in msg for msg in messages):
2129
+ pass
2130
+ else:
2131
+ raise e
2132
+ if txn_id:
2133
+ artifact_info = self._list_exec_async_artifacts(txn_id)
2134
+ with debugging.span("fetch"):
2135
+ artifacts = self._download_results(artifact_info, txn_id, "ABORTED")
2136
+ return (temp, artifacts)
2137
+
2138
+ #--------------------------------------------------
2139
+ # Custom model types
2140
+ #--------------------------------------------------
2141
+
2142
+ def _get_ns(self, model:dsl.Graph):
2143
+ if model not in self._ns_cache:
2144
+ self._ns_cache[model] = _Snowflake(model)
2145
+ return self._ns_cache[model]
2146
+
2147
+ def to_model_type(self, model:dsl.Graph, name: str, source:str):
2148
+ parser = IdentityParser(source)
2149
+ if not parser.is_complete:
2150
+ raise SnowflakeInvalidSource(Errors.call_source(), source)
2151
+ ns = self._get_ns(model)
2152
+ # skip the last item in the list (the full identifier)
2153
+ for part in parser.to_list()[:-1]:
2154
+ ns = ns._safe_get(part)
2155
+ assert parser.identity, f"Error parsing source in to_model_type: {source}"
2156
+ self.sources.add(parser.identity)
2157
+ return ns
2158
+
2159
+ #--------------------------------------------------
2160
+ # Source Management
2161
+ #--------------------------------------------------
2162
+
2163
+ def _check_source_updates(self, sources: Iterable[str]):
2164
+ if not sources:
2165
+ return {}
2166
+ app_name = self.get_app_name()
2167
+
2168
+ source_types = dict[str, SourceInfo]()
2169
+ partitioned_sources: dict[str, dict[str, list[dict[str, str]]]] = defaultdict(
2170
+ lambda: defaultdict(list)
2171
+ )
2172
+ fqn_to_parts: dict[str, tuple[str, str, str]] = {}
2173
+
2174
+ for source in sources:
2175
+ parser = IdentityParser(source, True)
2176
+ parsed = parser.to_list()
2177
+ assert len(parsed) == 4, f"Invalid source: {source}"
2178
+ db, schema, entity, identity = parsed
2179
+ assert db and schema and entity and identity, f"Invalid source: {source}"
2180
+ source_types[identity] = cast(
2181
+ SourceInfo,
2182
+ {
2183
+ "type": None,
2184
+ "state": "",
2185
+ "columns_hash": None,
2186
+ "table_created_at": None,
2187
+ "stream_created_at": None,
2188
+ "last_ddl": None,
2189
+ },
2190
+ )
2191
+ partitioned_sources[db][schema].append({"entity": entity, "identity": identity})
2192
+ fqn_to_parts[identity] = (db, schema, entity)
2193
+
2194
+ if not partitioned_sources:
2195
+ return source_types
2196
+
2197
+ state_queries: list[str] = []
2198
+ for db, schemas in partitioned_sources.items():
2199
+ select_rows: list[str] = []
2200
+ for schema, tables in schemas.items():
2201
+ for table_info in tables:
2202
+ select_rows.append(
2203
+ "SELECT "
2204
+ f"{IdentityParser.to_sql_value(db)} AS catalog_name, "
2205
+ f"{IdentityParser.to_sql_value(schema)} AS schema_name, "
2206
+ f"{IdentityParser.to_sql_value(table_info['entity'])} AS table_name"
2207
+ )
2208
+
2209
+ if not select_rows:
2210
+ continue
2211
+
2212
+ target_entities_clause = "\n UNION ALL\n ".join(select_rows)
2213
+ # Main query:
2214
+ # 1. Enumerate the target tables via target_entities.
2215
+ # 2. Pull their metadata (last_altered, type) from INFORMATION_SCHEMA.TABLES.
2216
+ # 3. Look up the most recent stream activity for those FQNs only.
2217
+ # 4. Capture creation timestamps and use last_ddl vs created_at to classify each target,
2218
+ # so we mark tables as stale when they were recreated even if column hashes still match.
2219
+ state_queries.append(
2220
+ f"""WITH target_entities AS (
2221
+ {target_entities_clause}
2222
+ ),
2223
+ table_info AS (
2224
+ SELECT
2225
+ {app_name}.api.normalize_fq_ids(
2226
+ ARRAY_CONSTRUCT(
2227
+ CASE
2228
+ WHEN t.table_catalog = UPPER(t.table_catalog) THEN t.table_catalog
2229
+ ELSE '"' || t.table_catalog || '"'
2230
+ END || '.' ||
2231
+ CASE
2232
+ WHEN t.table_schema = UPPER(t.table_schema) THEN t.table_schema
2233
+ ELSE '"' || t.table_schema || '"'
2234
+ END || '.' ||
2235
+ CASE
2236
+ WHEN t.table_name = UPPER(t.table_name) THEN t.table_name
2237
+ ELSE '"' || t.table_name || '"'
2238
+ END
2239
+ )
2240
+ )[0]:identifier::string AS fqn,
2241
+ CONVERT_TIMEZONE('UTC', t.last_altered) AS last_ddl,
2242
+ CONVERT_TIMEZONE('UTC', t.created) AS table_created_at,
2243
+ t.table_type AS kind
2244
+ FROM {db}.INFORMATION_SCHEMA.tables t
2245
+ JOIN target_entities te
2246
+ ON t.table_catalog = te.catalog_name
2247
+ AND t.table_schema = te.schema_name
2248
+ AND t.table_name = te.table_name
2249
+ ),
2250
+ stream_activity AS (
2251
+ SELECT
2252
+ sa.fqn,
2253
+ MAX(sa.created_at) AS created_at
2254
+ FROM (
2255
+ SELECT
2256
+ {app_name}.api.normalize_fq_ids(ARRAY_CONSTRUCT(fq_object_name))[0]:identifier::string AS fqn,
2257
+ created_at
2258
+ FROM {app_name}.api.data_streams
2259
+ WHERE rai_database = '{PYREL_ROOT_DB}'
2260
+ ) sa
2261
+ JOIN table_info ti
2262
+ ON sa.fqn = ti.fqn
2263
+ GROUP BY sa.fqn
2264
+ )
2265
+ SELECT
2266
+ ti.fqn,
2267
+ ti.kind,
2268
+ ti.last_ddl,
2269
+ ti.table_created_at,
2270
+ sa.created_at AS stream_created_at,
2271
+ IFF(
2272
+ DATEDIFF(second, sa.created_at::timestamp, ti.last_ddl::timestamp) > 0,
2273
+ 'STALE',
2274
+ 'CURRENT'
2275
+ ) AS state
2276
+ FROM table_info ti
2277
+ LEFT JOIN stream_activity sa
2278
+ ON sa.fqn = ti.fqn
2279
+ """
2280
+ )
2281
+
2282
+ stale_fqns: list[str] = []
2283
+ for state_query in state_queries:
2284
+ for row in self._exec(state_query):
2285
+ row_dict = row.as_dict() if hasattr(row, "as_dict") else dict(row)
2286
+ row_fqn = row_dict["FQN"]
2287
+ parser = IdentityParser(row_fqn, True)
2288
+ fqn = parser.identity
2289
+ assert fqn, f"Error parsing returned FQN: {row_fqn}"
2290
+
2291
+ source_types[fqn]["type"] = (
2292
+ "TABLE" if row_dict["KIND"] == "BASE TABLE" else row_dict["KIND"]
2293
+ )
2294
+ source_types[fqn]["state"] = row_dict["STATE"]
2295
+ source_types[fqn]["last_ddl"] = normalize_datetime(row_dict.get("LAST_DDL"))
2296
+ source_types[fqn]["table_created_at"] = normalize_datetime(row_dict.get("TABLE_CREATED_AT"))
2297
+ source_types[fqn]["stream_created_at"] = normalize_datetime(row_dict.get("STREAM_CREATED_AT"))
2298
+ if row_dict["STATE"] == "STALE":
2299
+ stale_fqns.append(fqn)
2300
+
2301
+ if not stale_fqns:
2302
+ return source_types
2303
+
2304
+ # We batch stale tables by database/schema so each Snowflake query can hash
2305
+ # multiple objects at once instead of issuing one statement per table.
2306
+ stale_partitioned: dict[str, dict[str, list[dict[str, str]]]] = defaultdict(
2307
+ lambda: defaultdict(list)
2308
+ )
2309
+ for fqn in stale_fqns:
2310
+ db, schema, table = fqn_to_parts[fqn]
2311
+ stale_partitioned[db][schema].append({"table": table, "identity": fqn})
2312
+
2313
+ # Build one hash query per database, grouping schemas/tables inside so we submit
2314
+ # at most a handful of set-based statements to Snowflake.
2315
+ for db, schemas in stale_partitioned.items():
2316
+ column_select_rows: list[str] = []
2317
+ for schema, tables in schemas.items():
2318
+ for table_info in tables:
2319
+ # Build the literal rows for this db/schema so we can join back
2320
+ # against INFORMATION_SCHEMA.COLUMNS in a single statement.
2321
+ column_select_rows.append(
2322
+ "SELECT "
2323
+ f"{IdentityParser.to_sql_value(db)} AS catalog_name, "
2324
+ f"{IdentityParser.to_sql_value(schema)} AS schema_name, "
2325
+ f"{IdentityParser.to_sql_value(table_info['table'])} AS table_name"
2326
+ )
2327
+
2328
+ if not column_select_rows:
2329
+ continue
2330
+
2331
+ target_entities_clause = "\n UNION ALL\n ".join(column_select_rows)
2332
+ # Main query: compute deterministic column hashes for every stale table
2333
+ # in this database/schema batch so we can compare schemas without a round trip per table.
2334
+ column_query = f"""WITH target_entities AS (
2335
+ {target_entities_clause}
2336
+ ),
2337
+ column_info AS (
2338
+ SELECT
2339
+ {app_name}.api.normalize_fq_ids(
2340
+ ARRAY_CONSTRUCT(
2341
+ CASE
2342
+ WHEN c.table_catalog = UPPER(c.table_catalog) THEN c.table_catalog
2343
+ ELSE '"' || c.table_catalog || '"'
2344
+ END || '.' ||
2345
+ CASE
2346
+ WHEN c.table_schema = UPPER(c.table_schema) THEN c.table_schema
2347
+ ELSE '"' || c.table_schema || '"'
2348
+ END || '.' ||
2349
+ CASE
2350
+ WHEN c.table_name = UPPER(c.table_name) THEN c.table_name
2351
+ ELSE '"' || c.table_name || '"'
2352
+ END
2353
+ )
2354
+ )[0]:identifier::string AS fqn,
2355
+ c.column_name,
2356
+ CASE
2357
+ WHEN c.numeric_precision IS NOT NULL AND c.numeric_scale IS NOT NULL
2358
+ THEN c.data_type || '(' || c.numeric_precision || ',' || c.numeric_scale || ')'
2359
+ WHEN c.datetime_precision IS NOT NULL
2360
+ THEN c.data_type || '(0,' || c.datetime_precision || ')'
2361
+ WHEN c.character_maximum_length IS NOT NULL
2362
+ THEN c.data_type || '(' || c.character_maximum_length || ')'
2363
+ ELSE c.data_type
2364
+ END AS type_signature,
2365
+ IFF(c.is_nullable = 'YES', 'YES', 'NO') AS nullable_flag
2366
+ FROM {db}.INFORMATION_SCHEMA.COLUMNS c
2367
+ JOIN target_entities te
2368
+ ON c.table_catalog = te.catalog_name
2369
+ AND c.table_schema = te.schema_name
2370
+ AND c.table_name = te.table_name
2371
+ )
2372
+ SELECT
2373
+ fqn,
2374
+ HEX_ENCODE(
2375
+ HASH_AGG(
2376
+ HASH(
2377
+ column_name,
2378
+ type_signature,
2379
+ nullable_flag
2380
+ )
2381
+ )
2382
+ ) AS columns_hash
2383
+ FROM column_info
2384
+ GROUP BY fqn
2385
+ """
2386
+
2387
+ for row in self._exec(column_query):
2388
+ row_fqn = row["FQN"]
2389
+ parser = IdentityParser(row_fqn, True)
2390
+ fqn = parser.identity
2391
+ assert fqn, f"Error parsing returned FQN: {row_fqn}"
2392
+ source_types[fqn]["columns_hash"] = row["COLUMNS_HASH"]
2393
+
2394
+ return source_types
2395
+
2396
+ def _get_source_references(self, source_info: dict[str, SourceInfo]):
2397
+ app_name = self.get_app_name()
2398
+ missing_sources = []
2399
+ invalid_sources = {}
2400
+ source_references = []
2401
+ for source, info in source_info.items():
2402
+ source_type = info.get("type")
2403
+ if source_type is None:
2404
+ missing_sources.append(source)
2405
+ elif source_type not in ("TABLE", "VIEW"):
2406
+ invalid_sources[source] = source_type
2407
+ else:
2408
+ source_references.append(f"{app_name}.api.object_reference('{source_type}', '{source}')")
2409
+
2410
+ if missing_sources:
2411
+ current_role = self.get_sf_session().get_current_role()
2412
+ if current_role is None:
2413
+ current_role = self.config.get("role", None)
2414
+ debugging.warn(UnknownSourceWarning(missing_sources, current_role))
2415
+
2416
+ if invalid_sources:
2417
+ debugging.warn(InvalidSourceTypeWarning(invalid_sources))
2418
+
2419
+ self.source_references = source_references
2420
+ return source_references
2421
+
2422
+ #--------------------------------------------------
2423
+ # Transactions
2424
+ #--------------------------------------------------
2425
+
2426
+ def get_transaction(self, transaction_id):
2427
+ results = self._exec(
2428
+ f"CALL {APP_NAME}.api.get_transaction(?);", [transaction_id])
2429
+ if not results:
2430
+ return None
2431
+
2432
+ results = txn_list_to_dicts(results)
2433
+
2434
+ txn = {field: results[0][field] for field in GET_TXN_SQL_FIELDS}
2435
+
2436
+ state = txn.get("state")
2437
+ created_on = txn.get("created_on")
2438
+ finished_at = txn.get("finished_at")
2439
+ if created_on:
2440
+ # Transaction is still running
2441
+ if state not in TERMINAL_TXN_STATES:
2442
+ tz_info = created_on.tzinfo
2443
+ txn['duration'] = datetime.now(tz_info) - created_on
2444
+ # Transaction is terminal
2445
+ elif finished_at:
2446
+ txn['duration'] = finished_at - created_on
2447
+ # Transaction is still running and we have no state or finished_at
2448
+ else:
2449
+ txn['duration'] = timedelta(0)
2450
+ return txn
2451
+
2452
+ def list_transactions(self, **kwargs):
2453
+ id = kwargs.get("id", None)
2454
+ state = kwargs.get("state", None)
2455
+ engine = kwargs.get("engine", None)
2456
+ limit = kwargs.get("limit", 100)
2457
+ all_users = kwargs.get("all_users", False)
2458
+ created_by = kwargs.get("created_by", None)
2459
+ only_active = kwargs.get("only_active", False)
2460
+ where_clause_arr = []
2461
+
2462
+ if id:
2463
+ where_clause_arr.append(f"id = '{id}'")
2464
+ if state:
2465
+ where_clause_arr.append(f"state = '{state.upper()}'")
2466
+ if engine:
2467
+ where_clause_arr.append(f"LOWER(engine_name) = '{engine.lower()}'")
2468
+ else:
2469
+ if only_active:
2470
+ where_clause_arr.append("state in ('CREATED', 'RUNNING', 'PENDING')")
2471
+ if not all_users and created_by is not None:
2472
+ where_clause_arr.append(f"LOWER(created_by) = '{created_by.lower()}'")
2473
+
2474
+ if len(where_clause_arr):
2475
+ where_clause = f'WHERE {" AND ".join(where_clause_arr)}'
2476
+ else:
2477
+ where_clause = ""
2478
+
2479
+ sql_fields = ", ".join(LIST_TXN_SQL_FIELDS)
2480
+ query = f"SELECT {sql_fields} from {APP_NAME}.api.transactions {where_clause} ORDER BY created_on DESC LIMIT ?"
2481
+ results = self._exec(query, [limit])
2482
+ if not results:
2483
+ return []
2484
+ return txn_list_to_dicts(results)
2485
+
2486
+ def cancel_transaction(self, transaction_id):
2487
+ self._exec(f"CALL {APP_NAME}.api.cancel_own_transaction(?);", [transaction_id])
2488
+ if transaction_id in self._pending_transactions:
2489
+ self._pending_transactions.remove(transaction_id)
2490
+
2491
+ def cancel_pending_transactions(self):
2492
+ for txn_id in self._pending_transactions:
2493
+ self.cancel_transaction(txn_id)
2494
+
2495
+ def get_transaction_events(self, transaction_id: str, continuation_token:str=''):
2496
+ results = self._exec(
2497
+ f"SELECT {APP_NAME}.api.get_own_transaction_events(?, ?);",
2498
+ [transaction_id, continuation_token],
2499
+ )
2500
+ if not results:
2501
+ return {
2502
+ "events": [],
2503
+ "continuation_token": None
2504
+ }
2505
+ row = results[0][0]
2506
+ return json.loads(row)
2507
+
2508
+ #--------------------------------------------------
2509
+ # Snowflake specific
2510
+ #--------------------------------------------------
2511
+
2512
+ def get_version(self):
2513
+ results = self._exec(f"SELECT {APP_NAME}.app.get_release()")
2514
+ if not results:
2515
+ return None
2516
+ return results[0][0]
2517
+
2518
+ # CLI methods (list_warehouses, list_compute_pools, list_roles, list_apps,
2519
+ # list_databases, list_sf_schemas, list_tables) are now in CLIResources class
2520
+ # schema_info is kept in base Resources class since it's used by SnowflakeSchema._fetch_info()
2521
+
2522
+ def schema_info(self, database: str, schema: str, tables: Iterable[str]):
2523
+ """Get detailed schema information including primary keys, foreign keys, and columns."""
2524
+ app_name = self.get_app_name()
2525
+ # Only pass the db + schema as the identifier so that the resulting identity is correct
2526
+ parser = IdentityParser(f"{database}.{schema}")
2527
+
2528
+ with debugging.span("schema_info"):
2529
+ with debugging.span("primary_keys") as span:
2530
+ pk_query = f"SHOW PRIMARY KEYS IN SCHEMA {parser.identity};"
2531
+ pks = self._exec(pk_query)
2532
+ span["sql"] = pk_query
2533
+
2534
+ with debugging.span("foreign_keys") as span:
2535
+ fk_query = f"SHOW IMPORTED KEYS IN SCHEMA {parser.identity};"
2536
+ fks = self._exec(fk_query)
2537
+ span["sql"] = fk_query
2538
+
2539
+ # IdentityParser will parse a single value (with no ".") and store it in this case in the db field
2540
+ with debugging.span("columns") as span:
2541
+ tables_str = ", ".join([f"'{IdentityParser(t).db}'" for t in tables])
2542
+ query = textwrap.dedent(f"""
2543
+ begin
2544
+ SHOW COLUMNS IN SCHEMA {parser.identity};
2545
+ let r resultset := (
2546
+ SELECT
2547
+ CASE
2548
+ WHEN "table_name" = UPPER("table_name") THEN "table_name"
2549
+ ELSE '"' || "table_name" || '"'
2550
+ END as "table_name",
2551
+ "column_name",
2552
+ "data_type",
2553
+ CASE
2554
+ WHEN ARRAY_CONTAINS(PARSE_JSON("data_type"):"type", {app_name}.app.get_supported_column_types()) THEN TRUE
2555
+ ELSE FALSE
2556
+ END as "supported_type"
2557
+ FROM table(result_scan(-1)) as t
2558
+ WHERE "table_name" in ({tables_str})
2559
+ );
2560
+ return table(r);
2561
+ end;
2562
+ """)
2563
+ span["sql"] = query
2564
+ columns = self._exec(query)
2565
+
2566
+ results = defaultdict(lambda: {"pks": [], "fks": {}, "columns": {}, "invalid_columns": {}})
2567
+ if pks:
2568
+ for row in pks:
2569
+ results[row[3]]["pks"].append(row[4]) # type: ignore
2570
+ if fks:
2571
+ for row in fks:
2572
+ results[row[7]]["fks"][row[8]] = row[3]
2573
+ if columns:
2574
+ # It seems that a SF parameter (QUOTED_IDENTIFIERS_IGNORE_CASE) can control
2575
+ # whether snowflake will ignore case on `row.data_type`,
2576
+ # so we have to use column indexes instead :(
2577
+ for row in columns:
2578
+ table_name = row[0]
2579
+ column_name = row[1]
2580
+ data_type = row[2]
2581
+ supported_type = row[3]
2582
+ # Filter out unsupported types
2583
+ if supported_type:
2584
+ results[table_name]["columns"][column_name] = data_type
2585
+ else:
2586
+ results[table_name]["invalid_columns"][column_name] = data_type
2587
+ return results
2588
+
2589
+ def get_cloud_provider(self) -> str:
2590
+ """
2591
+ Detect whether this is Snowflake on Azure, or AWS using Snowflake's CURRENT_REGION().
2592
+ Returns 'azure' or 'aws'.
2593
+ """
2594
+ if self._session:
2595
+ try:
2596
+ # Query Snowflake's current region using the built-in function
2597
+ result = self._session.sql("SELECT CURRENT_REGION()").collect()
2598
+ if result:
2599
+ region_info = result[0][0]
2600
+ # Check if the region string contains the cloud provider name
2601
+ if isinstance(region_info, str):
2602
+ region_str = region_info.lower()
2603
+ # Check for cloud providers in the region string
2604
+ if 'azure' in region_str:
2605
+ return 'azure'
2606
+ else:
2607
+ return 'aws'
2608
+ except Exception:
2609
+ pass
2610
+
2611
+ # Fallback to AWS as default if detection fails
2612
+ return 'aws'
2613
+
2614
+ #--------------------------------------------------
2615
+ # Snowflake Wrapper
2616
+ #--------------------------------------------------
2617
+
2618
+ class PrimaryKey:
2619
+ pass
2620
+
2621
+ class _Snowflake:
2622
+ def __init__(self, model, auto_import=False):
2623
+ self._model = model
2624
+ self._auto_import = auto_import
2625
+ if not isinstance(model._client.resources, Resources):
2626
+ raise ValueError("Snowflake model must be used with a snowflake config")
2627
+ self._dbs = {}
2628
+ imports = model._client.resources.list_imports(model=model.name)
2629
+ self._import_structure(imports)
2630
+
2631
+ def _import_structure(self, imports: list[Import]):
2632
+ tree = self._dbs
2633
+ # pre-create existing imports
2634
+ schemas = set()
2635
+ for item in imports:
2636
+ parser = IdentityParser(item["name"])
2637
+ database_name, schema_name, table_name = parser.to_list()[:-1]
2638
+ database = getattr(self, database_name)
2639
+ schema = getattr(database, schema_name)
2640
+ schemas.add(schema)
2641
+ schema._add(table_name, is_imported=True)
2642
+ return tree
2643
+
2644
+ def _safe_get(self, name:str) -> 'SnowflakeDB':
2645
+ name = name
2646
+ if name in self._dbs:
2647
+ return self._dbs[name]
2648
+ self._dbs[name] = SnowflakeDB(self, name)
2649
+ return self._dbs[name]
2650
+
2651
+ def __getattr__(self, name: str) -> 'SnowflakeDB':
2652
+ return self._safe_get(name)
2653
+
2654
+
2655
+ class Snowflake(_Snowflake):
2656
+ def __init__(self, model: dsl.Graph, auto_import=False):
2657
+ if model._config.get_bool("use_graph_index", USE_GRAPH_INDEX):
2658
+ raise SnowflakeProxySourceError()
2659
+ else:
2660
+ debugging.warn(SnowflakeProxyAPIDeprecationWarning())
2661
+
2662
+ super().__init__(model, auto_import)
2663
+
2664
+ class SnowflakeDB:
2665
+ def __init__(self, parent, name):
2666
+ self._name = name
2667
+ self._parent = parent
2668
+ self._model = parent._model
2669
+ self._schemas = {}
2670
+
2671
+ def _safe_get(self, name: str) -> 'SnowflakeSchema':
2672
+ name = name
2673
+ if name in self._schemas:
2674
+ return self._schemas[name]
2675
+ self._schemas[name] = SnowflakeSchema(self, name)
2676
+ return self._schemas[name]
2677
+
2678
+ def __getattr__(self, name: str) -> 'SnowflakeSchema':
2679
+ return self._safe_get(name)
2680
+
2681
+ class SnowflakeSchema:
2682
+ def __init__(self, parent, name):
2683
+ self._name = name
2684
+ self._parent = parent
2685
+ self._model = parent._model
2686
+ self._tables = {}
2687
+ self._imported = set()
2688
+ self._table_info = defaultdict(lambda: {"pks": [], "fks": {}, "columns": {}, "invalid_columns": {}})
2689
+ self._dirty = True
2690
+
2691
+ def _fetch_info(self):
2692
+ if not self._dirty:
2693
+ return
2694
+ self._table_info = self._model._client.resources.schema_info(self._parent._name, self._name, list(self._tables.keys()))
2695
+
2696
+ check_column_types = self._model._config.get("check_column_types", True)
2697
+
2698
+ if check_column_types:
2699
+ self._check_and_confirm_invalid_columns()
2700
+
2701
+ self._dirty = False
2702
+
2703
+ def _check_and_confirm_invalid_columns(self):
2704
+ """Check for invalid columns across the schema's tables."""
2705
+ tables_with_invalid_columns = {}
2706
+ for table_name, table_info in self._table_info.items():
2707
+ if table_info.get("invalid_columns"):
2708
+ tables_with_invalid_columns[table_name] = table_info["invalid_columns"]
2709
+
2710
+ if tables_with_invalid_columns:
2711
+ from relationalai.errors import UnsupportedColumnTypesWarning
2712
+ UnsupportedColumnTypesWarning(tables_with_invalid_columns)
2713
+
2714
+ def _add(self, name, is_imported=False):
2715
+ if name in self._tables:
2716
+ return self._tables[name]
2717
+ self._dirty = True
2718
+ if is_imported:
2719
+ self._imported.add(name)
2720
+ else:
2721
+ self._tables[name] = SnowflakeTable(self, name)
2722
+ return self._tables.get(name)
2723
+
2724
+ def _safe_get(self, name: str) -> 'SnowflakeTable | None':
2725
+ table = self._add(name)
2726
+ return table
2727
+
2728
+ def __getattr__(self, name: str) -> 'SnowflakeTable | None':
2729
+ return self._safe_get(name)
2730
+
2731
+
2732
+ class SnowflakeTable(dsl.Type):
2733
+ def __init__(self, parent, name):
2734
+ super().__init__(parent._model, f"sf_{name}")
2735
+ # hack to make this work for pathfinder
2736
+ self._type.parents.append(m.Builtins.PQFilterAnnotation)
2737
+ self._name = name
2738
+ self._model = parent._model
2739
+ self._parent = parent
2740
+ self._aliases = {}
2741
+ self._finalzed = False
2742
+ self._source = runtime_env.get_source()
2743
+ relation_name = to_fqn_relation_name(self.fqname())
2744
+ self._model.install_raw(f"declare {relation_name}")
2745
+
2746
+ def __call__(self, *args, **kwargs):
2747
+ self._lazy_init()
2748
+ return super().__call__(*args, **kwargs)
2749
+
2750
+ def add(self, *args, **kwargs):
2751
+ self._lazy_init()
2752
+ return super().add(*args, **kwargs)
2753
+
2754
+ def extend(self, *args, **kwargs):
2755
+ self._lazy_init()
2756
+ return super().extend(*args, **kwargs)
2757
+
2758
+ def known_properties(self):
2759
+ self._lazy_init()
2760
+ return super().known_properties()
2761
+
2762
+ def _lazy_init(self):
2763
+ if self._finalzed:
2764
+ return
2765
+
2766
+ parent = self._parent
2767
+ name = self._name
2768
+ use_graph_index = self._model._config.get("use_graph_index", USE_GRAPH_INDEX)
2769
+
2770
+ if not use_graph_index and name not in parent._imported:
2771
+ if self._parent._parent._parent._auto_import:
2772
+ with Spinner(f"Creating stream for {self.fqname()}", f"Stream for {self.fqname()} created successfully"):
2773
+ db_name = parent._parent._name
2774
+ schema_name = parent._name
2775
+ self._model._client.resources.create_import_stream(ImportSourceTable(db_name, schema_name, name), self._model.name)
2776
+ print("")
2777
+ parent._imported.add(name)
2778
+ else:
2779
+ imports = self._model._client.resources.list_imports(model=self._model.name)
2780
+ for item in imports:
2781
+ cur_name = item["name"].lower().split(".")[-1]
2782
+ parent._imported.add(cur_name)
2783
+ if name not in parent._imported:
2784
+ exception = SnowflakeImportMissingException(runtime_env.get_source(), self.fqname(), self._model.name)
2785
+ raise exception from None
2786
+
2787
+ parent._fetch_info()
2788
+ self._finalize()
2789
+
2790
+ def _finalize(self):
2791
+ if self._finalzed:
2792
+ return
2793
+
2794
+ self._finalzed = True
2795
+ self._schema = self._parent._table_info[self._name]
2796
+
2797
+ # Set the relation name to the sanitized version of the fully qualified name
2798
+ relation_name = to_fqn_relation_name(self.fqname())
2799
+
2800
+ model:dsl.Graph = self._model
2801
+ edb = getattr(std.rel, relation_name)
2802
+ edb._rel.parents.append(m.Builtins.EDB)
2803
+ id_rel = getattr(std.rel, f"{relation_name}_pyrel_id")
2804
+
2805
+ with model.rule(globalize=True, source=self._source):
2806
+ id, val = dsl.create_vars(2)
2807
+ edb(dsl.Symbol("METADATA$ROW_ID"), id, val)
2808
+ std.rel.SHA1(id)
2809
+ id_rel.add(id)
2810
+
2811
+ with model.rule(dynamic=True, globalize=True, source=self._source):
2812
+ prop, id, val = dsl.create_vars(3)
2813
+ id_rel(id)
2814
+ std.rel.SHA1(id)
2815
+ self.add(snowflake_id=id)
2816
+
2817
+ for prop, prop_type in self._schema["columns"].items():
2818
+ _prop = prop
2819
+ if _prop.startswith("_"):
2820
+ _prop = "col" + prop
2821
+
2822
+ prop_ident = sanitize_identifier(_prop.lower())
2823
+
2824
+ with model.rule(dynamic=True, globalize=True, source=self._source):
2825
+ id, val = dsl.create_vars(2)
2826
+ edb(dsl.Symbol(prop), id, val)
2827
+ std.rel.SHA1(id)
2828
+ _prop = getattr(self, prop_ident)
2829
+ if not _prop:
2830
+ raise ValueError(f"Property {_prop} couldn't be accessed on {self.fqname()}")
2831
+ if _prop.is_multi_valued:
2832
+ inst = self(snowflake_id=id)
2833
+ getattr(inst, prop_ident).add(val)
2834
+ else:
2835
+ self(snowflake_id=id).set(**{prop_ident: val})
2836
+
2837
+ # Because we're bypassing a bunch of the normal Type.add machinery here,
2838
+ # we need to manually account for the case where people are using value types.
2839
+ def wrapped(x):
2840
+ if not model._config.get("compiler.use_value_types", False):
2841
+ return x
2842
+ other_id = dsl.create_var()
2843
+ model._action(dsl.build.construct(self._type, [x, other_id]))
2844
+ return other_id
2845
+
2846
+ # new UInt128 schema mapping rules
2847
+ with model.rule(dynamic=True, globalize=True, source=self._source):
2848
+ id = dsl.create_var()
2849
+ # This will generate an arity mismatch warning when used with the old SHA-1 Data Streams.
2850
+ # Ideally we have the `@no_diagnostics(:ARITY_MISMATCH)` attribute on the relation using
2851
+ # the METADATA$KEY column but that ended up being a more involved change then expected
2852
+ # for avoiding a non-blocking warning
2853
+ edb(dsl.Symbol("METADATA$KEY"), id)
2854
+ std.rel.UInt128(id)
2855
+ self.add(wrapped(id), snowflake_id=id)
2856
+
2857
+ for prop, prop_type in self._schema["columns"].items():
2858
+ _prop = prop
2859
+ if _prop.startswith("_"):
2860
+ _prop = "col" + prop
2861
+
2862
+ prop_ident = sanitize_identifier(_prop.lower())
2863
+ with model.rule(dynamic=True, globalize=True, source=self._source):
2864
+ id, val = dsl.create_vars(2)
2865
+ edb(dsl.Symbol(prop), id, val)
2866
+ std.rel.UInt128(id)
2867
+ _prop = getattr(self, prop_ident)
2868
+ if not _prop:
2869
+ raise ValueError(f"Property {_prop} couldn't be accessed on {self.fqname()}")
2870
+ if _prop.is_multi_valued:
2871
+ inst = self(id)
2872
+ getattr(inst, prop_ident).add(val)
2873
+ else:
2874
+ model._check_property(_prop._prop)
2875
+ raw_relation = getattr(std.rel, prop_ident)
2876
+ dsl.tag(raw_relation, dsl.Builtins.FunctionAnnotation)
2877
+ raw_relation.add(wrapped(id), val)
2878
+
2879
+ def namespace(self):
2880
+ return f"{self._parent._parent._name}.{self._parent._name}"
2881
+
2882
+ def fqname(self):
2883
+ return f"{self.namespace()}.{self._name}"
2884
+
2885
+ def describe(self, **kwargs):
2886
+ model = self._model
2887
+ for k, v in kwargs.items():
2888
+ if v is PrimaryKey:
2889
+ self._schema["pks"] = [k]
2890
+ elif isinstance(v, tuple):
2891
+ (table, name) = v
2892
+ if isinstance(table, SnowflakeTable):
2893
+ fk_table = table
2894
+ pk = fk_table._schema["pks"]
2895
+ with model.rule():
2896
+ inst = fk_table()
2897
+ me = self()
2898
+ getattr(inst, pk[0]) == getattr(me, k)
2899
+ if getattr(self, name).is_multi_valued:
2900
+ getattr(me, name).add(inst)
2901
+ else:
2902
+ me.set(**{name: inst})
2903
+ else:
2904
+ raise ValueError(f"Invalid foreign key {v}")
2905
+ else:
2906
+ raise ValueError(f"Invalid column {k}={v}")
2907
+ return self
2908
+
2909
+ class Provider(ProviderBase):
2910
+ def __init__(
2911
+ self,
2912
+ profile: str | None = None,
2913
+ config: Config | None = None,
2914
+ resources: Resources | None = None,
2915
+ generation: Generation | None = None,
2916
+ ):
2917
+ if resources:
2918
+ self.resources = resources
2919
+ else:
2920
+ from .resources_factory import create_resources_instance
2921
+ self.resources = create_resources_instance(
2922
+ config=config,
2923
+ profile=profile,
2924
+ generation=generation or Generation.V0,
2925
+ dry_run=False,
2926
+ language="rel",
2927
+ )
2928
+
2929
+ def list_streams(self, model:str):
2930
+ return self.resources.list_imports(model=model)
2931
+
2932
+ def create_streams(self, sources:List[str], model:str, force=False):
2933
+ if not self.resources.get_graph(model):
2934
+ self.resources.create_graph(model)
2935
+ def parse_source(raw:str):
2936
+ parser = IdentityParser(raw)
2937
+ assert parser.is_complete, "Snowflake table imports must be in `database.schema.table` format"
2938
+ return ImportSourceTable(*parser.to_list())
2939
+ for source in sources:
2940
+ source_table = parse_source(source)
2941
+ try:
2942
+ with Spinner(f"Creating stream for {source_table.name}", f"Stream for {source_table.name} created successfully"):
2943
+ if force:
2944
+ self.resources.delete_import(source_table.name, model, True)
2945
+ self.resources.create_import_stream(source_table, model)
2946
+ except Exception as e:
2947
+ if "stream already exists" in f"{e}":
2948
+ raise Exception(f"\n\nStream'{source_table.name.upper()}' already exists.")
2949
+ elif "engine not found" in f"{e}":
2950
+ raise Exception("\n\nNo engines found in a READY state. Please use `engines:create` to create an engine that will be used to initialize the target relation.")
2951
+ else:
2952
+ raise e
2953
+ with Spinner("Waiting for imports to complete", "Imports complete"):
2954
+ self.resources.poll_imports(sources, model)
2955
+
2956
+ def delete_stream(self, stream_id: str, model: str):
2957
+ return self.resources.delete_import(stream_id, model)
2958
+
2959
+ def sql(self, query:str, params:List[Any]=[], format:Literal["list", "pandas", "polars", "lazy"]="list"):
2960
+ # note: default format cannot be pandas because .to_pandas() only works on SELECT queries
2961
+ result = self.resources._exec(query, params, raw=True, help=False)
2962
+ if format == "lazy":
2963
+ return cast(snowflake.snowpark.DataFrame, result)
2964
+ elif format == "list":
2965
+ return cast(list, result.collect())
2966
+ elif format == "pandas":
2967
+ import pandas as pd
2968
+ try:
2969
+ # use to_pandas for SELECT queries
2970
+ return cast(pd.DataFrame, result.to_pandas())
2971
+ except Exception:
2972
+ # handle non-SELECT queries like SHOW
2973
+ return pd.DataFrame(result.collect())
2974
+ elif format == "polars":
2975
+ import polars as pl # type: ignore
2976
+ return pl.DataFrame(
2977
+ [row.as_dict() for row in result.collect()],
2978
+ orient="row",
2979
+ strict=False,
2980
+ infer_schema_length=None
2981
+ )
2982
+ else:
2983
+ raise ValueError(f"Invalid format {format}. Should be one of 'list', 'pandas', 'polars', 'lazy'")
2984
+
2985
+ def activate(self):
2986
+ with Spinner("Activating RelationalAI app...", "RelationalAI app activated"):
2987
+ self.sql("CALL RELATIONALAI.APP.ACTIVATE();")
2988
+
2989
+ def deactivate(self):
2990
+ with Spinner("Deactivating RelationalAI app...", "RelationalAI app deactivated"):
2991
+ self.sql("CALL RELATIONALAI.APP.DEACTIVATE();")
2992
+
2993
+ def drop_service(self):
2994
+ warnings.warn(
2995
+ "The drop_service method has been deprecated in favor of deactivate",
2996
+ DeprecationWarning,
2997
+ stacklevel=2,
2998
+ )
2999
+ self.deactivate()
3000
+
3001
+ def resume_service(self):
3002
+ warnings.warn(
3003
+ "The resume_service method has been deprecated in favor of activate",
3004
+ DeprecationWarning,
3005
+ stacklevel=2,
3006
+ )
3007
+ self.activate()
3008
+
3009
+
3010
+ #--------------------------------------------------
3011
+ # SnowflakeClient
3012
+ #--------------------------------------------------
3013
+ class SnowflakeClient(Client):
3014
+ def create_database(self, isolated=True, nowait_durable=True, headers: Dict | None = None):
3015
+ from relationalai.tools.cli_helpers import validate_engine_name
3016
+
3017
+ assert isinstance(self.resources, Resources)
3018
+
3019
+ if self.last_database_version == len(self.resources.sources):
3020
+ return
3021
+
3022
+ model = self._source_database
3023
+ app_name = self.resources.get_app_name()
3024
+ engine_name = self.resources.get_default_engine_name()
3025
+ engine_size = self.resources.config.get_default_engine_size()
3026
+
3027
+ # Validate engine name
3028
+ is_name_valid, _ = validate_engine_name(engine_name)
3029
+ if not is_name_valid:
3030
+ raise EngineNameValidationException(engine_name)
3031
+
3032
+ # Validate engine size
3033
+ valid_sizes = self.resources.get_engine_sizes()
3034
+ if not isinstance(engine_size, str) or engine_size not in valid_sizes:
3035
+ raise InvalidEngineSizeError(str(engine_size), valid_sizes)
3036
+
3037
+ program_span_id = debugging.get_program_span_id()
3038
+
3039
+ query_attrs_dict = json.loads(headers.get("X-Query-Attributes", "{}")) if headers else {}
3040
+ with debugging.span("poll_use_index", sources=self.resources.sources, model=model, engine=engine_name, **query_attrs_dict):
3041
+ self.maybe_poll_use_index(
3042
+ app_name=app_name,
3043
+ sources=self.resources.sources,
3044
+ model=model,
3045
+ engine_name=engine_name,
3046
+ engine_size=engine_size,
3047
+ program_span_id=program_span_id,
3048
+ headers=headers
3049
+ )
3050
+
3051
+ self.last_database_version = len(self.resources.sources)
3052
+ self._manage_packages()
3053
+
3054
+ if isolated and not self.keep_model:
3055
+ atexit.register(self.delete_database)
3056
+
3057
+ def maybe_poll_use_index(
3058
+ self,
3059
+ app_name: str,
3060
+ sources: Iterable[str],
3061
+ model: str,
3062
+ engine_name: str,
3063
+ engine_size: str | None = None,
3064
+ program_span_id: str | None = None,
3065
+ headers: Dict | None = None,
3066
+ ):
3067
+ """Only call _poll_use_index if there are sources to process."""
3068
+ assert isinstance(self.resources, Resources)
3069
+ return self.resources.maybe_poll_use_index(
3070
+ app_name=app_name,
3071
+ sources=sources,
3072
+ model=model,
3073
+ engine_name=engine_name,
3074
+ engine_size=engine_size,
3075
+ program_span_id=program_span_id,
3076
+ headers=headers
3077
+ )
3078
+
3079
+
3080
+ #--------------------------------------------------
3081
+ # Graph
3082
+ #--------------------------------------------------
3083
+
3084
+ def Graph(
3085
+ name,
3086
+ *,
3087
+ profile: str | None = None,
3088
+ config: Config,
3089
+ dry_run: bool = False,
3090
+ isolated: bool = True,
3091
+ connection: Session | None = None,
3092
+ keep_model: bool = False,
3093
+ nowait_durable: bool = True,
3094
+ format: str = "default",
3095
+ ):
3096
+ from .resources_factory import create_resources_instance
3097
+ from .use_index_resources import UseIndexResources
3098
+
3099
+ use_graph_index = config.get("use_graph_index", USE_GRAPH_INDEX)
3100
+ use_monotype_operators = config.get("compiler.use_monotype_operators", False)
3101
+
3102
+ # Create resources instance using factory
3103
+ resources = create_resources_instance(
3104
+ config=config,
3105
+ profile=profile,
3106
+ connection=connection,
3107
+ generation=Generation.V0,
3108
+ dry_run=False, # Resources instance dry_run is separate from client dry_run
3109
+ language="rel",
3110
+ )
3111
+
3112
+ # Determine client class based on resources type and config
3113
+ # SnowflakeClient is used for resources that support use_index functionality
3114
+ if use_graph_index or isinstance(resources, UseIndexResources):
3115
+ client_class = SnowflakeClient
3116
+ else:
3117
+ client_class = Client
3118
+
3119
+ client = client_class(
3120
+ resources,
3121
+ rel.Compiler(config),
3122
+ name,
3123
+ config,
3124
+ dry_run=dry_run,
3125
+ isolated=isolated,
3126
+ keep_model=keep_model,
3127
+ nowait_durable=nowait_durable
3128
+ )
3129
+ base_rel = """
3130
+ @inline
3131
+ def make_identity(x..., z):
3132
+ rel_primitive_hash_tuple_uint128(x..., z)
3133
+
3134
+ @inline
3135
+ def pyrel_default({F}, c, k..., v):
3136
+ F(k..., v) or (not F(k..., _) and v = c)
3137
+
3138
+ @inline
3139
+ def pyrel_unwrap(x in UInt128, y): y = x
3140
+
3141
+ @inline
3142
+ def pyrel_dates_period_days(x in Date, y in Date, z in Int):
3143
+ exists((u) | dates_period_days(x, y , u) and u = ::std::common::^Day[z])
3144
+
3145
+ @inline
3146
+ def pyrel_datetimes_period_milliseconds(x in DateTime, y in DateTime, z in Int):
3147
+ exists((u) | datetimes_period_milliseconds(x, y , u) and u = ^Millisecond[z])
3148
+
3149
+ @inline
3150
+ def pyrel_bool_filter(a, b, {F}, z): { z = if_then_else[F(a, b), boolean_true, boolean_false] }
3151
+
3152
+ @inline
3153
+ def pyrel_strftime(v, fmt, tz in String, s in String):
3154
+ (Date(v) and s = format_date[v, fmt])
3155
+ or (DateTime(v) and s = format_datetime[v, fmt, tz])
3156
+
3157
+ @inline
3158
+ def pyrel_regex_match_all(pattern, string in String, pos in Int, offset in Int, match in String):
3159
+ regex_match_all(pattern, string, offset, match) and offset >= pos
3160
+
3161
+ @inline
3162
+ def pyrel_regex_match(pattern, string in String, pos in Int, offset in Int, match in String):
3163
+ pyrel_regex_match_all(pattern, string, pos, offset, match) and offset = pos
3164
+
3165
+ @inline
3166
+ def pyrel_regex_search(pattern, string in String, pos in Int, offset in Int, match in String):
3167
+ enumerate(pyrel_regex_match_all[pattern, string, pos], 1, offset, match)
3168
+
3169
+ @inline
3170
+ def pyrel_regex_sub(pattern, repl in String, string in String, result in String):
3171
+ string_replace_multiple(string, {(last[regex_match_all[pattern, string]], repl)}, result)
3172
+
3173
+ @inline
3174
+ def pyrel_capture_group(regex in Pattern, string in String, pos in Int, index, match in String):
3175
+ (Integer(index) and capture_group_by_index(regex, string, pos, index, match)) or
3176
+ (String(index) and capture_group_by_name(regex, string, pos, index, match))
3177
+
3178
+ declare __resource
3179
+ declare __compiled_patterns
3180
+ """
3181
+ if use_monotype_operators:
3182
+ base_rel += """
3183
+
3184
+ // use monotyped operators
3185
+ from ::std::monotype import +, -, *, /, <, <=, >, >=
3186
+ """
3187
+ pyrel_base = dsl.build.raw_task(base_rel)
3188
+ debugging.set_source(pyrel_base)
3189
+ client.install("pyrel_base", pyrel_base)
3190
+ return dsl.Graph(client, name, format=format)