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

CURRENT_USER, 'USER and SESSION_USER` system functions #5807

Merged
merged 1 commit into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ the appropriate section of the docs.
Breaking Changes
================

- ``CURRENT_USER``, ``USER`` and ``SESSION_USER`` are now reserved words as we
introduced them as system functions. These terms will not be available to
be used as table, and column names and for already existing entities they
will have to be quoted when referenced (otherwise the terms will be treated
as function calls).

- ``SELECT`` statements without any ``FROM`` items are no longer executed
against the ``sys.cluster`` table, but against a virtual table with no
columns. Queries including ``sys.cluster`` columns but no explicit ``FROM``
Expand Down
42 changes: 21 additions & 21 deletions blackbox/docs/best_practice/data_import.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Defining the Data Structure
Before starting the import you'll have to consider how your table structure
will look. Decisions made at this point will influence the import process later.

For this example we have a simple single ``user`` table with 6 columns of
For this example we have a simple single ``users`` table with 6 columns of
various types::

cr> CREATE TABLE user (
cr> CREATE TABLE users (
... id INT primary key,
... name STRING,
... day_joined TIMESTAMP,
Expand All @@ -40,7 +40,7 @@ various types::

.. hide:

cr> DROP TABLE user;
cr> DROP TABLE users;
DROP OK, 1 row affected (... sec)

There's nothing wrong with this method and it does the job, but it's not very
Expand Down Expand Up @@ -80,7 +80,7 @@ according to your availability requirements.

The ``CREATE TABLE`` statement now looks like::

cr> CREATE TABLE user (
cr> CREATE TABLE users(
... id INT primary key,
... name STRING,
... day_joined TIMESTAMP,
Expand All @@ -107,17 +107,17 @@ therefore also minimise the overhead during import.

::

cr> ALTER TABLE user SET (refresh_interval = 0);
cr> ALTER TABLE users SET (refresh_interval = 0);
ALTER OK, -1 rows affected (... sec)

.. hide:

cr> DROP TABLE user;
cr> DROP TABLE users;
DROP OK, 1 row affected (... sec)

It's possible to set the refresh interval in the ``CREATE TABLE`` statement::

cr> CREATE TABLE user (
cr> CREATE TABLE users (
... id INT primary key,
... name STRING,
... day_joined TIMESTAMP,
Expand All @@ -136,7 +136,7 @@ It's possible to set the refresh interval in the ``CREATE TABLE`` statement::
Once the import is finished you can set the refresh interval to a reasonable
value (time in ms)::

cr> ALTER TABLE user SET (refresh_interval = 1000);
cr> ALTER TABLE users SET (refresh_interval = 1000);
ALTER OK, -1 rows affected (... sec)

.. seealso::
Expand Down Expand Up @@ -212,18 +212,18 @@ will return successfully, reporting ``COPY OK, 0 rows affected (... sec)``.

::

cr> COPY user FROM '/tmp/best_practice_data/users.json';
cr> COPY users FROM '/tmp/best_practice_data/users.json';
COPY OK, 150 rows affected (... sec)

.. hide:

cr> REFRESH TABLE user;
cr> REFRESH TABLE users;
REFRESH OK, 1 row affected (... sec)

cr> delete from user;
cr> delete from users;
DELETE OK, 150 rows affected (... sec)

cr> REFRESH TABLE user;
cr> REFRESH TABLE users;
REFRESH OK, 1 row affected (... sec)

.. note::
Expand All @@ -242,19 +242,19 @@ defaults to 10 000 if not specified.

For example::

cr> COPY user FROM '/tmp/best_practice_data/users.json'
cr> COPY users FROM '/tmp/best_practice_data/users.json'
... WITH (bulk_size = 2000);
COPY OK, 150 rows affected (... sec)

.. hide:

cr> REFRESH TABLE user;
cr> REFRESH TABLE users;
REFRESH OK, 1 row affected (... sec)

cr> delete from user;
cr> delete from users;
DELETE OK, 150 rows affected (... sec)

cr> REFRESH TABLE user;
cr> REFRESH TABLE users;
REFRESH OK, 1 row affected (... sec)

In our example it will not make a difference, but if you have a more complex
Expand All @@ -274,13 +274,13 @@ CrateDB does not automatically detect compression, so you'll need to specify

For example::

cr> COPY user FROM '/tmp/best_practice_data/users.json.gz'
cr> COPY users FROM '/tmp/best_practice_data/users.json.gz'
... WITH (compression = 'gzip');
COPY OK, 150 rows affected (... sec)

.. hide:

cr> REFRESH TABLE user;
cr> REFRESH TABLE users;
REFRESH OK, 1 row affected (... sec)

Partitioned Tables
Expand All @@ -300,12 +300,12 @@ partition column.

.. hide:

cr> DROP TABLE user;
cr> DROP TABLE users;
DROP OK, 1 row affected (... sec)

::

cr> CREATE TABLE user (
cr> CREATE TABLE users (
... id INT primary key,
... name STRING,
... day_joined TIMESTAMP primary key,
Expand Down Expand Up @@ -333,7 +333,7 @@ For example: ``users_1408312800.json``
The value of the partition column must be defined in the ``COPY FROM``
statement using the ``PARTITION`` clause::

cr> COPY user PARTITION (day_joined=1408312800)
cr> COPY users PARTITION (day_joined=1408312800)
... FROM '/tmp/best_practice_data/users_1408312800.json';
COPY OK, 23 rows affected (... sec)

Expand Down
4 changes: 4 additions & 0 deletions blackbox/docs/enterprise/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Current features:
more
- :ref:`Support for JavaScript in UDF <udf_lang_js>`: write user-defined
functions in JavaScript
- System Information Functions:
- :ref:`Current user function <current_user>`
- :ref:`User function <user>`
- :ref:`Session user function <session_user>`

.. _`enterprise edition`: https://crate.io/enterprise-edition/
.. _`enterprise license`: https://crate.io/enterprise-edition/
6 changes: 6 additions & 0 deletions blackbox/docs/sql/administration/user_management.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ To list all existing users query that table::
The column ``name`` shows the unique name of the user, the column ``superuser``
shows whether the user has superuser privileges or not.

.. note::

CrateDB also supports retrieving the current connected user using the
system information functions: :ref:`CURRENT_USER <current_user>`,
:ref:`USER <user>` and :ref:`SESSION_USER <session_user>`.

.. warning::

When the :ref:`es_api_setting` is enabled, it is possible to read the users
Expand Down
103 changes: 100 additions & 3 deletions blackbox/docs/sql/scalar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1621,9 +1621,11 @@ The default schema can be set when using the `JDBC
<https://crate.io/docs/reference/protocols/http.html#default-schema>`_ such as
`CrateDB PDO`_.

The ``CURRENT_SCHEMA`` function has a special SQL syntax, meaning that it must
be called without trailing parenthesis (``()``). However, CrateDB also supports
the optional parenthesis.
.. note::

The ``CURRENT_SCHEMA`` function has a special SQL syntax, meaning that it
must be called without trailing parenthesis (``()``). However, CrateDB also
supports the optional parenthesis.

Synopsis::

Expand All @@ -1639,6 +1641,101 @@ Example::
+----------------+
SELECT 1 row in set (... sec)

.. _current_user:

``CURRENT_USER``
----------------

.. NOTE::

``CURRENT_USER`` is an Enterprise Edition feature.

The ``CURRENT_USER`` system information function returns the name of the
current connected user or NULL if the user management module is disabled.

Returns: ``string``

Synopsis::

CURRENT_USER

Example::

cr> select current_user;
+--------------+
| current_user |
+--------------+
| crate |
+--------------+
SELECT 1 row in set (... sec)

.. _user:

``USER``
--------

.. NOTE::

``USER`` is an Enterprise Edition feature.

Equivalent to `CURRENT_USER`_.

Returns: ``string``

Synopsis::

USER

Example::

cr> select user;
+--------------+
| current_user |
+--------------+
| crate |
+--------------+
SELECT 1 row in set (... sec)

.. _session_user:

``SESSION_USER``
----------------

.. NOTE::

``SESSION_USER`` is an Enterprise Edition feature.

The ``SESSION_USER`` system information function returns the name of the
current connected user or NULL if the user management module is disabled.

Returns: ``string``

Synopsis::

SESSION_USER

Example::

cr> select session_user;
+--------------+
| session_user |
+--------------+
| crate |
+--------------+
SELECT 1 row in set (... sec)

.. note::

CrateDB doesn't currently support the switching of execution context. This
makes `SESSION_USER`_ functionally equivalent to `CURRENT_USER`_. We
provide it as it's part of the SQL standard.

.. note::

The `CURRENT_USER`_, `SESSION_USER`_ and `USER`_ functions have a
a special SQL syntax, meaning that they must be called without trailing
parenthesis (``()``).

.. rubric:: Footnotes

.. [#MySQL-Docs] http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format
Expand Down
48 changes: 31 additions & 17 deletions enterprise/users/src/main/java/io/crate/plugin/UsersPlugin.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
/*
* Licensed to Crate under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership. Crate licenses this file
* to you under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
* This file is part of a module with proprietary Enterprise Features.
*
* http://www.apache.org/licenses/LICENSE-2.0
* Licensed to Crate.io Inc. ("Crate.io") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
* Unauthorized copying of this file, via any medium is strictly prohibited.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial
* agreement.
* To use this file, Crate.io must have given you permission to enable and
* use such Enterprise Features and you must have a valid Enterprise or
* Subscription Agreement with Crate.io. If you enable or use the Enterprise
* Features, you represent and warrant that you have a valid Enterprise or
* Subscription Agreement with Crate.io. Your use of the Enterprise Features
* if governed by the terms and conditions of your Enterprise or Subscription
* Agreement with Crate.io.
*/

package io.crate.plugin;

import io.crate.metadata.UsersMetaData;
import io.crate.metadata.UsersPrivilegesMetaData;
import io.crate.scalar.UsersScalarFunctionModule;
import io.crate.settings.SharedSettings;
import org.elasticsearch.cluster.NamedDiff;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.plugins.Plugin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class UsersPlugin extends Plugin {

public UsersPlugin() {
private final Settings settings;

public UsersPlugin(Settings settings) {
this.settings = settings;
}

@Override
Expand Down Expand Up @@ -81,4 +86,13 @@ public List<NamedXContentRegistry.Entry> getNamedXContent() {
));
return entries;
}

@Override
public Collection<Module> createGuiceModules() {
if (SharedSettings.ENTERPRISE_LICENSE_SETTING.setting().get(settings)) {
return Collections.singletonList(new UsersScalarFunctionModule());
} else {
return Collections.emptyList();
}
}
}
Loading