Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/public/specs/esm/gaia.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WITH prep AS (
WHERE parallax BETWEEN -5 AND 20 AND phot_g_mean_mag IS NOT NULL AND bp_rp IS NOT NULL
)
SELECT
(1.340264 * lambda * cos(t)) / (sqrt(3)/2 * (1.340264 + (-0.081106 * 3 * t2) + (t6 * (0.000893 * 7 + 0.003796 * 9 * t2)))) AS u,
(1.340264 * "lambda" * cos(t)) / (sqrt(3)/2 * (1.340264 + (-0.081106 * 3 * t2) + (t6 * (0.000893 * 7 + 0.003796 * 9 * t2)))) AS u,
t * (1.340264 + (-0.081106 * t2) + (t6 * (0.000893 + 0.003796 * t2))) AS v,
* EXCLUDE('t', 't2', 't6')
FROM prep`
Expand Down
2 changes: 1 addition & 1 deletion docs/public/specs/json/gaia.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A 5M row sample of the 1.8B element Gaia star catalog.\nA `raster` sky map reveals our Milky Way galaxy. Select high parallax stars in the histogram to reveal a\n[Hertzsprung-Russel diagram](https://en.wikipedia.org/wiki/Hertzsprung%E2%80%93Russell_diagram)\nin the plot of stellar color vs. magnitude on the right.\n\n_You may need to wait a few seconds for the dataset to load._\n"
},
"data": {
"gaia": "-- compute u and v with natural earth projection\nWITH prep AS (\n SELECT\n radians((-l + 540) % 360 - 180) AS lambda,\n radians(b) AS phi,\n asin(sqrt(3)/2 * sin(phi)) AS t,\n t^2 AS t2,\n t2^3 AS t6,\n *\n FROM 'https://pub-1da360b43ceb401c809f68ca37c7f8a4.r2.dev/data/gaia-5m.parquet'\n WHERE parallax BETWEEN -5 AND 20 AND phot_g_mean_mag IS NOT NULL AND bp_rp IS NOT NULL\n)\nSELECT\n (1.340264 * lambda * cos(t)) / (sqrt(3)/2 * (1.340264 + (-0.081106 * 3 * t2) + (t6 * (0.000893 * 7 + 0.003796 * 9 * t2)))) AS u,\n t * (1.340264 + (-0.081106 * t2) + (t6 * (0.000893 + 0.003796 * t2))) AS v,\n * EXCLUDE('t', 't2', 't6')\nFROM prep\n"
"gaia": "-- compute u and v with natural earth projection\nWITH prep AS (\n SELECT\n radians((-l + 540) % 360 - 180) AS lambda,\n radians(b) AS phi,\n asin(sqrt(3)/2 * sin(phi)) AS t,\n t^2 AS t2,\n t2^3 AS t6,\n *\n FROM 'https://pub-1da360b43ceb401c809f68ca37c7f8a4.r2.dev/data/gaia-5m.parquet'\n WHERE parallax BETWEEN -5 AND 20 AND phot_g_mean_mag IS NOT NULL AND bp_rp IS NOT NULL\n)\nSELECT\n (1.340264 * \"lambda\" * cos(t)) / (sqrt(3)/2 * (1.340264 + (-0.081106 * 3 * t2) + (t6 * (0.000893 * 7 + 0.003796 * 9 * t2)))) AS u,\n t * (1.340264 + (-0.081106 * t2) + (t6 * (0.000893 + 0.003796 * t2))) AS v,\n * EXCLUDE('t', 't2', 't6')\nFROM prep\n"
},
"params": {
"brush": {
Expand Down
2 changes: 1 addition & 1 deletion docs/public/specs/yaml/gaia.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data:
WHERE parallax BETWEEN -5 AND 20 AND phot_g_mean_mag IS NOT NULL AND bp_rp IS NOT NULL
)
SELECT
(1.340264 * lambda * cos(t)) / (sqrt(3)/2 * (1.340264 + (-0.081106 * 3 * t2) + (t6 * (0.000893 * 7 + 0.003796 * 9 * t2)))) AS u,
(1.340264 * "lambda" * cos(t)) / (sqrt(3)/2 * (1.340264 + (-0.081106 * 3 * t2) + (t6 * (0.000893 * 7 + 0.003796 * 9 * t2)))) AS u,
t * (1.340264 + (-0.081106 * t2) + (t6 * (0.000893 + 0.003796 * t2))) AS v,
* EXCLUDE('t', 't2', 't6')
FROM prep
Expand Down
16 changes: 0 additions & 16 deletions packages/mosaic/sql/src/ast/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,6 @@ export class AggregateNode extends ExprNode {
frame(framedef: WindowFrameNode) {
return this.window().frame(framedef);
}

/**
* Generate a SQL query string for this node.
*/
toString() {
const { name, args, isDistinct, filter, order } = this;
const arg = [
isDistinct ? 'DISTINCT' : '',
args?.length ? args.join(', ')
: name.toLowerCase() === 'count' ? '*'
: '',
order.length ? `ORDER BY ${order.join(', ')}` : ''
].filter(x => x).join(' ');
const filt = filter ? ` FILTER (WHERE ${filter})` : '';
return `${name}(${arg})${filt}`;
}
}

/**
Expand Down
14 changes: 0 additions & 14 deletions packages/mosaic/sql/src/ast/between-op.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ export class BetweenOpNode extends AbstractBetweenOpNode {
constructor(expr: ExprNode, extent?: Extent) {
super(BETWEEN_OPERATOR, expr, extent);
}

/**
* Generate a SQL query string for this node.
*/
toString() {
return super.toSQL('BETWEEN');
}
}

export class NotBetweenOpNode extends AbstractBetweenOpNode {
Expand All @@ -58,11 +51,4 @@ export class NotBetweenOpNode extends AbstractBetweenOpNode {
constructor(expr: ExprNode, extent?: Extent) {
super(NOT_BETWEEN_OPERATOR, expr, extent);
}

/**
* Generate a SQL query string for this node.
*/
toString() {
return super.toSQL('NOT BETWEEN');
}
}
7 changes: 0 additions & 7 deletions packages/mosaic/sql/src/ast/binary-op.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,4 @@ export class BinaryOpNode extends ExprNode {
this.left = left;
this.right = right;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
return `(${this.left} ${this.op} ${this.right})`;
}
}
18 changes: 0 additions & 18 deletions packages/mosaic/sql/src/ast/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,6 @@ export class CaseNode extends ExprNode {
else(expr: ExprValue) {
return new CaseNode(this.expr, this._when, asNode(expr));
}

/**
* Generate a SQL query string for this node.
*/
toString() {
return 'CASE '
+ (this.expr ? `${this.expr} ` : '')
+ this._when.join(' ')
+ (this._else ? ` ELSE ${this._else}` : '')
+ ' END';
}
}

export class WhenNode extends SQLNode {
Expand All @@ -81,11 +70,4 @@ export class WhenNode extends SQLNode {
this.when = when;
this.then = then;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
return `WHEN ${this.when} THEN ${this.then}`;
}
}
8 changes: 0 additions & 8 deletions packages/mosaic/sql/src/ast/cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,4 @@ export class CastNode extends ExprNode {
this.expr = expr;
this.cast = type;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
// TODO? could include check to see if parens are necessary
return `(${this.expr})::${this.cast}`;
}
}
7 changes: 0 additions & 7 deletions packages/mosaic/sql/src/ast/collate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,4 @@ export class CollateNode extends ExprNode {
this.expr = expr;
this.collation = collation;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
return `${this.expr} ${COLLATE} ${this.collation}`;
}
}
11 changes: 0 additions & 11 deletions packages/mosaic/sql/src/ast/column-ref.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { TableRefNode } from './table-ref.js';
import { COLUMN_REF } from '../constants.js';
import { quoteIdentifier } from '../util/string.js';
import { ExprNode } from './node.js';

/**
Expand Down Expand Up @@ -31,16 +30,6 @@ export class ColumnRefNode extends ExprNode {
get column() {
return ''; // subclasses to override
}

/**
* Generate a SQL query string for this node.
*/
toString() {
const { column, table } = this;
const tref = `${table ?? ''}`;
const id = column === '*' ? '*' : quoteIdentifier(column);
return (tref ? (tref + '.') : '') + id;
}
}

export class ColumnNameRefNode extends ColumnRefNode {
Expand Down
7 changes: 0 additions & 7 deletions packages/mosaic/sql/src/ast/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,4 @@ export class FragmentNode extends ExprNode {
super(FRAGMENT);
this.spans = spans;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
return this.spans.join('');
}
}
15 changes: 0 additions & 15 deletions packages/mosaic/sql/src/ast/from.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { SampleClauseNode } from './sample.js';
import { FROM_CLAUSE } from '../constants.js';
import { quoteIdentifier } from '../util/string.js';
import { SQLNode } from './node.js';
import { isQuery } from './query.js';
import { isTableRef } from './table-ref.js';

/**
* AST node corresponding to a table source which can appear
Expand Down Expand Up @@ -40,16 +37,4 @@ export class FromClauseNode extends FromNode {
as(alias: string) {
return new FromClauseNode(this.expr, alias, this.sample);
}

/**
* Generate a SQL query string for this node.
*/
toString() {
const { expr, alias, sample } = this;
const ref = isQuery(expr) ? `(${expr})` : `${expr}`;
const from = alias && !(isTableRef(expr) && expr.table.join('.') === alias)
? `${ref} AS ${quoteIdentifier(alias)}`
: `${ref}`;
return `${from}${sample ? ` TABLESAMPLE ${sample}` : ''}`;
}
}
8 changes: 0 additions & 8 deletions packages/mosaic/sql/src/ast/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,4 @@ export class FunctionNode extends ExprNode {
this.name = name;
this.args = args;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
const { name, args } = this;
return `${name}(${args.join(', ')})`;
}
}
7 changes: 0 additions & 7 deletions packages/mosaic/sql/src/ast/in-op.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,4 @@ export class InOpNode extends ExprNode {
this.expr = expr;
this.values = values;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
return `(${this.expr} IN (${this.values.join(', ')}))`;
}
}
7 changes: 0 additions & 7 deletions packages/mosaic/sql/src/ast/interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,4 @@ export class IntervalNode extends ExprNode {
this.name = name;
this.steps = steps;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
return `INTERVAL ${this.steps} ${this.name}`;
}
}
19 changes: 0 additions & 19 deletions packages/mosaic/sql/src/ast/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,4 @@ export class JoinNode extends FromNode {
this.using = using;
this.sample = sample;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
const { left, right, joinVariant, joinType, condition, using, sample } = this;
const variant = joinVariant === 'REGULAR' ? '' : `${joinVariant} `;
let type = '';
let cond = '';

if (joinVariant !== 'CROSS') {
type = joinType !== 'INNER' ? `${joinType} ` : '';
cond = condition ? ` ON ${condition}`
: using ? ` USING (${using?.join(', ')})`
: '';
}
const samp = sample ? ` USING SAMPLE ${sample}` : '';
return `${left} ${variant}${type}JOIN ${right}${cond}${samp}`;
}
}
7 changes: 0 additions & 7 deletions packages/mosaic/sql/src/ast/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,4 @@ export class ListNode extends ExprNode {
super(LIST);
this.values = values;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
return `[${this.values.join(', ')}]`;
}
}
7 changes: 0 additions & 7 deletions packages/mosaic/sql/src/ast/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ export class LiteralNode extends ExprNode {
super(LITERAL);
this.value = value;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
return literalToSQL(this.value);
}
}

export function literalToSQL(value: unknown) {
Expand Down
10 changes: 0 additions & 10 deletions packages/mosaic/sql/src/ast/logical-op.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ export class LogicalOpNode<T extends ExprNode> extends ExprNode {
this.op = op;
this.clauses = clauses;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
const c = this.clauses;
return c.length === 0 ? ''
: c.length === 1 ? `${c[0]}`
: `(${c.join(` ${this.op} `)})`;
}
}

export class AndNode<T extends ExprNode> extends LogicalOpNode<T> {
Expand Down
33 changes: 30 additions & 3 deletions packages/mosaic/sql/src/ast/node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { SQLCodeGenerator } from '../visit/codegen/sql.js';

/**
* Check if a value is a SQL AST node.
* @param value The value to check.
Expand All @@ -6,6 +8,16 @@ export function isNode(value: unknown): value is SQLNode {
return value instanceof SQLNode;
}

let _defaultVisitor: SQLCodeGenerator | undefined;

/**
* Set the default visitor for toString operations.
* This is used when no visitor is explicitly provided.
*/
export function setDefaultVisitor(visitor: SQLCodeGenerator) {
_defaultVisitor = visitor;
}

export class SQLNode {
/** The SQL AST node type. */
readonly type: string;
Expand All @@ -23,13 +35,28 @@ export class SQLNode {
* @returns The shallow clone node.
*/
clone(): this {
// @ts-expect-error use constructor
const clone = new this.constructor();
// @ts-expect-error use constructor with type
const clone = new this.constructor(this.type);
for (const key in this) {
clone[key] = this[key];
if (key !== 'type') { // Skip type since it's already set by constructor
clone[key] = this[key];
}
}
return clone;
}

/**
* Generate a SQL query string for this node using a specific dialect visitor.
* @param visitor Optional SQL visitor to use for string generation.
* If not provided, uses the default visitor.
* @returns The SQL string representation.
*/
toString(visitor: SQLCodeGenerator | undefined = _defaultVisitor): string {
if (!visitor) {
throw new Error('No visitor provided and no default visitor set.');
}
return visitor.toString(this);
}
}

/**
Expand Down
14 changes: 0 additions & 14 deletions packages/mosaic/sql/src/ast/order-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,4 @@ export class OrderByNode extends ExprNode {
this.desc = desc;
this.nullsFirst = nullsFirst;
}

/**
* Generate a SQL query string for this node.
*/
toString() {
const { expr, desc, nullsFirst } = this;
const dir = desc ? ' DESC'
: desc === false ? ' ASC'
: '';
const nf = nullsFirst ? ' NULLS FIRST'
: nullsFirst === false ? ' NULLS LAST'
: '';
return `${expr}${dir}${nf}`;
}
}
Loading