@uwdata/mosaic-sql 0.12.1 → 0.13.0

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.
@@ -1,6 +1,7 @@
1
- import { Query } from '../ast/query.js';
1
+ import { Query, isQuery } from '../ast/query.js';
2
2
  import { argmax, argmin, max, min } from '../functions/aggregate.js';
3
3
  import { int32 } from '../functions/cast.js';
4
+ import { cte } from '../functions/cte.js';
4
5
  import { floor } from '../functions/numeric.js';
5
6
 
6
7
  /**
@@ -22,12 +23,20 @@ import { floor } from '../functions/numeric.js';
22
23
  export function m4(input, bin, x, y, groups = []) {
23
24
  const pixel = int32(floor(bin));
24
25
 
26
+ // Below, we treat input as a CTE when it is a query. In this case,
27
+ // we also request that the CTE be explicitly materialized.
28
+ const useCTE = isQuery(input);
29
+ const from = useCTE ? 'input' : input;
30
+ const query = useCTE
31
+ ? Query.with(cte(/** @type {string} */(from), input, true))
32
+ : Query;
33
+
25
34
  const q = (sel) => Query
26
- .from(input)
35
+ .from(from)
27
36
  .select(sel)
28
37
  .groupby(pixel, groups);
29
38
 
30
- return Query
39
+ return query
31
40
  .union(
32
41
  q([{ [x]: min(x), [y]: argmin(y, x) }, ...groups]),
33
42
  q([{ [x]: max(x), [y]: argmax(y, x) }, ...groups]),
package/src/types.ts CHANGED
@@ -2,6 +2,7 @@ import { ColumnRefNode } from './ast/column-ref.js';
2
2
  import { ExprNode, SQLNode } from './ast/node.js';
3
3
  import { TableRefNode } from './ast/table-ref.js';
4
4
  import { Query } from './ast/query.js';
5
+ import { WithClauseNode } from './ast/with.js';
5
6
 
6
7
  /**
7
8
  * Interface representing a dynamic parameter value.
@@ -80,7 +81,11 @@ export type SelectEntry =
80
81
 
81
82
  export type SelectExpr = MaybeArray<SelectEntry>;
82
83
 
83
- export type WithExpr = MaybeArray<Record<string, Query>>;
84
+ export type WithEntry =
85
+ | WithClauseNode
86
+ | Record<string, Query>;
87
+
88
+ export type WithExpr = MaybeArray<WithEntry>;
84
89
 
85
90
  export type FromEntry =
86
91
  | string
@@ -1,4 +1,4 @@
1
- import { AGGREGATE, COLUMN_REF, FRAGMENT, PARAM, VERBATIM, WINDOW } from '../constants.js';
1
+ import { AGGREGATE, COLUMN_PARAM, COLUMN_REF, FRAGMENT, PARAM, VERBATIM, WINDOW } from '../constants.js';
2
2
  import { aggregateNames, AggregateNode } from '../ast/aggregate.js';
3
3
  import { ColumnRefNode } from '../ast/column-ref.js';
4
4
  import { SQLNode } from '../ast/node.js';
@@ -82,7 +82,7 @@ export function collectAggregates(root) {
82
82
  export function collectColumns(root) {
83
83
  const cols = {};
84
84
  walk(root, (node) => {
85
- if (node.type === COLUMN_REF) {
85
+ if (node.type === COLUMN_REF || node.type === COLUMN_PARAM) {
86
86
  cols[node] = node; // key on string-coerced node
87
87
  }
88
88
  });
@@ -0,0 +1,3 @@
1
+ import { defineConfig } from 'vite';
2
+
3
+ export default defineConfig({});