Skip to content

Commit

Permalink
commenting on format
Browse files Browse the repository at this point in the history
Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com>
  • Loading branch information
abbyhu2000 committed Feb 26, 2024
1 parent e5c1a64 commit 879a2c8
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 19 deletions.
22 changes: 22 additions & 0 deletions src/plugins/data/common/opensearch_query/kuery/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ const fromExpression = (
}

return parse(expression, { ...parseOptions, helpers: { nodeTypes } });
/*
* input: 'customer_id: 10'
*
* output: {
arguments: [
{
type: 'literal',
value: 'customer_id'
},
{
type: 'literal',
value: 10
},
{
type: 'literal',
value: false
}
]
}
*/
};

export const fromLiteralExpression = (
Expand Down Expand Up @@ -109,4 +129,6 @@ export const toOpenSearchQuery = (
const nodeType = (nodeTypes[node.type] as unknown) as any;

return nodeType.toOpenSearchQuery(node, indexPattern, config, context);
// every node type has its own unique opensearch query,
// if its a function node type, then do to opensearch_query/kuery/node_types/function.ts
};
17 changes: 17 additions & 0 deletions src/plugins/data/common/opensearch_query/kuery/functions/and.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,21 @@ export function toOpenSearchQuery(
}),
},
};

/*
* return {
bool: {
filter: [
bool: {
minimum_should_match: 1,
should: [
match: {
customer_id: 10
}
]
}
]
}
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ export function toOpenSearchQuery(
) {
const kueryFunction = functions[node.function as FunctionName];
return kueryFunction.toOpenSearchQuery(node, indexPattern, config, context);

// go to opensearch_query/kuery/functions/and.ts
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { IIndexPattern } from '../../index_patterns';
import { Filter } from '../filters';
import { Query } from '../../query/types';
import { buildQueryFromSql } from './from_sql';
import Mustache from 'mustache';

Check failure on line 39 in src/plugins/data/common/opensearch_query/opensearch_query/build_opensearch_query.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

`mustache` import should occur before import of `./from_kuery`

export interface OpenSearchQueryConfig {
allowLeadingWildcards: boolean;
Expand Down Expand Up @@ -67,16 +68,22 @@ export function buildOpenSearchQuery(

// TODO: SQL make this combinable. SQL needs to support DSL
// console.log('queries', queries);
const sqlQueries = queries.filter((query) => query.language === 'SQL');
if (sqlQueries.length > 0) {
// console.log('sqlQueries', sqlQueries);
return buildQueryFromSql(sqlQueries, config.dateFormatTZ);
}
// const sqlQueries = queries.filter((query) => query.language === 'SQL');
// if (sqlQueries.length > 0) {
// // console.log('sqlQueries', sqlQueries);
// return buildQueryFromSql(sqlQueries, config.dateFormatTZ);
// }

const validQueries = queries
.filter((query) => query.language !== 'SQL')
//.filter((query) => query.language !== 'SQL')

Check failure on line 78 in src/plugins/data/common/opensearch_query/opensearch_query/build_opensearch_query.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
.filter((query) => has(query, 'query'));
const queriesByLanguage = groupBy(validQueries, 'language');
// const sqlQuery = buildQueryFromSql(
// indexPattern,
// queriesByLanguage.SQL,
// config.allowLeadingWildcards,
// config.dateFormatTZ
// );
const kueryQuery = buildQueryFromKuery(
indexPattern,
queriesByLanguage.kuery,
Expand All @@ -96,10 +103,30 @@ export function buildOpenSearchQuery(

return {
bool: {
must: [...kueryQuery.must, ...luceneQuery.must, ...filterQuery.must],
filter: [...kueryQuery.filter, ...luceneQuery.filter, ...filterQuery.filter],
should: [...kueryQuery.should, ...luceneQuery.should, ...filterQuery.should],
must_not: [...kueryQuery.must_not, ...luceneQuery.must_not, ...filterQuery.must_not],
must: [
//...sqlQuery.must,

Check failure on line 107 in src/plugins/data/common/opensearch_query/opensearch_query/build_opensearch_query.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
...kueryQuery.must,
...luceneQuery.must,
...filterQuery.must,
],
filter: [
//...sqlQuery.filter,

Check failure on line 113 in src/plugins/data/common/opensearch_query/opensearch_query/build_opensearch_query.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
...kueryQuery.filter,
...luceneQuery.filter,
...filterQuery.filter,
],
should: [
//...sqlQuery.should,

Check failure on line 119 in src/plugins/data/common/opensearch_query/opensearch_query/build_opensearch_query.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
...kueryQuery.should,
...luceneQuery.should,
...filterQuery.should,
],
must_not: [
//...sqlQuery.must_not,

Check failure on line 125 in src/plugins/data/common/opensearch_query/opensearch_query/build_opensearch_query.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
...kueryQuery.must_not,
...luceneQuery.must_not,
...filterQuery.must_not,
],
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,42 @@ function buildQuery(
config: Record<string, any> = {}
) {
const compoundQueryAST = nodeTypes.function.buildNode('and', queryASTs);
/*
* compoundQueryAST:
* {
* arguments: [{
* arguments: [],
* function: "is",
* type: "function"
* }],
* function: "and",
* type: "function"
* }
*/

const kueryQuery = toOpenSearchQuery(compoundQueryAST, indexPattern, config);

/*
* kueryQuery: {
bool: {
filter: [
{
bool: {
minimum_should_match: 1,
should: [
{
match: {
customer_id: 10
}
}
]
}
}
]
}
}
*/

return Object.assign(
{
must: [],
Expand All @@ -62,4 +96,8 @@ function buildQuery(
},
kueryQuery.bool
);

/*
* assign to filter
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,77 @@
import { decorateQuery } from './decorate_query';
import { getIndexPatternFromSql, sqlStringToDsl } from './sql_string_to_dsl';
import { Query } from '../../query/types';
import { IIndexPattern } from '../../types';
import { DslQuery } from '../kuery';

export function buildQueryFromSql(queries: Query[], dateFormatTZ?: string) {
const combinedQueries = (queries || []).map((query) => {
const indexPattern = getIndexPatternFromSql(query.query);
const queryDsl = sqlStringToDsl(query.query);
// export function buildQueryFromSql(
// indexPattern: IIndexPattern | undefined,
// queries: Query[] = [],
// allowLeadingWildcards: boolean = false,
// dateFormatTZ?: string
// ) {
// const sqlQueryASTs = (queries || []).map((query) => {
// return fromSqlExpression(query.query);
// });

return decorateQuery(queryDsl, indexPattern, dateFormatTZ);
});
// return buildQuery(indexPattern, sqlQueryASTs, { dateFormatTZ });
// }

return {
combinedQueries,
};
const parse = (expression: string) => {
// look for keywords
const from = expression.match(new RegExp(/FROM\s+([\w*-.!@$^()~;]+)/, 'i'));
const filterField = expression.match(new RegExp(/WHERE\s+([\w*-.!@$^()~;]+)/, 'i'));
};

const fromExpression = (
expression: string | DslQuery
//parseOptions: Partial<SQLParseOptions> = {},

Check failure on line 33 in src/plugins/data/common/opensearch_query/opensearch_query/from_sql.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
//parse: Function = parseSQL

Check failure on line 34 in src/plugins/data/common/opensearch_query/opensearch_query/from_sql.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
) => {
if (typeof expression === 'undefined') {
throw new Error('expression must be a string, got undefined instead');
}

return parse(
expression
//{ ...parseOptions, helpers: { nodeTypes } }

Check failure on line 42 in src/plugins/data/common/opensearch_query/opensearch_query/from_sql.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
);
};

const fromSqlExpression = (
expression: string | DslQuery
//parseOptions: Partial<SQLParseOptions> = {}

Check failure on line 48 in src/plugins/data/common/opensearch_query/opensearch_query/from_sql.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
) => {
try {
return fromExpression(
expression
//parseOptions,
//parseKuery
);
} catch (error) {
// if (error.name === 'SyntaxError') {
// throw new SQLSyntaxError(error, expression);
// } else {
// throw error;
// }
}
};

function buildQuery(
indexPattern: IIndexPattern | undefined,
queryASTs: KueryNode[],
config: Record<string, any> = {}
) {
const compoundQueryAST = nodeTypes.function.buildNode('and', queryASTs);
const kueryQuery = toOpenSearchQuery(compoundQueryAST, indexPattern, config);

return Object.assign(
{
must: [],
filter: [],
should: [],
must_not: [],
},
kueryQuery.bool
);
}

0 comments on commit 879a2c8

Please sign in to comment.