@malloydata/malloy 0.0.1 → 0.0.3

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 (56) hide show
  1. package/README.md +12 -11
  2. package/dist/dialect/dialect.d.ts +1 -1
  3. package/dist/dialect/dialect.js +13 -0
  4. package/dist/dialect/duckdb.d.ts +1 -1
  5. package/dist/dialect/duckdb.js +35 -37
  6. package/dist/dialect/postgres.d.ts +1 -1
  7. package/dist/dialect/postgres.js +15 -8
  8. package/dist/dialect/standardsql.js +1 -0
  9. package/dist/index.d.ts +4 -4
  10. package/dist/index.js +2 -2
  11. package/dist/lang/ast/apply-expr.d.ts +2 -2
  12. package/dist/lang/ast/apply-expr.js +19 -3
  13. package/dist/lang/ast/ast-expr.d.ts +37 -37
  14. package/dist/lang/ast/ast-expr.js +24 -39
  15. package/dist/lang/ast/ast-main.d.ts +14 -14
  16. package/dist/lang/ast/ast-main.js +36 -45
  17. package/dist/lang/ast/ast-time-expr.d.ts +19 -19
  18. package/dist/lang/ast/ast-time-expr.js +6 -7
  19. package/dist/lang/ast/time-utils.d.ts +1 -0
  20. package/dist/lang/ast/time-utils.js +12 -1
  21. package/dist/lang/field-space.d.ts +43 -41
  22. package/dist/lang/field-space.js +115 -135
  23. package/dist/lang/lib/Malloy/MalloyLexer.d.ts +108 -106
  24. package/dist/lang/lib/Malloy/MalloyLexer.js +1019 -1006
  25. package/dist/lang/lib/Malloy/MalloyParser.d.ts +581 -590
  26. package/dist/lang/lib/Malloy/MalloyParser.js +1141 -1202
  27. package/dist/lang/lib/Malloy/MalloyParserListener.d.ts +2015 -0
  28. package/dist/lang/lib/Malloy/MalloyParserListener.js +4 -0
  29. package/dist/lang/lib/Malloy/MalloyParserVisitor.d.ts +1269 -0
  30. package/dist/lang/lib/Malloy/MalloyParserVisitor.js +4 -0
  31. package/dist/lang/parse-to-ast.d.ts +3 -4
  32. package/dist/lang/parse-to-ast.js +1 -1
  33. package/dist/lang/parse-tree-walkers/document-completion-walker.js +2 -0
  34. package/dist/lang/parse-tree-walkers/explore-query-walker.d.ts +2 -2
  35. package/dist/lang/space-field.d.ts +10 -7
  36. package/dist/lang/space-field.js +11 -4
  37. package/dist/lang/test/field-symbols.spec.js +2 -2
  38. package/dist/lang/test/parse.spec.js +16 -1
  39. package/dist/lang/test/test-translator.js +1 -1
  40. package/dist/malloy.d.ts +1 -1
  41. package/dist/malloy.js +11 -5
  42. package/dist/md5.d.ts +1 -0
  43. package/dist/md5.js +30 -0
  44. package/dist/model/malloy_query.js +10 -6
  45. package/dist/model/malloy_types.d.ts +15 -2
  46. package/package.json +9 -13
  47. package/dist/dialect/dialect-map.d.ts +0 -3
  48. package/dist/dialect/dialect-map.js +0 -33
  49. package/dist/model/malloy-query.d.ts +0 -313
  50. package/dist/model/malloy-query.js +0 -2603
  51. package/dist/model/malloy-types.d.ts +0 -404
  52. package/dist/model/malloy-types.js +0 -242
  53. package/dist/model/sql-block.d.ts +0 -11
  54. package/dist/model/sql-block.js +0 -38
  55. package/dist/runtime-types.d.ts +0 -116
  56. package/dist/runtime-types.js +0 -40
package/README.md CHANGED
@@ -9,17 +9,16 @@ This package facilitates building the Malloy language - or the usage of data mod
9
9
  ## Show me an example!
10
10
 
11
11
  ```
12
- import { Runtime } from "@malloydata/malloy";
13
- import { DuckDBConnection } from "@malloydata/db-duckdb";
14
-
15
- const connection = new DuckDBConnection("duckdb-example");
16
- const runtime = new Runtime(files, connection);
17
-
18
- runtime.loadModel("source: flights is table('duckdb:data/flights.parquet')")
19
-
20
- const runner = runtime.loadQuery("query: flights->{group_by: flight_num}")
21
- runner.run() // <- executes query, returns JSON of results
22
-
12
+ const malloy = require("@malloydata/malloy")
13
+ const bigquery = require("@malloydata/db-bigquery")
14
+
15
+ const connection = new bigquery.BigQueryConnection("bigquery");
16
+ const runtime = new malloy.SingleConnectionRuntime(connection);
17
+ const model = runtime.loadModel("source: airports is table('malloytest.airports')")
18
+ const runner = model.loadQuery("query: airports->{aggregate: airport_count is count()}")
19
+ runner.run().then((result) => {
20
+ console.log(result.data.value) // [ { airport_count: 19793 } ]
21
+ })
23
22
  ```
24
23
 
25
24
  Note: These APIs are still in beta and subject to change.
@@ -37,3 +36,5 @@ In practice, much of this loop is handled by various database plugins:
37
36
  ## Do you have any examples?
38
37
 
39
38
  You can find a (very) simple example of writing a CLI for executing Malloy queries [here](https://github.com/looker-open-source/malloy/tree/main/demo/malloy-demo-bq-cli)
39
+
40
+ A more realistic and complex use case - we use these libraries to power our VSCode Extension. Some examples can be found [here](https://github.com/looker-open-source/malloy/tree/main/vscode-extension/src/extension/commands)
@@ -1,5 +1,5 @@
1
1
  import { AtomicFieldTypeInner, TimeFieldType, TimestampUnit, ExtractUnit, DialectFragment, TimeValue } from "..";
2
- import { Expr, Sampling, StructDef, TypecastFragment } from "../model";
2
+ import { Expr, Sampling, StructDef, TypecastFragment } from "../model/malloy_types";
3
3
  interface DialectField {
4
4
  type: string;
5
5
  sqlExpression: string;
@@ -13,6 +13,11 @@
13
13
  */
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.Dialect = void 0;
16
+ // Can't get these from "../model" because model includes this file
17
+ // and that can create a circular reference problem. This is a patch
18
+ // and really indicates a problem in the relationship between
19
+ // dialect and model, it's going to come up again some time.
20
+ const malloy_types_1 = require("../model/malloy_types");
16
21
  class Dialect {
17
22
  sqlFinalStage(_lastStageName, _fields) {
18
23
  throw new Error("Dialect has no final Stage but called Anyway");
@@ -45,6 +50,14 @@ class Dialect {
45
50
  return this.sqlCast(df);
46
51
  case "regexpMatch":
47
52
  return this.sqlRegexpMatch(df.expr, df.regexp);
53
+ case "div": {
54
+ if (this.divisionIsInteger) {
55
+ return (0, malloy_types_1.mkExpr) `${df.numerator}*1.0/${df.denominator}`;
56
+ }
57
+ return (0, malloy_types_1.mkExpr) `${df.numerator}/${df.denominator}`;
58
+ }
59
+ case "timeLiteral":
60
+ return [this.sqlLiteralTime(df.literal, df.literalType, df.timezone)];
48
61
  }
49
62
  }
50
63
  sqlSumDistinct(_key, _value) {
@@ -34,7 +34,7 @@ export declare class DuckDBDialect extends Dialect {
34
34
  sqlSelectAliasAsStruct(alias: string, physicalFieldNames: string[]): string;
35
35
  sqlMaybeQuoteIdentifier(identifier: string): string;
36
36
  sqlCreateTableAsSelect(_tableName: string, _sql: string): string;
37
- getFunctionInfo(_functionName: string): FunctionInfo | undefined;
37
+ getFunctionInfo(functionName: string): FunctionInfo | undefined;
38
38
  sqlMeasureTime(from: TimeValue, to: TimeValue, units: string): Expr;
39
39
  sqlNow(): Expr;
40
40
  sqlTrunc(sqlTime: TimeValue, units: TimestampUnit): Expr;
@@ -109,8 +109,6 @@ const inSeconds = {
109
109
  second: 1,
110
110
  minute: 60,
111
111
  hour: 3600,
112
- day: 86400,
113
- week: 604800,
114
112
  };
115
113
  class DuckDBDialect extends dialect_1.Dialect {
116
114
  constructor() {
@@ -125,7 +123,9 @@ class DuckDBDialect extends dialect_1.Dialect {
125
123
  this.defaultSampling = { rows: 50000 };
126
124
  this.supportUnnestArrayAgg = true;
127
125
  this.supportsCTEinCoorelatedSubQueries = true;
128
- this.functionInfo = {};
126
+ this.functionInfo = {
127
+ concat: { returnType: "string" },
128
+ };
129
129
  }
130
130
  // hack until they support temporary macros.
131
131
  get udfPrefix() {
@@ -230,8 +230,8 @@ class DuckDBDialect extends dialect_1.Dialect {
230
230
  sqlCreateTableAsSelect(_tableName, _sql) {
231
231
  throw new Error("Not implemented Yet");
232
232
  }
233
- getFunctionInfo(_functionName) {
234
- return undefined;
233
+ getFunctionInfo(functionName) {
234
+ return this.functionInfo[functionName];
235
235
  }
236
236
  sqlMeasureTime(from, to, units) {
237
237
  let lVal = from.value;
@@ -242,21 +242,20 @@ class DuckDBDialect extends dialect_1.Dialect {
242
242
  const duration = (0, model_1.mkExpr) `(${rVal} - ${lVal})`;
243
243
  return units == "second"
244
244
  ? duration
245
- : (0, model_1.mkExpr) `TRUNC(${duration}/${inSeconds[units].toString()})`;
245
+ : (0, model_1.mkExpr) `FLOOR(${duration}/${inSeconds[units].toString()})`;
246
246
  }
247
- const yearDiff = (0, model_1.mkExpr) `TRUNC(EXTRACT(YEAR FROM ${rVal}) - EXTRACT(YEAR FROM ${lVal}))`;
248
- if (units == "year") {
249
- return yearDiff;
247
+ if (from.valueType != "date") {
248
+ lVal = (0, model_1.mkExpr) `CAST((${lVal}) AS DATE)`;
250
249
  }
251
- if (units == "month") {
252
- const monthDiff = (0, model_1.mkExpr) `TRUNC(EXTRACT(MONTH FROM ${rVal}) - EXTRACT(MONTH FROM ${lVal}))`;
253
- return (0, model_1.mkExpr) `${yearDiff} * 12 + ${monthDiff}`;
250
+ if (to.valueType != "date") {
251
+ rVal = (0, model_1.mkExpr) `CAST((${rVal}) AS DATE)`;
254
252
  }
255
- if (units == "quarter") {
256
- const qDiff = (0, model_1.mkExpr) `TRUNC(EXTRACT(QUARTER FROM ${rVal}) - EXTRACT(QUARTER FROM ${lVal}))`;
257
- return (0, model_1.mkExpr) `${yearDiff} * 4 + ${qDiff}`;
253
+ if (units == "week") {
254
+ // DuckDB's weeks start on Monday, but Malloy's weeks start on Sunday
255
+ lVal = (0, model_1.mkExpr) `(${lVal} + INTERVAL 1 DAY)`;
256
+ rVal = (0, model_1.mkExpr) `(${rVal} + INTERVAL 1 DAY)`;
258
257
  }
259
- throw new Error(`Unknown or unhandled postgres time unit: ${units}`);
258
+ return (0, model_1.mkExpr) `DATE_DIFF('${units}', ${lVal}, ${rVal})`;
260
259
  }
261
260
  sqlNow() {
262
261
  return (0, model_1.mkExpr) `CURRENT_TIMESTAMP`;
@@ -265,10 +264,10 @@ class DuckDBDialect extends dialect_1.Dialect {
265
264
  // adjusting for monday/sunday weeks
266
265
  const week = units == "week";
267
266
  const truncThis = week
268
- ? (0, model_1.mkExpr) `${sqlTime.value}+interval'1'day`
267
+ ? (0, model_1.mkExpr) `${sqlTime.value} + INTERVAL 1 DAY`
269
268
  : sqlTime.value;
270
269
  const trunced = (0, model_1.mkExpr) `DATE_TRUNC('${units}', ${truncThis})`;
271
- return week ? (0, model_1.mkExpr) `(${trunced}-interval'1'day)` : trunced;
270
+ return week ? (0, model_1.mkExpr) `(${trunced} - INTERVAL 1 DAY)` : trunced;
272
271
  }
273
272
  sqlExtract(from, units) {
274
273
  const pgUnits = pgExtractionMap[units] || units;
@@ -284,8 +283,8 @@ class DuckDBDialect extends dialect_1.Dialect {
284
283
  timeframe = "day";
285
284
  n = (0, model_1.mkExpr) `${n}*7`;
286
285
  }
287
- const interval = (0, model_1.mkExpr) `INTERVAL ${n} ${timeframe}`;
288
- return (0, model_1.mkExpr) `((${expr.value})${op}${interval})`;
286
+ const interval = (0, model_1.mkExpr) `INTERVAL (${n}) ${timeframe}`;
287
+ return (0, model_1.mkExpr) `((${expr.value})) ${op} ${interval}`;
289
288
  }
290
289
  sqlCast(cast) {
291
290
  if (cast.dstType !== cast.srcType) {
@@ -299,7 +298,7 @@ class DuckDBDialect extends dialect_1.Dialect {
299
298
  }
300
299
  sqlLiteralTime(timeString, type, _timezone) {
301
300
  if (type == "date") {
302
- return `DATE('${timeString}')`;
301
+ return `DATE '${timeString}'`;
303
302
  }
304
303
  else if (type == "timestamp") {
305
304
  return `TIMESTAMP '${timeString}'`;
@@ -308,24 +307,23 @@ class DuckDBDialect extends dialect_1.Dialect {
308
307
  throw new Error(`Unknown Literal time format ${type}`);
309
308
  }
310
309
  }
311
- // sqlSumDistinct(key: string, value: string): string {
312
- // // return `sum_distinct(list({key:${key}, val: ${value}}))`;
313
- // return `(
314
- // fail -- force the query for fail until the bug is fixed.
315
- // SELECT sum(a.val) as value
316
- // FROM (
317
- // SELECT UNNEST(list(distinct {key:${key}, val: ${value}})) a
318
- // )
319
- // )`;
320
- // }
321
310
  sqlSumDistinct(key, value) {
322
- const _factor = 32;
323
- const precision = 0.000001;
324
- const keySQL = `md5_number_lower(${key}::varchar)::int128`;
325
- return `
326
- (SUM(DISTINCT ${keySQL} + FLOOR(IFNULL(${value},0)/${precision})::int128) - SUM(DISTINCT ${keySQL}))*${precision}
327
- `;
311
+ // return `sum_distinct(list({key:${key}, val: ${value}}))`;
312
+ return `(
313
+ SELECT sum(a.val) as value
314
+ FROM (
315
+ SELECT UNNEST(list(distinct {key:${key}, val: ${value}})) a
316
+ )
317
+ )`;
328
318
  }
319
+ // sqlSumDistinct(key: string, value: string): string {
320
+ // const _factor = 32;
321
+ // const precision = 0.000001;
322
+ // const keySQL = `md5_number_lower(${key}::varchar)::int128`;
323
+ // return `
324
+ // (SUM(DISTINCT ${keySQL} + FLOOR(IFNULL(${value},0)/${precision})::int128) - SUM(DISTINCT ${keySQL}))*${precision}
325
+ // `;
326
+ // }
329
327
  // default duckdb to sampling 50K rows.
330
328
  sqlSampleTable(tableSQL, sample) {
331
329
  if (sample !== undefined) {
@@ -41,7 +41,7 @@ export declare class PostgresDialect extends Dialect {
41
41
  sqlCast(cast: TypecastFragment): Expr;
42
42
  sqlRegexpMatch(expr: Expr, regexp: string): Expr;
43
43
  sqlLiteralTime(timeString: string, type: TimeFieldType, _timezone: string): string;
44
- getFunctionInfo(_functionName: string): FunctionInfo | undefined;
44
+ getFunctionInfo(functionName: string): FunctionInfo | undefined;
45
45
  sqlMeasureTime(from: TimeValue, to: TimeValue, units: string): Expr;
46
46
  sqlSumDistinct(key: string, value: string): string;
47
47
  sqlSampleTable(tableSQL: string, sample: Sampling | undefined): string;
@@ -37,8 +37,6 @@ const inSeconds = {
37
37
  second: 1,
38
38
  minute: 60,
39
39
  hour: 3600,
40
- day: 86400,
41
- week: 604800,
42
40
  };
43
41
  class PostgresDialect extends dialect_1.Dialect {
44
42
  constructor() {
@@ -54,7 +52,9 @@ class PostgresDialect extends dialect_1.Dialect {
54
52
  this.defaultSampling = { rows: 50000 };
55
53
  this.supportUnnestArrayAgg = true;
56
54
  this.supportsCTEinCoorelatedSubQueries = true;
57
- this.functionInfo = {};
55
+ this.functionInfo = {
56
+ concat: { returnType: "string" },
57
+ };
58
58
  }
59
59
  quoteTablePath(tablePath) {
60
60
  return tablePath
@@ -249,8 +249,8 @@ class PostgresDialect extends dialect_1.Dialect {
249
249
  throw new Error(`Unknown Literal time format ${type}`);
250
250
  }
251
251
  }
252
- getFunctionInfo(_functionName) {
253
- return undefined;
252
+ getFunctionInfo(functionName) {
253
+ return this.functionInfo[functionName];
254
254
  }
255
255
  sqlMeasureTime(from, to, units) {
256
256
  let lVal = from.value;
@@ -263,16 +263,23 @@ class PostgresDialect extends dialect_1.Dialect {
263
263
  ? duration
264
264
  : (0, model_1.mkExpr) `TRUNC(${duration}/${inSeconds[units].toString()})`;
265
265
  }
266
- const yearDiff = (0, model_1.mkExpr) `TRUNC(EXTRACT(YEAR FROM ${rVal}) - EXTRACT(YEAR FROM ${lVal}))`;
266
+ if (units === "day") {
267
+ return (0, model_1.mkExpr) `${rVal}::date - ${lVal}::date`;
268
+ }
269
+ const yearDiff = (0, model_1.mkExpr) `(DATE_PART('year', ${rVal}) - DATE_PART('year', ${lVal}))`;
267
270
  if (units == "year") {
268
271
  return yearDiff;
269
272
  }
273
+ if (units == "week") {
274
+ const dayDiffForWeekStart = (0, model_1.mkExpr) `(DATE_TRUNC('week', ${rVal} + '1 day'::interval)::date - DATE_TRUNC('week', ${lVal} + '1 day'::interval)::date)`;
275
+ return (0, model_1.mkExpr) `${dayDiffForWeekStart} / 7`;
276
+ }
270
277
  if (units == "month") {
271
- const monthDiff = (0, model_1.mkExpr) `TRUNC(EXTRACT(MONTH FROM ${rVal}) - EXTRACT(MONTH FROM ${lVal}))`;
278
+ const monthDiff = (0, model_1.mkExpr) `DATE_PART('month', ${rVal}) - DATE_PART('month', ${lVal})`;
272
279
  return (0, model_1.mkExpr) `${yearDiff} * 12 + ${monthDiff}`;
273
280
  }
274
281
  if (units == "quarter") {
275
- const qDiff = (0, model_1.mkExpr) `TRUNC(EXTRACT(QUARTER FROM ${rVal}) - EXTRACT(QUARTER FROM ${lVal}))`;
282
+ const qDiff = (0, model_1.mkExpr) `DATE_PART('quarter', ${rVal}) - DATE_PART('quarter', ${lVal})`;
276
283
  return (0, model_1.mkExpr) `${yearDiff} * 4 + ${qDiff}`;
277
284
  }
278
285
  throw new Error(`Unknown or unhandled postgres time unit: ${units}`);
@@ -48,6 +48,7 @@ class StandardSQLDialect extends dialect_1.Dialect {
48
48
  this.supportsCTEinCoorelatedSubQueries = false;
49
49
  this.functionInfo = {
50
50
  timestamp_seconds: { returnType: "timestamp" },
51
+ concat: { returnType: "string" },
51
52
  };
52
53
  this.keywords = `
53
54
  ALL
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- export type { QueryDataRow, ModelDef, Fragment, Query, StructDef, NamedStructDefs, MalloyQueryData, AtomicFieldType as AtomicFieldTypeInner, DateUnit, ExtractUnit, TimestampUnit, TimeFieldType, QueryData, FieldTypeDef, Expr, DialectFragment, TimeValue, FilterExpression, SQLBlock, FieldDef, PipeSegment, QueryFieldDef, TurtleDef, SearchValueMapResult, SearchIndexResult, } from "./model";
1
+ export type { QueryDataRow, ModelDef, Fragment, Query, StructDef, StructRelationship, NamedStructDefs, MalloyQueryData, AtomicFieldType as AtomicFieldTypeInner, DateUnit, ExtractUnit, TimestampUnit, TimeFieldType, QueryData, FieldTypeDef, Expr, DialectFragment, TimeValue, FilterExpression, SQLBlock, FieldDef, FilteredAliasedName, PipeSegment, QueryFieldDef, TurtleDef, SearchValueMapResult, SearchIndexResult, } from "./model";
2
2
  export { Segment, isFilteredAliasedName, } from "./model";
3
3
  export { HighlightType, MalloyTranslator, } from "./lang";
4
4
  export type { LogMessage, TranslateResponse } from "./lang";
5
- export { Malloy, Runtime, AtomicFieldType, ConnectionRuntime, SingleConnectionRuntime, EmptyURLReader, InMemoryURLReader, FixedConnectionMap, MalloyError, JoinRelationship, SourceRelationship, DateTimeframe, TimestampTimeframe, Result, parseTableURL, QueryMaterializer, CSVWriter, JSONWriter, DataWriter, } from "./malloy";
6
- export type { Explore, Model, PreparedQuery, PreparedResult, Field, AtomicField, ExploreField, QueryField, DataArray, DataRecord, DataColumn, DataArrayOrRecord, ModelMaterializer, DocumentSymbol, DocumentHighlight, ResultJSON, PreparedResultMaterializer, SQLBlockMaterializer, ExploreMaterializer, WriteStream, } from "./malloy";
7
- export type { URLReader, InfoConnection, LookupConnection, Connection, QueryString, ModelString, QueryURL, ModelURL, PooledConnection, TestableConnection, PersistSQLResults, } from "./runtime_types";
5
+ export { Malloy, Runtime, AtomicFieldType, ConnectionRuntime, SingleConnectionRuntime, EmptyURLReader, InMemoryURLReader, FixedConnectionMap, MalloyError, JoinRelationship, SourceRelationship, DateTimeframe, TimestampTimeframe, Result, parseTableURI, QueryMaterializer, CSVWriter, JSONWriter, DataWriter, } from "./malloy";
6
+ export type { Explore, Model, PreparedQuery, PreparedResult, Field, AtomicField, ExploreField, QueryField, DataArray, DataRecord, DataColumn, DataArrayOrRecord, ModelMaterializer, DocumentSymbol, DocumentHighlight, ResultJSON, RunSQLOptions, PreparedResultMaterializer, SQLBlockMaterializer, ExploreMaterializer, WriteStream, } from "./malloy";
7
+ export type { URLReader, InfoConnection, LookupConnection, Connection, QueryString, ModelString, QueryURL, ModelURL, PooledConnection, TestableConnection, PersistSQLResults, FetchSchemaAndRunSimultaneously, StreamingConnection, FetchSchemaAndRunStreamSimultaneously, } from "./runtime_types";
8
8
  export type { Loggable } from "./malloy";
9
9
  export { toAsyncGenerator } from "./connection_utils";
package/dist/index.js CHANGED
@@ -12,7 +12,7 @@
12
12
  * GNU General Public License for more details.
13
13
  */
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.toAsyncGenerator = exports.DataWriter = exports.JSONWriter = exports.CSVWriter = exports.QueryMaterializer = exports.parseTableURL = exports.Result = 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.MalloyTranslator = exports.HighlightType = exports.isFilteredAliasedName = exports.Segment = void 0;
15
+ exports.toAsyncGenerator = exports.DataWriter = exports.JSONWriter = exports.CSVWriter = exports.QueryMaterializer = exports.parseTableURI = exports.Result = 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.MalloyTranslator = exports.HighlightType = exports.isFilteredAliasedName = exports.Segment = void 0;
16
16
  var model_1 = require("./model");
17
17
  // Used in Composer Demo
18
18
  Object.defineProperty(exports, "Segment", { enumerable: true, get: function () { return model_1.Segment; } });
@@ -37,7 +37,7 @@ Object.defineProperty(exports, "SourceRelationship", { enumerable: true, get: fu
37
37
  Object.defineProperty(exports, "DateTimeframe", { enumerable: true, get: function () { return malloy_1.DateTimeframe; } });
38
38
  Object.defineProperty(exports, "TimestampTimeframe", { enumerable: true, get: function () { return malloy_1.TimestampTimeframe; } });
39
39
  Object.defineProperty(exports, "Result", { enumerable: true, get: function () { return malloy_1.Result; } });
40
- Object.defineProperty(exports, "parseTableURL", { enumerable: true, get: function () { return malloy_1.parseTableURL; } });
40
+ Object.defineProperty(exports, "parseTableURI", { enumerable: true, get: function () { return malloy_1.parseTableURI; } });
41
41
  Object.defineProperty(exports, "QueryMaterializer", { enumerable: true, get: function () { return malloy_1.QueryMaterializer; } });
42
42
  Object.defineProperty(exports, "CSVWriter", { enumerable: true, get: function () { return malloy_1.CSVWriter; } });
43
43
  Object.defineProperty(exports, "JSONWriter", { enumerable: true, get: function () { return malloy_1.JSONWriter; } });
@@ -1,5 +1,5 @@
1
1
  import { Expr } from "../../model/malloy_types";
2
- import { FSPair } from "../field-space";
2
+ import { FieldSpace } from "../field-space";
3
3
  import { ExprValue, ExpressionDef, Equality } from "./index";
4
4
  /**
5
5
  * All of the magic of malloy expressions eventually flows to here,
@@ -12,5 +12,5 @@ import { ExprValue, ExpressionDef, Equality } from "./index";
12
12
  * @param right Right Value
13
13
  * @returns ExprValue of the expression
14
14
  */
15
- export declare function applyBinary(fs: FSPair, left: ExpressionDef, op: string, right: ExpressionDef): ExprValue;
15
+ export declare function applyBinary(fs: FieldSpace, left: ExpressionDef, op: string, right: ExpressionDef): ExprValue;
16
16
  export declare function nullsafeNot(expr: Expr, op?: Equality): Expr;
@@ -40,12 +40,28 @@ function applyBinary(fs, left, op, right) {
40
40
  return numeric(fs, left, op, right);
41
41
  }
42
42
  if (oneOf(op, "/")) {
43
- if (fs.in.getDialect().divisionIsInteger) {
44
- return numeric(fs, left, "*1.0/", right);
43
+ const num = left.getExpression(fs);
44
+ const denom = right.getExpression(fs);
45
+ if (num.dataType != "number") {
46
+ left.log("Numerator for division must be a number");
47
+ }
48
+ else if (denom.dataType != "number") {
49
+ right.log("Denominator for division must be a number");
45
50
  }
46
51
  else {
47
- return numeric(fs, left, op, right);
52
+ const div = {
53
+ type: "dialect",
54
+ function: "div",
55
+ numerator: num.value,
56
+ denominator: denom.value,
57
+ };
58
+ return {
59
+ dataType: "number",
60
+ aggregate: num.aggregate || denom.aggregate,
61
+ value: [div],
62
+ };
48
63
  }
64
+ return (0, index_1.errorFor)("divide type mismatch");
49
65
  }
50
66
  left.log(`Canot use ${op} operator here`);
51
67
  return (0, index_1.errorFor)("applybinary bad operator");
@@ -1,5 +1,5 @@
1
1
  import { By, AtomicFieldType, FieldTypeDef, Fragment, TimeFieldType } from "../../model/malloy_types";
2
- import { FSPair, FieldSpace } from "../field-space";
2
+ import { FieldSpace } from "../field-space";
3
3
  import { Filter, MalloyElement, ExprValue, FieldValueType, FragType } from "./index";
4
4
  import { FieldName, FieldReference } from "./ast-main";
5
5
  /**
@@ -17,7 +17,7 @@ export declare abstract class ExpressionDef extends MalloyElement {
17
17
  * the translation code a chance to convert to match your expectations
18
18
  * @param space Namespace for looking up field references
19
19
  */
20
- abstract getExpression(fs: FSPair): ExprValue;
20
+ abstract getExpression(fs: FieldSpace): ExprValue;
21
21
  legalChildTypes: FragType[];
22
22
  /**
23
23
  * Some operators want to give the right hand value a chance to
@@ -26,7 +26,7 @@ export declare abstract class ExpressionDef extends MalloyElement {
26
26
  * @param fs FieldSpace
27
27
  * @returns Translated expression or undefined
28
28
  */
29
- requestExpression(fs: FSPair): ExprValue | undefined;
29
+ requestExpression(fs: FieldSpace): ExprValue | undefined;
30
30
  defaultFieldName(): string | undefined;
31
31
  /**
32
32
  * Check an expression for type compatibility
@@ -44,19 +44,19 @@ export declare abstract class ExpressionDef extends MalloyElement {
44
44
  * @param expr The "other" (besdies 'this') value
45
45
  * @returns The translated expression
46
46
  */
47
- apply(fs: FSPair, op: string, left: ExpressionDef): ExprValue;
47
+ apply(fs: FieldSpace, op: string, left: ExpressionDef): ExprValue;
48
48
  }
49
49
  export declare class ConstantSubExpression extends ExpressionDef {
50
50
  readonly expr: ExpressionDef;
51
51
  elementType: string;
52
52
  private cfs?;
53
53
  constructor(expr: ExpressionDef);
54
- getExpression(_fs: FSPair): ExprValue;
54
+ getExpression(_fs: FieldSpace): ExprValue;
55
55
  private get constantFs();
56
56
  constantValue(): ExprValue;
57
57
  constantCondition(type: AtomicFieldType): ExprValue;
58
- apply(fs: FSPair, op: string, expr: ExpressionDef): ExprValue;
59
- requestExpression(fs: FSPair): ExprValue | undefined;
58
+ apply(fs: FieldSpace, op: string, expr: ExpressionDef): ExprValue;
59
+ requestExpression(fs: FieldSpace): ExprValue | undefined;
60
60
  }
61
61
  export declare class FieldDeclaration extends MalloyElement {
62
62
  readonly expr: ExpressionDef;
@@ -66,7 +66,7 @@ export declare class FieldDeclaration extends MalloyElement {
66
66
  isMeasure?: boolean;
67
67
  constructor(expr: ExpressionDef, defineName: string, exprSrc?: string | undefined);
68
68
  fieldDef(fs: FieldSpace, exprName: string): FieldTypeDef;
69
- queryFieldDef(exprFS: FSPair, exprName: string): FieldTypeDef;
69
+ queryFieldDef(exprFS: FieldSpace, exprName: string): FieldTypeDef;
70
70
  }
71
71
  export declare class TypeMistmatch extends Error {
72
72
  }
@@ -74,13 +74,13 @@ export declare class ExprString extends ExpressionDef {
74
74
  readonly value: string;
75
75
  elementType: string;
76
76
  constructor(value: string);
77
- getExpression(_fs: FSPair): ExprValue;
77
+ getExpression(_fs: FieldSpace): ExprValue;
78
78
  }
79
79
  export declare class ExprNumber extends ExpressionDef {
80
80
  readonly n: string;
81
81
  elementType: string;
82
82
  constructor(n: string);
83
- getExpression(_fs: FSPair): ExprValue;
83
+ getExpression(_fs: FieldSpace): ExprValue;
84
84
  constantExpression(): ExprValue;
85
85
  }
86
86
  export declare class ExprRegEx extends ExpressionDef {
@@ -93,7 +93,7 @@ export declare class ExprTime extends ExpressionDef {
93
93
  elementType: string;
94
94
  readonly translationValue: ExprValue;
95
95
  constructor(timeType: TimeFieldType, value: Fragment[] | string, aggregate?: boolean);
96
- getExpression(_fs: FSPair): ExprValue;
96
+ getExpression(_fs: FieldSpace): ExprValue;
97
97
  static fromValue(timeType: TimeFieldType, expr: ExprValue): ExprTime;
98
98
  }
99
99
  declare abstract class Unary extends ExpressionDef {
@@ -104,7 +104,7 @@ export declare class ExprNot extends Unary {
104
104
  elementType: string;
105
105
  legalChildTypes: FragType[];
106
106
  constructor(expr: ExpressionDef);
107
- getExpression(fs: FSPair): ExprValue;
107
+ getExpression(fs: FieldSpace): ExprValue;
108
108
  }
109
109
  export declare class Boolean extends ExpressionDef {
110
110
  readonly value: "true" | "false";
@@ -119,7 +119,7 @@ export declare abstract class BinaryBoolean<opType extends string> extends Expre
119
119
  elementType: string;
120
120
  legalChildTypes: FragType[];
121
121
  constructor(left: ExpressionDef, op: opType, right: ExpressionDef);
122
- getExpression(fs: FSPair): ExprValue;
122
+ getExpression(fs: FieldSpace): ExprValue;
123
123
  }
124
124
  export declare class ExprLogicalOp extends BinaryBoolean<"and" | "or"> {
125
125
  elementType: string;
@@ -130,8 +130,8 @@ export declare class ExprIdReference extends ExpressionDef {
130
130
  elementType: string;
131
131
  constructor(fieldReference: FieldReference);
132
132
  get refString(): string;
133
- getExpression(fs: FSPair): ExprValue;
134
- apply(fs: FSPair, op: string, expr: ExpressionDef): ExprValue;
133
+ getExpression(fs: FieldSpace): ExprValue;
134
+ apply(fs: FieldSpace, op: string, expr: ExpressionDef): ExprValue;
135
135
  }
136
136
  export declare class ExprNULL extends ExpressionDef {
137
137
  elementType: string;
@@ -141,15 +141,15 @@ export declare class ExprParens extends ExpressionDef {
141
141
  readonly expr: ExpressionDef;
142
142
  elementType: string;
143
143
  constructor(expr: ExpressionDef);
144
- apply(fs: FSPair, op: string, expr: ExpressionDef): ExprValue;
145
- requestExpression(fs: FSPair): ExprValue | undefined;
146
- getExpression(fs: FSPair): ExprValue;
144
+ apply(fs: FieldSpace, op: string, expr: ExpressionDef): ExprValue;
145
+ requestExpression(fs: FieldSpace): ExprValue | undefined;
146
+ getExpression(fs: FieldSpace): ExprValue;
147
147
  }
148
148
  export declare class ExprMinus extends ExpressionDef {
149
149
  readonly expr: ExpressionDef;
150
150
  elementType: string;
151
151
  constructor(expr: ExpressionDef);
152
- getExpression(fs: FSPair): ExprValue;
152
+ getExpression(fs: FieldSpace): ExprValue;
153
153
  }
154
154
  export declare abstract class BinaryNumeric<opType extends string> extends ExpressionDef {
155
155
  readonly left: ExpressionDef;
@@ -157,7 +157,7 @@ export declare abstract class BinaryNumeric<opType extends string> extends Expre
157
157
  readonly right: ExpressionDef;
158
158
  elementType: string;
159
159
  constructor(left: ExpressionDef, op: opType, right: ExpressionDef);
160
- getExpression(fs: FSPair): ExprValue;
160
+ getExpression(fs: FieldSpace): ExprValue;
161
161
  }
162
162
  export declare class ExprAddSub extends BinaryNumeric<"+" | "-"> {
163
163
  elementType: string;
@@ -168,9 +168,9 @@ export declare class ExprMulDiv extends BinaryNumeric<"*" | "/"> {
168
168
  export declare class ExprAlternationTree extends BinaryBoolean<"|" | "&"> {
169
169
  elementType: string;
170
170
  constructor(left: ExpressionDef, op: "|" | "&", right: ExpressionDef);
171
- apply(fs: FSPair, applyOp: string, expr: ExpressionDef): ExprValue;
172
- requestExpression(_fs: FSPair): ExprValue | undefined;
173
- getExpression(_fs: FSPair): ExprValue;
171
+ apply(fs: FieldSpace, applyOp: string, expr: ExpressionDef): ExprValue;
172
+ requestExpression(_fs: FieldSpace): ExprValue | undefined;
173
+ getExpression(_fs: FieldSpace): ExprValue;
174
174
  }
175
175
  declare abstract class ExprAggregateFunction extends ExpressionDef {
176
176
  readonly func: string;
@@ -180,7 +180,7 @@ declare abstract class ExprAggregateFunction extends ExpressionDef {
180
180
  legalChildTypes: FragType[];
181
181
  constructor(func: string, expr?: ExpressionDef);
182
182
  returns(_forExpression: ExprValue): FieldValueType;
183
- getExpression(fs: FSPair): ExprValue;
183
+ getExpression(fs: FieldSpace): ExprValue;
184
184
  }
185
185
  export declare class ExprMin extends ExprAggregateFunction {
186
186
  legalChildTypes: FragType[];
@@ -208,7 +208,7 @@ export declare class ExprCount extends ExprAggregateFunction {
208
208
  elementType: string;
209
209
  constructor(source?: FieldReference | undefined);
210
210
  defaultFieldName(): string | undefined;
211
- getExpression(_fs: FSPair): ExprValue;
211
+ getExpression(_fs: FieldSpace): ExprValue;
212
212
  }
213
213
  export declare class ExprAvg extends ExprAsymmetric {
214
214
  constructor(expr: ExpressionDef | undefined, source?: FieldReference);
@@ -224,21 +224,21 @@ export declare class ExprUngroup extends ExpressionDef {
224
224
  elementType: string;
225
225
  constructor(control: "all" | "exclude", expr: ExpressionDef, fields: FieldName[]);
226
226
  returns(_forExpression: ExprValue): FieldValueType;
227
- getExpression(fs: FSPair): ExprValue;
227
+ getExpression(fs: FieldSpace): ExprValue;
228
228
  }
229
229
  export declare class WhenClause extends ExpressionDef {
230
230
  readonly whenThis: ExpressionDef;
231
231
  readonly thenThis: ExpressionDef;
232
232
  elementType: string;
233
233
  constructor(whenThis: ExpressionDef, thenThis: ExpressionDef);
234
- getExpression(_fs: FSPair): ExprValue;
234
+ getExpression(_fs: FieldSpace): ExprValue;
235
235
  }
236
236
  export declare class ExprCase extends ExpressionDef {
237
237
  readonly when: WhenClause[];
238
238
  readonly elseClause?: ExpressionDef | undefined;
239
239
  elementType: string;
240
240
  constructor(when: WhenClause[], elseClause?: ExpressionDef | undefined);
241
- getExpression(fs: FSPair): ExprValue;
241
+ getExpression(fs: FieldSpace): ExprValue;
242
242
  }
243
243
  export declare class ExprFilter extends ExpressionDef {
244
244
  readonly expr: ExpressionDef;
@@ -246,7 +246,7 @@ export declare class ExprFilter extends ExpressionDef {
246
246
  elementType: string;
247
247
  legalChildTypes: FragType[];
248
248
  constructor(expr: ExpressionDef, filter: Filter);
249
- getExpression(fs: FSPair): ExprValue;
249
+ getExpression(fs: FieldSpace): ExprValue;
250
250
  }
251
251
  declare type CastType = "string" | "number" | "boolean" | "date" | "timestamp";
252
252
  export declare function isCastType(t: string): t is CastType;
@@ -256,22 +256,22 @@ export declare class ExprCast extends ExpressionDef {
256
256
  readonly safe: boolean;
257
257
  elementType: string;
258
258
  constructor(expr: ExpressionDef, castType: CastType, safe?: boolean);
259
- getExpression(fs: FSPair): ExprValue;
259
+ getExpression(fs: FieldSpace): ExprValue;
260
260
  }
261
261
  export declare class TopBy extends MalloyElement {
262
262
  readonly by: string | ExpressionDef;
263
263
  elementType: string;
264
264
  constructor(by: string | ExpressionDef);
265
- getBy(fs: FSPair): By;
265
+ getBy(fs: FieldSpace): By;
266
266
  }
267
267
  export declare class Range extends ExpressionDef {
268
268
  readonly first: ExpressionDef;
269
269
  readonly last: ExpressionDef;
270
270
  elementType: string;
271
271
  constructor(first: ExpressionDef, last: ExpressionDef);
272
- apply(fs: FSPair, op: string, expr: ExpressionDef): ExprValue;
273
- requestExpression(_fs: FSPair): ExprValue | undefined;
274
- getExpression(_fs: FSPair): ExprValue;
272
+ apply(fs: FieldSpace, op: string, expr: ExpressionDef): ExprValue;
273
+ requestExpression(_fs: FieldSpace): ExprValue | undefined;
274
+ getExpression(_fs: FieldSpace): ExprValue;
275
275
  }
276
276
  export declare class PickWhen extends MalloyElement {
277
277
  readonly pick: ExpressionDef | undefined;
@@ -284,8 +284,8 @@ export declare class Pick extends ExpressionDef {
284
284
  readonly elsePick?: ExpressionDef | undefined;
285
285
  elementType: string;
286
286
  constructor(choices: PickWhen[], elsePick?: ExpressionDef | undefined);
287
- requestExpression(fs: FSPair): ExprValue | undefined;
288
- apply(fs: FSPair, op: string, expr: ExpressionDef): ExprValue;
289
- getExpression(fs: FSPair): ExprValue;
287
+ requestExpression(fs: FieldSpace): ExprValue | undefined;
288
+ apply(fs: FieldSpace, op: string, expr: ExpressionDef): ExprValue;
289
+ getExpression(fs: FieldSpace): ExprValue;
290
290
  }
291
291
  export {};