Skip to content

Commit

Permalink
record literals
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoy-googly-moogly committed Nov 7, 2024
1 parent d8411a4 commit 4f01be9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 49 deletions.
68 changes: 34 additions & 34 deletions packages/malloy/src/lang/ast/expressions/expr-record-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

// import {
// isAtomicFieldType,
// TD,
// RecordLiteralNode,
// TypedExpr,
// } from '../../../model';
import {ExprValue} from '../types/expr-value';
import {TD, RecordLiteralNode, RecordTypeDef} from '../../../model';

Check failure on line 8 in packages/malloy/src/lang/ast/expressions/expr-record-literal.ts

View workflow job for this annotation

GitHub Actions / test-all (18.x)

'RecordTypeDef' is defined but never used. Allowed unused vars must match /^_/u
import {ExprValue, computedExprValue} from '../types/expr-value';
import {ExpressionDef} from '../types/expression-def';
import {FieldSpace} from '../types/field-space';
import {MalloyElement} from '../types/malloy-element';
import * as TDU from '../typedesc-utils';

export class RecordElement extends MalloyElement {
elementType = 'record element';
Expand All @@ -34,32 +30,36 @@ export class RecordLiteral extends ExpressionDef {
this.has({pairs});
}

getExpression(_fs: FieldSpace): ExprValue {
throw new Error('get expression on record todo');
// const recLit: RecordLiteralNode = {
// node: 'recordLiteral',
// kids: {},
// };
// const dependents: ExprValue[] = [];
// for (const el of this.pairs) {
// const xVal = el.value.getExpression(fs);
// const expr: TypedExpr = {typeDef: {type: 'error'}, ...xVal.value};
// if (TD.isError(expr.typeDef) && isAtomicFieldType(xVal.dataType)) {
// expr.typeDef = xVal.dataType;
// }
// if (TD.isError(expr.typeDef) && xVal.dataType !== 'error') {
// this.logError(
// 'illegal-record-property-type',
// `Type '${xVal.dataType}' not a legal record value`
// );
// }
// recLit.kids[el.key] = expr;
// dependents.push(xVal);
// }
// return computedExprValue({
// dataType: 'record',
// value: recLit,
// from: dependents,
// });
getExpression(fs: FieldSpace): ExprValue {
const recLit: RecordLiteralNode = {
node: 'recordLiteral',
kids: {},
typeDef: {
name: '',
type: 'record',
join: 'one',
dialect: fs.dialectName(),
fields: [],
},
};
const dependents: ExprValue[] = [];
for (const el of this.pairs) {
const xVal = el.value.getExpression(fs);
if (TD.isAtomic(xVal)) {
dependents.push(xVal);
recLit.kids[el.key] = xVal.value;
recLit.typeDef.fields.push({...TDU.atomicDef(xVal), name: el.key});
} else {
this.logError(
'illegal-record-property-type',
`Record property '${el.key} is type '${xVal.type}', which is not a legal property value type`
);
}
}
return computedExprValue({
value: recLit,
dataType: recLit.typeDef,
from: dependents,
});
}
}
5 changes: 0 additions & 5 deletions packages/malloy/src/lang/malloy-to-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1964,11 +1964,6 @@ export class MalloyToAST
}

visitExprLiteralRecord(pcx: parse.ExprLiteralRecordContext) {
this.contextError(
pcx,
'not-yet-implemented',
'Record data is not yet implemented'
);
const els = this.only<ast.RecordElement>(
pcx.recordElement().map(elCx => this.astAt(this.visit(elCx), elCx)),
visited => visited instanceof ast.RecordElement && visited,
Expand Down
8 changes: 2 additions & 6 deletions packages/malloy/src/model/malloy_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ export type Expr =
| InCompareExpr
| ErrorNode;

interface HasTypeDef {
typeDef: AtomicTypeDef;
}
export type TypedExpr = Expr & HasTypeDef;

export type BinaryOperator =
| '+'
| '-'
Expand Down Expand Up @@ -335,7 +330,8 @@ export interface BooleanLiteralNode extends ExprLeaf {

export interface RecordLiteralNode extends ExprWithKids {
node: 'recordLiteral';
kids: Record<string, TypedExpr>;
kids: Record<string, Expr>;
typeDef: RecordTypeDef;
}

export interface ArrayLiteralNode extends ExprWithKids {
Expand Down
24 changes: 20 additions & 4 deletions test/src/databases/all/composite-atomic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ describe.each(runtimes.runtimeList)(
(databaseName, runtime) => {
describe('simple arrays', () => {
const evens = 'duckdb.sql("SELECT [2,4] as evens")';
test('array literal', async () => {
test('select array literal', async () => {
await expect(`
run: duckdb.sql("SELECT 1 AS row") -> { select: odds is [1,3] }
`).malloyResultMatches(runtime, {odds: [1, 3]});
});
test('array-un-nest', async () => {
test('array-un-nest on each', async () => {
await expect(`
run: ${evens}->{ select: n is evens.each }
`).malloyResultMatches(runtime, [{n: 2}, {n: 4}]);
Expand All @@ -31,12 +31,28 @@ describe.each(runtimes.runtimeList)(
`run: ${evens}->{ select: two is len!number(evens); } `
).malloyResultMatches(runtime, {two: 2});
});
test('array columns can be selected', async () => {
test('select array columns', async () => {
await expect(`run: ${evens}->{ select: evens }`).malloyResultMatches(
runtime,
{evens: [2, 4]}
);
});
test('array literal in source', async () => {
await expect(`
# test.debug
run: duckdb.sql("select 1") extend { dimension: d1 is [1,2,3,4,5,6] }
// -> { select: d1 }
-> { select: die_roll is d1.each }
`).malloyResultMatches(runtime, [
{die_roll: 1},
{die_roll: 2},
{die_roll: 3},
{die_roll: 4},
{die_roll: 5},
{die_roll: 6},
]);
});
test.todo('cross join two arrays');
});
describe('record', () => {
const record = 'duckdb.sql("SELECT {s: 0, m: 1, l:2, xl: 3} as record")';
Expand All @@ -51,7 +67,7 @@ describe.each(runtimes.runtimeList)(
'record/xl': 3,
});
});
test.skip('record literal', async () => {
test('record literal', async () => {
await expect(`
run: duckdb.sql("select 1") -> {
select: record is {s is 0, m is 1, l is 2, xl is 3}
Expand Down

0 comments on commit 4f01be9

Please sign in to comment.