stmt.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. worker_after_drop_view_stmt_sqlite = "DROP VIEW IF EXISTS gpu_devices_view"
  2. worker_after_create_view_stmt_sqlite = """
  3. CREATE VIEW IF NOT EXISTS gpu_devices_view AS
  4. SELECT
  5. w.name || ':' || json_extract(value, '$.type') || ':' || json_extract(value, '$.index') AS 'id',
  6. w.id AS 'worker_id',
  7. w.name AS 'worker_name',
  8. w.ip AS 'worker_ip',
  9. w.ifname AS 'worker_ifname',
  10. w.cluster_id,
  11. w.owner_principal_id,
  12. w.created_at,
  13. w.updated_at,
  14. w.deleted_at,
  15. json_extract(value, '$.vendor') AS 'vendor',
  16. json_extract(value, '$.type') AS 'type',
  17. json_extract(value, '$.index') AS 'index',
  18. json_extract(value, '$.device_index') AS 'device_index',
  19. json_extract(value, '$.device_chip_index') AS 'device_chip_index',
  20. json_extract(value, '$.arch_family') AS 'arch_family',
  21. json_extract(value, '$.name') AS 'name',
  22. json_extract(value, '$.uuid') AS 'uuid',
  23. json_extract(value, '$.driver_version') AS 'driver_version',
  24. json_extract(value, '$.runtime_version') AS 'runtime_version',
  25. json_extract(value, '$.compute_capability') AS 'compute_capability',
  26. json_extract(value, '$.core') AS 'core',
  27. json_extract(value, '$.memory') AS 'memory',
  28. json_extract(value, '$.temperature') AS 'temperature',
  29. json_extract(value, '$.network') AS 'network'
  30. FROM
  31. workers w,
  32. json_each(w.status, '$.gpu_devices')
  33. WHERE
  34. json_array_length(w.status, '$.gpu_devices') > 0
  35. """
  36. worker_after_drop_view_stmt_mysql = "DROP VIEW IF EXISTS gpu_devices_view"
  37. worker_after_create_view_stmt_mysql = """
  38. CREATE VIEW gpu_devices_view AS
  39. SELECT
  40. CONCAT(w.name, ':', JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.type')), ':', JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.index'))) AS `id`,
  41. w.id AS `worker_id`,
  42. w.name AS `worker_name`,
  43. w.ip AS `worker_ip`,
  44. w.ifname AS `worker_ifname`,
  45. w.cluster_id,
  46. w.owner_principal_id,
  47. w.created_at,
  48. w.updated_at,
  49. w.deleted_at,
  50. JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.vendor')) AS `vendor`,
  51. JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.type')) AS `type`,
  52. CAST(JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.index')) AS UNSIGNED) AS `index`,
  53. CAST(JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.device_index')) AS UNSIGNED) AS `device_index`,
  54. CAST(JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.device_chip_index')) AS UNSIGNED) AS `device_chip_index`,
  55. JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.arch_family')) AS `arch_family`,
  56. JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.name')) AS `name`,
  57. JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.uuid')) AS `uuid`,
  58. JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.driver_version')) AS `driver_version`,
  59. JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.runtime_version')) AS `runtime_version`,
  60. JSON_UNQUOTE(JSON_EXTRACT(gpu_device, '$.compute_capability')) AS `compute_capability`,
  61. JSON_EXTRACT(gpu_device, '$.core') AS `core`,
  62. JSON_EXTRACT(gpu_device, '$.memory') AS `memory`,
  63. CAST(COALESCE(JSON_VALUE(gpu_device, '$.temperature'), '0') AS DECIMAL(10, 2)) AS `temperature`,
  64. JSON_EXTRACT(gpu_device, '$.network') AS `network`
  65. FROM
  66. workers w,
  67. JSON_TABLE(w.status, '$.gpu_devices[*]' COLUMNS(
  68. gpu_device JSON PATH '$'
  69. )) AS gpu_devices
  70. WHERE
  71. JSON_LENGTH(w.status, '$.gpu_devices') IS NOT NULL
  72. AND JSON_LENGTH(w.status, '$.gpu_devices') > 0
  73. """
  74. worker_after_drop_view_stmt_postgres = "DROP VIEW IF EXISTS gpu_devices_view CASCADE"
  75. worker_after_create_view_stmt_postgres = """
  76. CREATE VIEW gpu_devices_view AS
  77. SELECT
  78. w.name || ':' || (gpu_device::json->>'type') || ':' || (gpu_device::json->>'index') AS "id",
  79. w.id AS "worker_id",
  80. w.name AS "worker_name",
  81. w.ip AS "worker_ip",
  82. w.ifname AS "worker_ifname",
  83. w.cluster_id,
  84. w.owner_principal_id,
  85. w.created_at,
  86. w.updated_at,
  87. w.deleted_at,
  88. (gpu_device::json->>'vendor') AS "vendor",
  89. (gpu_device::json->>'type') AS "type",
  90. (gpu_device::json->>'index')::INTEGER AS "index",
  91. (gpu_device::json->>'device_index')::INTEGER AS "device_index",
  92. (gpu_device::json->>'device_chip_index')::INTEGER AS "device_chip_index",
  93. (gpu_device::json->>'arch_family') AS "arch_family",
  94. (gpu_device::json->>'name') AS "name",
  95. (gpu_device::json->>'uuid') AS "uuid",
  96. (gpu_device::json->>'driver_version') AS "driver_version",
  97. (gpu_device::json->>'runtime_version') AS "runtime_version",
  98. (gpu_device::json->>'compute_capability') AS "compute_capability",
  99. (gpu_device::json->>'core')::JSONB AS "core",
  100. (gpu_device::json->>'memory')::JSONB AS "memory",
  101. (gpu_device::json->>'temperature')::FLOAT AS "temperature",
  102. (gpu_device::json->>'network')::JSONB AS "network"
  103. FROM
  104. workers w,
  105. LATERAL json_array_elements(w.status::json->'gpu_devices') AS gpu_device
  106. WHERE
  107. json_typeof(w.status::json->'gpu_devices') = 'array';
  108. """
  109. # openGauss does not support json_array_elements (added in PostgreSQL 9.3).
  110. # Use generate_series + integer-index -> operator to expand the JSONB array instead.
  111. worker_after_create_view_stmt_opengauss = """
  112. CREATE VIEW gpu_devices_view AS
  113. SELECT
  114. w.name || ':' || (w.status::jsonb->'gpu_devices'->s.idx->>'type') || ':' || (w.status::jsonb->'gpu_devices'->s.idx->>'index') AS "id",
  115. w.id AS "worker_id",
  116. w.name AS "worker_name",
  117. w.ip AS "worker_ip",
  118. w.ifname AS "worker_ifname",
  119. w.cluster_id,
  120. w.owner_principal_id,
  121. w.created_at,
  122. w.updated_at,
  123. w.deleted_at,
  124. (w.status::jsonb->'gpu_devices'->s.idx->>'vendor') AS "vendor",
  125. (w.status::jsonb->'gpu_devices'->s.idx->>'type') AS "type",
  126. (w.status::jsonb->'gpu_devices'->s.idx->>'index')::INTEGER AS "index",
  127. (w.status::jsonb->'gpu_devices'->s.idx->>'device_index')::INTEGER AS "device_index",
  128. (w.status::jsonb->'gpu_devices'->s.idx->>'device_chip_index')::INTEGER AS "device_chip_index",
  129. (w.status::jsonb->'gpu_devices'->s.idx->>'arch_family') AS "arch_family",
  130. (w.status::jsonb->'gpu_devices'->s.idx->>'name') AS "name",
  131. (w.status::jsonb->'gpu_devices'->s.idx->>'uuid') AS "uuid",
  132. (w.status::jsonb->'gpu_devices'->s.idx->>'driver_version') AS "driver_version",
  133. (w.status::jsonb->'gpu_devices'->s.idx->>'runtime_version') AS "runtime_version",
  134. (w.status::jsonb->'gpu_devices'->s.idx->>'compute_capability') AS "compute_capability",
  135. (w.status::jsonb->'gpu_devices'->s.idx->'core')::JSONB AS "core",
  136. (w.status::jsonb->'gpu_devices'->s.idx->'memory')::JSONB AS "memory",
  137. (w.status::jsonb->'gpu_devices'->s.idx->>'temperature')::FLOAT AS "temperature",
  138. (w.status::jsonb->'gpu_devices'->s.idx->'network')::JSONB AS "network"
  139. FROM
  140. workers w,
  141. generate_series(0, jsonb_array_length(w.status::jsonb->'gpu_devices') - 1) AS s(idx)
  142. WHERE
  143. jsonb_typeof(w.status::jsonb->'gpu_devices') = 'array';
  144. """
  145. model_user_after_drop_view_stmt = "DROP VIEW IF EXISTS non_admin_user_models"
  146. principal_users_after_drop_view_stmt = "DROP VIEW IF EXISTS principal_users"
  147. def principal_users_after_create_view_stmt() -> str:
  148. """Helper view: (principal_id, user_id) — every user covered by a
  149. principal, expanded across direct USER ownership and active
  150. ORG/GROUP memberships. Used by ``non_admin_user_models`` so the
  151. ALLOWED_PRINCIPALS branch can index-join instead of running a
  152. correlated EXISTS over ``principal_memberships`` per row.
  153. """
  154. return '''
  155. CREATE VIEW principal_users AS
  156. SELECT u.principal_id AS principal_id, u.id AS user_id
  157. FROM users u
  158. UNION ALL
  159. SELECT pm.parent_principal_id AS principal_id, u.id AS user_id
  160. FROM principal_memberships pm
  161. JOIN users u ON u.principal_id = pm.member_principal_id
  162. JOIN principals pr ON pr.id = pm.parent_principal_id
  163. WHERE pm.deleted_at IS NULL
  164. AND pr.deleted_at IS NULL
  165. AND pr.kind IN ('ORG', 'GROUP')
  166. '''
  167. def model_user_after_create_view_stmt(db_type: str) -> str:
  168. sql_false = '0' if db_type == "sqlite" else 'FALSE'
  169. pid = (
  170. "CONCAT(m.id, ':', u.id)"
  171. if db_type == "mysql"
  172. else "CAST(m.id AS TEXT) || ':' || CAST(u.id AS TEXT)"
  173. )
  174. # 4-branch UNION ALL — each branch is a straight index join, so the
  175. # planner doesn't have to materialize every (user, route) pair to
  176. # then OR-filter EXISTS subqueries against it. ``mrp.deleted_at IS
  177. # NULL`` is required on every ACL branch: leaving it off was the
  178. # soft-delete-leak bug from review.
  179. return f'''
  180. CREATE VIEW non_admin_user_models AS
  181. SELECT {pid} AS pid, u.id AS user_id, m.*
  182. FROM users u
  183. CROSS JOIN model_routes m
  184. WHERE u.is_admin = {sql_false} AND u.is_system = {sql_false}
  185. AND m.access_policy IN ('PUBLIC', 'AUTHED')
  186. UNION ALL
  187. SELECT {pid} AS pid, u.id AS user_id, m.*
  188. FROM users u
  189. JOIN principal_memberships pm
  190. ON pm.member_principal_id = u.principal_id
  191. AND pm.deleted_at IS NULL
  192. JOIN model_routes m
  193. ON m.owner_principal_id = pm.parent_principal_id
  194. AND m.access_policy = 'ORG'
  195. WHERE u.is_admin = {sql_false} AND u.is_system = {sql_false}
  196. UNION ALL
  197. SELECT {pid} AS pid, u.id AS user_id, m.*
  198. FROM users u
  199. JOIN model_route_principals mrp
  200. ON mrp.principal_id = u.principal_id
  201. AND mrp.deleted_at IS NULL
  202. JOIN model_routes m
  203. ON m.id = mrp.route_id
  204. AND m.access_policy = 'ALLOWED_USERS'
  205. WHERE u.is_admin = {sql_false} AND u.is_system = {sql_false}
  206. UNION ALL
  207. SELECT {pid} AS pid, u.id AS user_id, m.*
  208. FROM users u
  209. JOIN principal_users pu ON pu.user_id = u.id
  210. JOIN model_route_principals mrp
  211. ON mrp.principal_id = pu.principal_id
  212. AND mrp.deleted_at IS NULL
  213. JOIN model_routes m
  214. ON m.id = mrp.route_id
  215. AND m.access_policy = 'ALLOWED_PRINCIPALS'
  216. WHERE u.is_admin = {sql_false} AND u.is_system = {sql_false}
  217. '''