use of herddb.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method caseWhenTest.
@Test
public void caseWhenTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string,t1 timestamp)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey2", Integer.valueOf(1235))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey3", Integer.valueOf(1236))).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT k1, " + "CASE " + "WHEN k1='mykey' THEN 'a' " + "WHEN k1='mykey2' THEN 'b' " + "ELSE 'c' " + "END as mycase " + "FROM tblspace1.tsql " + "ORDER BY k1", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
for (DataAccessor t : res) {
System.out.println("t:" + t);
}
assertEquals(3, res.size());
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey", "mycase", "a"))));
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey2", "mycase", "b"))));
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey3", "mycase", "c"))));
}
try (DataScanner scan = scan(manager, "SELECT k1, " + "CASE " + "WHEN k1='mykey' THEN 'a' " + "WHEN k1='mykey2' THEN 'b' " + "END as mycase " + "FROM tblspace1.tsql " + "ORDER BY k1", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
for (DataAccessor t : res) {
System.out.println("t:" + t);
}
assertEquals(3, res.size());
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey", "mycase", "a"))));
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey2", "mycase", "b"))));
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey3", "mycase", null))));
}
try (DataScanner scan = scan(manager, "SELECT k1, " + "SUM(CASE " + "WHEN k1='mykey' THEN 1 " + "WHEN k1='mykey2' THEN 2 " + "ELSE 3 " + "END) as mysum " + "FROM tblspace1.tsql " + "GROUP BY k1", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
for (DataAccessor t : res) {
System.out.println("t2:" + t);
}
assertEquals(3, res.size());
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey", "mysum", 1L))));
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey2", "mysum", 2L))));
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey3", "mysum", 3L))));
}
try (DataScanner scan = scan(manager, "SELECT " + "SUM(CASE " + "WHEN k1='mykey' THEN 1 " + "WHEN k1='mykey2' THEN 2 " + "ELSE 3 " + "END) as mysum " + "FROM tblspace1.tsql " + "", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
for (DataAccessor t : res) {
System.out.println("t:" + t);
}
assertEquals(1, res.size());
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("mysum", 6L))));
}
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method multiInsert.
@Test
public void multiInsert() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string,t1 timestamp)", Collections.emptyList());
java.sql.Timestamp tt1 = new java.sql.Timestamp(System.currentTimeMillis());
java.sql.Timestamp tt2 = new java.sql.Timestamp(System.currentTimeMillis() + 60000);
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,?),(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), tt1, "mykey2", Integer.valueOf(1235), tt2)).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT k1,n1,t1 FROM tblspace1.tsql ORDER BY n1 desc", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
assertEquals(RawString.of("mykey2"), res.get(0).get("k1"));
assertEquals(RawString.of("mykey"), res.get(1).get("k1"));
assertEquals(Integer.valueOf(1235), res.get(0).get("n1"));
assertEquals(Integer.valueOf(1234), res.get(1).get("n1"));
assertEquals(tt2, res.get(0).get("t1"));
assertEquals(tt1, res.get(1).get("t1"));
}
execute(manager, "CREATE TABLE tblspace1.tsql2 (a1 integer auto_increment primary key, k1 string ,n1 int,s1 string,t1 timestamp)", Collections.emptyList());
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql2(k1,n1,t1) values(?,?,?),(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), tt1, "mykey2", Integer.valueOf(1235), tt2)).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT a1,k1,n1,t1 FROM tblspace1.tsql2 ORDER BY n1 desc", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
assertEquals(RawString.of("mykey2"), res.get(0).get("k1"));
assertEquals(RawString.of("mykey"), res.get(1).get("k1"));
assertEquals(Integer.valueOf(1235), res.get(0).get("n1"));
assertEquals(Integer.valueOf(1234), res.get(1).get("n1"));
assertEquals(tt2, res.get(0).get("t1"));
assertEquals(tt1, res.get(1).get("t1"));
assertEquals(2, res.get(0).get("a1"));
assertEquals(1, res.get(1).get("a1"));
}
// auto-transaction
execute(manager, "CREATE TABLE tblspace1.tsql3 (a1 integer auto_increment primary key, k1 string ,n1 int,s1 string,t1 timestamp)", Collections.emptyList());
DMLStatementExecutionResult resInsert = executeUpdate(manager, "INSERT INTO tblspace1.tsql3(k1,n1,t1) values(?,?,?),(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), tt1, "mykey2", Integer.valueOf(1235), tt2), TransactionContext.AUTOTRANSACTION_TRANSACTION);
assertEquals(2, resInsert.getUpdateCount());
assertTrue(resInsert.transactionId > 0);
try (DataScanner scan = scan(manager, "SELECT a1,k1,n1,t1 FROM tblspace1.tsql3 ORDER BY n1 desc", Collections.emptyList(), 0, new TransactionContext(resInsert.transactionId))) {
List<DataAccessor> res = scan.consume();
assertEquals(RawString.of("mykey2"), res.get(0).get("k1"));
assertEquals(RawString.of("mykey"), res.get(1).get("k1"));
assertEquals(Integer.valueOf(1235), res.get(0).get("n1"));
assertEquals(Integer.valueOf(1234), res.get(1).get("n1"));
assertEquals(tt2, res.get(0).get("t1"));
assertEquals(tt1, res.get(1).get("t1"));
assertEquals(2, res.get(0).get("a1"));
assertEquals(1, res.get(1).get("a1"));
}
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method updateMultiColumns.
@Test
public void updateMultiColumns() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string,n1 int,s1 string,t1 timestamp,n2 int,n3 int, primary key (k1) )", Collections.emptyList());
java.sql.Timestamp now = new java.sql.Timestamp(System.currentTimeMillis());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1,t1,n2,n3) values(?,?,?,?,?,?)", Arrays.asList("mykey", "a", Integer.valueOf(1234), now, 1, 2)).getUpdateCount());
DataAccessor before = scan(manager, "SELECT n2,n3 FROM tblspace1.tsql WHERE k1=?", Arrays.asList("mykey")).consume().get(0);
assertEquals(1, before.get(0));
assertEquals(2, before.get(1));
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n2=n2+1,n3=n3+5 WHERE k1=?", Arrays.asList("mykey")).getUpdateCount());
DataAccessor after = scan(manager, "SELECT n2,n3 FROM tblspace1.tsql WHERE k1=?", Arrays.asList("mykey")).consume().get(0);
assertEquals(2, after.get(0));
assertEquals(7, after.get(1));
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n2=n2+n2,n3=n3+n2 WHERE k1=?", Arrays.asList("mykey")).getUpdateCount());
DataAccessor after2 = scan(manager, "SELECT n2,n3 FROM tblspace1.tsql WHERE k1=?", Arrays.asList("mykey")).consume().get(0);
assertEquals(4, after2.get(0));
assertEquals(9, after2.get(1));
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method multipleColumnPrimaryKeyTest.
@Test
public void multipleColumnPrimaryKeyTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string," + "n1 int," + "s1 string, " + "primary key (k1,n1)" + ")", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
try (DataScanner scan1 = scan(manager, "SELECT k1 as theKey,'one' as theStringConstant,3 LongConstant FROM tblspace1.tsql where k1 ='mykey'", Collections.emptyList())) {
assertEquals(1, scan1.consume().size());
}
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1235))).getUpdateCount());
try (DataScanner scan1 = scan(manager, "SELECT k1 as theKey,'one' as theStringConstant,3 LongConstant FROM tblspace1.tsql where k1 ='mykey'", Collections.emptyList())) {
assertEquals(2, scan1.consume().size());
}
try {
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1235))).getUpdateCount());
fail();
} catch (DuplicatePrimaryKeyException err) {
}
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql where k1 ='mykey' order by n1", Collections.emptyList())) {
List<DataAccessor> rows = scan1.consume();
assertEquals(2, rows.size());
assertEquals(1234, rows.get(0).get("n1"));
assertEquals(1235, rows.get(1).get("n1"));
}
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql where k1 ='mykey' and n1=1234", Collections.emptyList())) {
assertEquals(1, scan1.consume().size());
}
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(n1,k1) values(?,?)", Arrays.asList(Integer.valueOf(1236), "mykey")).getUpdateCount());
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql where k1 ='mykey' order by n1 desc", Collections.emptyList())) {
List<DataAccessor> rows = scan1.consume();
assertEquals(3, rows.size());
assertEquals(1236, rows.get(0).get("n1"));
assertEquals(1235, rows.get(1).get("n1"));
assertEquals(1234, rows.get(2).get("n1"));
}
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql where k1 ='mykey' and n1=1234", Collections.emptyList())) {
assertEquals(1, scan1.consume().size());
}
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql where k1 ='mykey'", Collections.emptyList())) {
assertEquals(3, scan1.consume().size());
}
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set s1=? where k1 =? and n1=?", Arrays.asList("newvalue", "mykey", Integer.valueOf(1236))).getUpdateCount());
try (DataScanner scan1 = scan(manager, "SELECT k1,n1,s1 FROM tblspace1.tsql where k1 ='mykey' and n1=1236", Collections.emptyList())) {
List<DataAccessor> rows = scan1.consume();
assertEquals(1, rows.size());
assertEquals(RawString.of("newvalue"), rows.get(0).get("s1"));
}
assertEquals(1, executeUpdate(manager, "DELETE FROM tblspace1.tsql where k1 =? and n1=?", Arrays.asList("mykey", Integer.valueOf(1236))).getUpdateCount());
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql where k1 ='mykey' and n1=1236", Collections.emptyList())) {
assertEquals(0, scan1.consume().size());
}
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class SimpleJoinTest method testSimpleJoinNoWhere.
@Test
public void testSimpleJoinNoWhere() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.table1 (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
execute(manager, "CREATE TABLE tblspace1.table2 (k2 string primary key,n2 int,s2 string)", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.table1 (k1,n1,s1) values('a',1,'A')", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.table1 (k1,n1,s1) values('b',2,'B')", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.table2 (k2,n2,s2) values('c',3,'A')", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.table2 (k2,n2,s2) values('d',4,'A')", Collections.emptyList());
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM" + " tblspace1.table1 t1" + " NATURAL JOIN tblspace1.table2 t2" + " WHERE t1.n1 > 0" + " and t2.n2 >= 1", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
System.out.println("t:" + t);
assertEquals(6, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("n1", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
assertEquals("k2", t.getFieldNames()[3]);
assertEquals("n2", t.getFieldNames()[4]);
assertEquals("s2", t.getFieldNames()[5]);
}
assertEquals(4, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "c", "n2", 3, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "d", "n2", 4, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "c", "n2", 3, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "d", "n2", 4, "s2", "A"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT t1.k1, t2.k2 FROM" + " tblspace1.table1 t1 " + " NATURAL JOIN tblspace1.table2 t2 " + " WHERE t1.n1 > 0" + " and t2.n2 >= 1", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(2, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("k2", t.getFieldNames()[1]);
}
assertEquals(4, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "k2", "c"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "k2", "d"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "k2", "c"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "k2", "d"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT t1.k1, t2.k2 FROM" + " tblspace1.table1 t1 " + " NATURAL JOIN tblspace1.table2 t2 " + " WHERE t1.n1 >= 2" + " and t2.n2 >= 4", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(2, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("k2", t.getFieldNames()[1]);
}
assertEquals(1, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "k2", "d"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT t1.*,t2.* FROM" + " tblspace1.table1 t1" + " NATURAL JOIN tblspace1.table2 t2" + " WHERE t1.n1 > 0" + " and t2.n2 >= 1", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(6, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("n1", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
assertEquals("k2", t.getFieldNames()[3]);
assertEquals("n2", t.getFieldNames()[4]);
assertEquals("s2", t.getFieldNames()[5]);
}
assertEquals(4, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "c", "n2", 3, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "d", "n2", 4, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "c", "n2", 3, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "d", "n2", 4, "s2", "A"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT t1.* FROM" + " tblspace1.table1 t1" + " NATURAL JOIN tblspace1.table2 t2" + " WHERE t1.n1 > 0" + " and t2.n2 >= 1", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(3, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("n1", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
}
assertEquals(4, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT t2.* FROM" + " tblspace1.table1 t1" + " NATURAL JOIN tblspace1.table2 t2" + " WHERE t1.n1 > 0" + " and t2.n2 >= 1", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(3, t.getFieldNames().length);
assertEquals("k2", t.getFieldNames()[0]);
assertEquals("n2", t.getFieldNames()[1]);
assertEquals("s2", t.getFieldNames()[2]);
}
assertEquals(4, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "c", "n2", 3, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "d", "n2", 4, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "c", "n2", 3, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "d", "n2", 4, "s2", "A"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT t2.s2 FROM" + " tblspace1.table1 t1" + " NATURAL JOIN tblspace1.table2 t2" + " WHERE t1.n1 > 0" + " and t2.n2 >= 1", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(1, t.getFieldNames().length);
assertEquals("s2", t.getFieldNames()[0]);
}
assertEquals(4, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("s2", "A"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM" + " tblspace1.table1 t1" + " NATURAL JOIN tblspace1.table2 t2" + " WHERE t1.n1 > 0" + " and t2.n2 >= 4", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(6, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("n1", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
assertEquals("k2", t.getFieldNames()[3]);
assertEquals("n2", t.getFieldNames()[4]);
assertEquals("s2", t.getFieldNames()[5]);
}
assertEquals(2, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "d", "n2", 4, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "d", "n2", 4, "s2", "A"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM" + " tblspace1.table1 t1" + " NATURAL JOIN tblspace1.table2 t2" + " WHERE t1.n1 <= t2.n2", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(6, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("n1", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
assertEquals("k2", t.getFieldNames()[3]);
assertEquals("n2", t.getFieldNames()[4]);
assertEquals("s2", t.getFieldNames()[5]);
}
assertEquals(4, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "c", "n2", 3, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "d", "n2", 4, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "c", "n2", 3, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "d", "n2", 4, "s2", "A"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM " + " tblspace1.table1 t1 " + " NATURAL JOIN tblspace1.table2 t2 " + " WHERE t1.n1 <= t2.n2 " + "and t2.n2 <= 3", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(6, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("n1", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
assertEquals("k2", t.getFieldNames()[3]);
assertEquals("n2", t.getFieldNames()[4]);
assertEquals("s2", t.getFieldNames()[5]);
}
assertEquals(2, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "c", "n2", 3, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "c", "n2", 3, "s2", "A"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM " + " tblspace1.table1 t1 " + " JOIN tblspace1.table2 t2 ON t1.n1 <= t2.n2 " + " and t2.n2 <= 3", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(6, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("n1", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
assertEquals("k2", t.getFieldNames()[3]);
assertEquals("n2", t.getFieldNames()[4]);
assertEquals("s2", t.getFieldNames()[5]);
}
assertEquals(2, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "c", "n2", 3, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "c", "n2", 3, "s2", "A"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT t1.k1, t2.k2 FROM" + " tblspace1.table1 t1 " + " NATURAL JOIN tblspace1.table2 t2 " + " WHERE t1.n1 + 3 = t2.n2", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
System.out.println("tuple -: " + t.toMap());
assertEquals(2, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("k2", t.getFieldNames()[1]);
}
assertEquals(1, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "k2", "d"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT t2.n2, t1.s1, t2.k2 FROM" + " tblspace1.table1 t1" + " NATURAL JOIN tblspace1.table2 t2" + " WHERE t1.n1 > 0" + " and t2.n2 >= 1", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(3, t.getFieldNames().length);
assertEquals("n2", t.getFieldNames()[0]);
assertEquals("s1", t.getFieldNames()[1]);
assertEquals("k2", t.getFieldNames()[2]);
}
assertEquals(4, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("s1", "A", "k2", "c", "n2", 3))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("s1", "A", "k2", "d", "n2", 4))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("s1", "B", "k2", "c", "n2", 3))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("s1", "B", "k2", "d", "n2", 4))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT t2.*, t1.* FROM" + " tblspace1.table1 t1" + " NATURAL JOIN tblspace1.table2 t2" + " WHERE t1.n1 > 0" + " and t2.n2 >= 1", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(6, t.getFieldNames().length);
assertEquals("k2", t.getFieldNames()[0]);
assertEquals("n2", t.getFieldNames()[1]);
assertEquals("s2", t.getFieldNames()[2]);
assertEquals("k1", t.getFieldNames()[3]);
assertEquals("n1", t.getFieldNames()[4]);
assertEquals("s1", t.getFieldNames()[5]);
}
assertEquals(4, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "c", "n2", 3, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "d", "n2", 4, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "c", "n2", 3, "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "d", "n2", 4, "s2", "A"))));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM" + " tblspace1.table1 t1" + " NATURAL JOIN tblspace1.table2 t2 " + " ORDER BY n2,n1", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(6, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("n1", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
assertEquals("k2", t.getFieldNames()[3]);
assertEquals("n2", t.getFieldNames()[4]);
assertEquals("s2", t.getFieldNames()[5]);
}
assertEquals(4, tuples.size());
int i = 0;
assertTrue(tuples.get(i++).toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "c", "n2", 3, "s2", "A")));
assertTrue(tuples.get(i++).toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "c", "n2", 3, "s2", "A")));
assertTrue(tuples.get(i++).toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "d", "n2", 4, "s2", "A")));
assertTrue(tuples.get(i++).toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "d", "n2", 4, "s2", "A")));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM" + " tblspace1.table1 t1" + " NATURAL JOIN tblspace1.table2 t2 " + " ORDER BY n2 desc,n1", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
assertEquals(6, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("n1", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
assertEquals("k2", t.getFieldNames()[3]);
assertEquals("n2", t.getFieldNames()[4]);
assertEquals("s2", t.getFieldNames()[5]);
}
assertEquals(4, tuples.size());
int i = 0;
assertTrue(tuples.get(i++).toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "d", "n2", 4, "s2", "A")));
assertTrue(tuples.get(i++).toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "d", "n2", 4, "s2", "A")));
assertTrue(tuples.get(i++).toMap().equals(MapUtils.map("k1", "a", "n1", 1, "s1", "A", "k2", "c", "n2", 3, "s2", "A")));
assertTrue(tuples.get(i++).toMap().equals(MapUtils.map("k1", "b", "n1", 2, "s1", "B", "k2", "c", "n2", 3, "s2", "A")));
}
{
List<DataAccessor> tuples = scan(manager, "SELECT t1.k1, t2.k2 FROM" + " tblspace1.table1 t1 " + " NATURAL JOIN tblspace1.table2 t2 " + " WHERE t1.n1 + 3 <= t2.n2", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
System.out.println("tuple -: " + t.toMap());
assertEquals(2, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("k2", t.getFieldNames()[1]);
}
assertEquals(1, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "k2", "d"))));
}
if (manager.getPlanner() instanceof CalcitePlanner) {
List<DataAccessor> tuples = scan(manager, "SELECT t1.k1, t2.k2, t1.s1, t2.s2 FROM" + " tblspace1.table1 t1 " + " LEFT JOIN tblspace1.table2 t2 " + " ON t1.s1 = t2.s2" + " ", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
System.out.println("tuple -: " + t.toMap());
assertEquals(4, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("k2", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
assertEquals("s2", t.getFieldNames()[3]);
}
assertEquals(3, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "k2", "c", "s1", "A", "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "k2", "d", "s1", "A", "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "b", "k2", null, "s1", "B", "s2", null))));
}
if (manager.getPlanner() instanceof CalcitePlanner) {
List<DataAccessor> tuples = scan(manager, "SELECT t1.k1, t2.k2, t1.s1, t2.s2 FROM" + " tblspace1.table1 t1 " + " RIGHT JOIN tblspace1.table2 t2 " + " ON t1.s1 = t2.s2" + " ", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
System.out.println("tuple -: " + t.toMap());
assertEquals(4, t.getFieldNames().length);
assertEquals("k1", t.getFieldNames()[0]);
assertEquals("k2", t.getFieldNames()[1]);
assertEquals("s1", t.getFieldNames()[2]);
assertEquals("s2", t.getFieldNames()[3]);
}
assertEquals(2, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "k2", "c", "s1", "A", "s2", "A"))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "a", "k2", "d", "s1", "A", "s2", "A"))));
}
}
}
Aggregations