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

feat(search): enable deep search with path expressions #590

Merged
merged 43 commits into from
Jul 8, 2024

Conversation

patricebender
Copy link
Member

@patricebender patricebender commented Apr 16, 2024

support search via path expressions:

  • deep search via associations
  • include calculated elements if explicitly requested

with cds.infer being a part of all our query objects, each query comes with target and elements even without the infer function of the db-servicebeing called. Hence, it was possible to calculate the search term as a very first step of cqn4sql. After the contains(…) function has been added to the queries where or having clause the usual join tree algorithm is executed, making it possible to use path expressions within the search term.


rework of #252

support search via path expressions:

- deep search via associations
- include calculated elements if explicitly requested
- keep existing caching mechanisms

---

rework of #252
@patricebender patricebender marked this pull request as ready for review April 17, 2024 14:58
@patricebender patricebender changed the title feat: deep search feat(search): enable deep search with path expressions Apr 17, 2024
patricebender added a commit that referenced this pull request Apr 23, 2024
This is necessary if we expose an association in the inner query and
traverse it in the outer query:

```cds
select from (select from Books) { author.name }
```

to come up with a proper join node, we must add a table alias around the
subquery in from.
I came up with `__select__` as artifical name. To make it always unique,
I request a unique alias from the subquery based on `__select__`, the
subquery knows all outer aliases and is able to calculate a universally
unique alias.

bugs found in #590
@johannes-vogel johannes-vogel added the next release pr to be checked for next release label Jun 7, 2024
if (searchTerm) {
// Search target can be a navigation, in that case use _target to get the correct entity
const { where, having } = transformSearch(searchTerm)
if (where) inferred.SELECT.where = where
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you need to append the search terms instead of replacing the existing where/having clauses?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done in line 243ff

@patricebender
Copy link
Member Author

patricebender commented Jun 18, 2024

blocked by cap/issues/issues/16172cds.infer can't handle table aliases:

> q = SELECT`from ${Books} as Foo { Foo.ID }`
Query {
  SELECT: {
    from: { ref: [ 'my.bookshop.Books' ], as: 'Foo' },
    columns: [ { ref: [ 'Foo', 'ID' ] } ]
  }
}
> q.elements
Uncaught:
Error: Couldn't resolve element "Foo/ID" in entity my.bookshop.Books [ 'ID', 'title', 'stock' ]
    at /Users/patricebender/SAPDevelop/foo/node_modules/@sap/cds/lib/ql/infer.js:80:22
    at Array.forEach (<anonymous>)
    at Function.elements4 (/Users/patricebender/SAPDevelop/foo/node_modules/@sap/cds/lib/ql/infer.js:61:32)
    at get elements [as elements] (/Users/patricebender/SAPDevelop/foo/node_modules/@sap/cds/lib/ql/SELECT.js:157:53)

@johannes-vogel
Copy link
Contributor

blocked by cap/issues/issues/16172cds.infer can't handle table aliases:

> q = SELECT`from ${Books} as Foo { Foo.ID }`
Query {
  SELECT: {
    from: { ref: [ 'my.bookshop.Books' ], as: 'Foo' },
    columns: [ { ref: [ 'Foo', 'ID' ] } ]
  }
}
> q.elements
Uncaught:
Error: Couldn't resolve element "Foo/ID" in entity my.bookshop.Books [ 'ID', 'title', 'stock' ]
    at /Users/patricebender/SAPDevelop/foo/node_modules/@sap/cds/lib/ql/infer.js:80:22
    at Array.forEach (<anonymous>)
    at Function.elements4 (/Users/patricebender/SAPDevelop/foo/node_modules/@sap/cds/lib/ql/infer.js:61:32)
    at get elements [as elements] (/Users/patricebender/SAPDevelop/foo/node_modules/@sap/cds/lib/ql/SELECT.js:157:53)

Why would cds.infer have to handle aliases? Where do they stem from?

@patricebender
Copy link
Member Author

patricebender commented Jun 18, 2024

Why would cds.infer have to handle aliases?

If we want to support path expressions in a search annotation, we must attach the search function to the query before it is going through the join algorithm for processing. But for the calculation of the searchable columns, we need to know the target of the query, to find out which elements must be searched. Hence, I rely on cds.infer to do the work instead of doing it by myself. And by doing so,I dont attach the elements to the original query anymore. For an expand column, cqn4sql builds a subquery, attaches a table alias and transforms the query. cds.infer can't resolve the columns starting with an alias name anymore

Where do they stem from?

Each cqn4sql transformation assigns unique table aliases to the given query. All transformed columns will be absolutified → all refs are prepended with the unique table alias.
This also accounts for where exists subqueries, or correlated subqueries stemming from expand

@patricebender patricebender merged commit e9e9461 into main Jul 8, 2024
4 checks passed
@patricebender patricebender deleted the patrice/search branch July 8, 2024 11:34
@cap-bots cap-bots mentioned this pull request Jul 8, 2024
johannes-vogel added a commit that referenced this pull request Jul 8, 2024
🤖 I have created a release *beep* *boop*
---


<details><summary>db-service: 1.11.0</summary>

##
[1.11.0](db-service-v1.10.3...db-service-v1.11.0)
(2024-07-08)


### Added

* **search:** enable deep search with path expressions
([#590](#590))
([e9e9461](e9e9461))

### Changed

* `search` interprets only first search term instead of raising an error
([#707](#707))
([0b9108c](0b9108c))

### Fixed

* optimize foreign key access for expand with aggregations
([#734](#734))
([77b7978](77b7978))
</details>

<details><summary>hana: 1.1.0</summary>

##
[1.1.0](hana-v1.0.1...hana-v1.1.0)
(2024-07-08)


### Added

* Enable native HANA fuzzy search for `search` function queries
([#707](#707))
([0b9108c](0b9108c))


### Fixed

* **mtx:** sidecar scenario due to usage of wrong credentials
([#732](#732))
([0b5c91f](0b5c91f))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Co-authored-by: Johannes Vogel <31311694+johannes-vogel@users.noreply.github.com>
@cap-bots cap-bots mentioned this pull request Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next release pr to be checked for next release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants