use of herddb.utils.DataAccessor in project herddb by diennea.
the class RecordSetSuite method testLimitsAfterEndSwap.
@Test
public void testLimitsAfterEndSwap() throws Exception {
RecordSetFactory factory = buildRecordSetFactory(1);
Column[] columns = new Column[2];
columns[0] = Column.column("s1", ColumnTypes.STRING);
columns[1] = Column.column("n1", ColumnTypes.LONG);
String[] fieldNames = Column.buildFieldNamesList(columns);
try (MaterializedRecordSet rs = factory.createRecordSet(fieldNames, columns)) {
for (int i = 0; i < 100; i++) {
Map<String, Object> record = new HashMap<>();
String s1 = "test_" + i;
record.put("s1", s1);
record.put("n1", i);
rs.add(new Tuple(record, fieldNames));
}
rs.writeFinished();
rs.applyLimits(new ScanLimitsImpl(20, 100000), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT());
for (DataAccessor t : rs) {
fail();
}
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class RecordSetSuite method testLimitsNoSwap.
@Test
public void testLimitsNoSwap() throws Exception {
RecordSetFactory factory = buildRecordSetFactory(Integer.MAX_VALUE);
Column[] columns = new Column[2];
columns[0] = Column.column("s1", ColumnTypes.STRING);
columns[1] = Column.column("n1", ColumnTypes.LONG);
String[] fieldNames = Column.buildFieldNamesList(columns);
try (MaterializedRecordSet rs = factory.createRecordSet(fieldNames, columns)) {
Set<String> expected_s1 = new HashSet<>();
Set<Integer> expected_n1 = new HashSet<>();
for (int i = 0; i < 100; i++) {
Map<String, Object> record = new HashMap<>();
String s1 = "test_" + i;
record.put("s1", s1);
record.put("n1", i);
if (i >= 10 && i < 30) {
expected_s1.add(s1);
expected_n1.add(i);
}
rs.add(new Tuple(record, fieldNames));
}
rs.writeFinished();
rs.applyLimits(new ScanLimitsImpl(20, 10), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT());
for (DataAccessor t : rs) {
expected_s1.remove(t.get("s1").toString());
expected_n1.remove(t.get("n1"));
}
assertTrue(expected_n1.isEmpty());
assertTrue(expected_s1.isEmpty());
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class AutocheckPointTest method autoCheckPointTest.
@Test
public void autoCheckPointTest() throws Exception {
Path dataPath = folder.newFolder("data").toPath();
Path logsPath = folder.newFolder("logs").toPath();
Path metadataPath = folder.newFolder("metadata").toPath();
Path tmoDir = folder.newFolder("tmoDir").toPath();
String nodeId = "localhost";
try (DBManager manager = new DBManager(nodeId, new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (K1 int ,s1 string,n1 int, primary key(k1))", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList(1, "a", Integer.valueOf(1234))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList(2, "a", Integer.valueOf(1234))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList(3, "a", Integer.valueOf(1234))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList(4, "a", Integer.valueOf(1234))).getUpdateCount());
manager.checkpoint();
long tx = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
execute(manager, "UPDATE tblspace1.tsql set s1='b' where k1=1", Collections.emptyList(), new TransactionContext(tx));
long lastCheckpont = manager.getLastCheckPointTs();
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql WHERE N1=1234", Collections.emptyList(), new TransactionContext(tx))) {
List<DataAccessor> data = scan.consume();
assertEquals(4, data.size());
}
manager.setCheckpointPeriod(1000);
for (int i = 0; i < 100; i++) {
if (lastCheckpont != manager.getLastCheckPointTs()) {
break;
}
Thread.sleep(100);
}
assertNotEquals(lastCheckpont, manager.getLastCheckPointTs());
execute(manager, "EXECUTE committransaction 'tblspace1'," + tx, Collections.emptyList());
assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql WHERE N1=1234 and s1='b'", Collections.emptyList()).consumeAndClose().size());
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class DataConsistencyCheckTest method consistencyCheckSyntax.
@Test
public void consistencyCheckSyntax() throws Exception {
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
assertTrue(manager.waitForBootOfLocalTablespaces(10000));
execute(manager, "CREATE TABLESPACE 'consistence'", Collections.emptyList());
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE consistence.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
execute(manager, "CREATE TABLE consistence.tsql1 (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
execute(manager, "CREATE TABLE consistence.tsql2 (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
for (int i = 0; i < 10; i++) {
execute(manager, "INSERT INTO consistence.tsql (k1,n1 ,s1) values (?,?,?)", Arrays.asList(i, 1, "b"));
execute(manager, "INSERT INTO consistence.tsql1 (k1,n1 ,s1) values (?,?,?)", Arrays.asList(i, 1, "b"));
execute(manager, "INSERT INTO consistence.tsql2 (k1,n1 ,s1) values (?,?,?)", Arrays.asList(i, 1, "b"));
}
try (DataScanner scan = TestUtils.scan(manager, "SELECT COUNT(*) FROM consistence.tsql", Collections.emptyList())) {
DataAccessor first = scan.consume().get(0);
Number count = (Number) first.get(first.getFieldNames()[0]);
assertEquals(10, count.intValue());
}
try (DataScanner scan = TestUtils.scan(manager, "SELECT COUNT(*) FROM consistence.tsql1", Collections.emptyList())) {
DataAccessor first = scan.consume().get(0);
Number count = (Number) first.get(first.getFieldNames()[0]);
assertEquals(10, count.intValue());
}
try (DataScanner scan = TestUtils.scan(manager, "SELECT COUNT(*) FROM consistence.tsql2", Collections.emptyList())) {
DataAccessor first = scan.consume().get(0);
Number count = (Number) first.get(first.getFieldNames()[0]);
assertEquals(10, count.intValue());
}
execute(manager, "tableconsistencycheck consistence.tsql1", Collections.emptyList());
execute(manager, "tableconsistencycheck consistence.tsql2", Collections.emptyList());
execute(manager, "tablespaceconsistencycheck consistence", Collections.emptyList());
execute(manager, "tablespaceconsistencycheck 'consistence'", Collections.emptyList());
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class PrimaryIndexScanRangeTest method primaryIndexPrefixScanWithStringsFileTest.
@Test
public void primaryIndexPrefixScanWithStringsFileTest() throws Exception {
Path dataPath = folder.newFolder("data").toPath();
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new FileDataStorageManager(dataPath), 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);
Table table = Table.builder().tablespace("tblspace1").name("t1").column("n1", ColumnTypes.STRING).column("n2", ColumnTypes.INTEGER).column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("n1").build();
CreateTableStatement st2 = new CreateTableStatement(table);
manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('a','1',5,'n1')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('b','2',5,'n1')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('c','3',6,'n2')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('d','5',7,'n2')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('e','6',5,'n2')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('f','7',7,'n2')", Collections.emptyList());
assertTrue(manager.getTableSpaceManager("tblspace1").getTableManager("t1").isKeyToPageSortedAscending());
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=6 " + "order by n1", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(3, tuples.size());
assertEquals(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), tuples.get(1).get("n1"));
assertEquals(RawString.of("6"), tuples.get(2).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=6 " + "order by n1 " + "limit 2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=6 " + "limit 2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertNull(scan.getComparator());
assertNotNull(scan.getLimits());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=6 " + "order by n1 " + "limit 1,3", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(RawString.of("3"), tuples.get(0).get("n1"));
assertEquals(RawString.of("6"), tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=7 " + "order by n1 " + "limit 1,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(RawString.of("3"), tuples.get(0).get("n1"));
assertEquals(RawString.of("5"), tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=7 " + "order by n1 " + "limit 3,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(RawString.of("6"), tuples.get(0).get("n1"));
assertEquals(RawString.of("7"), tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=7 " + "order by n1 " + "limit 4,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(1, tuples.size());
assertEquals(RawString.of("7"), tuples.get(0).get("n1"));
}
}
// add a record in the middle of the sorted recordset
DMLStatementExecutionResult res = TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('g','4',6,'n2')", Collections.emptyList(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
long tx = res.transactionId;
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=6 " + "order by n1", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(4, tuples.size());
assertEquals(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), tuples.get(1).get("n1"));
assertEquals(RawString.of("4"), tuples.get(2).get("n1"));
assertEquals(RawString.of("6"), tuples.get(3).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=7 " + "order by n1", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(6, tuples.size());
assertEquals(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), tuples.get(1).get("n1"));
assertEquals(RawString.of("4"), tuples.get(2).get("n1"));
assertEquals(RawString.of("5"), tuples.get(3).get("n1"));
assertEquals(RawString.of("6"), tuples.get(4).get("n1"));
assertEquals(RawString.of("7"), tuples.get(5).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=6 " + "order by n1 " + "limit 2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=7 " + "order by n1 " + "limit 1,3", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
for (DataAccessor tuple : tuples) {
System.out.println("tuple: " + tuple.toMap());
}
assertEquals(3, tuples.size());
assertEquals(RawString.of("3"), tuples.get(0).get("n1"));
assertEquals(RawString.of("4"), tuples.get(1).get("n1"));
assertEquals(RawString.of("5"), tuples.get(2).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=6 " + "order by n1 " + "limit 1,3", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(3, tuples.size());
assertEquals(RawString.of("3"), tuples.get(0).get("n1"));
assertEquals(RawString.of("4"), tuples.get(1).get("n1"));
assertEquals(RawString.of("6"), tuples.get(2).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=7 " + "order by n1 " + "limit 1,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(RawString.of("3"), tuples.get(0).get("n1"));
assertEquals(RawString.of("4"), tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=7 " + "order by n1 " + "limit 3,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(RawString.of("5"), tuples.get(0).get("n1"));
assertEquals(RawString.of("6"), tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=7 " + "order by n1 " + "limit 4,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(2, tuples.size());
assertEquals(RawString.of("6"), tuples.get(0).get("n1"));
assertEquals(RawString.of("7"), tuples.get(1).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=7 " + "order by n1 " + "limit 5,2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(1, tuples.size());
assertEquals(RawString.of("7"), tuples.get(0).get("n1"));
}
}
// add other records in the context of the transaction
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('g','8',6,'n3')", Collections.emptyList(), new TransactionContext(tx));
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('g','9',6,'n3')", Collections.emptyList(), new TransactionContext(tx));
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('g','A',6,'n3')", Collections.emptyList(), new TransactionContext(tx));
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=7 " + "order by n1 ", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(9, tuples.size());
assertEquals(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), tuples.get(1).get("n1"));
assertEquals(RawString.of("4"), tuples.get(2).get("n1"));
assertEquals(RawString.of("5"), tuples.get(3).get("n1"));
assertEquals(RawString.of("6"), tuples.get(4).get("n1"));
assertEquals(RawString.of("7"), tuples.get(5).get("n1"));
assertEquals(RawString.of("8"), tuples.get(6).get("n1"));
assertEquals(RawString.of("9"), tuples.get(7).get("n1"));
assertEquals(RawString.of("A"), tuples.get(8).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and n2<=7 " + "order by n1 " + "limit 7", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
assertEquals(7, tuples.size());
assertEquals(RawString.of("2"), tuples.get(0).get("n1"));
assertEquals(RawString.of("3"), tuples.get(1).get("n1"));
assertEquals(RawString.of("4"), tuples.get(2).get("n1"));
assertEquals(RawString.of("5"), tuples.get(3).get("n1"));
assertEquals(RawString.of("6"), tuples.get(4).get("n1"));
assertEquals(RawString.of("7"), tuples.get(5).get("n1"));
assertEquals(RawString.of("8"), tuples.get(6).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2,name " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and name='n3' " + "order by n1 " + "limit 200", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
tuples.forEach(t -> {
System.out.println("OK sortedByClusteredIndex tuple " + t.toMap());
});
assertEquals(3, tuples.size());
assertEquals(RawString.of("8"), tuples.get(0).get("n1"));
assertEquals(RawString.of("9"), tuples.get(1).get("n1"));
assertEquals(RawString.of("A"), tuples.get(2).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2,name " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and name='n3' " + "order by n1 ", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
tuples.forEach(t -> {
System.out.println("OK sortedByClusteredIndex tuple " + t.toMap());
});
assertEquals(3, tuples.size());
assertEquals(RawString.of("8"), tuples.get(0).get("n1"));
assertEquals(RawString.of("9"), tuples.get(1).get("n1"));
assertEquals(RawString.of("A"), tuples.get(2).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2,name " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and name='n3' " + "order by n1 desc", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertFalse(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
tuples.forEach(t -> {
System.out.println("OK sortedByClusteredIndex tuple " + t.toMap());
});
assertEquals(3, tuples.size());
assertEquals(RawString.of("A"), tuples.get(0).get("n1"));
assertEquals(RawString.of("9"), tuples.get(1).get("n1"));
assertEquals(RawString.of("8"), tuples.get(2).get("n1"));
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2,name " + "FROM tblspace1.t1 " + "WHERE n1>='2' " + "and name='n3' " + "order by n1 " + "limit 2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
List<DataAccessor> tuples = scan1.consume();
tuples.forEach(t -> {
System.out.println("OK sortedByClusteredIndex tuple " + t.toMap());
});
assertEquals(2, tuples.size());
assertEquals(RawString.of("8"), tuples.get(0).get("n1"));
assertEquals(RawString.of("9"), tuples.get(1).get("n1"));
}
}
}
}
Aggregations