API SQL script editing

Updated at:

This topic explains how to edit API SQL scripts for query operations (Get/List) and data manipulation operations (Create/Update/Delete).

Get/List (query operations)

  • Supported query modes:

    • Supports single-table queries, multi-table joins (JOIN), and subqueries within the same data source.

    • Supports advanced SQL mode (MyBatis style).

    • You can configure parameters in the SELECT clause. For example: SELECT id_card, SUM(CASE WHEN id_card LIKE ${id_card} THEN 1 ELSE 0 END) AS proj_score FROM table WHERE c LIKE ${id_card} GROUP BY id_card.

  • Unsupported syntax and usage:

    • Multiple SQL statements: You cannot execute multiple SQL statements in a single API, such as SELECT ...; SELECT ...;.

    • Non-DQL statements: In APIs for Get/List operations, non-Data Query Language (DQL) statements such as INSERT, UPDATE, CREATE, and DELETE are not supported.

    • SELECT *: The SELECT * query is not supported. You must explicitly specify the columns to query.

    • Column prefixes: If a column name includes a table prefix, such as t.name, you must use an alias for the response parameter. For example: SELECT t.name AS name FROM table.

    • Aggregate functions: When you use an aggregate function such as SUM(), COUNT(), MAX(), or MIN(), you must use an alias for the response parameter. For example: SELECT SUM(num) AS total_num FROM table.

    • Pagination statements: Do not use pagination statements in SQL. Use pagination parameters instead.

      Note
      • For an SAP HANA data source, pagination is not supported, and passing pagination parameters has no effect.

      • For a TDengine data source, you must use a pagination statement in the SQL. If you do not pass pagination parameters (PageStart and PageSize), the API returns all data. If you pass pagination parameters, the API returns data based on the defined pagination parameters.

  • Dynamic column queries (in advanced SQL mode) let you dynamically specify response columns by including parameters in the query statement.

    • Parameter format: The parameter must start with var_cols, in the format var_cols_xxx (for example, var_cols_args). For example: SELECT id, ${var_cols_args} FROM table1.

    • Response parameters: You must add all supported dynamic columns as response parameters.

    • Call the API: When calling the API, pass the fields that you want to query in the dynamic parameters. Fields that are not passed will have a null value in the returned result. For example: var_cols_args=name,age,dept.

    • Executed SQL: The actual SQL executed is: SELECT id, name, age, dept FROM table1.

  • When using advanced SQL mode, you must use escaped characters for comparison operators in MyBatis SQL.

    Original operator

    Escaped character

    <

    &lt;

    >

    &gt;

    <=

    &lt;=

    >=

    &gt;=

  • Data source-specific limitations:

    Data source

    Limitations

    TDengine

    • You must use LIMIT/OFFSET in the SQL statement to implement pagination.

    • Optional parameters are not supported.

    SAP HANA

    • Pagination is not supported. Passing PageStart and PageSize parameters has no effect.

    • Optional parameters are not supported.

    Elasticsearch

    • Pagination is implemented by using ScrollId. If you do not specify a ScrollId, you can query a maximum of 10,000 records. When you set the PageStart and PageSize pagination parameters, ensure that the query does not exceed 10,000 records. Otherwise, the call fails.

      For example, if PageStart is set to 9998, the maximum value for PageSize is 2.

    • To query records beyond the first 10,000, you must use a WHERE clause to specify a scrollId. You can define a scrollId request parameter when you create the API and pass the corresponding scrollId value when you run a query.

      Note

      When you specify a scrollId for a query, you cannot use pagination. Do not pass values for PageStart and PageSize. Otherwise, the call fails.

    • In the WHERE clause, only the scrollId field is supported as a query condition. Other fields are ignored.

      For example: select a from table where scrollId=${scrollId}.

    • Subqueries are not supported.

    HBase

    • Only single rowkey queries are supported. The rowkey is a required request parameter. The query syntax is:

      select info1.id as name from table where rowkey = ${rowkey}

    • The ORDER BY clause is not supported.

    • Pagination is not supported. Passing PageStart and PageSize parameters has no effect.

    • The JOIN statement is not supported.

    PostgreSQL

    Cross-schema queries are not supported. You can only query data within the schema of the current connection.

    Impala

    Advanced SQL mode (MyBatis style) is not supported.

    Lindorm

    The JOIN statement is not supported.

    DolphinDB

    DolphinDB supports only the Oracle and MySQL SQL Compatibility Modes. The native DolphinDB mode is not supported. When you create a data source, you must set the SQL Compatibility Mode to Oracle or MySQL to use it for API creation.

    Important

    Modifying the compatibility mode of the data source may cause unpredictable errors in existing APIs. Adjust this setting based on your business requirements.

    GBase 8a

    The QUALIFY statement is not supported in basic SQL mode.

    OpenSearch

    • Simple subqueries are supported. Multi-level nested IN subqueries may not execute correctly.

      For example: SELECT ... WHERE age IN (SELECT age FROM ... WHERE age IN (18,39));.

    • When you query data by using JDBC, a subquery that contains a JOIN may return an empty result set.

      For example: SELECT ... FROM (SELECT ... FROM t1 LEFT JOIN t2 ON ...) e1 WHERE ...;.

    • The WITH clause, used for common table expressions, is not supported.

      For example: WITH test_with AS (...) SELECT ...;.

    • The UNION and UNION ALL statements are not supported, even within subqueries.

      For example: SELECT id FROM (SELECT id FROM t1 UNION SELECT id FROM t2) t;.

    • The COALESCE syntax has compatibility issues with the current JDBC driver. We recommend using the CASE WHEN syntax as an alternative.

      Replace SELECT COALESCE(id, 0) FROM ...; with SELECT CASE WHEN id IS NULL THEN 0 ELSE id END FROM ...;.

    • Using a CASE WHEN expression with a JOIN statement may cause field parsing errors or "column not found" errors. Verify syntax compatibility before you build complex queries.

      For example: SELECT CASE WHEN a.id=... END FROM t1 a JOIN t2 b ON ...;.

    Hive

    If a Hive table is a data lake table, for example, with a format like Iceberg or Hudi, queries use the Spark engine. If it is a standard Hive table, for example, with a file format like ORC, Parquet, or TextFile, queries use the Hive engine.

Create/Update/Delete (data manipulation operations)

  • Supported SQL modes:

    • basic SQL mode: Supports standard INSERT, UPDATE, and DELETE statements. Use the ${param} placeholder to pass parameters.

    • Advanced SQL mode (MyBatis style): You can use this mode for complex logic, such as conditional statements, dynamic fields, and batch operations. The supported tags are as follows:

      <if>, <choose>, <when>, <otherwise>, <trim>, <foreach>
  • When you perform batch operations in advanced SQL mode, each record is processed individually, which can lead to poor performance. Therefore, avoid using advanced SQL mode for APIs that perform batch inserts, updates, or deletes.

  • If an SQL statement contains an in condition, each record is executed individually, resulting in poor performance. Avoid using in conditions whenever possible.

  • Only PostgreSQL supports the RETURNING clause.