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

describe partitions statement #253

Merged
merged 29 commits into from
Sep 26, 2016
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
42965fb
parse DESCRIBE PARTITONS tbl
lauraschlimmer Sep 16, 2016
4912f12
build DescribePartitionsNode
lauraschlimmer Sep 16, 2016
7d45e10
listPartitions wip
lauraschlimmer Sep 17, 2016
1cfcb26
Merge branch 'master' of github.com:eventql/eventql into feature/desc…
lauraschlimmer Sep 17, 2016
7338346
list table partitions
lauraschlimmer Sep 17, 2016
c4eb258
Merge branch 'master' of github.com:eventql/eventql into feature/desc…
lauraschlimmer Sep 17, 2016
067d5c8
docu
lauraschlimmer Sep 17, 2016
f46e3f9
don't randomize the server list in queries
lauraschlimmer Sep 17, 2016
1e46d6f
fixed test
lauraschlimmer Sep 18, 2016
a5308b0
merged master
lauraschlimmer Sep 20, 2016
29acb92
moved TablePartitionInfo to the eventql namespace
lauraschlimmer Sep 20, 2016
769d0d8
CHANGELOG
lauraschlimmer Sep 20, 2016
a8db51c
DESCRIBE PARTITIONS docu
lauraschlimmer Sep 21, 2016
937f85a
Merge branch 'master' of github.com:eventql/eventql into feature/desc…
lauraschlimmer Sep 21, 2016
36614e2
better describe partuitions stmt docu
lauraschlimmer Sep 21, 2016
921e6e3
fixed describe partitions result column order
lauraschlimmer Sep 21, 2016
f2a0b03
fixed missing keyrange
lauraschlimmer Sep 21, 2016
035a355
marked TableProvider::listPartitions virtual
lauraschlimmer Sep 21, 2016
b84d9de
merged master
lauraschlimmer Sep 21, 2016
5fb651b
get PartitionMap and ConfigDirectory from dbctx
lauraschlimmer Sep 21, 2016
ce1027e
use StringUtil::join
lauraschlimmer Sep 21, 2016
c6f378e
added column extra info
lauraschlimmer Sep 22, 2016
7e53ed2
added column Keyrange end
lauraschlimmer Sep 22, 2016
da47014
naming: col -> server
lauraschlimmer Sep 22, 2016
831b82e
Merge branch 'master' of github.com:eventql/eventql into feature/desc…
lauraschlimmer Sep 22, 2016
0cfcc5b
describe partitions example
lauraschlimmer Sep 22, 2016
2b90d27
Merge branch 'master' of github.com:eventql/eventql into feature/desc…
lauraschlimmer Sep 25, 2016
155de44
init metadata client
lauraschlimmer Sep 25, 2016
ea75cc5
docu header
lauraschlimmer Sep 25, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ EventQL v0.5.0 beta;

* Added new frames to native protocol: ACK, INSERT, REPL_INSERT

* Added the DESCRIBE PARTITIONS table statement to obtain information about
the partitions of a table

* Added the following new configuration options: server.disk_capacity,
server.load_limit_{soft,hard}, server.partitions_loading_limit_{soft,hard},
server.loadinfo_publish_interval, server.noalloc
Expand Down
1 change: 1 addition & 0 deletions doc/sql/statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ The following SQL statements are currently supported.
- [ALTER TABLE](./alter-table/) is used to modify an existing table's schema
- [DROP TABLE](./drop-table/) is used to drop an existing table
- [DESCRIBE](./describe/) shows the structure of a table
- [DESCRIBE PARTITIONS](./describe-partitions/) list the table's partitions
- [SHOW TABLES](./show-tables/) lists the tables in a database

1 change: 0 additions & 1 deletion doc/sql/statements/describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@

DESCRIBE table_name


20 changes: 20 additions & 0 deletions doc/sql/statements/describe_partitions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
5.2.7 DESCRIBE PARTITIONS
===============================

DESCRIBE PARTITIONS table_name



DESCRIBE PARTITIONS displays information about all partitions of the table.


Example

evql> DESCRIBE PARTITIONS sensors;

+------------------------------------------+------------------------+---------------------+
| Partition id | Keyrange Begin | Servers |
+------------------------------------------+------------------------+---------------------+
| 48127ec1a9c0812af6ba0240013c99baea40e22e | 2016-02-08 00:00:00 | node1, node3, node4 |
+------------------------------------------+------------------------+---------------------+

7 changes: 7 additions & 0 deletions doc/toc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,13 @@
visible_in_menu: true
visible_in_toc: true
source: "/sql/statements/describe.md"
-
url: "/sql/statements/describe-partitions"
title: "DESCRIBE PARTITIONS"
type: page
visible_in_menu: true
visible_in_toc: true
source: "/sql/statements/describe_partitions.md"
-
url: "/sql/statements/show-tables"
title: "SHOW TABLES"
Expand Down
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ EVQL_CORE_SOURCES_ = \
eventql/sql/expressions/string.h \
eventql/sql/expressions/table/describe_table.cc \
eventql/sql/expressions/table/describe_table.h \
eventql/sql/expressions/table/describe_partitions.cc \
eventql/sql/expressions/table/describe_partitions.h \
eventql/sql/expressions/table/groupby.cc \
eventql/sql/expressions/table/groupby.h \
eventql/sql/expressions/table/limit.cc \
Expand Down Expand Up @@ -718,6 +720,8 @@ EVQL_CORE_SOURCES_ = \
eventql/sql/qtree/nodes/insert_into.h \
eventql/sql/qtree/nodes/insert_json.cc \
eventql/sql/qtree/nodes/insert_json.h \
eventql/sql/qtree/nodes/describe_partitions.cc \
eventql/sql/qtree/nodes/describe_partitions.h \
eventql/sql/qtree/qtree_coder.cc \
eventql/sql/qtree/qtree_coder.h \
eventql/sql/qtree/qtree_coder_impl.h \
Expand Down
6 changes: 6 additions & 0 deletions src/eventql/db/table_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@

namespace eventql {

struct TablePartitionInfo {
std::vector<std::string> server_ids;
std::string partition_id;
std::string keyrange_begin;
};

}
45 changes: 45 additions & 0 deletions src/eventql/db/table_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,51 @@ void TableService::listTables(
});
}

Status TableService::listPartitions(
const String& db_namespace,
const String& table_name,
Function<void (const TablePartitionInfo& partition)> fn) const {
auto table = dbctx_->partition_map->findTable(db_namespace, table_name);
MetadataClient metadata_client(dbctx_->config_directory);
MetadataFile metadata_file;
auto rc = metadata_client.fetchLatestMetadataFile(
db_namespace,
table_name,
&metadata_file);

if (!rc.isSuccess()) {
return rc;
}

for (const auto& e : metadata_file.getPartitionMap()) {
TablePartitionInfo p_info;
p_info.partition_id = e.partition_id.toString();

for (const auto& s : e.servers) {
p_info.server_ids.emplace_back(s.server_id);
}

String keyrange;
switch (metadata_file.getKeyspaceType()) {
case KEYSPACE_UINT64: {
uint64_t keyrange_uint = -1;
memcpy((char*) &keyrange_uint, e.begin.data(), sizeof(e.begin));
p_info.keyrange_begin = keyrange_uint > -1 ?
UnixTime(keyrange_uint).toString() : "";
break;
}
case KEYSPACE_STRING: {
p_info.keyrange_begin = e.begin;
break;
}
}

fn(p_info);
}

return Status::success();
}

ReturnCode TableService::insertRecord(
const String& tsdb_namespace,
const String& table_name,
Expand Down
7 changes: 7 additions & 0 deletions src/eventql/db/table_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <eventql/db/record_envelope.pb.h>
#include <eventql/db/partition_map.h>
#include <eventql/db/shredded_record.h>
#include <eventql/db/metadata_file.h>
#include <eventql/sql/TableInfo.h>
#include <eventql/config/config_directory.h>

#include "eventql/eventql.h"
Expand Down Expand Up @@ -85,6 +87,11 @@ class TableService {
const String& tsdb_namespace,
Function<void (const TableDefinition& table)> fn) const;

Status listPartitions(
const String& db_namespace,
const String& table_name,
Function<void (const TablePartitionInfo& partition)> fn) const;

// insert one record
ReturnCode insertRecord(
const String& tsdb_namespace,
Expand Down
9 changes: 9 additions & 0 deletions src/eventql/server/sql/table_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,15 @@ void TSDBTableProvider::listTables(
});
}

Status TSDBTableProvider::listPartitions(
const String& table_name,
Function<void (const TablePartitionInfo& partition)> fn) const {
return table_service_->listPartitions(
tsdb_namespace_,
table_name,
fn);
}

Option<csql::TableInfo> TSDBTableProvider::describe(
const String& table_name) const {
auto table_ref = TSDBTableRef::parse(table_name);
Expand Down
4 changes: 4 additions & 0 deletions src/eventql/server/sql/table_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ struct TSDBTableProvider : public csql::TableProvider {
void listTables(
Function<void (const csql::TableInfo& table)> fn) const override;

Status listPartitions(
const String& table_name,
Function<void (const TablePartitionInfo& partition)> fn) const override;

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

Status createTable(const csql::CreateTableNode& req) override;
Expand Down
89 changes: 89 additions & 0 deletions src/eventql/sql/expressions/table/describe_partitions.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright (c) 2016 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/expressions/table/describe_partitions.h>
#include <eventql/sql/transaction.h>

namespace csql {

DescribePartitionsExpression::DescribePartitionsExpression(
Transaction* txn,
const String& table_name) :
txn_(txn),
table_name_(table_name),
counter_(0) {}

ScopedPtr<ResultCursor> DescribePartitionsExpression::execute() {
txn_->getTableProvider()->listPartitions(
table_name_,
[this] (const eventql::TablePartitionInfo& p_info) {
rows_.emplace_back(p_info);
});

return mkScoped(
new DefaultResultCursor(
kNumColumns,
std::bind(
&DescribePartitionsExpression::next,
this,
std::placeholders::_1,
std::placeholders::_2)));

}

size_t DescribePartitionsExpression::getNumColumns() const {
return kNumColumns;
}

bool DescribePartitionsExpression::next(SValue* row, size_t row_len) {
if (counter_ < rows_.size()) {
const auto& col = rows_[counter_];
Copy link
Member

Choose a reason for hiding this comment

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

why col, not row?

Copy link
Member Author

Choose a reason for hiding this comment

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

as the method's first argument is named 'row', I changed 'col' to 'partition'

switch (row_len) {
default:
case 3: {
std::string server_ids;
for (size_t i = 0; i < col.server_ids.size(); ++i) {
if (i > 0) {
server_ids += ", ";
}
server_ids += col.server_ids[i];
}
row[2] = SValue::newString(server_ids); //Server id
Copy link
Member

Choose a reason for hiding this comment

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

StringUtil::join(server_ids, ", ")

}
case 2:
row[1] = SValue::newString(col.keyrange_begin); //Keyrange begin
case 1:
row[0] = SValue::newString(col.partition_id); //Partition id
case 0:
break;
}

++counter_;
return true;
} else {
return false;
}
}

} //csql

56 changes: 56 additions & 0 deletions src/eventql/sql/expressions/table/describe_partitions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2016 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/ShowTablesNode.h>
#include <eventql/sql/runtime/tablerepository.h>

namespace csql {

class DescribePartitionsExpression : public TableExpression {
public:

static const size_t kNumColumns = 3;

DescribePartitionsExpression(
Transaction* txn,
const String& table_name);

ScopedPtr<ResultCursor> execute() override;

size_t getNumColumns() const override;

protected:

bool next(SValue* row, size_t row_len);

Transaction* txn_;
String table_name_;
Vector<eventql::TablePartitionInfo> rows_;
size_t counter_;
};

} //csql

3 changes: 3 additions & 0 deletions src/eventql/sql/parser/astnode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ void ASTNode::debugPrint(int indent /* = 0 */) const {
case T_DESCRIBE_TABLE:
printf("- T_DESCRIBE_TABLE");
break;
case T_DESCRIBE_PARTITIONS:
printf("- T_DESCRIBE_PARTITIONS");
break;
case T_EXPLAIN_QUERY:
printf("- T_EXPLAIN_QUERY");
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 @@ -95,6 +95,7 @@ class ASTNode {

T_SHOW_TABLES,
T_DESCRIBE_TABLE,
T_DESCRIBE_PARTITIONS,
T_EXPLAIN_QUERY,
T_CREATE_TABLE,
T_COLUMN_LIST,
Expand Down
10 changes: 10 additions & 0 deletions src/eventql/sql/parser/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,8 @@ ASTNode* Parser::explainStatement() {
switch (cur_token_->getType()) {
case Token::T_SELECT:
return explainQueryStatement();
case Token::T_PARTITIONS:
return describePartitionsStatement();
default:
return describeTableStatement();
}
Expand All @@ -892,6 +894,14 @@ ASTNode* Parser::explainQueryStatement() {
return stmt;
}

ASTNode* Parser::describePartitionsStatement() {
consumeToken();
auto stmt = new ASTNode(ASTNode::T_DESCRIBE_PARTITIONS);
stmt->appendChild(tableName());
consumeIf(Token::T_SEMICOLON);
return stmt;
}

ASTNode* Parser::describeTableStatement() {
auto stmt = new ASTNode(ASTNode::T_DESCRIBE_TABLE);
stmt->appendChild(tableName());
Expand Down
1 change: 1 addition & 0 deletions src/eventql/sql/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class Parser {
ASTNode* showStatement();
ASTNode* explainStatement();
ASTNode* explainQueryStatement();
ASTNode* describePartitionsStatement();
ASTNode* describeTableStatement();

ASTNode* fromClause();
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 @@ -126,6 +126,7 @@ const char* Token::getTypeName(kTokenType type) {
case T_SHOW: return "T_SHOW";
case T_DESCRIBE: return "T_DESCRIBE";
case T_EXPLAIN: return "T_EXPLAIN";
case T_PARTITIONS: return "T_PARTITIONS";
case T_EOF: return "T_EOF";
case T_DRAW: return "T_DRAW";
case T_LINECHART: return "T_LINECHART";
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 @@ -92,6 +92,7 @@ class Token {
T_SHOW,
T_DESCRIBE,
T_EXPLAIN,
T_PARTITIONS,
T_PRIMARY,
T_KEY,
T_REPEATED,
Expand Down