@malloydata/malloy 0.0.182-dev240911142514 → 0.0.182-dev240911163224

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 (275) hide show
  1. package/dist/dialect/dialect.d.ts +3 -1
  2. package/dist/dialect/dialect_map.d.ts +1 -2
  3. package/dist/dialect/dialect_map.js +5 -74
  4. package/dist/dialect/duckdb/dialect_functions.d.ts +2 -0
  5. package/dist/dialect/duckdb/dialect_functions.js +67 -0
  6. package/dist/dialect/duckdb/duckdb.d.ts +3 -1
  7. package/dist/dialect/duckdb/duckdb.js +6 -5
  8. package/dist/dialect/duckdb/function_overrides.d.ts +2 -0
  9. package/dist/dialect/duckdb/function_overrides.js +55 -0
  10. package/dist/dialect/functions/index.d.ts +3 -1
  11. package/dist/dialect/functions/index.js +6 -3
  12. package/dist/dialect/functions/malloy_standard_functions.d.ts +134 -0
  13. package/dist/dialect/functions/malloy_standard_functions.js +612 -0
  14. package/dist/dialect/functions/util.d.ts +78 -2
  15. package/dist/dialect/functions/util.js +257 -3
  16. package/dist/dialect/index.d.ts +5 -4
  17. package/dist/dialect/index.js +3 -4
  18. package/dist/dialect/postgres/dialect_functions.d.ts +2 -0
  19. package/dist/dialect/postgres/dialect_functions.js +51 -0
  20. package/dist/dialect/postgres/function_overrides.d.ts +2 -0
  21. package/dist/dialect/postgres/function_overrides.js +73 -0
  22. package/dist/dialect/postgres/postgres.d.ts +3 -2
  23. package/dist/dialect/postgres/postgres.js +6 -6
  24. package/dist/dialect/snowflake/dialect_functions.d.ts +2 -0
  25. package/dist/dialect/snowflake/dialect_functions.js +57 -0
  26. package/dist/dialect/snowflake/function_overrides.d.ts +2 -0
  27. package/dist/dialect/snowflake/function_overrides.js +39 -0
  28. package/dist/dialect/snowflake/snowflake.d.ts +3 -2
  29. package/dist/dialect/snowflake/snowflake.js +6 -6
  30. package/dist/dialect/standardsql/dialect_functions.d.ts +2 -0
  31. package/dist/dialect/standardsql/dialect_functions.js +65 -0
  32. package/dist/dialect/standardsql/function_overrides.d.ts +2 -0
  33. package/dist/dialect/standardsql/function_overrides.js +17 -0
  34. package/dist/dialect/standardsql/standardsql.d.ts +3 -1
  35. package/dist/dialect/standardsql/standardsql.js +6 -5
  36. package/dist/dialect/trino/dialect_functions.d.ts +2 -0
  37. package/dist/dialect/trino/dialect_functions.js +63 -0
  38. package/dist/dialect/trino/function_overrides.d.ts +2 -0
  39. package/dist/dialect/trino/function_overrides.js +50 -0
  40. package/dist/dialect/trino/trino.d.ts +3 -1
  41. package/dist/dialect/trino/trino.js +6 -6
  42. package/dist/index.d.ts +2 -2
  43. package/dist/index.js +3 -4
  44. package/dist/lang/ast/expressions/expr-func.js +25 -29
  45. package/dist/lang/ast/types/dialect-name-space.js +5 -5
  46. package/dist/lang/ast/types/global-name-space.d.ts +4 -2
  47. package/dist/lang/ast/types/global-name-space.js +59 -6
  48. package/dist/model/malloy_query.d.ts +1 -1
  49. package/dist/model/malloy_query.js +7 -6
  50. package/dist/model/malloy_types.d.ts +7 -7
  51. package/package.json +1 -1
  52. package/dist/dialect/duckdb/functions/byte_length.d.ts +0 -2
  53. package/dist/dialect/duckdb/functions/byte_length.js +0 -33
  54. package/dist/dialect/duckdb/functions/count_approx.d.ts +0 -2
  55. package/dist/dialect/duckdb/functions/count_approx.js +0 -20
  56. package/dist/dialect/duckdb/functions/dialect_functions.d.ts +0 -4
  57. package/dist/dialect/duckdb/functions/dialect_functions.js +0 -16
  58. package/dist/dialect/duckdb/functions/div.d.ts +0 -2
  59. package/dist/dialect/duckdb/functions/div.js +0 -38
  60. package/dist/dialect/duckdb/functions/duckdb_functions.d.ts +0 -1
  61. package/dist/dialect/duckdb/functions/duckdb_functions.js +0 -54
  62. package/dist/dialect/duckdb/functions/ends_with.d.ts +0 -2
  63. package/dist/dialect/duckdb/functions/ends_with.js +0 -35
  64. package/dist/dialect/duckdb/functions/greatest_and_least.d.ts +0 -3
  65. package/dist/dialect/duckdb/functions/greatest_and_least.js +0 -51
  66. package/dist/dialect/duckdb/functions/index.d.ts +0 -1
  67. package/dist/dialect/duckdb/functions/index.js +0 -28
  68. package/dist/dialect/duckdb/functions/is_inf.d.ts +0 -2
  69. package/dist/dialect/duckdb/functions/is_inf.js +0 -33
  70. package/dist/dialect/duckdb/functions/is_nan.d.ts +0 -2
  71. package/dist/dialect/duckdb/functions/is_nan.js +0 -33
  72. package/dist/dialect/duckdb/functions/log.d.ts +0 -2
  73. package/dist/dialect/duckdb/functions/log.js +0 -38
  74. package/dist/dialect/duckdb/functions/rand.d.ts +0 -2
  75. package/dist/dialect/duckdb/functions/rand.js +0 -31
  76. package/dist/dialect/duckdb/functions/replace.d.ts +0 -2
  77. package/dist/dialect/duckdb/functions/replace.js +0 -40
  78. package/dist/dialect/duckdb/functions/trunc.d.ts +0 -2
  79. package/dist/dialect/duckdb/functions/trunc.js +0 -39
  80. package/dist/dialect/duckdb/functions/unicode.d.ts +0 -2
  81. package/dist/dialect/duckdb/functions/unicode.js +0 -35
  82. package/dist/dialect/functions/all_functions.d.ts +0 -8
  83. package/dist/dialect/functions/all_functions.js +0 -146
  84. package/dist/dialect/functions/atan2.d.ts +0 -2
  85. package/dist/dialect/functions/atan2.js +0 -33
  86. package/dist/dialect/functions/avg_moving.d.ts +0 -2
  87. package/dist/dialect/functions/avg_moving.js +0 -46
  88. package/dist/dialect/functions/chr.d.ts +0 -4
  89. package/dist/dialect/functions/chr.js +0 -45
  90. package/dist/dialect/functions/coalesce.d.ts +0 -2
  91. package/dist/dialect/functions/coalesce.js +0 -38
  92. package/dist/dialect/functions/concat.d.ts +0 -2
  93. package/dist/dialect/functions/concat.js +0 -38
  94. package/dist/dialect/functions/div.d.ts +0 -2
  95. package/dist/dialect/functions/div.js +0 -36
  96. package/dist/dialect/functions/first_value_window.d.ts +0 -3
  97. package/dist/dialect/functions/first_value_window.js +0 -49
  98. package/dist/dialect/functions/function_map.d.ts +0 -16
  99. package/dist/dialect/functions/function_map.js +0 -72
  100. package/dist/dialect/functions/greatest_and_least.d.ts +0 -3
  101. package/dist/dialect/functions/greatest_and_least.js +0 -44
  102. package/dist/dialect/functions/ifnull.d.ts +0 -2
  103. package/dist/dialect/functions/ifnull.js +0 -38
  104. package/dist/dialect/functions/is_inf.d.ts +0 -2
  105. package/dist/dialect/functions/is_inf.js +0 -33
  106. package/dist/dialect/functions/is_nan.d.ts +0 -2
  107. package/dist/dialect/functions/is_nan.js +0 -33
  108. package/dist/dialect/functions/lag.d.ts +0 -2
  109. package/dist/dialect/functions/lag.js +0 -53
  110. package/dist/dialect/functions/lead.d.ts +0 -2
  111. package/dist/dialect/functions/lead.js +0 -50
  112. package/dist/dialect/functions/length.d.ts +0 -3
  113. package/dist/dialect/functions/length.js +0 -40
  114. package/dist/dialect/functions/log.d.ts +0 -2
  115. package/dist/dialect/functions/log.js +0 -35
  116. package/dist/dialect/functions/lower.d.ts +0 -2
  117. package/dist/dialect/functions/lower.js +0 -34
  118. package/dist/dialect/functions/nullif.d.ts +0 -2
  119. package/dist/dialect/functions/nullif.js +0 -42
  120. package/dist/dialect/functions/pi.d.ts +0 -2
  121. package/dist/dialect/functions/pi.js +0 -31
  122. package/dist/dialect/functions/pow.d.ts +0 -2
  123. package/dist/dialect/functions/pow.js +0 -35
  124. package/dist/dialect/functions/rand.d.ts +0 -2
  125. package/dist/dialect/functions/rand.js +0 -31
  126. package/dist/dialect/functions/rank.d.ts +0 -2
  127. package/dist/dialect/functions/rank.js +0 -36
  128. package/dist/dialect/functions/regexp_extract.d.ts +0 -2
  129. package/dist/dialect/functions/regexp_extract.js +0 -41
  130. package/dist/dialect/functions/repeat.d.ts +0 -2
  131. package/dist/dialect/functions/repeat.js +0 -35
  132. package/dist/dialect/functions/replace.d.ts +0 -2
  133. package/dist/dialect/functions/replace.js +0 -42
  134. package/dist/dialect/functions/reverse.d.ts +0 -2
  135. package/dist/dialect/functions/reverse.js +0 -34
  136. package/dist/dialect/functions/round.d.ts +0 -2
  137. package/dist/dialect/functions/round.js +0 -40
  138. package/dist/dialect/functions/row_number.d.ts +0 -2
  139. package/dist/dialect/functions/row_number.js +0 -40
  140. package/dist/dialect/functions/simple_numeric_functions.d.ts +0 -15
  141. package/dist/dialect/functions/simple_numeric_functions.js +0 -60
  142. package/dist/dialect/functions/sql.d.ts +0 -6
  143. package/dist/dialect/functions/sql.js +0 -52
  144. package/dist/dialect/functions/starts_ends_with.d.ts +0 -3
  145. package/dist/dialect/functions/starts_ends_with.js +0 -43
  146. package/dist/dialect/functions/stddev.d.ts +0 -2
  147. package/dist/dialect/functions/stddev.js +0 -34
  148. package/dist/dialect/functions/string_agg.d.ts +0 -3
  149. package/dist/dialect/functions/string_agg.js +0 -55
  150. package/dist/dialect/functions/strpos.d.ts +0 -2
  151. package/dist/dialect/functions/strpos.js +0 -35
  152. package/dist/dialect/functions/substr.d.ts +0 -2
  153. package/dist/dialect/functions/substr.js +0 -37
  154. package/dist/dialect/functions/sum_min_max_window.d.ts +0 -7
  155. package/dist/dialect/functions/sum_min_max_window.js +0 -94
  156. package/dist/dialect/functions/sum_moving.d.ts +0 -2
  157. package/dist/dialect/functions/sum_moving.js +0 -43
  158. package/dist/dialect/functions/trim_functions.d.ts +0 -4
  159. package/dist/dialect/functions/trim_functions.js +0 -42
  160. package/dist/dialect/functions/trunc.d.ts +0 -2
  161. package/dist/dialect/functions/trunc.js +0 -38
  162. package/dist/dialect/functions/upper.d.ts +0 -2
  163. package/dist/dialect/functions/upper.js +0 -34
  164. package/dist/dialect/postgres/functions/byte_length.d.ts +0 -2
  165. package/dist/dialect/postgres/functions/byte_length.js +0 -33
  166. package/dist/dialect/postgres/functions/dialect_functions.d.ts +0 -4
  167. package/dist/dialect/postgres/functions/dialect_functions.js +0 -11
  168. package/dist/dialect/postgres/functions/ends_with.d.ts +0 -2
  169. package/dist/dialect/postgres/functions/ends_with.js +0 -38
  170. package/dist/dialect/postgres/functions/greatest_and_least.d.ts +0 -3
  171. package/dist/dialect/postgres/functions/greatest_and_least.js +0 -43
  172. package/dist/dialect/postgres/functions/ifnull.d.ts +0 -2
  173. package/dist/dialect/postgres/functions/ifnull.js +0 -40
  174. package/dist/dialect/postgres/functions/index.d.ts +0 -1
  175. package/dist/dialect/postgres/functions/index.js +0 -28
  176. package/dist/dialect/postgres/functions/is_inf.d.ts +0 -2
  177. package/dist/dialect/postgres/functions/is_inf.js +0 -34
  178. package/dist/dialect/postgres/functions/is_nan.d.ts +0 -2
  179. package/dist/dialect/postgres/functions/is_nan.js +0 -33
  180. package/dist/dialect/postgres/functions/log.d.ts +0 -2
  181. package/dist/dialect/postgres/functions/log.js +0 -37
  182. package/dist/dialect/postgres/functions/postgres_functions.d.ts +0 -1
  183. package/dist/dialect/postgres/functions/postgres_functions.js +0 -63
  184. package/dist/dialect/postgres/functions/rand.d.ts +0 -2
  185. package/dist/dialect/postgres/functions/rand.js +0 -31
  186. package/dist/dialect/postgres/functions/regexp_extract.d.ts +0 -2
  187. package/dist/dialect/postgres/functions/regexp_extract.js +0 -35
  188. package/dist/dialect/postgres/functions/replace.d.ts +0 -2
  189. package/dist/dialect/postgres/functions/replace.js +0 -40
  190. package/dist/dialect/postgres/functions/round.d.ts +0 -2
  191. package/dist/dialect/postgres/functions/round.js +0 -39
  192. package/dist/dialect/postgres/functions/stddev.d.ts +0 -2
  193. package/dist/dialect/postgres/functions/stddev.js +0 -37
  194. package/dist/dialect/postgres/functions/string_agg.d.ts +0 -3
  195. package/dist/dialect/postgres/functions/string_agg.js +0 -55
  196. package/dist/dialect/postgres/functions/substr.d.ts +0 -2
  197. package/dist/dialect/postgres/functions/substr.js +0 -38
  198. package/dist/dialect/postgres/functions/trunc.d.ts +0 -2
  199. package/dist/dialect/postgres/functions/trunc.js +0 -40
  200. package/dist/dialect/postgres/functions/unicode.d.ts +0 -2
  201. package/dist/dialect/postgres/functions/unicode.js +0 -34
  202. package/dist/dialect/snowflake/functions/chr.d.ts +0 -2
  203. package/dist/dialect/snowflake/functions/chr.js +0 -36
  204. package/dist/dialect/snowflake/functions/dialect_functions.d.ts +0 -4
  205. package/dist/dialect/snowflake/functions/dialect_functions.js +0 -11
  206. package/dist/dialect/snowflake/functions/div.d.ts +0 -2
  207. package/dist/dialect/snowflake/functions/div.js +0 -37
  208. package/dist/dialect/snowflake/functions/index.d.ts +0 -1
  209. package/dist/dialect/snowflake/functions/index.js +0 -28
  210. package/dist/dialect/snowflake/functions/is_inf.d.ts +0 -2
  211. package/dist/dialect/snowflake/functions/is_inf.js +0 -34
  212. package/dist/dialect/snowflake/functions/is_nan.d.ts +0 -2
  213. package/dist/dialect/snowflake/functions/is_nan.js +0 -33
  214. package/dist/dialect/snowflake/functions/length.d.ts +0 -3
  215. package/dist/dialect/snowflake/functions/length.js +0 -39
  216. package/dist/dialect/snowflake/functions/log.d.ts +0 -2
  217. package/dist/dialect/snowflake/functions/log.js +0 -37
  218. package/dist/dialect/snowflake/functions/rand.d.ts +0 -2
  219. package/dist/dialect/snowflake/functions/rand.js +0 -33
  220. package/dist/dialect/snowflake/functions/regexp_extract.d.ts +0 -2
  221. package/dist/dialect/snowflake/functions/regexp_extract.js +0 -35
  222. package/dist/dialect/snowflake/functions/snowflake_functions.d.ts +0 -1
  223. package/dist/dialect/snowflake/functions/snowflake_functions.js +0 -56
  224. package/dist/dialect/snowflake/functions/starts_ends_with.d.ts +0 -3
  225. package/dist/dialect/snowflake/functions/starts_ends_with.js +0 -43
  226. package/dist/dialect/snowflake/functions/string_agg.d.ts +0 -3
  227. package/dist/dialect/snowflake/functions/string_agg.js +0 -55
  228. package/dist/dialect/snowflake/functions/strpos.d.ts +0 -2
  229. package/dist/dialect/snowflake/functions/strpos.js +0 -35
  230. package/dist/dialect/snowflake/functions/trunc.d.ts +0 -2
  231. package/dist/dialect/snowflake/functions/trunc.js +0 -39
  232. package/dist/dialect/standardsql/functions/chr.d.ts +0 -2
  233. package/dist/dialect/standardsql/functions/chr.js +0 -37
  234. package/dist/dialect/standardsql/functions/dialect_functions.d.ts +0 -4
  235. package/dist/dialect/standardsql/functions/dialect_functions.js +0 -16
  236. package/dist/dialect/standardsql/functions/index.d.ts +0 -1
  237. package/dist/dialect/standardsql/functions/index.js +0 -28
  238. package/dist/dialect/standardsql/functions/pi.d.ts +0 -2
  239. package/dist/dialect/standardsql/functions/pi.js +0 -32
  240. package/dist/dialect/standardsql/functions/standardsql_functions.d.ts +0 -1
  241. package/dist/dialect/standardsql/functions/standardsql_functions.js +0 -36
  242. package/dist/dialect/standardsql/functions/string_agg.d.ts +0 -3
  243. package/dist/dialect/standardsql/functions/string_agg.js +0 -59
  244. package/dist/dialect/trino/functions/byte_length.d.ts +0 -2
  245. package/dist/dialect/trino/functions/byte_length.js +0 -33
  246. package/dist/dialect/trino/functions/chr.d.ts +0 -4
  247. package/dist/dialect/trino/functions/chr.js +0 -45
  248. package/dist/dialect/trino/functions/concat.d.ts +0 -2
  249. package/dist/dialect/trino/functions/concat.js +0 -38
  250. package/dist/dialect/trino/functions/count_approx.d.ts +0 -2
  251. package/dist/dialect/trino/functions/count_approx.js +0 -20
  252. package/dist/dialect/trino/functions/dialect_functions.d.ts +0 -4
  253. package/dist/dialect/trino/functions/dialect_functions.js +0 -16
  254. package/dist/dialect/trino/functions/div.d.ts +0 -2
  255. package/dist/dialect/trino/functions/div.js +0 -36
  256. package/dist/dialect/trino/functions/ifnull.d.ts +0 -2
  257. package/dist/dialect/trino/functions/ifnull.js +0 -40
  258. package/dist/dialect/trino/functions/index.d.ts +0 -1
  259. package/dist/dialect/trino/functions/index.js +0 -28
  260. package/dist/dialect/trino/functions/is_inf.d.ts +0 -2
  261. package/dist/dialect/trino/functions/is_inf.js +0 -33
  262. package/dist/dialect/trino/functions/log.d.ts +0 -2
  263. package/dist/dialect/trino/functions/log.js +0 -37
  264. package/dist/dialect/trino/functions/repeat.d.ts +0 -2
  265. package/dist/dialect/trino/functions/repeat.js +0 -35
  266. package/dist/dialect/trino/functions/reverse.d.ts +0 -2
  267. package/dist/dialect/trino/functions/reverse.js +0 -34
  268. package/dist/dialect/trino/functions/starts_ends_with.d.ts +0 -3
  269. package/dist/dialect/trino/functions/starts_ends_with.js +0 -43
  270. package/dist/dialect/trino/functions/string_agg.d.ts +0 -3
  271. package/dist/dialect/trino/functions/string_agg.js +0 -55
  272. package/dist/dialect/trino/functions/trino_functions.d.ts +0 -1
  273. package/dist/dialect/trino/functions/trino_functions.js +0 -59
  274. package/dist/dialect/trino/functions/trunc.d.ts +0 -2
  275. package/dist/dialect/trino/functions/trunc.js +0 -39
@@ -25,9 +25,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.SnowflakeDialect = void 0;
26
26
  const utils_1 = require("../../model/utils");
27
27
  const malloy_types_1 = require("../../model/malloy_types");
28
- const functions_1 = require("./functions");
28
+ const functions_1 = require("../functions");
29
29
  const dialect_1 = require("../dialect");
30
- const dialect_functions_1 = require("./functions/dialect_functions");
30
+ const dialect_functions_1 = require("./dialect_functions");
31
+ const function_overrides_1 = require("./function_overrides");
31
32
  const extractionMap = {
32
33
  'day_of_week': 'dayofweek',
33
34
  'day_of_year': 'dayofyear',
@@ -86,7 +87,6 @@ class SnowflakeDialect extends dialect_1.Dialect {
86
87
  this.supportsSafeCast = true;
87
88
  this.supportsNesting = true;
88
89
  this.defaultSampling = { rows: 50000 };
89
- this.globalFunctions = functions_1.SNOWFLAKE_FUNCTIONS;
90
90
  // NOTE: safely setting all these to false for now
91
91
  // more many be implemented in future
92
92
  this.unnestWithNumbers = false;
@@ -366,11 +366,11 @@ ${(0, utils_1.indent)(sql)}
366
366
  const noVirgule = literal.replace(/\\/g, '\\\\');
367
367
  return "'" + noVirgule.replace(/'/g, "\\'") + "'";
368
368
  }
369
- getGlobalFunctionDef(name) {
370
- return functions_1.SNOWFLAKE_FUNCTIONS.get(name);
369
+ getDialectFunctionOverrides() {
370
+ return (0, functions_1.expandOverrideMap)(function_overrides_1.SNOWFLAKE_MALLOY_STANDARD_OVERLOADS);
371
371
  }
372
372
  getDialectFunctions() {
373
- return dialect_functions_1.SNOWFLAKE_DIALECT_FUNCTIONS;
373
+ return (0, functions_1.expandBlueprintMap)(dialect_functions_1.SNOWFLAKE_DIALECT_FUNCTIONS);
374
374
  }
375
375
  malloyTypeToSQLType(malloyType) {
376
376
  if (malloyType.type === 'number') {
@@ -0,0 +1,2 @@
1
+ import { DefinitionBlueprintMap } from '../functions/util';
2
+ export declare const STANDARDSQL_DIALECT_FUNCTIONS: DefinitionBlueprintMap;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.STANDARDSQL_DIALECT_FUNCTIONS = void 0;
10
+ const date_from_unix_date = {
11
+ takes: { 'unix_date': 'number' },
12
+ returns: 'date',
13
+ impl: { function: 'DATE_FROM_UNIX_DATE' },
14
+ };
15
+ const string_agg = {
16
+ 'default_separator': {
17
+ takes: { 'value': { dimension: 'string' } },
18
+ returns: { measure: 'string' },
19
+ supportsOrderBy: true,
20
+ supportsLimit: true,
21
+ impl: {
22
+ sql: 'STRING_AGG(${value}${order_by:}${limit:})',
23
+ defaultOrderByArgIndex: 0,
24
+ },
25
+ },
26
+ 'with_separator': {
27
+ takes: {
28
+ 'value': { dimension: 'string' },
29
+ 'separator': { literal: 'string' },
30
+ },
31
+ returns: { measure: 'string' },
32
+ supportsOrderBy: true,
33
+ supportsLimit: true,
34
+ impl: {
35
+ sql: 'STRING_AGG(${value}, ${separator}${order_by:}${limit:})',
36
+ defaultOrderByArgIndex: 0,
37
+ },
38
+ },
39
+ };
40
+ const string_agg_distinct = {
41
+ 'default_separator': {
42
+ ...string_agg['default_separator'],
43
+ isSymmetric: true,
44
+ supportsOrderBy: 'only_default',
45
+ impl: {
46
+ sql: 'STRING_AGG(DISTINCT ${value}${order_by:}${limit:})',
47
+ defaultOrderByArgIndex: 0,
48
+ },
49
+ },
50
+ 'with_separator': {
51
+ ...string_agg['with_separator'],
52
+ isSymmetric: true,
53
+ supportsOrderBy: 'only_default',
54
+ impl: {
55
+ sql: 'STRING_AGG(DISTINCT ${value}, ${separator}${order_by:}${limit:})',
56
+ defaultOrderByArgIndex: 0,
57
+ },
58
+ },
59
+ };
60
+ exports.STANDARDSQL_DIALECT_FUNCTIONS = {
61
+ date_from_unix_date,
62
+ string_agg,
63
+ string_agg_distinct,
64
+ };
65
+ //# sourceMappingURL=dialect_functions.js.map
@@ -0,0 +1,2 @@
1
+ import { MalloyStandardFunctionImplementations as OverrideMap } from '../functions/malloy_standard_functions';
2
+ export declare const STANDARDSQL_MALLOY_STANDARD_OVERLOADS: OverrideMap;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.STANDARDSQL_MALLOY_STANDARD_OVERLOADS = void 0;
10
+ exports.STANDARDSQL_MALLOY_STANDARD_OVERLOADS = {
11
+ chr: {
12
+ sql: "CASE WHEN IFNULL(${value}, 1) = 0 THEN '' ELSE CHR(${value}) END",
13
+ },
14
+ // There's no built-in PI function, but we can compute it easily.
15
+ pi: { sql: 'ACOS(-1)' },
16
+ };
17
+ //# sourceMappingURL=function_overrides.js.map
@@ -53,7 +53,9 @@ export declare class StandardSQLDialect extends Dialect {
53
53
  sqlSampleTable(tableSQL: string, sample: Sampling | undefined): string;
54
54
  sqlLiteralString(literal: string): string;
55
55
  sqlLiteralRegexp(literal: string): string;
56
- getGlobalFunctionDef(name: string): DialectFunctionOverloadDef[] | undefined;
56
+ getDialectFunctionOverrides(): {
57
+ [name: string]: DialectFunctionOverloadDef[];
58
+ };
57
59
  getDialectFunctions(): {
58
60
  [name: string]: DialectFunctionOverloadDef[];
59
61
  };
@@ -25,9 +25,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.StandardSQLDialect = void 0;
26
26
  const utils_1 = require("../../model/utils");
27
27
  const malloy_types_1 = require("../../model/malloy_types");
28
- const functions_1 = require("./functions");
28
+ const functions_1 = require("../functions");
29
29
  const dialect_1 = require("../dialect");
30
- const dialect_functions_1 = require("./functions/dialect_functions");
30
+ const dialect_functions_1 = require("./dialect_functions");
31
+ const function_overrides_1 = require("./function_overrides");
31
32
  // These are the units that "TIMESTAMP_ADD" "TIMESTAMP_DIFF" accept
32
33
  function timestampMeasureable(units) {
33
34
  return [
@@ -441,11 +442,11 @@ ${(0, utils_1.indent)(sql)}
441
442
  const noVirgule = literal.replace(/\\/g, '\\\\');
442
443
  return "'" + noVirgule.replace(/'/g, "\\'") + "'";
443
444
  }
444
- getGlobalFunctionDef(name) {
445
- return functions_1.STANDARDSQL_FUNCTIONS.get(name);
445
+ getDialectFunctionOverrides() {
446
+ return (0, functions_1.expandOverrideMap)(function_overrides_1.STANDARDSQL_MALLOY_STANDARD_OVERLOADS);
446
447
  }
447
448
  getDialectFunctions() {
448
- return dialect_functions_1.STANDARDSQL_DIALECT_FUNCTIONS;
449
+ return (0, functions_1.expandBlueprintMap)(dialect_functions_1.STANDARDSQL_DIALECT_FUNCTIONS);
449
450
  }
450
451
  malloyTypeToSQLType(malloyType) {
451
452
  if (malloyType.type === 'number') {
@@ -0,0 +1,2 @@
1
+ import { DefinitionBlueprintMap } from '../functions/util';
2
+ export declare const TRINO_DIALECT_FUNCTIONS: DefinitionBlueprintMap;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.TRINO_DIALECT_FUNCTIONS = void 0;
10
+ const from_unixtime = {
11
+ takes: { 'unixtime': 'number' },
12
+ returns: 'timestamp',
13
+ impl: { function: 'FROM_UNIXTIME' },
14
+ };
15
+ const count_approx = {
16
+ takes: { 'value': { dimension: 'any' } },
17
+ returns: { measure: 'number' },
18
+ impl: { function: 'APPROX_DISTINCT' },
19
+ };
20
+ const string_agg = {
21
+ default_separator: {
22
+ takes: { 'value': { dimension: 'string' } },
23
+ returns: { measure: 'string' },
24
+ supportsOrderBy: true,
25
+ impl: {
26
+ sql: "ARRAY_JOIN(ARRAY_AGG(${value} ${order_by:}), ',')",
27
+ },
28
+ },
29
+ with_separator: {
30
+ takes: {
31
+ 'value': { dimension: 'string' },
32
+ 'separator': { literal: 'string' },
33
+ },
34
+ returns: { measure: 'string' },
35
+ supportsOrderBy: true,
36
+ impl: {
37
+ sql: 'ARRAY_JOIN(ARRAY_AGG(${value} ${order_by:}), ${separator})',
38
+ },
39
+ },
40
+ };
41
+ const string_agg_distinct = {
42
+ default_separator: {
43
+ ...string_agg['default_separator'],
44
+ isSymmetric: true,
45
+ impl: {
46
+ sql: "ARRAY_JOIN(ARRAY_AGG(DISTINCT ${value} ${order_by:}), ',')",
47
+ },
48
+ },
49
+ with_separator: {
50
+ ...string_agg['with_separator'],
51
+ isSymmetric: true,
52
+ impl: {
53
+ sql: 'ARRAY_JOIN(ARRAY_AGG(DISTINCT ${value} ${order_by:}), ${separator})',
54
+ },
55
+ },
56
+ };
57
+ exports.TRINO_DIALECT_FUNCTIONS = {
58
+ count_approx,
59
+ from_unixtime,
60
+ string_agg,
61
+ string_agg_distinct,
62
+ };
63
+ //# sourceMappingURL=dialect_functions.js.map
@@ -0,0 +1,2 @@
1
+ import { MalloyStandardFunctionImplementations as OverrideMap } from '../functions/malloy_standard_functions';
2
+ export declare const TRINO_MALLOY_STANDARD_OVERLOADS: OverrideMap;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.TRINO_MALLOY_STANDARD_OVERLOADS = void 0;
10
+ const util_1 = require("../functions/util");
11
+ exports.TRINO_MALLOY_STANDARD_OVERLOADS = {
12
+ byte_length: { sql: '(LENGTH(CAST(${value} AS VARBINARY)))' },
13
+ chr: { sql: "CASE WHEN ${value} = 0 THEN '' ELSE CHR(${value}) END" },
14
+ ascii: { sql: "CODEPOINT(NULLIF(CAST(${value} as VARCHAR(1)),''))" },
15
+ unicode: { sql: "CODEPOINT(NULLIF(CAST(${value} as VARCHAR(1)),''))" },
16
+ concat: {
17
+ variadic: {
18
+ // Using `expr` definition because each spread argument needs to be cast
19
+ expr: (0, util_1.sql) `CONCAT(${(0, util_1.spread)((0, util_1.arg)('values'), 'CAST(', 'AS VARCHAR)')})`,
20
+ },
21
+ },
22
+ div: { sql: 'FLOOR(${dividend} / ${divisor})' },
23
+ ifnull: { sql: 'COALESCE(${value}, ${default})' },
24
+ is_inf: { sql: 'COALESCE(IS_INFINITE(${value}), false)' },
25
+ // Trino has it but Presto doesn't
26
+ // TODO only apply this override for Presto, not Trino
27
+ log: { sql: '(LN(${value}) / LN(${base}))' },
28
+ repeat: {
29
+ sql: "ARRAY_JOIN(REPEAT(${value}, CASE WHEN ${value} IS NOT NULL THEN ${count} END),'')",
30
+ },
31
+ reverse: {
32
+ sql: 'REVERSE(CAST(${value} AS VARCHAR))',
33
+ },
34
+ starts_with: { sql: 'COALESCE(STARTS_WITH(${value}, ${prefix}), false)' },
35
+ ends_with: {
36
+ sql: 'COALESCE(STARTS_WITH(REVERSE(CAST(${value} AS VARCHAR)), REVERSE(CAST(${suffix} AS VARCHAR))), false)',
37
+ },
38
+ // Trunc function doesn't exist in Trino, so we emulate it.
39
+ // For both overloads, we switch between CEIL and FLOOR based on the sign of the arugment
40
+ // For the overload with precision, we multiply by a power of 10 before rounding, then divide.
41
+ trunc: {
42
+ to_integer: {
43
+ sql: 'CASE WHEN ${value} < 0 THEN CEIL(${value}) ELSE FLOOR(${value}) END',
44
+ },
45
+ to_precision: {
46
+ sql: 'CASE WHEN ${value} < 0 THEN CEIL(${value} * POW(10, ${precision})) / POW(10, ${precision}) ELSE FLOOR(${value} * POW(10, ${precision})) / POW(10, ${precision}) END',
47
+ },
48
+ },
49
+ };
50
+ //# sourceMappingURL=function_overrides.js.map
@@ -58,7 +58,9 @@ export declare class TrinoDialect extends PostgresBase {
58
58
  sqlSampleTable(tableSQL: string, sample: Sampling | undefined): string;
59
59
  sqlLiteralString(literal: string): string;
60
60
  sqlLiteralRegexp(literal: string): string;
61
- getGlobalFunctionDef(name: string): DialectFunctionOverloadDef[] | undefined;
61
+ getDialectFunctionOverrides(): {
62
+ [name: string]: DialectFunctionOverloadDef[];
63
+ };
62
64
  getDialectFunctions(): {
63
65
  [name: string]: DialectFunctionOverloadDef[];
64
66
  };
@@ -25,10 +25,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.PrestoDialect = exports.TrinoDialect = void 0;
26
26
  const utils_1 = require("../../model/utils");
27
27
  const malloy_types_1 = require("../../model/malloy_types");
28
- const functions_1 = require("./functions");
28
+ const functions_1 = require("../functions");
29
29
  const dialect_1 = require("../dialect");
30
30
  const pg_impl_1 = require("../pg_impl");
31
- const dialect_functions_1 = require("./functions/dialect_functions");
31
+ const dialect_functions_1 = require("./dialect_functions");
32
+ const function_overrides_1 = require("./function_overrides");
32
33
  // These are the units that "TIMESTAMP_ADD" "TIMESTAMP_DIFF" accept
33
34
  function timestampMeasureable(units) {
34
35
  return [
@@ -417,12 +418,11 @@ ${(0, utils_1.indent)(sql)}
417
418
  sqlLiteralRegexp(literal) {
418
419
  return "'" + literal.replace(/'/g, "''") + "'";
419
420
  }
420
- getGlobalFunctionDef(name) {
421
- // TODO: implement
422
- return functions_1.TRINO_FUNCTIONS.get(name);
421
+ getDialectFunctionOverrides() {
422
+ return (0, functions_1.expandOverrideMap)(function_overrides_1.TRINO_MALLOY_STANDARD_OVERLOADS);
423
423
  }
424
424
  getDialectFunctions() {
425
- return dialect_functions_1.TRINO_DIALECT_FUNCTIONS;
425
+ return (0, functions_1.expandBlueprintMap)(dialect_functions_1.TRINO_DIALECT_FUNCTIONS);
426
426
  }
427
427
  malloyTypeToSQLType(malloyType) {
428
428
  if (malloyType.type === 'number') {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export { DuckDBDialect, StandardSQLDialect, PostgresDialect, SnowflakeDialect, registerDialect, arg, qtz, overload, minScalar, anyExprType, minAggregate, maxScalar, sql, makeParam, param, params, literal, spread, Dialect, FUNCTIONS, } from './dialect';
2
- export type { DialectFieldList, DialectFunctionOverloadDef, QueryInfo, } from './dialect';
1
+ export { DuckDBDialect, StandardSQLDialect, PostgresDialect, SnowflakeDialect, registerDialect, arg, qtz, overload, minScalar, anyExprType, minAggregate, maxScalar, sql, makeParam, param, variadicParam, literal, spread, Dialect, } from './dialect';
2
+ export type { DialectFieldList, DialectFunctionOverloadDef, QueryInfo, MalloyStandardFunctionImplementations, DefinitionBlueprint, DefinitionBlueprintMap, OverloadedDefinitionBlueprint, } from './dialect';
3
3
  export type { QueryDataRow, StructDef, StructRelationship, NamedStructDefs, MalloyQueryData, AtomicFieldType as AtomicFieldTypeInner, DateUnit, ExtractUnit, TimestampUnit, TimeFieldType, QueryData, QueryValue, FieldTypeDef, Expr, FilterCondition, SQLBlock, FieldAtomicDef, FieldDef, PipeSegment, QueryFieldDef, IndexFieldDef, TurtleDef, SearchValueMapResult, SearchIndexResult, ModelDef, Query, QueryResult, QueryRunStats, NamedQuery, NamedModelObject, ExpressionType, FunctionDef, FunctionOverloadDef, FunctionParameterDef, ExpressionValueType, TypeDesc, FieldValueType, ExpressionTypeDesc, FunctionParamTypeDesc, DocumentLocation, DocumentRange, DocumentPosition, Sampling, Annotation, FieldAtomicTypeDef, SQLBlockStructDef, } from './model';
4
4
  export { Segment, isSamplingEnable, isSamplingPercent, isSamplingRows, expressionIsCalculation, indent, composeSQLExpr, } from './model';
5
5
  export { MalloyTranslator, } from './lang';
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Explore = exports.DataWriter = exports.Parse = exports.JSONWriter = exports.CSVWriter = exports.QueryMaterializer = exports.Result = exports.PreparedResult = exports.TimestampTimeframe = exports.DateTimeframe = exports.SourceRelationship = exports.JoinRelationship = exports.MalloyError = exports.FixedConnectionMap = exports.InMemoryURLReader = exports.EmptyURLReader = exports.SingleConnectionRuntime = exports.ConnectionRuntime = exports.AtomicFieldType = exports.Runtime = exports.Malloy = exports.Model = exports.MalloyTranslator = exports.composeSQLExpr = exports.indent = exports.expressionIsCalculation = exports.isSamplingRows = exports.isSamplingPercent = exports.isSamplingEnable = exports.Segment = exports.FUNCTIONS = exports.Dialect = exports.spread = exports.literal = exports.params = exports.param = exports.makeParam = exports.sql = exports.maxScalar = exports.minAggregate = exports.anyExprType = exports.minScalar = exports.overload = exports.qtz = exports.arg = exports.registerDialect = exports.SnowflakeDialect = exports.PostgresDialect = exports.StandardSQLDialect = exports.DuckDBDialect = void 0;
4
- exports.Tag = exports.toAsyncGenerator = void 0;
3
+ exports.toAsyncGenerator = exports.Explore = exports.DataWriter = exports.Parse = exports.JSONWriter = exports.CSVWriter = exports.QueryMaterializer = exports.Result = exports.PreparedResult = exports.TimestampTimeframe = exports.DateTimeframe = exports.SourceRelationship = exports.JoinRelationship = exports.MalloyError = exports.FixedConnectionMap = exports.InMemoryURLReader = exports.EmptyURLReader = exports.SingleConnectionRuntime = exports.ConnectionRuntime = exports.AtomicFieldType = exports.Runtime = exports.Malloy = exports.Model = exports.MalloyTranslator = exports.composeSQLExpr = exports.indent = exports.expressionIsCalculation = exports.isSamplingRows = exports.isSamplingPercent = exports.isSamplingEnable = exports.Segment = exports.Dialect = exports.spread = exports.literal = exports.variadicParam = exports.param = exports.makeParam = exports.sql = exports.maxScalar = exports.minAggregate = exports.anyExprType = exports.minScalar = exports.overload = exports.qtz = exports.arg = exports.registerDialect = exports.SnowflakeDialect = exports.PostgresDialect = exports.StandardSQLDialect = exports.DuckDBDialect = void 0;
4
+ exports.Tag = void 0;
5
5
  /*
6
6
  * Copyright 2023 Google LLC
7
7
  *
@@ -40,11 +40,10 @@ Object.defineProperty(exports, "maxScalar", { enumerable: true, get: function ()
40
40
  Object.defineProperty(exports, "sql", { enumerable: true, get: function () { return dialect_1.sql; } });
41
41
  Object.defineProperty(exports, "makeParam", { enumerable: true, get: function () { return dialect_1.makeParam; } });
42
42
  Object.defineProperty(exports, "param", { enumerable: true, get: function () { return dialect_1.param; } });
43
- Object.defineProperty(exports, "params", { enumerable: true, get: function () { return dialect_1.params; } });
43
+ Object.defineProperty(exports, "variadicParam", { enumerable: true, get: function () { return dialect_1.variadicParam; } });
44
44
  Object.defineProperty(exports, "literal", { enumerable: true, get: function () { return dialect_1.literal; } });
45
45
  Object.defineProperty(exports, "spread", { enumerable: true, get: function () { return dialect_1.spread; } });
46
46
  Object.defineProperty(exports, "Dialect", { enumerable: true, get: function () { return dialect_1.Dialect; } });
47
- Object.defineProperty(exports, "FUNCTIONS", { enumerable: true, get: function () { return dialect_1.FUNCTIONS; } });
48
47
  var model_1 = require("./model");
49
48
  // Used in Composer Demo
50
49
  Object.defineProperty(exports, "Segment", { enumerable: true, get: function () { return model_1.Segment; } });
@@ -196,39 +196,33 @@ class ExprFunc extends expression_def_1.ExpressionDef {
196
196
  structPath,
197
197
  };
198
198
  let funcCall = frag;
199
- const dialectOverload = dialect ? overload.dialect[dialect] : undefined;
200
199
  // TODO add in an error if you use an asymmetric function in BQ
201
200
  // and the function uses joins
202
201
  // TODO add in an error if you use an illegal join pattern
203
- if (dialectOverload === undefined) {
204
- this.log(`Function ${this.name} is not defined in dialect ${dialect}`);
205
- }
206
- else {
207
- if ((props === null || props === void 0 ? void 0 : props.orderBys) && props.orderBys.length > 0) {
208
- const isAnalytic = (0, malloy_types_1.expressionIsAnalytic)(overload.returnType.expressionType);
209
- if (!isAnalytic) {
210
- if (!this.inExperiment('aggregate_order_by', true)) {
211
- props.orderBys[0].log('Enable experiment `aggregate_order_by` to use `order_by` with an aggregate function');
212
- }
213
- }
214
- if (dialectOverload.supportsOrderBy || isAnalytic) {
215
- const allowExpression = dialectOverload.supportsOrderBy !== 'only_default';
216
- const allObs = props.orderBys.flatMap(orderBy => isAnalytic
217
- ? orderBy.getAnalyticOrderBy(fs)
218
- : orderBy.getAggregateOrderBy(fs, allowExpression));
219
- frag.kids.orderBy = allObs;
220
- }
221
- else {
222
- props.orderBys[0].log(`Function \`${this.name}\` does not support \`order_by\``);
202
+ if ((props === null || props === void 0 ? void 0 : props.orderBys) && props.orderBys.length > 0) {
203
+ const isAnalytic = (0, malloy_types_1.expressionIsAnalytic)(overload.returnType.expressionType);
204
+ if (!isAnalytic) {
205
+ if (!this.inExperiment('aggregate_order_by', true)) {
206
+ props.orderBys[0].log('Enable experiment `aggregate_order_by` to use `order_by` with an aggregate function');
223
207
  }
224
208
  }
225
- if ((props === null || props === void 0 ? void 0 : props.limit) !== undefined) {
226
- if (dialectOverload.supportsLimit) {
227
- frag.limit = props.limit.limit;
228
- }
229
- else {
230
- this.log(`Function ${this.name} does not support limit`);
231
- }
209
+ if (overload.supportsOrderBy || isAnalytic) {
210
+ const allowExpression = overload.supportsOrderBy !== 'only_default';
211
+ const allObs = props.orderBys.flatMap(orderBy => isAnalytic
212
+ ? orderBy.getAnalyticOrderBy(fs)
213
+ : orderBy.getAggregateOrderBy(fs, allowExpression));
214
+ frag.kids.orderBy = allObs;
215
+ }
216
+ else {
217
+ props.orderBys[0].log(`Function \`${this.name}\` does not support \`order_by\``);
218
+ }
219
+ }
220
+ if ((props === null || props === void 0 ? void 0 : props.limit) !== undefined) {
221
+ if (overload.supportsLimit) {
222
+ frag.limit = props.limit.limit;
223
+ }
224
+ else {
225
+ this.log(`Function ${this.name} does not support limit`);
232
226
  }
233
227
  }
234
228
  if ((props === null || props === void 0 ? void 0 : props.partitionBys) && props.partitionBys.length > 0) {
@@ -377,7 +371,9 @@ function findOverload(func, args) {
377
371
  (paramT.evalSpace === 'constant' &&
378
372
  (arg.evalSpace === 'input' || arg.evalSpace === 'output')) ||
379
373
  // Error if output is required but arg is input
380
- (paramT.evalSpace === 'output' && arg.evalSpace === 'input')) {
374
+ // TODO: Assumption that calculations cannot take input things
375
+ ((0, malloy_types_1.expressionIsAnalytic)(overload.returnType.expressionType) &&
376
+ arg.evalSpace === 'input')) {
381
377
  evalSpaceErrors.push({
382
378
  argIndex,
383
379
  param,
@@ -23,17 +23,17 @@ class DialectNameSpace {
23
23
  return {
24
24
  returnType: overload.returnType,
25
25
  params: overload.params,
26
+ supportsOrderBy: overload.supportsOrderBy,
27
+ supportsLimit: overload.supportsLimit,
28
+ isSymmetric: overload.isSymmetric,
26
29
  dialect: {
27
30
  [dialect.name]: {
28
31
  e: overload.e,
29
- supportsOrderBy: overload.supportsOrderBy,
30
32
  defaultOrderByArgIndex: overload.defaultOrderByArgIndex,
31
- supportsLimit: overload.supportsLimit,
33
+ between: overload.between,
34
+ needsWindowOrderBy: overload.needsWindowOrderBy,
32
35
  },
33
36
  },
34
- needsWindowOrderBy: overload.needsWindowOrderBy,
35
- between: overload.between,
36
- isSymmetric: overload.isSymmetric,
37
37
  };
38
38
  }),
39
39
  });
@@ -1,12 +1,14 @@
1
+ import { FunctionDef } from '../../../model';
1
2
  import { ModelEntry } from './model-entry';
2
3
  import { NameSpace } from './name-space';
3
4
  /**
4
- * We may want to remove this in the future...
5
- *
6
5
  * This is a global namespace which exists in the root of all Documents
7
6
  * and includes SQL function definitions.
8
7
  */
9
8
  export declare class GlobalNameSpace implements NameSpace {
9
+ entries: Map<string, FunctionDef>;
10
+ constructor();
10
11
  getEntry(name: string): ModelEntry | undefined;
11
12
  setEntry(_name: string, _value: ModelEntry, _exported: boolean): void;
12
13
  }
14
+ export declare function getDialectFunctions(): Map<string, FunctionDef>;
@@ -22,19 +22,19 @@
22
22
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
  */
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.GlobalNameSpace = void 0;
25
+ exports.getDialectFunctions = exports.GlobalNameSpace = void 0;
26
+ const dialect_1 = require("../../../dialect");
26
27
  const dialect_map_1 = require("../../../dialect/dialect_map");
27
28
  /**
28
- * We may want to remove this in the future...
29
- *
30
29
  * This is a global namespace which exists in the root of all Documents
31
30
  * and includes SQL function definitions.
32
31
  */
33
32
  class GlobalNameSpace {
33
+ constructor() {
34
+ this.entries = getDialectFunctions();
35
+ }
34
36
  getEntry(name) {
35
- // TODO cache this or precompute this so we're not comparing/merging dialect
36
- // overloads on every usage
37
- const func = (0, dialect_map_1.getDialectFunction)(name);
37
+ const func = this.entries.get(name);
38
38
  if (func === undefined) {
39
39
  return undefined;
40
40
  }
@@ -48,4 +48,57 @@ class GlobalNameSpace {
48
48
  }
49
49
  }
50
50
  exports.GlobalNameSpace = GlobalNameSpace;
51
+ function paramsEqual(a, b) {
52
+ return (a.params.length === b.params.length &&
53
+ a.params.every((param, i) => {
54
+ const otherParam = b.params[i];
55
+ return (param.isVariadic === otherParam.isVariadic &&
56
+ param.name === otherParam.name &&
57
+ param.allowedTypes.length === otherParam.allowedTypes.length &&
58
+ param.allowedTypes.every(t => otherParam.allowedTypes.some(ot => t.dataType === ot.dataType &&
59
+ t.expressionType === ot.expressionType)));
60
+ }));
61
+ }
62
+ function getDialectFunctions() {
63
+ var _a, _b;
64
+ const baseImplementations = (0, dialect_1.getMalloyStandardFunctions)();
65
+ const dialectOverrides = {};
66
+ const dialects = (0, dialect_map_1.getDialects)();
67
+ for (const dialect of dialects) {
68
+ dialectOverrides[dialect.name] = dialect.getDialectFunctionOverrides();
69
+ }
70
+ const functions = new Map();
71
+ for (const name in baseImplementations) {
72
+ const baseOverloads = baseImplementations[name];
73
+ const func = {
74
+ type: 'function',
75
+ name,
76
+ overloads: [],
77
+ };
78
+ for (const baseOverload of baseOverloads) {
79
+ const overload = {
80
+ returnType: baseOverload.returnType,
81
+ params: baseOverload.params,
82
+ dialect: {},
83
+ supportsOrderBy: baseOverload.supportsOrderBy,
84
+ supportsLimit: baseOverload.supportsLimit,
85
+ isSymmetric: baseOverload.isSymmetric,
86
+ };
87
+ for (const dialect of dialects) {
88
+ const overloads = (_a = dialectOverrides[dialect.name][name]) !== null && _a !== void 0 ? _a : [];
89
+ const dialectOverload = (_b = overloads.find(o => paramsEqual(o, overload))) !== null && _b !== void 0 ? _b : baseOverload;
90
+ overload.dialect[dialect.name] = {
91
+ e: dialectOverload.e,
92
+ needsWindowOrderBy: dialectOverload.needsWindowOrderBy,
93
+ between: dialectOverload.between,
94
+ defaultOrderByArgIndex: dialectOverload.defaultOrderByArgIndex,
95
+ };
96
+ }
97
+ func.overloads.push(overload);
98
+ }
99
+ functions.set(name, func);
100
+ }
101
+ return functions;
102
+ }
103
+ exports.getDialectFunctions = getDialectFunctions;
51
104
  //# sourceMappingURL=global-name-space.js.map
@@ -87,7 +87,7 @@ declare class QueryField extends QueryNode {
87
87
  generateCountFragment(resultSet: FieldInstanceResult, context: QueryStruct, expr: AggregateExpr, state: GenerateState): string;
88
88
  generateSourceReference(resultSet: FieldInstanceResult, context: QueryStruct, expr: SourceReferenceNode): string;
89
89
  getAnalyticPartitions(resultStruct: FieldInstanceResult, extraPartitionFields?: string[]): string[];
90
- generateAnalyticFragment(resultStruct: FieldInstanceResult, context: QueryStruct, expr: Expr, overload: FunctionOverloadDef, state: GenerateState, args: Expr[], partitionByFields?: string[], funcOrdering?: string): string;
90
+ generateAnalyticFragment(dialect: string, resultStruct: FieldInstanceResult, context: QueryStruct, expr: Expr, overload: FunctionOverloadDef, state: GenerateState, args: Expr[], partitionByFields?: string[], funcOrdering?: string): string;
91
91
  generatePickSQL(pf: PickExpr): string;
92
92
  exprToSQL(resultSet: FieldInstanceResult, context: QueryStruct, exprToTranslate: Expr, state?: GenerateState): string;
93
93
  getExpr(): Expr;
@@ -445,7 +445,7 @@ class QueryField extends QueryNode {
445
445
  const extraPartitions = ((_c = frag.partitionBy) !== null && _c !== void 0 ? _c : []).map(outputName => {
446
446
  return `(${resultSet.getField(outputName).getAnalyticalSQL(false)})`;
447
447
  });
448
- return this.generateAnalyticFragment(resultSet, context, funcCall, overload, state, args, extraPartitions, orderBySql);
448
+ return this.generateAnalyticFragment(context.dialect.name, resultSet, context, funcCall, overload, state, args, extraPartitions, orderBySql);
449
449
  }
450
450
  return this.exprToSQL(resultSet, context, funcCall, state);
451
451
  }
@@ -614,7 +614,7 @@ class QueryField extends QueryNode {
614
614
  }
615
615
  return ret;
616
616
  }
617
- generateAnalyticFragment(resultStruct, context, expr, overload, state, args, partitionByFields, funcOrdering) {
617
+ generateAnalyticFragment(dialect, resultStruct, context, expr, overload, state, args, partitionByFields, funcOrdering) {
618
618
  const isComplex = resultStruct.root().isComplexQuery;
619
619
  const partitionFields = this.getAnalyticPartitions(resultStruct, partitionByFields);
620
620
  const allPartitions = [
@@ -625,7 +625,8 @@ class QueryField extends QueryNode {
625
625
  ? `PARTITION BY ${allPartitions.join(', ')}`
626
626
  : '';
627
627
  let orderBy = funcOrdering !== null && funcOrdering !== void 0 ? funcOrdering : '';
628
- if (!funcOrdering && overload.needsWindowOrderBy) {
628
+ const dialectOverload = overload.dialect[dialect];
629
+ if (!funcOrdering && dialectOverload.needsWindowOrderBy) {
629
630
  // calculate the ordering.
630
631
  const obSQL = [];
631
632
  let orderingField;
@@ -662,10 +663,10 @@ class QueryField extends QueryNode {
662
663
  }
663
664
  }
664
665
  let between = '';
665
- if (overload.between) {
666
+ if (dialectOverload.between) {
666
667
  const [preceding, following] = [
667
- overload.between.preceding,
668
- overload.between.following,
668
+ dialectOverload.between.preceding,
669
+ dialectOverload.between.following,
669
670
  ].map(value => {
670
671
  if (value === -1) {
671
672
  return 'UNBOUNDED';