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

[ES|QL] detect the type of COUNT(*) #197914

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -225,79 +225,47 @@ describe('getExpressionType', () => {
});

it('detects the return type of a function', () => {
expect(
getExpressionType(getASTForExpression('returns_keyword()'), new Map(), new Map())
).toBe('keyword');
expect(getExpressionType(getASTForExpression('returns_keyword()'))).toBe('keyword');
});

it('selects the correct signature based on the arguments', () => {
expect(getExpressionType(getASTForExpression('test("foo")'), new Map(), new Map())).toBe(
'keyword'
);
expect(getExpressionType(getASTForExpression('test(1.)'), new Map(), new Map())).toBe(
'double'
);
expect(getExpressionType(getASTForExpression('test(1., "foo")'), new Map(), new Map())).toBe(
'long'
);
expect(getExpressionType(getASTForExpression('test("foo")'))).toBe('keyword');
expect(getExpressionType(getASTForExpression('test(1.)'))).toBe('double');
expect(getExpressionType(getASTForExpression('test(1., "foo")'))).toBe('long');
});

it('supports nested functions', () => {
expect(
getExpressionType(
getASTForExpression('test(1., test(test(test(returns_keyword()))))'),
new Map(),
new Map()
)
getExpressionType(getASTForExpression('test(1., test(test(test(returns_keyword()))))'))
).toBe('long');
});

it('supports functions with casted results', () => {
expect(
getExpressionType(getASTForExpression('test(1.)::keyword'), new Map(), new Map())
).toBe('keyword');
expect(getExpressionType(getASTForExpression('test(1.)::keyword'))).toBe('keyword');
});

it('handles nulls and string-date casting', () => {
expect(getExpressionType(getASTForExpression('test(NULL)'), new Map(), new Map())).toBe(
'null'
);
expect(getExpressionType(getASTForExpression('test(NULL, NULL)'), new Map(), new Map())).toBe(
'null'
);
expect(
getExpressionType(getASTForExpression('accepts_dates("", "")'), new Map(), new Map())
).toBe('keyword');
expect(getExpressionType(getASTForExpression('test(NULL)'))).toBe('null');
expect(getExpressionType(getASTForExpression('test(NULL, NULL)'))).toBe('null');
expect(getExpressionType(getASTForExpression('accepts_dates("", "")'))).toBe('keyword');
});

it('deals with functions that do not exist', () => {
expect(getExpressionType(getASTForExpression('does_not_exist()'), new Map(), new Map())).toBe(
'unknown'
);
expect(getExpressionType(getASTForExpression('does_not_exist()'))).toBe('unknown');
});

it('deals with bad function invocations', () => {
expect(
getExpressionType(getASTForExpression('test(1., "foo", "bar")'), new Map(), new Map())
).toBe('unknown');
expect(getExpressionType(getASTForExpression('test(1., "foo", "bar")'))).toBe('unknown');

expect(getExpressionType(getASTForExpression('test()'), new Map(), new Map())).toBe(
'unknown'
);
expect(getExpressionType(getASTForExpression('test()'))).toBe('unknown');

expect(getExpressionType(getASTForExpression('test("foo", 1.)'), new Map(), new Map())).toBe(
'unknown'
);
expect(getExpressionType(getASTForExpression('test("foo", 1.)'))).toBe('unknown');
});

it('deals with the CASE function', () => {
expect(getExpressionType(getASTForExpression('CASE(true, 1, 2)'), new Map(), new Map())).toBe(
'integer'
);
expect(getExpressionType(getASTForExpression('CASE(true, 1, 2)'))).toBe('integer');

expect(
getExpressionType(getASTForExpression('CASE(true, 1., true, 1., 2.)'), new Map(), new Map())
).toBe('double');
expect(getExpressionType(getASTForExpression('CASE(true, 1., true, 1., 2.)'))).toBe('double');

expect(
getExpressionType(
Expand All @@ -306,6 +274,20 @@ describe('getExpressionType', () => {
new Map()
)
).toBe('keyword');

expect(
getExpressionType(
getASTForExpression('CASE(true, "", true, "", keywordVar)'),
new Map(),
new Map([
[`keywordVar`, [{ name: 'keywordVar', type: 'keyword', location: { min: 0, max: 0 } }]],
])
)
).toBe('keyword');
});

it('supports COUNT(*)', () => {
expect(getExpressionType(getASTForExpression('COUNT(*)'))).toBe<SupportedDataType>('long');
});
});

Expand Down
32 changes: 24 additions & 8 deletions packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,15 +815,31 @@ export function getExpressionType(
return 'unknown';
}

/**
* Special case for COUNT(*) because
* the "*" column doesn't match any
* of COUNT's function definitions
*/
if (
fnDefinition.name === 'count' &&
root.args[0] &&
isColumnItem(root.args[0]) &&
root.args[0].name === '*'
) {
return 'long';
}

if (fnDefinition.name === 'case' && root.args.length) {
// The CASE function doesn't fit our system of function definitions
// and needs special handling. This is imperfect, but it's a start because
// at least we know that the final argument to case will never be a conditional
// expression, always a result expression.
//
// One problem with this is that if a false case is not provided, the return type
// will be null, which we aren't detecting. But this is ok because we consider
// variables and fields to be nullable anyways and account for that during validation.
/**
* The CASE function doesn't fit our system of function definitions
* and needs special handling. This is imperfect, but it's a start because
* at least we know that the final argument to case will never be a conditional
* expression, always a result expression.
*
* One problem with this is that if a false case is not provided, the return type
* will be null, which we aren't detecting. But this is ok because we consider
* variables and fields to be nullable anyways and account for that during validation.
*/
return getExpressionType(root.args[root.args.length - 1], fields, variables);
}

Expand Down