Complex queries

Besides simple SFW (SELECT-FROM-WHERE) queries, the SQL plugin supports complex queries such as subquery, join, union, and minus. These queries operate on more than one OpenSearch index. To examine how these queries execute behind the scenes, use the explain operation.

Joins

OpenSearch SQL supports inner joins, cross joins, and left outer joins.

Constraints

Joins have a number of constraints:

  1. You can only join two indices.
  2. You must use aliases for indices (e.g. people p).
  3. Within an ON clause, you can only use AND conditions.
  4. In a WHERE statement, don’t combine trees that contain multiple indices. For example, the following statement works:

    1. WHERE (a.type1 > 3 OR a.type1 < 0) AND (b.type2 > 4 OR b.type2 < -1)

    The following statement does not:

    1. WHERE (a.type1 > 3 OR b.type2 < 0) AND (a.type1 > 4 OR b.type2 < -1)
  5. You can’t use GROUP BY or ORDER BY for results.

  6. LIMIT with OFFSET (e.g. LIMIT 25 OFFSET 25) is not supported.

Description

The JOIN clause combines columns from one or more indices using values common to each.

Syntax

Rule tableSource:

tableSource

Rule joinPart:

joinPart

Example 1: Inner join

Inner join creates a new result set by combining columns of two indices based on your join predicates. It iterates the two indices and compares each document to find the ones that satisfy the join predicates. You can optionally precede the JOIN clause with an INNER keyword.

The join predicate(s) is specified by the ON clause.

SQL query:

  1. SELECT
  2. a.account_number, a.firstname, a.lastname,
  3. e.id, e.name
  4. FROM accounts a
  5. JOIN employees_nested e
  6. ON a.account_number = e.id

Explain:

The explain output is complicated, because a JOIN clause is associated with two OpenSearch DSL queries that execute in separate query planner frameworks. You can interpret it by examining the Physical Plan and Logical Plan objects.

  1. {
  2. "Physical Plan" : {
  3. "Project [ columns=[a.account_number, a.firstname, a.lastname, e.name, e.id] ]" : {
  4. "Top [ count=200 ]" : {
  5. "BlockHashJoin[ conditions=( a.account_number = e.id ), type=JOIN, blockSize=[FixedBlockSize with size=10000] ]" : {
  6. "Scroll [ employees_nested as e, pageSize=10000 ]" : {
  7. "request" : {
  8. "size" : 200,
  9. "from" : 0,
  10. "_source" : {
  11. "excludes" : [ ],
  12. "includes" : [
  13. "id",
  14. "name"
  15. ]
  16. }
  17. }
  18. },
  19. "Scroll [ accounts as a, pageSize=10000 ]" : {
  20. "request" : {
  21. "size" : 200,
  22. "from" : 0,
  23. "_source" : {
  24. "excludes" : [ ],
  25. "includes" : [
  26. "account_number",
  27. "firstname",
  28. "lastname"
  29. ]
  30. }
  31. }
  32. },
  33. "useTermsFilterOptimization" : false
  34. }
  35. }
  36. }
  37. },
  38. "description" : "Hash Join algorithm builds hash table based on result of first query, and then probes hash table to find matched rows for each row returned by second query",
  39. "Logical Plan" : {
  40. "Project [ columns=[a.account_number, a.firstname, a.lastname, e.name, e.id] ]" : {
  41. "Top [ count=200 ]" : {
  42. "Join [ conditions=( a.account_number = e.id ) type=JOIN ]" : {
  43. "Group" : [
  44. {
  45. "Project [ columns=[a.account_number, a.firstname, a.lastname] ]" : {
  46. "TableScan" : {
  47. "tableAlias" : "a",
  48. "tableName" : "accounts"
  49. }
  50. }
  51. },
  52. {
  53. "Project [ columns=[e.name, e.id] ]" : {
  54. "TableScan" : {
  55. "tableAlias" : "e",
  56. "tableName" : "employees_nested"
  57. }
  58. }
  59. }
  60. ]
  61. }
  62. }
  63. }
  64. }
  65. }

Result set:

a.account_numbera.firstnamea.lastnamee.ide.name
6HattieBond6Jane Smith

Example 2: Cross join

Cross join, also known as cartesian join, combines each document from the first index with each document from the second. The result set is the the cartesian product of documents of both indices. This operation is similar to the inner join without the ON clause that specifies the join condition.

It’s risky to perform cross join on two indices of large or even medium size. It might trigger a circuit breaker that terminates the query to avoid running out of memory.

SQL query:

  1. SELECT
  2. a.account_number, a.firstname, a.lastname,
  3. e.id, e.name
  4. FROM accounts a
  5. JOIN employees_nested e

Result set:

a.account_numbera.firstnamea.lastnamee.ide.name
1AmberDuke3Bob Smith
1AmberDuke4Susan Smith
1AmberDuke6Jane Smith
6HattieBond3Bob Smith
6HattieBond4Susan Smith
6HattieBond6Jane Smith
13NanetteBates3Bob Smith
13NanetteBates4Susan Smith
13NanetteBates6Jane Smith
18DaleAdams3Bob Smith
18DaleAdams4Susan Smith
18DaleAdams6Jane Smith

Example 3: Left outer join

Use left outer join to retain rows from the first index if it does not satisfy the join predicate. The keyword OUTER is optional.

SQL query:

  1. SELECT
  2. a.account_number, a.firstname, a.lastname,
  3. e.id, e.name
  4. FROM accounts a
  5. LEFT JOIN employees_nested e
  6. ON a.account_number = e.id

Result set:

a.account_numbera.firstnamea.lastnamee.ide.name
1AmberDukenullnull
6HattieBond6Jane Smith
13NanetteBatesnullnull
18DaleAdamsnullnull

Subquery

A subquery is a complete SELECT statement used within another statement and enclosed in parenthesis. From the explain output, you can see that some subqueries are actually transformed to an equivalent join query to execute.

Example 1: Table subquery

SQL query:

  1. SELECT a1.firstname, a1.lastname, a1.balance
  2. FROM accounts a1
  3. WHERE a1.account_number IN (
  4. SELECT a2.account_number
  5. FROM accounts a2
  6. WHERE a2.balance > 10000
  7. )

Explain:

  1. {
  2. "Physical Plan" : {
  3. "Project [ columns=[a1.balance, a1.firstname, a1.lastname] ]" : {
  4. "Top [ count=200 ]" : {
  5. "BlockHashJoin[ conditions=( a1.account_number = a2.account_number ), type=JOIN, blockSize=[FixedBlockSize with size=10000] ]" : {
  6. "Scroll [ accounts as a2, pageSize=10000 ]" : {
  7. "request" : {
  8. "size" : 200,
  9. "query" : {
  10. "bool" : {
  11. "filter" : [
  12. {
  13. "bool" : {
  14. "adjust_pure_negative" : true,
  15. "must" : [
  16. {
  17. "bool" : {
  18. "adjust_pure_negative" : true,
  19. "must" : [
  20. {
  21. "bool" : {
  22. "adjust_pure_negative" : true,
  23. "must_not" : [
  24. {
  25. "bool" : {
  26. "adjust_pure_negative" : true,
  27. "must_not" : [
  28. {
  29. "exists" : {
  30. "field" : "account_number",
  31. "boost" : 1
  32. }
  33. }
  34. ],
  35. "boost" : 1
  36. }
  37. }
  38. ],
  39. "boost" : 1
  40. }
  41. },
  42. {
  43. "range" : {
  44. "balance" : {
  45. "include_lower" : false,
  46. "include_upper" : true,
  47. "from" : 10000,
  48. "boost" : 1,
  49. "to" : null
  50. }
  51. }
  52. }
  53. ],
  54. "boost" : 1
  55. }
  56. }
  57. ],
  58. "boost" : 1
  59. }
  60. }
  61. ],
  62. "adjust_pure_negative" : true,
  63. "boost" : 1
  64. }
  65. },
  66. "from" : 0
  67. }
  68. },
  69. "Scroll [ accounts as a1, pageSize=10000 ]" : {
  70. "request" : {
  71. "size" : 200,
  72. "from" : 0,
  73. "_source" : {
  74. "excludes" : [ ],
  75. "includes" : [
  76. "firstname",
  77. "lastname",
  78. "balance",
  79. "account_number"
  80. ]
  81. }
  82. }
  83. },
  84. "useTermsFilterOptimization" : false
  85. }
  86. }
  87. }
  88. },
  89. "description" : "Hash Join algorithm builds hash table based on result of first query, and then probes hash table to find matched rows for each row returned by second query",
  90. "Logical Plan" : {
  91. "Project [ columns=[a1.balance, a1.firstname, a1.lastname] ]" : {
  92. "Top [ count=200 ]" : {
  93. "Join [ conditions=( a1.account_number = a2.account_number ) type=JOIN ]" : {
  94. "Group" : [
  95. {
  96. "Project [ columns=[a1.balance, a1.firstname, a1.lastname, a1.account_number] ]" : {
  97. "TableScan" : {
  98. "tableAlias" : "a1",
  99. "tableName" : "accounts"
  100. }
  101. }
  102. },
  103. {
  104. "Project [ columns=[a2.account_number] ]" : {
  105. "Filter [ conditions=[AND ( AND account_number ISN null, AND balance GT 10000 ) ] ]" : {
  106. "TableScan" : {
  107. "tableAlias" : "a2",
  108. "tableName" : "accounts"
  109. }
  110. }
  111. }
  112. }
  113. ]
  114. }
  115. }
  116. }
  117. }
  118. }

Result set:

a1.firstnamea1.lastnamea1.balance
AmberDuke39225
NanetteBates32838

Example 2: From subquery

SQL query:

  1. SELECT a.f, a.l, a.a
  2. FROM (
  3. SELECT firstname AS f, lastname AS l, age AS a
  4. FROM accounts
  5. WHERE age > 30
  6. ) AS a

Explain:

  1. {
  2. "from" : 0,
  3. "size" : 200,
  4. "query" : {
  5. "bool" : {
  6. "filter" : [
  7. {
  8. "bool" : {
  9. "must" : [
  10. {
  11. "range" : {
  12. "age" : {
  13. "from" : 30,
  14. "to" : null,
  15. "include_lower" : false,
  16. "include_upper" : true,
  17. "boost" : 1.0
  18. }
  19. }
  20. }
  21. ],
  22. "adjust_pure_negative" : true,
  23. "boost" : 1.0
  24. }
  25. }
  26. ],
  27. "adjust_pure_negative" : true,
  28. "boost" : 1.0
  29. }
  30. },
  31. "_source" : {
  32. "includes" : [
  33. "firstname",
  34. "lastname",
  35. "age"
  36. ],
  37. "excludes" : [ ]
  38. }
  39. }

Result set:

fla
AmberDuke32
DaleAdams33
HattieBond36