diff --git a/QUERIES.md b/QUERIES.md index 5fa77fd5ea..8e350c5175 100644 --- a/QUERIES.md +++ b/QUERIES.md @@ -14,6 +14,9 @@ ALTER RETENTION POLICY ON (DURATION | REPLICATION -- drop a database DROP DATABASE + +-- drop a retention policy +DROP RETENTION POLICY ON ``` # Users and permissions diff --git a/influxql/INFLUXQL.md b/influxql/INFLUXQL.md index 31073dd806..9a374618f8 100644 --- a/influxql/INFLUXQL.md +++ b/influxql/INFLUXQL.md @@ -110,6 +110,7 @@ statement = alter_retention_policy_stmt | delete_stmt | drop_continuous_query_stmt | drop_database_stmt | + drop_retention_policy_stmt | drop_series_stmt | drop_user_stmt | grant_stmt | @@ -246,7 +247,22 @@ delete_stmt = "DELETE" from_clause where_clause . #### Example: ```sql -DELETE FROM cpu WHERE region = 'uswest' +-- delete data points from the cpu measurement where the region tag +-- equals 'uswest' +DELETE FROM cpu WHERE region = 'uswest'; +``` + +### DROP RETENTION POLICY + +``` +drop_retention_policy_stmt = "DROP RETENTION POLICY" policy_name "ON" db_name . +``` + +#### Example: + +```sql +-- drop the retention policy named 1h.cpu from mydb +DROP RETENTION POLICY "1h.cpu" ON mydb; ``` ### GRANT @@ -268,7 +284,7 @@ GRANT READ ON mydb TO jdoe; ### LIST DATABASES ``` -list_databases_stmt = "LIST DATABASES" +list_databases_stmt = "LIST DATABASES" . ``` #### Example: @@ -281,7 +297,7 @@ LIST DATABASES; ### LIST RETENTION POLICIES ``` -list_retention_policies = "LIST RETENTION POLICIES" db_name +list_retention_policies = "LIST RETENTION POLICIES" db_name . ``` #### Example: @@ -310,9 +326,11 @@ expr = measurements = -user_name = identifier . - password = identifier . +policy_name = identifier . + +user_name = identifier . + privilege = "ALL" [ "PRIVILEGES" ] | "READ" | "WRITE" . ``` diff --git a/influxql/ast.go b/influxql/ast.go index 13051fda2c..ff0e2abf98 100644 --- a/influxql/ast.go +++ b/influxql/ast.go @@ -55,6 +55,7 @@ func (_ *CreateUserStatement) node() {} func (_ *DeleteStatement) node() {} func (_ *DropContinuousQueryStatement) node() {} func (_ *DropDatabaseStatement) node() {} +func (_ *DropRetentionPolicyStatement) node() {} func (_ *DropSeriesStatement) node() {} func (_ *DropUserStatement) node() {} func (_ *GrantStatement) node() {} @@ -126,6 +127,7 @@ func (_ *CreateUserStatement) stmt() {} func (_ *DeleteStatement) stmt() {} func (_ *DropContinuousQueryStatement) stmt() {} func (_ *DropDatabaseStatement) stmt() {} +func (_ *DropRetentionPolicyStatement) stmt() {} func (_ *DropSeriesStatement) stmt() {} func (_ *DropUserStatement) stmt() {} func (_ *GrantStatement) stmt() {} @@ -226,6 +228,25 @@ func (s *DropDatabaseStatement) String() string { return buf.String() } +// DropRetentionPolicyStatement represents a command to drop a retention policy from a database. +type DropRetentionPolicyStatement struct { + // Name of the policy to drop. + Name string + + // Name of the database to drop the policy from. + Database string +} + +// String returns a string representation of the drop retention policy statement. +func (s *DropRetentionPolicyStatement) String() string { + var buf bytes.Buffer + _, _ = buf.WriteString("DROP RETENTION POLICY ") + _, _ = buf.WriteString(s.Name) + _, _ = buf.WriteString(" ON ") + _, _ = buf.WriteString(s.Database) + return buf.String() +} + // CreateUserStatement represents a command for creating a new user. type CreateUserStatement struct { // Name of the user to be created. diff --git a/influxql/parser.go b/influxql/parser.go index 54c3b018bb..8c3f38a8d2 100644 --- a/influxql/parser.go +++ b/influxql/parser.go @@ -155,6 +155,12 @@ func (p *Parser) parseDropStatement() (Statement, error) { return p.parseDropContinuousQueryStatement() } else if tok == DATABASE { return p.parseDropDatabaseStatement() + } else if tok == RETENTION { + if tok, pos, lit := p.scanIgnoreWhitespace(); tok == POLICY { + return p.parseDropRetentionPolicyStatement() + } else { + return nil, newParseError(tokstr(tok, lit), []string{"POLICY"}, pos) + } } else if tok == USER { return p.parseDropUserStatement() } @@ -932,6 +938,31 @@ func (p *Parser) parseDropDatabaseStatement() (*DropDatabaseStatement, error) { return stmt, nil } +// parseDropRetentionPolicyStatement parses a string and returns a DropRetentionPolicyStatement. +// This function assumes the DROP RETENTION POLICY tokens have been consumed. +func (p *Parser) parseDropRetentionPolicyStatement() (*DropRetentionPolicyStatement, error) { + stmt := &DropRetentionPolicyStatement{} + + // Parse the policy name. + ident, err := p.parseIdentifier() + if err != nil { + return nil, err + } + stmt.Name = ident + + // Consume the required ON token. + if tok, pos, lit := p.scanIgnoreWhitespace(); tok != ON { + return nil, newParseError(tokstr(tok, lit), []string{"ON"}, pos) + } + + // Parse the database name. + if stmt.Database, err = p.parseIdentifier(); err != nil { + return nil, err + } + + return stmt, nil +} + // parseCreateUserStatement parses a string and returns a CreateUserStatement. // This function assumes the "CREATE USER" tokens have already been consumed. func (p *Parser) parseCreateUserStatement() (*CreateUserStatement, error) { diff --git a/influxql/parser_test.go b/influxql/parser_test.go index c385daf8c1..07dbc35634 100644 --- a/influxql/parser_test.go +++ b/influxql/parser_test.go @@ -361,6 +361,15 @@ func TestParser_ParseStatement(t *testing.T) { stmt: &influxql.DropDatabaseStatement{Name: "testdb"}, }, + // DROP RETENTION POLICY + { + s: `DROP RETENTION POLICY "1h.cpu" ON mydb`, + stmt: &influxql.DropRetentionPolicyStatement{ + Name: "1h.cpu", + Database: "mydb", + }, + }, + // DROP USER statement { s: `DROP USER jdoe`, @@ -548,6 +557,10 @@ func TestParser_ParseStatement(t *testing.T) { {s: `DROP CONTINUOUS QUERY`, err: `found EOF, expected identifier, string at line 1, char 23`}, {s: `DROP FOO`, err: `found FOO, expected SERIES, CONTINUOUS at line 1, char 6`}, {s: `DROP DATABASE`, err: `found EOF, expected identifier at line 1, char 15`}, + {s: `DROP RETENTION`, err: `found EOF, expected POLICY at line 1, char 16`}, + {s: `DROP RETENTION POLICY`, err: `found EOF, expected identifier at line 1, char 23`}, + {s: `DROP RETENTION POLICY "1h.cpu"`, err: `found EOF, expected ON at line 1, char 31`}, + {s: `DROP RETENTION POLICY "1h.cpu" ON`, err: `found EOF, expected identifier at line 1, char 35`}, {s: `DROP USER`, err: `found EOF, expected identifier at line 1, char 11`}, {s: `CREATE USER testuser`, err: `found EOF, expected WITH at line 1, char 22`}, {s: `CREATE USER testuser WITH`, err: `found EOF, expected PASSWORD at line 1, char 27`},