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

Dynamic columns: Cast to double if type exists in schema #471

Merged
merged 5 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 9 additions & 2 deletions src/KustoExpressionParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ describe('KustoExpressionParser', () => {
);
});

it('should parse expression with summarize of sum on dynamic column', () => {
it('should parse expression with summarize of sum with cast to double on dynamic column with integer and double types', () => {
const expression = createQueryExpression({
from: createProperty('StormEvents'),
where: createArray([createOperator('column["isActive"]', '==', true)]),
Expand All @@ -524,10 +524,17 @@ describe('KustoExpressionParser', () => {
CslType: 'int',
isDynamic: true,
},
{
Name: 'column["level"]["active"]',
CslType: 'double',
isDynamic: true,
},
Copy link
Contributor

@andresmgot andresmgot Sep 16, 2022

Choose a reason for hiding this comment

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

I believe this does not represent reality. The schema will contain only one element per Name so, there won't be a repeated column["level"]["active"].

The fix should be in datasource.ts:

const recordSchemaArray = (name: string, types: AdxSchemaDefinition[], result: AdxColumnSchema[]) => {
  // If a field can have different types (e.g. long and double)
  // we select the first, assuming they are interchangeable
  const defaultCslType = types[0];
  if (types.every((t) => typeof t === 'string' && toPropertyType(t) === QueryEditorPropertyType.Number)) {
    // If all the types are numbers, the double takes precedence since it has more precission.
    const cslType = types.find((t) => typeof t === 'string' && (t === 'double' || t === 'real')) || defaultCslType;
    result.push({ Name: name, CslType: cslType as string, isDynamic: true });
  } else {
    console.warn(`schema ${name} may contain different types, assuming ${defaultCslType}`);
    if (typeof defaultCslType === 'object') {
      recordSchema(name, types[0], result);
    } else {
      result.push({ Name: name, CslType: defaultCslType, isDynamic: true });
    }
  }
};

here is where we record the schema in case the type has multiple values. We just need to remove the condition that checks if all the types are Number and simply set the type to double or real if it's part of the list.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, so when we get to escapeAndCastIfDynamic the selection of a single type has already been made, my bad!

I'll make the changes to datasource.ts 🙇‍♀️

];

expect(parser.toQuery(expression, tableSchema)).toEqual(
'StormEvents' + '\n| where column["isActive"] == true' + `\n| summarize sum(toint(column["level"]["active"]))`
'StormEvents' +
'\n| where column["isActive"] == true' +
`\n| summarize sum(todouble(column["level"]["active"]))`
);
});

Expand Down
8 changes: 4 additions & 4 deletions src/KustoExpressionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,13 @@ const defaultTimeColumn = (columns?: AdxColumnSchema[], expression?: QueryExpres
};

const escapeAndCastIfDynamic = (column: string, tableSchema?: AdxColumnSchema[], schemaName?: string): string => {
const columnSchema = tableSchema?.find((c) => c.Name === (schemaName || column));

if (!columnSchema?.isDynamic || !Array.isArray(tableSchema)) {
const columnSchemas = tableSchema?.filter((c) => c.Name === (schemaName || column));
if (!columnSchemas || columnSchemas.length === 0 || !Array.isArray(tableSchema)) {
return escapeColumn(column);
}

if (!columnSchema) {
const columnSchema = columnSchemas?.find((c) => c.CslType === 'double') ?? columnSchemas[0];
if (!columnSchema.isDynamic) {
return escapeColumn(column);
}

Expand Down