#P10486. SQL Query Parser

    ID: 12499 Type: Default 1000ms 256MiB

SQL Query Parser

SQL Query Parser

This problem requires you to implement a simple SQL query parser. The database is a collection of two-dimensional tables. Each table has a header row (listing the column names) followed by data rows, where each field is a string.

You should support queries in the following format:

select [columns] from [table_name] where [header]=x

Here, [columns] is a comma-separated list of column names (ordered) that you need to output, [table_name] is the name of one table in the database, and [header]=x is the filter condition; i.e. you need to find all rows in the specified table where the value in the column [header] exactly matches x. For each matching row, print the values of the specified columns in order, separated by a single space. If multiple rows match, output them in the order they appear in the table.

Example:

Table basic_info:
name sid grade
Fusu 001 1
Maxmilite 002 2
Expect2004 003 2

Query: select name from basic_info where grade=1 Output: Fusu

Query: select name,sid,grade from basic_info where grade=2 Output: Maxmilite 002 2 Expect2004 003 2

</p>

Note: In a full SQL language you may need to support joins and multiple conditions, but for this problem you only have to work with a single table per query and a single equality condition.

inputFormat

The input begins with an integer T, the number of tables in the database.

For each table, the following input is provided:

  • A line containing the table name, an integer R (number of data rows), and an integer C (number of columns), separated by spaces.
  • A line containing C strings representing the header (column names).
  • R lines follow, each containing C strings representing a data row. Each field is a string without spaces.

After all tables, a line containing a SQL query is given. The SQL query has the format:

select [columns] from [table_name] where [header]=x

There is exactly one table referenced in the query and exactly one equality condition.

outputFormat

For the given query, output the selected columns for every row in the specified table where the condition holds, each row on a new line. In each line, the column values should be printed in the order specified in the query and separated by a single space. If no rows match, output nothing.

sample

1
basic_info 3 3
name sid grade
Fusu 001 1
Maxmilite 002 2
Expect2004 003 2
select name from basic_info where grade=1
Fusu