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

feat(autocomplete): Ignore missing aliases and return whether the property list is complete #20334

Merged
merged 2 commits into from
Feb 14, 2024
Merged
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 1 addition & 5 deletions frontend/src/queries/nodes/HogQLQuery/HogQLQueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,6 @@ export function HogQLQueryEditor(props: HogQLQueryEditorProps): JSX.Element {
monaco.languages.registerCompletionItemProvider('mysql', {
triggerCharacters: [' ', ',', '.'],
provideCompletionItems: async (model, position) => {
if (!logic.isMounted()) {
return undefined
}

if (!featureFlags[FEATURE_FLAGS.HOGQL_AUTOCOMPLETE]) {
return undefined
}
Expand All @@ -226,7 +222,7 @@ export function HogQLQueryEditor(props: HogQLQueryEditorProps): JSX.Element {

const response = await query<HogQLAutocomplete>({
kind: NodeKind.HogQLAutocomplete,
select: logic.values.queryInput,
select: model.getValue(), // Use the text from the model instead of logic due to a race condition on the logic values updating quick enough
filters: props.query.filters,
startPosition: startOffset,
endPosition: endOffset,
Expand Down
14 changes: 10 additions & 4 deletions posthog/hogql/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ def get_hogql_autocomplete(query: HogQLAutocomplete, team: Team) -> HogQLAutocom
aliased_table = tables.get(str(chain_part))
if aliased_table is not None:
last_table = aliased_table
continue
continue
else:
# Dont continue if the alias is not found in the query
break

# Ignore last chain part, it's likely an incomplete word or added characters
is_last_part = index >= (chain_len - 2)
Expand Down Expand Up @@ -384,18 +387,21 @@ def get_hogql_autocomplete(query: HogQLAutocomplete, team: Team) -> HogQLAutocom
if match_term == MATCH_ANY_CHARACTER:
match_term = ""

properties = PropertyDefinition.objects.filter(
property_query = PropertyDefinition.objects.filter(
name__contains=match_term,
team_id=team.pk,
type=property_type,
)[:PROPERTY_DEFINITION_LIMIT].values("name", "property_type")
)

total_property_count = property_query.count()
properties = property_query[:PROPERTY_DEFINITION_LIMIT].values("name", "property_type")

extend_responses(
keys=[prop["name"] for prop in properties],
suggestions=response.suggestions,
details=[prop["property_type"] for prop in properties],
)
response.incomplete_list = True
response.incomplete_list = total_property_count > PROPERTY_DEFINITION_LIMIT
elif isinstance(field, VirtualTable) or isinstance(field, LazyTable):
fields = list(last_table.fields.items())
extend_responses(
Expand Down
29 changes: 28 additions & 1 deletion posthog/hogql/test/test_autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,28 @@ def test_autocomplete_complete_list(self):
results = self._query_response(query=query, start=7, end=12)
assert results.incomplete_list is False

def test_autocomplete_incomplete_list(self):
def test_autocomplete_properties_list_with_under_220_properties(self):
for index in range(20):
PropertyDefinition.objects.create(
team=self.team,
name=f"some_event_value_{index}",
property_type="String",
type=PropertyDefinition.Type.EVENT,
)

query = "select properties. from events"
results = self._query_response(query=query, start=18, end=18)
assert results.incomplete_list is False

def test_autocomplete_properties_list_with_over_220_properties(self):
for index in range(221):
PropertyDefinition.objects.create(
team=self.team,
name=f"some_event_value_{index}",
property_type="String",
type=PropertyDefinition.Type.EVENT,
)

query = "select properties. from events"
results = self._query_response(query=query, start=18, end=18)
assert results.incomplete_list is True
Expand Down Expand Up @@ -199,3 +220,9 @@ def test_autocomplete_joined_tables_aliases(self):
assert len(results.suggestions) == 2
assert results.suggestions[0].label == "e"
assert results.suggestions[1].label == "p"

def test_autocomplete_non_existing_alias(self):
query = "select o. from events e"
results = self._query_response(query=query, start=9, end=9)

assert len(results.suggestions) == 0
Loading