Skip to content

Commit

Permalink
[MOREL-204] Add take and skip relational operators
Browse files Browse the repository at this point in the history
Now that `take` is a keyword, quote uses of `List.take` function
in standard basis library.

Push skip and take to Calcite.

Fix pushing yield into Calcite (it only worked if it was the last step).

Remove call to RelJson constructor deprecated in Calcite 1.36

Fixes #204
  • Loading branch information
julianhyde committed Jan 4, 2024
1 parent 561ea6f commit b406d6d
Show file tree
Hide file tree
Showing 20 changed files with 478 additions and 62 deletions.
6 changes: 4 additions & 2 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Contributions are welcome!

In Morel but not Standard ML:
* `from` expression with `in`, `suchthat`, `join`, `where`, `group`,
`compute`, `order`, `yield` clauses
`compute`, `order`, `skip`, `take`, `yield` clauses
* `union`, `except`, `intersect`, `elem`, `notelem` operators
* "*lab* `=`" is optional in `exprow`
* identifiers may be quoted
Expand Down Expand Up @@ -159,7 +159,9 @@ In Standard ML but not in Morel:
compute clause (<i>a</i> &gt; 1)
| <b>order</b> <i>orderItem<sub>1</sub></i> <b>,</b> ... <b>,</b> <i>orderItem<sub>o</sub></i>
order clause (<i>o</i> &ge; 1)
| <b>yield</b> <i>exp</i>
| <b>skip</b> <i>exp</i> skip clause
| <b>take</b> <i>exp</i> take clause
| <b>yield</b> <i>exp</i> yield clause
<i>groupKey</i> &rarr; [ <i>id</i> <b>=</b> ] <i>exp</i>
<i>agg</i> &rarr; [ <i>id</i> <b>=</b> ] <i>exp</i> [ <b>of</b> <i>exp</i> ]
<i>orderItem</i> &rarr; <i>exp</i> [ <b>desc</b> ]
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/net/hydromatic/morel/ast/Ast.java
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,58 @@ public Where copy(Exp exp) {
}
}

/** A {@code skip} clause in a {@code from} expression. */
public static class Skip extends FromStep {
public final Exp exp;

Skip(Pos pos, Exp exp) {
super(pos, Op.SKIP);
this.exp = exp;
}

@Override AstWriter unparse(AstWriter w, int left, int right) {
return w.append(" skip ").append(exp, 0, 0);
}

@Override public AstNode accept(Shuttle shuttle) {
return shuttle.visit(this);
}

@Override public void accept(Visitor visitor) {
visitor.visit(this);
}

public Skip copy(Exp exp) {
return this.exp.equals(exp) ? this : new Skip(pos, exp);
}
}

/** A {@code take} clause in a {@code from} expression. */
public static class Take extends FromStep {
public final Exp exp;

Take(Pos pos, Exp exp) {
super(pos, Op.TAKE);
this.exp = exp;
}

@Override AstWriter unparse(AstWriter w, int left, int right) {
return w.append(" take ").append(exp, 0, 0);
}

@Override public AstNode accept(Shuttle shuttle) {
return shuttle.visit(this);
}

@Override public void accept(Visitor visitor) {
visitor.visit(this);
}

public Take copy(Exp exp) {
return this.exp.equals(exp) ? this : new Take(pos, exp);
}
}

/** A {@code yield} clause in a {@code from} expression. */
public static class Yield extends FromStep {
public final Exp exp;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/hydromatic/morel/ast/AstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,14 @@ public Ast.FromStep where(Pos pos, Ast.Exp exp) {
return new Ast.Where(pos, exp);
}

public Ast.FromStep skip(Pos pos, Ast.Exp exp) {
return new Ast.Skip(pos, exp);
}

public Ast.FromStep take(Pos pos, Ast.Exp exp) {
return new Ast.Take(pos, exp);
}

public Ast.FromStep yield(Pos pos, Ast.Exp exp) {
return new Ast.Yield(pos, exp);
}
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/net/hydromatic/morel/ast/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,66 @@ public Where copy(Exp exp, List<Binding> bindings) {
}
}

/** A {@code skip} clause in a {@code from} expression. */
public static class Skip extends FromStep {
public final Exp exp;

Skip(ImmutableList<Binding> bindings, Exp exp) {
super(Op.SKIP, bindings);
this.exp = requireNonNull(exp, "exp");
}

@Override public Skip accept(Shuttle shuttle) {
return shuttle.visit(this);
}

@Override public void accept(Visitor visitor) {
visitor.visit(this);
}

@Override protected AstWriter unparse(AstWriter w, From from, int ordinal,
int left, int right) {
return w.append(" skip ").append(exp, 0, 0);
}

public Skip copy(Exp exp, List<Binding> bindings) {
return exp == this.exp
&& bindings.equals(this.bindings)
? this
: core.skip(bindings, exp);
}
}

/** A {@code take} clause in a {@code from} expression. */
public static class Take extends FromStep {
public final Exp exp;

Take(ImmutableList<Binding> bindings, Exp exp) {
super(Op.TAKE, bindings);
this.exp = requireNonNull(exp, "exp");
}

@Override public Take accept(Shuttle shuttle) {
return shuttle.visit(this);
}

@Override public void accept(Visitor visitor) {
visitor.visit(this);
}

@Override protected AstWriter unparse(AstWriter w, From from, int ordinal,
int left, int right) {
return w.append(" take ").append(exp, 0, 0);
}

public Take copy(Exp exp, List<Binding> bindings) {
return exp == this.exp
&& bindings.equals(this.bindings)
? this
: core.take(bindings, exp);
}
}

/** An {@code order} clause in a {@code from} expression. */
public static class Order extends FromStep {
public final ImmutableList<OrderItem> orderItems;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/hydromatic/morel/ast/CoreBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,14 @@ public Core.Where where(List<Binding> bindings, Core.Exp exp) {
return new Core.Where(ImmutableList.copyOf(bindings), exp);
}

public Core.Skip skip(List<Binding> bindings, Core.Exp exp) {
return new Core.Skip(ImmutableList.copyOf(bindings), exp);
}

public Core.Take take(List<Binding> bindings, Core.Exp exp) {
return new Core.Take(ImmutableList.copyOf(bindings), exp);
}

public Core.Yield yield_(List<Binding> bindings, Core.Exp exp) {
return new Core.Yield(ImmutableList.copyOf(bindings), exp);
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/net/hydromatic/morel/ast/FromBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.calcite.util.Util;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -192,6 +193,19 @@ public FromBuilder where(Core.Exp condition) {
return addStep(core.where(bindings, condition));
}

public FromBuilder skip(Core.Exp count) {
if (count.op == Op.INT_LITERAL
&& ((Core.Literal) count).value.equals(BigDecimal.ZERO)) {
// skip "skip 0"
return this;
}
return addStep(core.skip(bindings, count));
}

public FromBuilder take(Core.Exp count) {
return addStep(core.take(bindings, count));
}

public FromBuilder group(SortedMap<Core.IdPat, Core.Exp> groupExps,
SortedMap<Core.IdPat, Core.Aggregate> aggregates) {
return addStep(core.group(groupExps, aggregates));
Expand Down Expand Up @@ -378,6 +392,14 @@ private class StepHandler extends Visitor {
where(where.exp);
}

@Override protected void visit(Core.Skip skip) {
skip(skip.exp);
}

@Override protected void visit(Core.Take take) {
take(take.exp);
}

@Override protected void visit(Core.Yield yield) {
yield_(false, yield.bindings, yield.exp);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/hydromatic/morel/ast/Op.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public enum Op {
COMPUTE,
ORDER,
ORDER_ITEM,
SKIP,
TAKE,
YIELD,
AGGREGATE,
IF;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/net/hydromatic/morel/ast/Shuttle.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ protected AstNode visit(Ast.Where where) {
return ast.where(where.pos, where.exp.accept(this));
}

protected AstNode visit(Ast.Skip skip) {
return ast.skip(skip.pos, skip.exp.accept(this));
}

protected AstNode visit(Ast.Take take) {
return ast.take(take.pos, take.exp.accept(this));
}

protected AstNode visit(Ast.Yield yield) {
return ast.yield(yield.pos, yield.exp.accept(this));
}
Expand Down Expand Up @@ -395,6 +403,14 @@ protected Core.Where visit(Core.Where where) {
return where.copy(where.exp.accept(this), where.bindings);
}

protected Core.Skip visit(Core.Skip skip) {
return skip.copy(skip.exp.accept(this), skip.bindings);
}

protected Core.Take visit(Core.Take take) {
return take.copy(take.exp.accept(this), take.bindings);
}

protected Core.Group visit(Core.Group group) {
return group.copy(visitSortedMap(group.groupExps),
visitSortedMap(group.aggregates));
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/net/hydromatic/morel/ast/Visitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ protected void visit(Ast.Where where) {
where.exp.accept(this);
}

protected void visit(Ast.Skip skip) {
skip.exp.accept(this);
}

protected void visit(Ast.Take take) {
take.exp.accept(this);
}

protected void visit(Ast.Yield yield) {
yield.exp.accept(this);
}
Expand Down Expand Up @@ -326,6 +334,14 @@ protected void visit(Core.Where where) {
where.exp.accept(this);
}

protected void visit(Core.Skip skip) {
skip.exp.accept(this);
}

protected void visit(Core.Take take) {
take.exp.accept(this);
}

protected void visit(Core.NonRecValDecl valDecl) {
valDecl.pat.accept(this);
valDecl.exp.accept(this);
Expand Down
Loading

0 comments on commit b406d6d

Please sign in to comment.