Complex queries

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

    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:

      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.

    Rule tableSource:

    Rule 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.

    Result set:

    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

    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:

    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.

    SQL query:

    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. {
    20. "bool" : {
    21. "adjust_pure_negative" : true,
    22. "must_not" : [
    23. {
    24. "bool" : {
    25. "adjust_pure_negative" : true,
    26. "must_not" : [
    27. {
    28. "exists" : {
    29. "field" : "account_number",
    30. "boost" : 1
    31. }
    32. }
    33. ],
    34. "boost" : 1
    35. }
    36. }
    37. ],
    38. "boost" : 1
    39. }
    40. },
    41. {
    42. "range" : {
    43. "balance" : {
    44. "include_lower" : false,
    45. "include_upper" : true,
    46. "from" : 10000,
    47. "boost" : 1,
    48. "to" : null
    49. }
    50. }
    51. }
    52. ],
    53. "boost" : 1
    54. }
    55. }
    56. ],
    57. "boost" : 1
    58. }
    59. }
    60. ],
    61. "adjust_pure_negative" : true,
    62. "boost" : 1
    63. }
    64. },
    65. "from" : 0
    66. }
    67. },
    68. "Scroll [ accounts as a1, pageSize=10000 ]" : {
    69. "request" : {
    70. "_source" : {
    71. "excludes" : [ ],
    72. "includes" : [
    73. "firstname",
    74. "lastname",
    75. "balance",
    76. "account_number"
    77. ]
    78. }
    79. }
    80. },
    81. "useTermsFilterOptimization" : false
    82. }
    83. }
    84. }
    85. },
    86. "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",
    87. "Logical Plan" : {
    88. "Project [ columns=[a1.balance, a1.firstname, a1.lastname] ]" : {
    89. "Top [ count=200 ]" : {
    90. "Join [ conditions=( a1.account_number = a2.account_number ) type=JOIN ]" : {
    91. "Group" : [
    92. {
    93. "Project [ columns=[a1.balance, a1.firstname, a1.lastname, a1.account_number] ]" : {
    94. "TableScan" : {
    95. "tableAlias" : "a1",
    96. "tableName" : "accounts"
    97. }
    98. }
    99. },
    100. {
    101. "Project [ columns=[a2.account_number] ]" : {
    102. "Filter [ conditions=[AND ( AND account_number ISN null, AND balance GT 10000 ) ] ]" : {
    103. "TableScan" : {
    104. "tableAlias" : "a2",
    105. "tableName" : "accounts"
    106. }
    107. }
    108. }
    109. }
    110. ]
    111. }
    112. }
    113. }
    114. }
    115. }

    Result set:

    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. ) AS a

    Explain: