Skip to content

Commit 5a5fd24

Browse files
authored
Enable Queries on Imported/Exported Tables (#225)
This PR enables filters on import/export of tables according to the Postgres SQL interface. The export filter will also be implemented in Hyrise.
1 parent 18b9d08 commit 5a5fd24

15 files changed

Lines changed: 1947 additions & 1839 deletions

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ cmake-build-debug/
4848

4949
# macOS compilation dirs
5050
*.dSYM
51-
52-
.vscode/*
51+
.DS_STORE
52+
53+
.vscode/*

src/SQLParserResult.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace hsql {
66

7-
SQLParserResult::SQLParserResult() : isValid_(false), errorMsg_(nullptr){};
7+
SQLParserResult::SQLParserResult() : isValid_(false), errorMsg_(nullptr) {}
88

9-
SQLParserResult::SQLParserResult(SQLStatement* stmt) : isValid_(false), errorMsg_(nullptr) { addStatement(stmt); };
9+
SQLParserResult::SQLParserResult(SQLStatement* stmt) : isValid_(false), errorMsg_(nullptr) { addStatement(stmt); }
1010

1111
// Move constructor.
1212
SQLParserResult::SQLParserResult(SQLParserResult&& moved) { *this = std::forward<SQLParserResult>(moved); }

src/parser/bison_parser.cpp

Lines changed: 1574 additions & 1545 deletions
Large diffs are not rendered by default.

src/parser/bison_parser.h

Lines changed: 198 additions & 207 deletions
Large diffs are not rendered by default.

src/parser/bison_parser.y

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
// Specify code that is included in the generated .h and .c files
3737
// clang-format off
3838
%code requires {
39-
// clang-format on
40-
// %code requires block
39+
// %code requires block
4140

4241
#include "../SQLParserResult.h"
4342
#include "../sql/statements.h"
@@ -64,7 +63,6 @@
6463
// %defines "bison_parser.h"
6564

6665
// Tell bison to create a reentrant parser
67-
// clang-format off
6866
%define api.pure full
6967

7068
// Prefix the parser
@@ -164,9 +162,12 @@
164162
*********************************/
165163
// clang-format off
166164
%destructor { } <fval> <ival> <bval> <join_type> <order_type> <datetime_field> <column_type_t> <column_constraint_t> <import_type_t> <column_constraint_set> <lock_mode_t> <lock_wait_policy_t>
167-
%destructor { free( ($$.name) ); free( ($$.schema) ); } <table_name>
168165
%destructor {
169-
if (($$) != nullptr) {
166+
free( ($$.name) );
167+
free( ($$.schema) );
168+
} <table_name>
169+
%destructor {
170+
if ($$) {
170171
for (auto ptr : *($$)) {
171172
free(ptr);
172173
}
@@ -175,7 +176,7 @@
175176
} <str_vec>
176177
%destructor { free( ($$) ); } <sval>
177178
%destructor {
178-
if (($$) != nullptr) {
179+
if ($$) {
179180
for (auto ptr : *($$)) {
180181
delete ptr;
181182
}
@@ -317,7 +318,7 @@ input : statement_list opt_semicolon {
317318

318319
unsigned param_id = 0;
319320
for (void* param : yyloc.param_list) {
320-
if (param != nullptr) {
321+
if (param) {
321322
Expr* expr = (Expr*)param;
322323
expr->ival = param_id;
323324
result->addParameter(expr);
@@ -433,11 +434,12 @@ import_statement : IMPORT FROM file_type FILE file_path INTO table_name {
433434
$$->schema = $7.schema;
434435
$$->tableName = $7.name;
435436
}
436-
| COPY table_name FROM file_path opt_file_type {
437+
| COPY table_name FROM file_path opt_file_type opt_where {
437438
$$ = new ImportStatement($5);
438439
$$->filePath = $4;
439440
$$->schema = $2.schema;
440441
$$->tableName = $2.name;
442+
$$->whereClause = $6;
441443
};
442444

443445
file_type : IDENTIFIER {
@@ -472,6 +474,11 @@ export_statement : COPY table_name TO file_path opt_file_type {
472474
$$->filePath = $4;
473475
$$->schema = $2.schema;
474476
$$->tableName = $2.name;
477+
}
478+
| COPY select_with_paren TO file_path opt_file_type {
479+
$$ = new ExportStatement($5);
480+
$$->filePath = $4;
481+
$$->select = $2;
475482
};
476483

477484
/******************************
@@ -778,12 +785,12 @@ select_no_paren : select_clause opt_order opt_limit opt_locking_clause {
778785
$$->order = $2;
779786

780787
// Limit could have been set by TOP.
781-
if ($3 != nullptr) {
788+
if ($3) {
782789
delete $$->limit;
783790
$$->limit = $3;
784791
}
785792

786-
if ($4 != nullptr) {
793+
if ($4) {
787794
$$->lockings = $4;
788795
}
789796
}
@@ -1233,11 +1240,19 @@ join_clause : table_ref_atomic NATURAL JOIN nonjoin_table_ref_atomic {
12331240
$$->join->left = $1;
12341241
$$->join->right = $4;
12351242
auto left_col = Expr::makeColumnRef(strdup($7->name));
1236-
if ($7->alias != nullptr) left_col->alias = strdup($7->alias);
1237-
if ($1->getName() != nullptr) left_col->table = strdup($1->getName());
1243+
if ($7->alias) {
1244+
left_col->alias = strdup($7->alias);
1245+
}
1246+
if ($1->getName()) {
1247+
left_col->table = strdup($1->getName());
1248+
}
12381249
auto right_col = Expr::makeColumnRef(strdup($7->name));
1239-
if ($7->alias != nullptr) right_col->alias = strdup($7->alias);
1240-
if ($4->getName() != nullptr) right_col->table = strdup($4->getName());
1250+
if ($7->alias) {
1251+
right_col->alias = strdup($7->alias);
1252+
}
1253+
if ($4->getName()) {
1254+
right_col->table = strdup($4->getName());
1255+
}
12411256
$$->join->condition = Expr::makeOpBinary(left_col, kOpEquals, right_col);
12421257
delete $7;
12431258
};

src/sql/CreateStatement.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "SelectStatement.h"
21
#include "CreateStatement.h"
2+
#include "SelectStatement.h"
33

44
namespace hsql {
55

@@ -16,7 +16,7 @@ CreateStatement::CreateStatement(CreateType type)
1616
columns(nullptr),
1717
tableConstraints(nullptr),
1818
viewColumns(nullptr),
19-
select(nullptr){};
19+
select(nullptr) {}
2020

2121
CreateStatement::~CreateStatement() {
2222
free(filePath);
@@ -25,28 +25,28 @@ CreateStatement::~CreateStatement() {
2525
free(indexName);
2626
delete select;
2727

28-
if (columns != nullptr) {
28+
if (columns) {
2929
for (ColumnDefinition* def : *columns) {
3030
delete def;
3131
}
3232
delete columns;
3333
}
3434

35-
if (tableConstraints != nullptr) {
35+
if (tableConstraints) {
3636
for (TableConstraint* def : *tableConstraints) {
3737
delete def;
3838
}
3939
delete tableConstraints;
4040
}
4141

42-
if (indexColumns != nullptr) {
42+
if (indexColumns) {
4343
for (char* column : *indexColumns) {
4444
free(column);
4545
}
4646
delete indexColumns;
4747
}
4848

49-
if (viewColumns != nullptr) {
49+
if (viewColumns) {
5050
for (char* column : *viewColumns) {
5151
free(column);
5252
}

src/sql/ExportStatement.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "ImportStatement.h"
55
#include "SQLStatement.h"
6+
#include "SelectStatement.h"
67

78
namespace hsql {
89
// Represents SQL Export statements.
@@ -15,6 +16,7 @@ struct ExportStatement : SQLStatement {
1516
char* filePath;
1617
char* schema;
1718
char* tableName;
19+
SelectStatement* select;
1820
};
1921

2022
} // namespace hsql

src/sql/Expr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Expr::Expr(ExprType type)
2121
columnType(DataType::UNKNOWN, 0),
2222
isBoolLiteral(false),
2323
opType(kOpNone),
24-
distinct(false){};
24+
distinct(false) {}
2525

2626
Expr::~Expr() {
2727
delete expr;
@@ -31,7 +31,7 @@ Expr::~Expr() {
3131
free(table);
3232
free(alias);
3333

34-
if (exprList != nullptr) {
34+
if (exprList) {
3535
for (Expr* e : *exprList) {
3636
delete e;
3737
}
@@ -255,7 +255,7 @@ bool Expr::hasAlias() const { return alias != nullptr; }
255255
bool Expr::hasTable() const { return table != nullptr; }
256256

257257
const char* Expr::getName() const {
258-
if (alias != nullptr)
258+
if (alias)
259259
return alias;
260260
else
261261
return name;

src/sql/ImportStatement.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct ImportStatement : SQLStatement {
2020
char* filePath;
2121
char* schema;
2222
char* tableName;
23+
Expr* whereClause;
2324
};
2425

2526
} // namespace hsql

src/sql/SQLStatement.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
namespace hsql {
55

66
// SQLStatement
7-
SQLStatement::SQLStatement(StatementType type) : hints(nullptr), type_(type){};
7+
SQLStatement::SQLStatement(StatementType type) : hints(nullptr), type_(type) {}
88

99
SQLStatement::~SQLStatement() {
10-
if (hints != nullptr) {
10+
if (hints) {
1111
for (Expr* hint : *hints) {
1212
delete hint;
1313
}
@@ -21,4 +21,4 @@ bool SQLStatement::isType(StatementType type) const { return (type_ == type); }
2121

2222
bool SQLStatement::is(StatementType type) const { return isType(type); }
2323

24-
} // namespace hsql
24+
} // namespace hsql

0 commit comments

Comments
 (0)