Skip to content

Conversation

mariusandra
Copy link
Collaborator

Problem

I saw one error where a materialised column was inaccessible outside a select subquery, since we didn't prepend the table name.

Changes

Adds a common sense security measure: in printed ClickHouse SQL, we now prepend each accessed field with the name of the table it's on.

How did you test this code?

Updated the tests. Hoping CI will catch anything else.

@mariusandra mariusandra requested review from yakkomajuri, pauldambra, Twixes and neilkakkar and removed request for yakkomajuri March 20, 2023 23:36
Copy link
Contributor

@neilkakkar neilkakkar left a comment

Choose a reason for hiding this comment

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

Looks good,

One thing I'm not sure about: How does this work with aliasing?

For example, if I do JSONExtractRaw(events.properties, blah) as properties and use properties in a line below, prepending events to the name will bork it.

@mariusandra
Copy link
Collaborator Author

mariusandra commented Mar 21, 2023

Thanks for the review!

Depending on how you use properties.

The following is not valid ClickHouse SQL:

select coalesce('not properties', null) as properties, coalesce(properties, events.properties) as `what is this` from events

For some reason columns in a row can't refer to previous ones. You need to use WITH for such magic.

If you alias a column as properties inside a subquery and access it from within a context where you can access it (it's basically in a join), then we'll either add or not add the alias of the subquery, depending on whether it has one or not.

@mariusandra mariusandra merged commit 33e7294 into master Mar 21, 2023
@mariusandra mariusandra deleted the hogql-always-table-name branch March 21, 2023 13:39
@neilkakkar
Copy link
Contributor

That doesn't sound 100% right. For example, this is valid:

SELECT JSONExtractRaw(events.properties, '$os') as properties, upper(properties) as props, lower(events.properties) from events LIMIT 1

and the upper(properties) is not referring to events.properties, but prepending events here will fail?

@neilkakkar
Copy link
Contributor

(also curious why your example fails, not sure why 🤔 )

@neilkakkar
Copy link
Contributor

I think it's a coalesce weirdness, if I reverse the ordering in the second coalesce, it passes. I'm guessing something to do with the restriction on arguments:

image

@mariusandra
Copy link
Collaborator Author

Odd stuff. This query however works fine in both hogql and clickhouse:

image

🤷

@neilkakkar
Copy link
Contributor

Looks like we're good then 🤷 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants