Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added SHOW DATABASES statement #331

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,8 @@ EVQL_CORE_SOURCES_ = \
eventql/sql/statements/select/select.h \
eventql/sql/statements/show_tables.cc \
eventql/sql/statements/show_tables.h \
eventql/sql/statements/show_databases.cc \
eventql/sql/statements/show_databases.h \
eventql/sql/statements/select/subquery.cc \
eventql/sql/statements/select/subquery.h \
eventql/sql/statements/select/tablescan.cc \
Expand Down Expand Up @@ -708,6 +710,8 @@ EVQL_CORE_SOURCES_ = \
eventql/sql/qtree/nodes/describe_partitions.h \
eventql/sql/qtree/nodes/cluster_show_servers.cc \
eventql/sql/qtree/nodes/cluster_show_servers.h \
eventql/sql/qtree/nodes/show_databases.cc \
eventql/sql/qtree/nodes/show_databases.h \
eventql/sql/qtree/qtree_coder.cc \
eventql/sql/qtree/qtree_coder.h \
eventql/sql/qtree/qtree_coder_impl.h \
Expand Down
4 changes: 0 additions & 4 deletions src/eventql/auth/client_auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ class ClientAuth {
return Status(eRuntimeError, "interactive auth not supported");
}

virtual Status changeNamespace(
Session* session,
const String& ns) = 0;

};

} // namespace eventql
Expand Down
14 changes: 0 additions & 14 deletions src/eventql/auth/client_auth_legacy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,5 @@ Status LegacyClientAuth::authenticateNonInteractive(
return Status::success();
}

Status LegacyClientAuth::changeNamespace(
Session* session,
const String& ns) {
if (session->isInternal()) {
session->setEffectiveNamespace(ns);
session->setDisplayNamespace(ns);
return Status::success();
} else if (ns == session->getEffectiveNamespace()) {
return Status::success();
} else {
return Status(eRuntimeError, "access denied");
}
}

} // namespace eventql

4 changes: 0 additions & 4 deletions src/eventql/auth/client_auth_legacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ class LegacyClientAuth : public ClientAuth {
Session* session,
HashMap<String, String> auth_data) override;

Status changeNamespace(
Session* session,
const String& ns) override;

protected:
mutable web::SecureCookieCoder cookie_coder_;
};
Expand Down
8 changes: 0 additions & 8 deletions src/eventql/auth/client_auth_trust.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,5 @@ Status TrustClientAuth::authenticateNonInteractive(
}
}

Status TrustClientAuth::changeNamespace(
Session* session,
const String& ns) {
session->setEffectiveNamespace(ns);
session->setDisplayNamespace(ns);
return Status::success();
}

} // namespace eventql

4 changes: 0 additions & 4 deletions src/eventql/auth/client_auth_trust.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ class TrustClientAuth : public ClientAuth {
Session* session,
HashMap<String, String> auth_data) override;

Status changeNamespace(
Session* session,
const String& ns) override;

};

} // namespace eventql
Expand Down
8 changes: 8 additions & 0 deletions src/eventql/server/session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ void Session::setUserID(const String& user_id) {
user_id_ = user_id;
}

Status Session::changeNamespace(const std::string& ns) {
//FIXME check ACLs
setEffectiveNamespace(ns);
setDisplayNamespace(ns);

return Status::success();
}

String Session::getEffectiveNamespace() const {
std::unique_lock<std::mutex> lk(mutex_);
return effective_namespace_;
Expand Down
3 changes: 3 additions & 0 deletions src/eventql/server/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "eventql/eventql.h"
#include "eventql/util/stdtypes.h"
#include "eventql/util/return_code.h"
#include "eventql/util/status.h"

namespace eventql {
struct DatabaseContext;
Expand All @@ -38,6 +39,8 @@ class Session {
String getUserID() const;
void setUserID(const String& user_id);

Status changeNamespace(const std::string& ns);

String getEffectiveNamespace() const;
void setEffectiveNamespace(const String& ns);

Expand Down
13 changes: 11 additions & 2 deletions src/eventql/server/sql/table_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@
namespace eventql {

TSDBTableProvider::TSDBTableProvider(
const String& tsdb_namespace,
PartitionMap* partition_map,
ConfigDirectory* cdir,
TableService* table_service,
InternalAuth* auth) :
tsdb_namespace_(tsdb_namespace),
partition_map_(partition_map),
cdir_(cdir),
table_service_(table_service),
Expand Down Expand Up @@ -509,6 +507,17 @@ Status TSDBTableProvider::listServers(
}
}

Status TSDBTableProvider::listDatabases(
Function<void (const NamespaceConfig& cfg)> fn) const {
try {
cdir_->listNamespaces(fn);
return Status::success();

} catch (const std::exception& e) {
return Status(eRuntimeError, e.what());
}
}

Option<csql::TableInfo> TSDBTableProvider::describe(
const String& table_name) const {
auto table_ref = TSDBTableRef::parse(table_name);
Expand Down
4 changes: 3 additions & 1 deletion src/eventql/server/sql/table_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ struct TSDBTableProvider : public csql::TableProvider {
public:

TSDBTableProvider(
const String& tsdb_namespace,
PartitionMap* partition_map,
ConfigDirectory* cdir,
TableService* table_service,
Expand All @@ -70,6 +69,9 @@ struct TSDBTableProvider : public csql::TableProvider {
Status listServers(
Function<void (const ServerConfig& server)> fn) const override;

Status listDatabases(
Function<void (const NamespaceConfig& cfg)> fn) const override;

Option<csql::TableInfo> describe(const String& table_name) const override;

Status createTable(const csql::CreateTableNode& req) override;
Expand Down
1 change: 0 additions & 1 deletion src/eventql/server/sql_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ ScopedPtr<csql::Transaction> SQLService::startTransaction(Session* session) {
txn->setHeartbeatCallback([session] { return session->triggerHeartbeat(); });
txn->setTableProvider(
new TSDBTableProvider(
session->getEffectiveNamespace(),
pmap_,
cdir_,
table_service_,
Expand Down
3 changes: 3 additions & 0 deletions src/eventql/sql/parser/astnode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ void ASTNode::debugPrint(int indent /* = 0 */) const {
case T_SHOW_TABLES:
printf("- T_SHOW_TABLES");
break;
case T_SHOW_DATABASES:
printf("- T_SHOW_DATABASES");
break;
case T_DESCRIBE_TABLE:
printf("- T_DESCRIBE_TABLE");
break;
Expand Down
1 change: 1 addition & 0 deletions src/eventql/sql/parser/astnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class ASTNode {
T_LIKE_EXPR,

T_SHOW_TABLES,
T_SHOW_DATABASES,
T_DESCRIBE_TABLE,
T_DESCRIBE_PARTITIONS,
T_CLUSTER_SHOW_SERVERS,
Expand Down
17 changes: 15 additions & 2 deletions src/eventql/sql/parser/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -878,11 +878,24 @@ ASTNode* Parser::importStatement() {

ASTNode* Parser::showStatement() {
consumeToken();
expectAndConsume(Token::T_TABLES);

auto stmt = new ASTNode(ASTNode::T_SHOW_TABLES);
ASTNode* stmt;
switch (cur_token_->getType()) {
case Token::T_TABLES:
stmt = new ASTNode(ASTNode::T_SHOW_TABLES);
break;

case Token::T_DATABASES:
stmt = new ASTNode(ASTNode::T_SHOW_DATABASES);
break;

default:
assertExpectation(Token::T_TABLES);
}

consumeToken();
consumeIf(Token::T_SEMICOLON);

return stmt;
}

Expand Down
1 change: 1 addition & 0 deletions src/eventql/sql/parser/token.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const char* Token::getTypeName(kTokenType type) {
case T_TABLE: return "T_TABLE";
case T_TABLES: return "T_TABLES";
case T_DATABASE: return "T_DATABASE";
case T_DATABASES: return "T_DATABASES";
case T_USE: return "T_USE";
case T_ON: return "T_ON";
case T_OFF: return "T_OFF";
Expand Down
1 change: 1 addition & 0 deletions src/eventql/sql/parser/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Token {
T_TABLE,
T_TABLES,
T_DATABASE,
T_DATABASES,
T_USE,
T_EOF,
T_SHOW,
Expand Down
5 changes: 5 additions & 0 deletions src/eventql/sql/parser/tokenize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,11 @@ void tokenizeQuery(
goto next;
}

if (token == "DATABASES") {
token_list->emplace_back(Token::T_DATABASES);
goto next;
}

if (token == "USE") {
token_list->emplace_back(Token::T_USE);
goto next;
Expand Down
87 changes: 87 additions & 0 deletions src/eventql/sql/qtree/nodes/show_databases.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright (c) 2017 DeepCortex GmbH <legal@eventql.io>
* Authors:
* - Laura Schlimmer <laura@eventql.io>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License ("the license") as
* published by the Free Software Foundation, either version 3 of the License,
* or any later version.
*
* In accordance with Section 7(e) of the license, the licensing of the Program
* under the license does not imply a trademark license. Therefore any rights,
* title and interest in our trademarks remain entirely with us.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the license for more details.
*
* You can be released from the requirements of the license by purchasing a
* commercial license. Buying such a license is mandatory as soon as you develop
* commercial activities involving this program without disclosing the source
* code of your own applications
*/
#include <eventql/sql/qtree/nodes/show_databases.h>
#include "eventql/sql/statements/show_databases.h"

namespace csql {

Vector<RefPtr<QueryTreeNode>> ShowDatabasesNode::inputTables() const {
return Vector<RefPtr<QueryTreeNode>>{};
}

RefPtr<QueryTreeNode> ShowDatabasesNode::deepCopy() const {
return new ShowDatabasesNode();
}

Vector<String> ShowDatabasesNode::getResultColumns() const {
std::vector<std::string> list;
for (const auto& e : ShowDatabasesExpression::kOutputColumns) {
list.emplace_back(e.first);
}
return list;
}

Vector<QualifiedColumn> ShowDatabasesNode::getAvailableColumns() const {
Vector<QualifiedColumn> cols;

for (const auto& e : ShowDatabasesExpression::kOutputColumns) {
cols.emplace_back(e.first, e.first, e.second);
}

return cols;
}

size_t ShowDatabasesNode::getComputedColumnIndex(
const String& column_name,
bool allow_add /* = false */) {
return -1; // FIXME
}

size_t ShowDatabasesNode::getNumComputedColumns() const {
return ShowDatabasesExpression::kOutputColumns.size();
}

SType ShowDatabasesNode::getColumnType(size_t idx) const {
assert(idx < ShowDatabasesExpression::kOutputColumns.size());
return ShowDatabasesExpression::kOutputColumns[idx].second;
}

String ShowDatabasesNode::toString() const {
return "(show-databases)";
}

void ShowDatabasesNode::encode(
QueryTreeCoder* coder,
const ShowDatabasesNode& node,
OutputStream* os) {
}

RefPtr<QueryTreeNode> ShowDatabasesNode::decode (
QueryTreeCoder* coder,
InputStream* is) {
return new ShowDatabasesNode();
}

} // namespace csql

64 changes: 64 additions & 0 deletions src/eventql/sql/qtree/nodes/show_databases.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (c) 2017 DeepCortex GmbH <legal@eventql.io>
* Authors:
* - Laura Schlimmer <laura@eventql.io>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License ("the license") as
* published by the Free Software Foundation, either version 3 of the License,
* or any later version.
*
* In accordance with Section 7(e) of the license, the licensing of the Program
* under the license does not imply a trademark license. Therefore any rights,
* title and interest in our trademarks remain entirely with us.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the license for more details.
*
* You can be released from the requirements of the license by purchasing a
* commercial license. Buying such a license is mandatory as soon as you develop
* commercial activities involving this program without disclosing the source
* code of your own applications
*/
#pragma once
#include "eventql/eventql.h"
#include <eventql/util/stdtypes.h>
#include <eventql/sql/qtree/TableExpressionNode.h>
#include <eventql/sql/qtree/qtree_coder.h>

namespace csql {

class ShowDatabasesNode : public TableExpressionNode {
public:

Vector<RefPtr<QueryTreeNode>> inputTables() const;

Vector<String> getResultColumns() const override;

Vector<QualifiedColumn> getAvailableColumns() const override;

RefPtr<QueryTreeNode> deepCopy() const override;

String toString() const override;

size_t getComputedColumnIndex(
const String& column_name,
bool allow_add = false) override;

size_t getNumComputedColumns() const override;

SType getColumnType(size_t idx) const override;

static void encode(
QueryTreeCoder* coder,
const ShowDatabasesNode& node,
OutputStream* os);

static RefPtr<QueryTreeNode> decode (
QueryTreeCoder* coder,
InputStream* is);
};

} // namespace csql