Skip to content

Commit

Permalink
feat(autocomplete): Ignore missing aliases and return whether the pro…
Browse files Browse the repository at this point in the history
…perty list is complete (#20334)

* Ignore missing aliases and return whether the property list is complete

* Update UI snapshots for `webkit` (2)

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Gilbert09 and github-actions[bot] authored Feb 14, 2024
1 parent f2b8462 commit b8f51e9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
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

0 comments on commit b8f51e9

Please sign in to comment.