[关闭]
@w1024020103 2017-03-21T17:06:56.000000Z 字数 3418 阅读 502

Proj2 2.6Query walkthrough

CS61B


2.6. Query walkthrough

The following code implements a simple join query between two tables, each consisting of three columns of integers. (The file some_data_file1.dat and some_data_file2.dat are binary representation of the pages from this file). This code is equivalent to the SQL statement:

SELECT *
FROM some_data_file1, some_data_file2
WHERE some_data_file1.field1 = some_data_file2.field1
AND some_data_file1.id > 1

For more extensive examples of query operations, you may find it helpful to browse the unit tests for joins, filters, and aggregates.
下面的代码实现了两个table之间的join命令,每个table由三个整数列构成。(some_data_file1.dat和some_data_file2.dat是这个文件内的页的二进制表示),这段代码等价于下面的SQL命令。

SELECT *
FROM some_data_file1, some_data_file2
WHERE some_data_file1.field1 = some_data_file2.field1
AND some_data_file1.id > 1

如果想要更多的例子,可以看joins,filters和aggregates的unit tests.

  1. package simpledb;
  2. import java.io.*;
  3. public class jointest {
  4. public static void main(String[] argv) {
  5. // construct a 3-column table schema
  6. Type types[] = new Type[]{ Type.INT_TYPE, Type.INT_TYPE, Type.INT_TYPE };
  7. String names[] = new String[]{ "field0", "field1", "field2" };
  8. TupleDesc td = new TupleDesc(types, names);
  9. // create the tables, associate them with the data files
  10. // and tell the catalog about the schema the tables.
  11. HeapFile table1 = new HeapFile(new File("some_data_file1.dat"), td);
  12. Database.getCatalog().addTable(table1, "t1");
  13. HeapFile table2 = new HeapFile(new File("some_data_file2.dat"), td);
  14. Database.getCatalog().addTable(table2, "t2");
  15. // construct the query: we use two SeqScans, which spoonfeed
  16. // tuples via iterators into join
  17. TransactionId tid = new TransactionId();
  18. SeqScan ss1 = new SeqScan(tid, table1.getId(), "t1");
  19. SeqScan ss2 = new SeqScan(tid, table2.getId(), "t2");
  20. // create a filter for the where condition
  21. Filter sf1 = new Filter(
  22. new Predicate(0,
  23. Predicate.Op.GREATER_THAN, new IntField(1)), ss1);
  24. JoinPredicate p = new JoinPredicate(1, Predicate.Op.EQUALS, 1);
  25. Join j = new Join(p, ss1, ss2);
  26. // and run it
  27. try {
  28. j.open();
  29. while (j.hasNext()) {
  30. Tuple tup = j.next();
  31. System.out.println(tup);
  32. }
  33. j.close();
  34. Database.getBufferPool().transactionComplete(tid);
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }

Both tables have three integer fields. To express this, we create a TupleDesc object and pass it an array of Type objects indicating field types and String objects indicating field names. Once we have created this TupleDesc, we initialize two HeapFile objects representing the tables. Once we have created the tables, we add them to the Catalog. (If this were a database server that was already running, we would have this catalog information loaded; we need to load this only for the purposes of this test).
两个table都有三个整数列。为了表达这个,我们新建了一个RowDesc对象,传给它一个Type对象的数组来表示每一列的类型,和一个String对象的数组来表述列名。一旦我们完成建立这个RowDesc,我们初始化两个HeapFile对象来表示tables.一旦我们建好了tables,就把他们加到Catalog里面。(如果这是一个已经在运行的数据库服务器,catalog信息应该已经被载入了;我们仅仅为了这次测试而载入。)

Once we have finished initializing the database system, we create a query plan. Our plan consists of two SeqScan operators that scan the tuples from each file on disk, connected to a Filter operator on the first HeapFile, connected to a Join operator that joins the tuples in the tables according to the JoinPredicate. In general, these operators are instantiated with references to the appropriate table (in the case of SeqScan) or child operator (in the case of e.g., Join). The test program then repeatedly calls next on the Join operator, which in turn pulls tuples from its children. As tuples are output from the Join, they are printed out on the command line.
一旦我们完成数据库系统的初始化,我们新建一个query plan。我们的plan由两个SeqScan运算符构成,他们可以扫描磁盘上两个文件中每一个的rows,在第一个HeapFile中连接到一个Filter运算符,连接一个Join运算符来根据JoinPredicate来连接tables里的rows. 总而言之,这些运算符初始化的时候,有着适当table(比如SeqScan)的引用,或是子运算符的引用(比如Join).测试程序反复调用在Join运算符里调用next,它可以从子运算符里拉出rows。当rows从Join方法输出时,他们被打印在命令行里。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注