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

[WIP] Query variables support #12

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cosmosDb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CosmosClient, Database, Container, SqlQuerySpec } from "@azure/cosmos"
import * as sdk from '@hasura/ndc-sdk-typescript'

export type RawCosmosDbConfig = {
databaseName: string,
Expand Down
53 changes: 39 additions & 14 deletions src/execution.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as sdk from "@hasura/ndc-sdk-typescript";
import * as schema from "./schema";
import * as sql from "./sqlGeneration";
import { Database } from "@azure/cosmos";
import { Database, SqlQuerySpec } from "@azure/cosmos";
import { runSQLQuery } from "./cosmosDb";


Expand All @@ -25,14 +25,15 @@ function validateOrderBy(orderBy: sdk.OrderBy, collectionObjectType: schema.Obje
}
}


function parseQueryRequest(collectionsSchema: schema.CollectionsSchema, queryRequest: sdk.QueryRequest): sql.SqlQueryGenerationContext {
function parseQueryRequest(collectionsSchema: schema.CollectionsSchema, queryRequest: sdk.QueryRequest): sql.SqlQueryContext {
let isAggregateQuery = false;

const collection: string = queryRequest.collection;

const collectionDefinition: schema.CollectionDefinition = collectionsSchema.collections[collection];

const rootContainerAlias = `root_${collection}`;

let requestedFields: sql.SelectColumns = {};

if (collectionDefinition === undefined)
Expand Down Expand Up @@ -64,7 +65,10 @@ function parseQueryRequest(collectionsSchema: schema.CollectionsSchema, queryReq
} else {
requestedFields[fieldName] = {
kind: 'column',
columnName: queryField.column
column: {
name: queryField.column,
prefix: [rootContainerAlias]
}
}
};

Expand All @@ -87,14 +91,20 @@ function parseQueryRequest(collectionsSchema: schema.CollectionsSchema, queryReq
if (aggregateField.distinct) {
requestedFields[fieldName] = {
kind: 'aggregate',
columnName: aggregateField.column,
column: {
name: aggregateField.column,
prefix: [rootContainerAlias],
},
aggregateFunction: 'DISTINCT COUNT'
};

} else {
requestedFields[fieldName] = {
kind: 'aggregate',
columnName: aggregateField.column,
column: {
name: aggregateField.column,
prefix: [rootContainerAlias],
},
aggregateFunction: 'COUNT'
}
}
Expand All @@ -107,7 +117,11 @@ function parseQueryRequest(collectionsSchema: schema.CollectionsSchema, queryReq
} else {
requestedFields[fieldName] = {
kind: 'aggregate',
columnName: aggregateField.column,
column: {
name: aggregateField.column,
prefix: [rootContainerAlias]
},

aggregateFunction: aggregateField.function
}

Expand All @@ -116,18 +130,28 @@ function parseQueryRequest(collectionsSchema: schema.CollectionsSchema, queryReq
case "star_count":
requestedFields[fieldName] = {
kind: 'aggregate',
columnName: '*',
column: {
name: "*",
prefix: [rootContainerAlias],
},
aggregateFunction: 'COUNT'
};
break;
}
})
}

let sqlGenCtx: sql.SqlQueryGenerationContext = {
selectFields: requestedFields,
isAggregateQuery
}
let fromClause: sql.FromClause = {
source: collection,
sourceAlias: rootContainerAlias,
};

let sqlGenCtx: sql.SqlQueryContext = {
select: requestedFields,
from: fromClause,
isAggregateQuery,
selectAsValue: false,
};

if (queryRequest.query.limit != null) {
if (queryRequest.query.offset != null) {
Expand All @@ -148,6 +172,7 @@ function parseQueryRequest(collectionsSchema: schema.CollectionsSchema, queryReq
}
sqlGenCtx.predicate = queryRequest.query.predicate;


return sqlGenCtx

}
Expand All @@ -166,9 +191,9 @@ export async function executeQuery(queryRequest: sdk.QueryRequest, collectionsSc
if (dbContainer === undefined || dbContainer == null)
throw new sdk.InternalServerError(`Couldn't find the container '${collection}' in the schema.`)

const sqlGenCtx: sql.SqlQueryGenerationContext = parseQueryRequest(collectionsSchema, queryRequest);
const sqlGenCtx: sql.SqlQueryContext = parseQueryRequest(collectionsSchema, queryRequest);

const sqlQuery = sql.generateSqlQuery(sqlGenCtx, collection, collection[0]);
const sqlQuery = sql.generateSqlQuerySpec(sqlGenCtx, collection, queryRequest.variables);

const queryResponse = await runSQLQuery<{ [k: string]: unknown }>(sqlQuery, dbContainer);

Expand Down
1 change: 0 additions & 1 deletion src/introspectContainerSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export async function fetchLatestNRowsFromContainer(n: number, container: Contai
}

export async function inferJSONSchemaFromContainerRows(rows: string[], containerTypeName: string): Promise<JSONSchema> {
console.log("Container rows are ", JSON.stringify(rows, null, 2));
const jsonInput = jsonInputForTargetLanguage("schema");

await jsonInput.addSource({
Expand Down
Loading