8000 Bad request when attempting to query collection · Issue #168 · cfworker/cfworker · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Bad request when attempting to query collection #168

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

Closed
Inrixia opened this issue Nov 26, 2021 · 10 comments
Closed

Bad request when attempting to query collection #168

Inrixia opened this issue Nov 26, 2021 · 10 comments

Comments

@Inrixia
Copy link
Contributor
Inrixia commented Nov 26, 2021

Hi, I don't know what I'm doing wrong but when attempting to use this query:

const result = await req.env.cosmos.queryDocuments<Listing>({ collId: "listings", query: `SELECT * FROM c` });

I am getting this result:

{
        "response": {
            "webSocket": null,
            "url": "https://REDACTED.documents.azure.com/dbs/REDACTED/colls/listings/docs",
            "redirected": false,
            "ok": false,
            "headers": {},
            "statusText": "Bad Request",
            "status": 400,
            "bodyUsed": false,
            "body": {
                "locked": false
            }
        },
        "itemsProperty": "Documents"
    }

Note: I replaced sensitive info with REDACTED.

Is this a bug with this sdk or is there something I am doing wrong?

@Inrixia
Copy link
Contributor Author
Inrixia commented Nov 26, 2021

Adding a WHERE clause seems to fix it.

This seems to mean that queryDocuments require's parameters and does not align with the docs for the Basic query...
Perhaps it should be removed and a note left on requirements for queryDocuments

@Inrixia
Copy link
Contributor Author
Inrixia commented Nov 26, 2021

What I don't understand is that I cannot use ORDER BY without a WHERE....

Maybe there is something I am missing

@Inrixia
Copy link
Contributor Author
Inrixia commented Nov 26, 2021

Looks like the enableCrossPartition header is set to false by default which causes queries to fail unless they failter against a single partition key or single-partitioned collections re:

If the collection is partitioned, this must be set to True to allow execution across multiple partitions. Queries that filter against a single partition key, or against single-partitioned collections do not need to set the header.

https://docs.microsoft.com/en-us/rest/api/cosmos-db/query-documents

Considering cosmos enforces partition keys on all new containers now afik, the default for this should probably be changed to true.

@Inrixia
Copy link
Contributor Author
Inrixia commented Nov 26, 2021

I still cannot use ORDER BY without a WHERE though.... Hmm

@Inrixia
Copy link
Contributor Author
Inrixia commented Nov 26, 2021

I was trying to investigate the response using .json(). Turns out that the .json() on a FeedResponse actually attempts to access the object properties and does not return the json parsed body of the response, this is really counterintuitive as it should be named something else to avoid confusion with the normal response api.

Looking at the body on the raw response gives the following error:

{
        "code": "BadRequest",
        "message": "The provided cross partition query can not be directly served by the gateway. This is a first chance (internal) exception that all newer clients will know how to handle gracefully. This exception is traced, but unless you see it bubble up as an exception (which only happens on older SDK clients), then you can safely ignore this message.\r\nActivityId: 670a6198-e094-4905-b6b9-da1fd56b73e4, Microsoft.Azure.Documents.Common/2.14.0",
        "additionalErrorInfo": "{\"partitionedQueryExecutionInfoVersion\":2,\"queryInfo\":{\"distinctType\":\"None\",\"top\":null,\"offset\":null,\"limit\":null,\"orderBy\":[\"Descending\"],\"orderByExpressions\":[\"c.sellerId\"],\"groupByExpressions\":[],\"groupByAliases\":[],\"aggregates\":[],\"groupByAliasToAggregateType\":{},\"rewrittenQuery\":\"SELECT c._rid, [{\\\"item\\\": c.sellerId}] AS orderByItems, {\\\"sellerId\\\": c.sellerId} AS payload\\nFROM ROOT AS c\\nWHERE ({documentdb-formattableorderbyquery-filter})\\nORDER BY c.sellerId DESC\",\"hasSelectValue\":false,\"dCountInfo\":null},\"queryRanges\":[{\"min\":\"\",\"max\":\"FF\",\"isMinInclusive\":true,\"isMaxInclusive\":false}]}"
    }

@Inrixia
Copy link
Contributor Author
Inrixia commented Nov 26, 2021

Looks like the enableCrossPartition header is set to false by default which causes queries to fail unless they failter against a single partition key or single-partitioned collections re:

If the collection is partitioned, this must be set to True to allow execution across multiple partitions. Queries that filter against a 8000 single partition key, or against single-partitioned collections do not need to set the header.

https://docs.microsoft.com/en-us/rest/api/cosmos-db/query-documents

Considering cosmos enforces partition keys on all new containers now afik, the default for this should probably be changed to true.

Looks like its actually been deprecated and should be updated...
Non cross partition queries are now deprecated: jupitern/cosmosdb#3

@Inrixia
Copy link
Contributor Author
Inrixia commented Nov 26, 2021

According to this the gateway cannot support certain queries.....
https://docs.microsoft.com/en-us/rest/api/cosmos-db/querying-cosmosdb-resources-using-the-rest-api#queries-that-cannot-be-served-by-gateway

Does that mean that it's just not possible to execute these queries or is there some sort of work around?

  • TOP
  • ORDER BY
  • OFFSET LIMIT
  • Aggregates
  • DISTINCT
  • GROUP BY

@Inrixia
Copy link
Contributor Author
Inrixia commented Nov 26, 2021

Looks like there is a way to handle this that the SDK's use that is covered here:
https://stackguides.com/questions/50240232/cosmos-db-rest-api-order-by-with-partitioning

I should be able to get this working in this library... Will fork and see what I can do.

Images of requests to get the above operations working:
image
image
image

@Inrixia
Copy link
Contributor Author
Inrixia commented Nov 26, 2021

Working with changes in pull request

@jdanyow
Copy link
Member
jdanyow commented Nov 27, 2021

Closing this as duplicate of #123. Thanks for the PRs! Left some comments- need to figure out what subset of cross-partition queries it makes sense to support.

@jdanyow jdanyow closed this as completed Nov 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0