=======================
Crate SQL HTTP Endpoint
=======================

Crate provides a HTTP Endpoint that can be used to submit SQL queries. The
endpoint is accessible under `<servername:port>/_sql`

SQL statements are sent to the `_sql` endpoint in `json` format,
whereby the statement is sent as value associated to the key `stmt`".

.. seealso::

    :doc:`dml`

A simple `SELECT` statement can be submitted like this::

    sh$ curl -sSXPOST '127.0.0.1:4200/_sql?pretty' -d '{
    ... "stmt":"select name, position from locations order by id limit 2"
    ... }'
    {
      "cols" : [ "name", "position" ],
      "rows" : [ [ "North West Ripple", 1 ], [ "Arkintoofle Minor", 3 ] ],
      "rowcount" : 2,
      "duration" : ...
    }

In addition to the `stmt` key the request body may also contain an `args` key
which can be used for SQL parameter substitution.

The SQL statement has to be changed to use placeholders where the values should
be inserted. Placeholders can either bei numbered (in the form of `$1`, `$2`,
etc.) or unnumbered using a question mark `?`

The placeholders will then be substituted with values from an array that is
expected under the `args` key::

    sh$ curl -sSXPOST '127.0.0.1:4200/_sql?pretty' -d@- <<- EOF
    ... {"stmt":
    ...     "select date,position from locations
    ...     where date <= \$1 and position < \$2 order by position",
    ...  "args": ["1979-10-12", 3]
    ... }
    ... EOF
    {
      "cols" : [ "date", "position" ],
      "rows" : [ [ 308534400000, 1 ], [ 308534400000, 2 ] ],
      "rowcount" : 2,
      "duration" : ...
    }

.. note::

    In this example the placeholders start with an backslash due to shell
    escaping.

The same query using question marks as placeholders looks like this::

    sh$ curl -sSXPOST '127.0.0.1:4200/_sql?pretty' -d@- <<- EOF
    ... {"stmt":
    ...     "select date,position from locations
    ...     where date <= ? and position < ? order by position",
    ...  "args": ["1979-10-12", 3]
    ... }
    ... EOF
    {
      "cols" : [ "date", "position" ],
      "rows" : [ [ 308534400000, 1 ], [ 308534400000, 2 ] ],
      "rowcount" : 2,
      "duration" : ...
    }

Error Handling
==============

Queries that are invalid or cannot be satisfied will result in an error
response. The response will contain an error code, an error message and in some
cases additional arguments that are specific to the error code.

Client libraries should use the error code to translate the error into an
appropriate exception::

    sh$ curl -sSXPOST '127.0.0.1:4200/_sql?pretty' -d '{
    ... "stmt":"select name, position"
    ... }'
    {
      "error" : {
        "message" : "SQLParseException[Only exactly one table is allowed in the from clause, got: 0]",
        "code" : 4000
      }
    }

To get more insight into what exactly went wrong an additional `error_trace`
GET parameter can be specified to return the stack trace::

    sh$ curl -sSXPOST '127.0.0.1:4200/_sql?pretty&error_trace=True' -d '{
    ... "stmt":"select name, position"
    ... }'
    {
      "error" : {
        "message" : "SQLParseException[Only exactly one table is allowed in the from clause, got: 0]",
        "code" : 4000
      },
      "error_trace" : {
        "message" : "Only exactly one table is allowed in the from clause, got: 0",
        "at" : {
          "class" : "...",
          "method" : "...",
          "file" : "....java",
          "line" : ...
        },
    ...

.. note::

    This parameter is intended for crate developers or for users requesting
    support for Crate. Client libraries shouldn't make use of this option and
    not include the stacktrace.

Currently the defined error codes are:

    ===== =====================================================================
    Code  Error
    ===== =====================================================================
    1000  Generic undefined error
    ----- ---------------------------------------------------------------------
    4000  The statement contains an invalid syntax or unsupported SQL statement
    ----- ---------------------------------------------------------------------
    4001  The statement contains an invalid analyzer definition.
    ----- ---------------------------------------------------------------------
    4002  The name of the table is invalid.
    ----- ---------------------------------------------------------------------
    4003  Field type validation Failed
    ----- ---------------------------------------------------------------------
    4004  Possible feature not supported (yet)
    ----- ---------------------------------------------------------------------
    4041  Unknown table.
    ----- ---------------------------------------------------------------------
    4042  Unknown analyzer.
    ----- ---------------------------------------------------------------------
    4043  Unknown column.
    ----- ---------------------------------------------------------------------
    4044  Unknown type.
    ----- ---------------------------------------------------------------------
    4045  Unknown schema.
    ----- ---------------------------------------------------------------------
    4046  Unknown Partition.
    ----- ---------------------------------------------------------------------
    4091  A document with the same primary key exists already.
    ----- ---------------------------------------------------------------------
    4092  A VersionConflict. Might be thrown if an attempt was made to update
          the same document concurrently.
    ----- ---------------------------------------------------------------------
    4093  A table with the same name exists already.
    ----- ---------------------------------------------------------------------
    4094  The used table alias contains tables with different schema.
    ----- ---------------------------------------------------------------------
    4095  Alter table using a table alias is not supported.
    ----- ---------------------------------------------------------------------
    5001  The execution of one or more tasks failed.
    ===== =====================================================================
