Skip to content

Commit

Permalink
Add a Table method which returns the join identity.
Browse files Browse the repository at this point in the history
  • Loading branch information
broneill committed Sep 15, 2024
1 parent 0a0a551 commit e90f05f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/cojen/tupl/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.cojen.tupl.table.AggregatedTable;
import org.cojen.tupl.table.ComparatorMaker;
import org.cojen.tupl.table.GroupedTable;
import org.cojen.tupl.table.JoinIdentityTable;
import org.cojen.tupl.table.MappedTable;
import org.cojen.tupl.table.PlainPredicateMaker;
import org.cojen.tupl.table.ViewedTable;
Expand Down Expand Up @@ -679,6 +680,15 @@ public static Table<Row> join(String spec, Table<?>... tables) throws IOExceptio
return JoinTableMaker.join(spec, tables);
}

/**
* Returns an unmodifiable table consisting of one row with no columns, representing the
* identity element when joining an empty set of tables. Calling {@link #derive derive}
* against the join identity table can be used to perform arbitrary expression evaluation.
*/
public static Table<Row> join() {
return JoinIdentityTable.THE;
}

/**
* Returns a view backed by this table, whose rows and natural ordering are defined by the
* given query. The returned table instance will throw a {@link ViewConstraintException}
Expand Down
8 changes: 3 additions & 5 deletions src/test/java/org/cojen/tupl/table/expr/BinaryOpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import org.cojen.tupl.Table;
import org.cojen.tupl.Unsigned;

import org.cojen.tupl.table.JoinIdentityTable;

import static org.cojen.tupl.table.expr.Token.*;
import static org.cojen.tupl.table.expr.Type.*;

Expand Down Expand Up @@ -433,21 +431,21 @@ private static BigDecimal toBigDecimal(Object value) {
@Test
public void broken() throws Exception {
try {
Parser.parse(JoinIdentityTable.THE, "true < 0");
Parser.parse("true < 0");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("No common type"));
}

try {
Parser.parse(JoinIdentityTable.THE, "0 && 1");
Parser.parse("0 && 1");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("Boolean operation not allowed"));
}

try {
Parser.parse(JoinIdentityTable.THE, "'a' & 'b'");
Parser.parse("'a' & 'b'");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("Bitwise operation not allowed"));
Expand Down
44 changes: 21 additions & 23 deletions src/test/java/org/cojen/tupl/table/expr/FunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import org.cojen.tupl.Table;
import org.cojen.tupl.Unsigned;

import org.cojen.tupl.table.JoinIdentityTable;

/**
*
*
Expand All @@ -57,7 +55,7 @@ public void teardown() throws Exception {
}

private static RelationExpr parse(String query) {
return parse(JoinIdentityTable.THE, query);
return Parser.parse(query);
}

private static RelationExpr parse(Table table, String query) {
Expand Down Expand Up @@ -149,14 +147,14 @@ private Table<TestRow> fill(int num) throws Exception {
@Test
public void coalesce() throws Exception {
try {
Parser.parse(JoinIdentityTable.THE, "{c = coalesce()}");
parse("{c = coalesce()}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("at least 1 unnamed argument"));
}

try {
Parser.parse(JoinIdentityTable.THE, "{c = coalesce(5, true)}");
parse("{c = coalesce(5, true)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("no common type"));
Expand Down Expand Up @@ -192,21 +190,21 @@ public void coalesce() throws Exception {
@Test
public void iif() throws Exception {
try {
Parser.parse(JoinIdentityTable.THE, "{v = iif(1)}");
parse("{v = iif(1)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("exactly 3 unnamed arguments"));
}

try {
Parser.parse(JoinIdentityTable.THE, "{v = iif(1, 2, 3)}");
parse("{v = iif(1, 2, 3)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("boolean type"));
}

try {
Parser.parse(JoinIdentityTable.THE, "{v = iif(true, 2, false)}");
parse("{v = iif(true, 2, false)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("no common type"));
Expand Down Expand Up @@ -237,21 +235,21 @@ public void iif() throws Exception {
@Test
public void random() throws Exception {
try {
Parser.parse(JoinIdentityTable.THE, "{v = random(1, 2, 3)}");
parse("{v = random(1, 2, 3)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("at most 2 unnamed arguments"));
}

try {
Parser.parse(JoinIdentityTable.THE, "{v = random(1, true)}");
parse("{v = random(1, true)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("must be a number"));
}

try {
Parser.parse(JoinIdentityTable.THE, "{v = random(99999999999999999999999999999)}");
parse("{v = random(99999999999999999999999999999)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("unsupported argument type"));
Expand Down Expand Up @@ -300,14 +298,14 @@ public void random() throws Exception {
@Test
public void count() throws Exception {
try {
Parser.parse(JoinIdentityTable.THE, "{v = count(1, 2)}");
parse("{v = count(1, 2)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("at most 1 unnamed argument"));
}

try {
Parser.parse(JoinIdentityTable.THE, "{v = count()}");
parse("{v = count()}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("requires grouping"));
Expand Down Expand Up @@ -339,14 +337,14 @@ public void count() throws Exception {
@Test
public void first() throws Exception {
try {
Parser.parse(JoinIdentityTable.THE, "{v = first()}");
parse("{v = first()}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("exactly 1 unnamed argument"));
}

try {
Parser.parse(JoinIdentityTable.THE, "{v = first(1)}");
parse("{v = first(1)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("requires grouping"));
Expand Down Expand Up @@ -382,14 +380,14 @@ public void first() throws Exception {
@Test
public void last() throws Exception {
try {
Parser.parse(JoinIdentityTable.THE, "{v = last()}");
parse("{v = last()}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("exactly 1 unnamed argument"));
}

try {
Parser.parse(JoinIdentityTable.THE, "{v = last(1)}");
parse("{v = last(1)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("requires grouping"));
Expand Down Expand Up @@ -425,14 +423,14 @@ public void last() throws Exception {
@Test
public void min() throws Exception {
try {
Parser.parse(JoinIdentityTable.THE, "{v = min()}");
parse("{v = min()}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("exactly 1 unnamed argument"));
}

try {
Parser.parse(JoinIdentityTable.THE, "{v = min(1)}");
parse("{v = min(1)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("requires grouping"));
Expand Down Expand Up @@ -480,14 +478,14 @@ public void min() throws Exception {
@Test
public void max() throws Exception {
try {
Parser.parse(JoinIdentityTable.THE, "{v = max()}");
parse("{v = max()}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("exactly 1 unnamed argument"));
}

try {
Parser.parse(JoinIdentityTable.THE, "{v = max(1)}");
parse("{v = max(1)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("requires grouping"));
Expand Down Expand Up @@ -629,14 +627,14 @@ private Table<SumRow> fillSum(int num) throws Exception {
@Test
public void countSumAvg() throws Exception {
try {
Parser.parse(JoinIdentityTable.THE, "{v = sum()}");
parse("{v = sum()}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("exactly 1 unnamed argument"));
}

try {
Parser.parse(JoinIdentityTable.THE, "{v = sum(1)}");
parse("{v = sum(1)}");
fail();
} catch (QueryException e) {
assertTrue(e.getMessage().contains("requires grouping"));
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/cojen/tupl/table/join/BasicJoinTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ public void teardown() {
mJoin = null;
}

@Test
public void identity() throws Exception {
try (var scanner = Table.join().derive("{a=1}").newScanner(null)) {
assertEquals(1, scanner.row().get_int("a"));
}
}

@Test
public void crossJoin() throws Exception {
join("department : employee");
Expand Down

0 comments on commit e90f05f

Please sign in to comment.