Skip to content

Conversation

pulpdrew
Copy link
Contributor

@pulpdrew pulpdrew commented Oct 1, 2025

Closes HDX-2042
Closes HDX-2524
Closes HDX-2307
Closes #1010

Summary

This PR fixes errors that occurred when attempting to open the sidebar by clicking a log table row using a JSON logs table schema.

The error was caused by node-sql-parser throwing exceptions when parsing SQL with JSON Expressions, resulting in HyperDX being unable to extract aliases from the SQL. In the long term, we'll want to have a true ClickHouse SQL parser. In the short term, this is fixed by:

  1. Finding and replacing all JSON expressions in the sql with placeholder tokens, prior to parsing with node-sql-parser
  2. Parsing with node-sql-parser to find aliases correctly
  3. Replacing the placeholder tokens with the original JSON expressions

Testing

(All of the following use a JSON schema)

Before

When selecting a JSON column with an alias Screenshot 2025-10-01 at 2 28 19 PM Screenshot 2025-10-01 at 2 28 06 PM
When filtering by a JSON column and using an alias on a non-JSON property Screenshot 2025-10-01 at 2 29 44 PM Screenshot 2025-10-01 at 2 28 06 PM

After

When selecting a JSON column with an alias Screenshot 2025-10-01 at 2 28 19 PM Screenshot 2025-10-01 at 2 30 42 PM
When filtering by a JSON column and using an alias on a non-JSON property Screenshot 2025-10-01 at 2 29 44 PM Screenshot 2025-10-01 at 2 30 24 PM

Copy link

changeset-bot bot commented Oct 1, 2025

🦋 Changeset detected

Latest commit: cc6d4c3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@hyperdx/common-utils Patch
@hyperdx/app Patch
@hyperdx/api Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Oct 1, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
hyperdx-v2-oss-app Ready Ready Preview Comment Oct 6, 2025 8:54pm

Copy link
Contributor

github-actions bot commented Oct 1, 2025

E2E Test Results

All tests passed • 25 passed • 3 skipped • 205s

Status Count
✅ Passed 25
❌ Failed 0
⚠️ Flaky 0
⏭️ Skipped 3

View full report →

// Currently we can't distinguish null or 'null'
if (value === 'null') {
return SqlString.format(`isNull(??)`, [column]);
return SqlString.format(`isNull(?)`, [SqlString.raw(valueExpr)]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This now ensures that the original expression, rather than the alias, is used. This matches the behavior elsewhere in this function and fixes another caused of sidebar loading failures for JSON types.

@wrn14897
Copy link
Member

wrn14897 commented Oct 2, 2025

We can ship patches for now. However, it looks like someone has already built the ClickHouse dialect and might get merged to the upstream. I think we should give it a try. In the long term, we should focus on improving this dialect instead of relying on the postgres one.

@pulpdrew pulpdrew closed this Oct 2, 2025
@pulpdrew pulpdrew reopened this Oct 2, 2025
@pulpdrew
Copy link
Contributor Author

pulpdrew commented Oct 2, 2025

(Accidentally closed, apologies)

@pulpdrew
Copy link
Contributor Author

pulpdrew commented Oct 2, 2025

We can ship patches for now. However, it looks like someone has already built the ClickHouse dialect and might get merged to the upstream. I think we should give it a try. In the long term, we should focus on improving this dialect instead of relying on the postgres one.

Yeah I saw that! I upvoted the comments asking the maintainer to merge the ClickHouse support 🤞 It would be nice to be able to contribute to that instead of building these workarounds.

}

/** Returns true if the given expression is a JSON expression, eg. `col.key.nestedKey` or "json_col"."key" */
const isJsonExpression = (expr: string) => {
Copy link
Member

@wrn14897 wrn14897 Oct 2, 2025

Choose a reason for hiding this comment

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

Are '.' or 1.5 valid JSON? This method returns true in these cases, but I’m not sure if that affects other methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, updated isJsonExpression so that it does not recognize . or 1.5 as JSON expressions

Copy link
Member

Choose a reason for hiding this comment

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

Could you add a few more unit tests for this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some more unit tests 👍

Comment on lines +688 to +689
const { sqlWithReplacements, replacements: jsonReplacementsToExpressions } =
replaceJsonExpressions(sql);
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to enable this only when node-sql-parser isn't able to parse the sql or if it contains JSON syntax?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately that isn't straightforward either, because node-sql-parser doesn't error on all JSON - sometimes it just parses the JSON incorrectly as schema.table.column or table.column. And determining if it contains json syntax would probably have to make use of the same functions being used here to find and replace the JSON.

*
* Note - This function does not distinguish between json references and `table.column` references - both are returned.
*/
export function findJsonExpressions(sql: string) {
Copy link
Member

Choose a reason for hiding this comment

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

Don’t have a lot of confidence that this will work for all edge cases, but I’m okay with shipping the patch until we fix the parser

@pulpdrew
Copy link
Contributor Author

pulpdrew commented Oct 3, 2025

@wrn14897 I also confirmed that the forked node-sql-parser with ClickHouse support does not yet support JSON 😞 We could consider contributing that support or try to get ClickHouse Analyzer in a usable state. 100% agreed that it would be best to have a working ClickHouse SQL parser instead of a bunch of patches on node-sql-parser

@kodiakhq kodiakhq bot merged commit b46ae2f into main Oct 6, 2025
7 of 8 checks passed
@kodiakhq kodiakhq bot deleted the drew/allow-select-json-as-alias branch October 6, 2025 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Unable to View Details of Rows When an Alias is Used for a JSON Field Path in a Select Query

2 participants