Skip to content
This repository has been archived by the owner on Feb 8, 2019. It is now read-only.

Implement parser and resolver for UNION and INTERSECT operators #229

Merged
merged 1 commit into from Apr 9, 2017

Conversation

Terranlee
Copy link
Contributor

This pull request implements the parser and resolver parts of set operations (UNION and INTERSECT).

It also supports composite set operations, e.g.

SELECT * FROM a
UNION ALL
SELECT * FROM b
UNION ALL
SELECT * FROM c
INTERSECT
SELECT * FROM d;

Note: will add parser and resolver tests, so do not merge yet.

* @brief The possible types of set operations.
*/
enum SetOperationType {
kIntersect,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change to kIntersect = 0,.

case kSingle:
return "Single";
default:
return "Unknown";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest to change to LOG(FATAL) << "Unknown set operation type.";.


private:
PtrList<ParseTreeNode> operands_;
SetOperationType set_operation_type_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mark as const.

*/
enum SetOperationType {
kIntersect,
kSingle,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: does kSingle has an extra meaning beyond representing a SELECT query?

@@ -479,49 +480,49 @@ class ParseStatementDropTable : public ParseStatement {
};

/**
* @brief The parsed representation of a SELECT statement.
* @brief The parsed representation of a UNION/INTERSECT/SELECT statement.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: a to an.

<< "and " << possible_type.getName();
}
} else {
THROW_SQL_ERROR_AT(&parse_set_operations)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduce to two-whitespace-indentation.

// Get the possible output attributes that the attributes of all operands can cast to.
std::vector<E::AttributeReferencePtr> possible_attributes;
for (std::size_t aid = 0; aid < operation_attributes.size(); ++aid) {
E::AttributeReferencePtr possible_attribute = attribute_matrix[0][aid];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark const?

E::AttributeReference::Create(context_->nextExprId(),
attr->attribute_name(),
attr->attribute_alias(),
""/* relation_name*/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to "" /* relation_name */,

/**
* @brief Resolves a set operation and returns a logical plan.
*/
logical::LogicalPtr resolveSetOperationDispatcher(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about renaming to resolveSetOperation?

L::SetOperation::kUnionAll, resolved_operations, output_attributes);
default:
LOG(FATAL) << "Unknown operation: " << parse_set_operations.toString();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add return nullptr; to fix the error in Travis CI.

@@ -676,7 +686,7 @@ sql_statement:
| quit_statement {
$$ = $1;
}
| select_statement {
| set_operation_statement {
Copy link
Member

@zuyu zuyu Apr 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we add set_operation_statement, while still keeping select_statement so that we don't need to modify test files with SELECT-only queries?

Copy link
Contributor

@jianqiao jianqiao Apr 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will lead to a conflict -- the parser cannot decide whether a SELECT query should be reduced to a select statement or a set operation statement.

@jianqiao
Copy link
Contributor

jianqiao commented Apr 8, 2017

LGTM! Merging.

@zuyu
Copy link
Member

zuyu commented Apr 8, 2017

@jianqiao We need to wait for squashing commits into one before merge.

@asfgit asfgit merged commit 5b7b5cb into apache:master Apr 9, 2017
$$ = new quickstep::ParseSetOperation(@1.first_line, @1.first_column, quickstep::ParseSetOperation::kSelect);
$$->addOperand($1);
}

select_query:
TOKEN_SELECT opt_all_distinct selection from_clause opt_where_clause opt_group_by_clause opt_having_clause
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I observed the following warning:

SqlParser.ypp:1243.16-31: warning: unused value: $2 [-Wother]
   TOKEN_SELECT opt_all_distinct selection from_clause opt_where_clause opt_group_by_clause opt_having_clause
                ^^^^^^^^^^^^^^^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from the original ypp file, since we do not implement the select query like 'SELECT ALL ...' or 'SELECT DISTINCT ...'

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
4 participants