use of com.questdb.ql.RecordSource in project questdb by bluestreak01.
the class AbstractOptimiserTest method assertThat0.
private void assertThat0(String expected, String query, boolean header) throws ParserException, IOException {
RecordSource rs = cache.peek(query);
if (rs == null) {
cache.put(query, rs = compiler.compile(FACTORY_CONTAINER.getFactory(), query));
}
assertThat(expected, rs, header);
}
use of com.questdb.ql.RecordSource in project questdb by bluestreak01.
the class AbstractOptimiserTest method assertRowId.
protected static void assertRowId(String query, String longColumn) throws ParserException {
RecordSource src = compiler.compile(FACTORY_CONTAINER.getFactory(), query);
try {
RecordCursor cursor = src.prepareCursor(FACTORY_CONTAINER.getFactory());
try {
int dateIndex = src.getMetadata().getColumnIndex(longColumn);
HashMap<Long, Long> map = new HashMap<>();
long count = 0;
while (cursor.hasNext()) {
Record record = cursor.next();
map.put(record.getRowId(), record.getLong(dateIndex));
count++;
}
Assert.assertTrue(count > 0);
Record record = cursor.newRecord();
for (Map.Entry<Long, Long> e : map.entrySet()) {
Assert.assertEquals((long) e.getValue(), cursor.recordAt(e.getKey()).getLong(dateIndex));
cursor.recordAt(record, e.getKey());
Assert.assertEquals((long) e.getValue(), record.getLong(dateIndex));
}
} finally {
cursor.releaseCursor();
}
} finally {
Misc.free(src);
}
}
use of com.questdb.ql.RecordSource in project questdb by bluestreak01.
the class JoinQueryTest method testJoinNoRowid.
@Test
public void testJoinNoRowid() throws Exception {
final String expected = "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t104281903\t100\t1138\tKWK\t2015-07-10T00:00:14.518Z\tFBLGGTZEN\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t1191623531\t100\t1210\tSR\t2015-07-10T00:00:18.175Z\tYM\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t1662742408\t100\t1828\tQH\t2015-07-10T00:00:19.509Z\tEYBI\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t220389\t100\t1293\tDGEEWB\t2015-07-10T00:00:56.196Z\tEYBI\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t798408721\t100\t803\tZIHLGS\t2015-07-10T00:00:56.977Z\tVTNNKVOLHLLNN\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t966974434\t100\t870\tJEOBQ\t2015-07-10T00:00:57.981Z\tW\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t258318715\t100\t1036\tOPWOGS\t2015-07-10T00:01:00.608Z\tEYBI\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t1528068156\t100\t400\tYBQE\t2015-07-10T00:01:20.643Z\tQXOLEEXZ\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t1935884354\t100\t1503\tD\t2015-07-10T00:01:43.507Z\tRZVZJQRNYSRKZSJ\n";
final RecordSource m = compileSource("customers where customerName ~ 'PJFSREKEUNMKWOF'");
final RecordSource s = new NoRowIdRecordSource().of(compileSource("orders"));
RecordSource r = new HashJoinRecordSource(m, new IntList() {
{
add(m.getMetadata().getColumnIndex("customerId"));
}
}, s, new IntList() {
{
add(s.getMetadata().getColumnIndex("customerId"));
}
}, false, 4 * 1024 * 1024, 4 * 1024 * 1024, 1024 * 1024, new RecordKeyCopierCompiler(new BytecodeAssembler()));
sink.clear();
printer.print(r, FACTORY_CONTAINER.getFactory());
TestUtils.assertEquals(expected, sink);
assertThat(expected, "customers c join orders o on c.customerId = o.customerId where customerName ~ 'PJFSREKEUNMKWOF'");
}
use of com.questdb.ql.RecordSource in project questdb by bluestreak01.
the class SQLExamples method main.
public static void main(String[] args) throws JournalException, ParserException, IOException {
if (args.length < 1) {
System.out.println("Usage: SQLExamples <path>");
System.exit(1);
}
try (Factory factory = new Factory(args[0], 1000, 1, 0)) {
// import movies data to query
ImportManager.importFile(factory, SQLExamples.class.getResource("/movies.csv").getFile(), ',', null, false);
// Create SQL engine instance.
QueryCompiler compiler = new QueryCompiler();
try (RecordSource rs = compiler.compile(factory, "'movies.csv'")) {
// Execute query and fetch results
RecordCursor cursor = rs.prepareCursor(factory);
try {
while (cursor.hasNext()) {
Record record = cursor.next();
}
} finally {
cursor.releaseCursor();
}
}
// to simplify query demonstration we have generic record source printer
RecordSourcePrinter printer = new RecordSourcePrinter(new StdoutSink());
printer.print(compiler.compile(factory, "'movies.csv'"), factory);
System.out.println("---------");
// find movie by ID
printer.print(compiler.compile(factory, "'movies.csv' where movieId = 62198"), factory);
System.out.println("---------");
// extract year from movie title
printer.print(compiler.compile(factory, "select title, pluck('\\(([0-9]*?)\\)', title) year from 'movies.csv' where movieId = 62198"), factory);
System.out.println("---------");
// order by movie year descending
printer.print(compiler.compile(factory, "select title, pluck('\\(([0-9]*?)\\)', title) year from 'movies.csv' order by year desc"), factory);
System.out.println("---------");
// count titles by year
printer.print(compiler.compile(factory, "select year, count() from (select title, pluck('\\(([0-9]*?)\\)', title) year from 'movies.csv' order by year desc)"), factory);
}
}
use of com.questdb.ql.RecordSource in project questdb by bluestreak01.
the class SQLExportToTextFile method main.
public static void main(String[] args) throws JournalException, IOException, ParserException {
if (args.length < 1) {
System.out.println("Usage: SQLExportToTextFile <path>");
System.exit(1);
}
try (Factory factory = new Factory(args[0], 1000, 1, 0)) {
// import movies data to query
ImportManager.importFile(factory, SQLExamples.class.getResource("/movies.csv").getFile(), ',', null, false);
// Create SQL engine instance.
QueryCompiler compiler = new QueryCompiler();
try (RecordSource rs = compiler.compile(factory, "select year, count() count from (select title, match('\\(([0-9]*?)\\)', title) year from 'movies.csv' order by year desc)")) {
ExportManager.export(rs, factory, new File(args[0], "export.csv"), ',');
}
}
}
Aggregations