Skip to content

Commit d811665

Browse files
authored
Add --explain-type option to %%gremlin (#503)
* Add --explain-type option to %%gremlin * Update changelog --------- Co-authored-by: Michael Chin <chnmch@amazon.com>
1 parent f55174f commit d811665

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Starting with v1.31.6, this file will contain a record of major features and updates made in each release of graph-notebook.
44

55
## Upcoming
6+
- Added `--explain-type` option to `%%gremlin` ([Link to PR](https://github.com/aws/graph-notebook/pull/503))
67

78
## Release 3.8.2 (June 5, 2023)
89
- New Sample Applications - Healthcare and Life Sciences notebooks ([Link to PR](https://github.com/aws/graph-notebook/pull/484))

src/graph_notebook/magics/graph_magic.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
LOAD_JOB_MODES, MODE_AUTO, FINAL_LOAD_STATUSES, SPARQL_ACTION, FORMAT_CSV, FORMAT_OPENCYPHER, FORMAT_NTRIPLE, \
4545
FORMAT_NQUADS, FORMAT_RDFXML, FORMAT_TURTLE, STREAM_RDF, STREAM_PG, STREAM_ENDPOINTS, \
4646
NEPTUNE_CONFIG_HOST_IDENTIFIERS, is_allowed_neptune_host, \
47-
STATISTICS_LANGUAGE_INPUTS, STATISTICS_MODES, SUMMARY_MODES
47+
STATISTICS_LANGUAGE_INPUTS, STATISTICS_MODES, SUMMARY_MODES, \
48+
SPARQL_EXPLAIN_MODES, OPENCYPHER_EXPLAIN_MODES
4849
from graph_notebook.network import SPARQLNetwork
4950
from graph_notebook.network.gremlin.GremlinNetwork import parse_pattern_list_str, GremlinNetwork
5051
from graph_notebook.visualization.rows_and_columns import sparql_get_rows_and_columns, opencypher_get_rows_and_columns
@@ -516,9 +517,9 @@ def sparql(self, line='', cell='', local_ns: dict = None):
516517
help='prefix path to sparql endpoint. For example, if "foo/bar" were specified, '
517518
'the endpoint called would be host:port/foo/bar')
518519
parser.add_argument('--expand-all', action='store_true')
519-
parser.add_argument('--explain-type', default='dynamic',
520-
help='explain mode to use when using the explain query mode',
521-
choices=['dynamic', 'static', 'details'])
520+
parser.add_argument('--explain-type', type=str.lower, default='dynamic',
521+
help=f'Explain mode to use when using the explain query mode. '
522+
f'Expected values: ${SPARQL_EXPLAIN_MODES}')
522523
parser.add_argument('--explain-format', default='text/html', help='response format for explain query mode',
523524
choices=['text/csv', 'text/html'])
524525
parser.add_argument('-m', '--media-type', type=str, default='',
@@ -804,6 +805,8 @@ def gremlin(self, line, cell, local_ns: dict = None):
804805
parser = argparse.ArgumentParser()
805806
parser.add_argument('query_mode', nargs='?', default='query',
806807
help='query mode (default=query) [query|explain|profile]')
808+
parser.add_argument('--explain-type', type=str.lower, default='',
809+
help='Explain mode to use when using the explain query mode.')
807810
parser.add_argument('-p', '--path-pattern', default='', help='path pattern')
808811
parser.add_argument('-g', '--group-by', type=str, default='T.label',
809812
help='Property used to group nodes (e.g. code, T.region) default is T.label')
@@ -881,7 +884,8 @@ def gremlin(self, line, cell, local_ns: dict = None):
881884
transport_args = {'max_content_length': args.max_content_length}
882885

883886
if mode == QueryMode.EXPLAIN:
884-
res = self.client.gremlin_explain(cell)
887+
res = self.client.gremlin_explain(cell,
888+
args={'explain.mode': args.explain_type} if args.explain_type else {})
885889
res.raise_for_status()
886890
# Replace strikethrough character bytes, can't be encoded to ASCII
887891
explain_bytes = res.content.replace(b'\xcc', b'-')
@@ -2685,9 +2689,9 @@ def handle_opencypher_query(self, line, cell, local_ns):
26852689
This method in its own handler so that the magics %%opencypher and %%oc can both call it
26862690
"""
26872691
parser = argparse.ArgumentParser()
2688-
parser.add_argument('--explain-type', default='dynamic',
2689-
help='explain mode to use when using the explain query mode',
2690-
choices=['dynamic', 'static', 'details', 'debug'])
2692+
parser.add_argument('--explain-type', type=str.lower, default='dynamic',
2693+
help=f'Explain mode to use when using the explain query mode. '
2694+
f'Accepted values: ${OPENCYPHER_EXPLAIN_MODES}')
26912695
parser.add_argument('-qp', '--query-parameters', type=str, default='',
26922696
help='Parameter definitions to apply to the query. This option can accept a local variable '
26932697
'name, or a string representation of the map.')

src/graph_notebook/neptune/client.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
SUMMARY_MODES = ["", "basic", "detailed"]
115115
STATISTICS_LANGUAGE_INPUTS = ["propertygraph", "pg", "gremlin", "oc", "opencypher", "sparql", "rdf"]
116116

117+
SPARQL_EXPLAIN_MODES = ['dynamic', 'static', 'details']
118+
OPENCYPHER_EXPLAIN_MODES = ['dynamic', 'static', 'details']
119+
117120

118121
def is_allowed_neptune_host(hostname: str, host_allowlist: list):
119122
for host_snippet in host_allowlist:
@@ -211,12 +214,8 @@ def do_sparql_request(self, data: dict, headers=None, explain: str = '', path: s
211214
if 'content-type' not in headers:
212215
headers['content-type'] = DEFAULT_SPARQL_CONTENT_TYPE
213216

214-
explain = explain.lower()
215217
if explain != '':
216-
if explain not in ['static', 'dynamic', 'details']:
217-
raise ValueError('explain mode not valid, must be one of "static", "dynamic", or "details"')
218-
else:
219-
data['explain'] = explain
218+
data['explain'] = explain
220219

221220
if path != '':
222221
sparql_path = f'/{path}'

0 commit comments

Comments
 (0)