bitflicker64 commented on code in PR #824: URL: https://github.com/apache/hugegraph-toolchain/pull/824#discussion_r4298391470
########## hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/util/ReadOnlyGremlin.java: ########## @@ -0,0 +1,261 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hugegraph.util; + +import java.util.Set; + +import org.apache.hugegraph.exception.ExternalException; + +/** + * A deliberately small traversal grammar for automatically executed model output. + * This accepts literal arguments, read steps and predicates, never Groovy scripts. + * General scripts remain available through the ordinary Gremlin console. + */ +public final class ReadOnlyGremlin { + + private static final Set<String> STEPS = Set.of( + "V", "E", "has", "hasId", "hasLabel", "hasKey", "hasValue", "hasNot", + "out", "in", "both", "outE", "inE", "bothE", "outV", "inV", "bothV", "otherV", + "values", "valueMap", "elementMap", "properties", "propertyMap", "label", "id", "key", "value", + "count", "sum", "min", "max", "mean", "fold", "unfold", "dedup", "order", "by", + "limit", "range", "tail", "skip", "sample", "group", "groupCount", "select", "project", + "path", "simplePath", "cyclicPath", "repeat", "until", "emit", "times", "loops", + "local", "union", "coalesce", "choose", "optional", "and", "or", "not", "where", + "filter", "map", "flatMap", "identity", "constant", "as", "match", "is", + "toList", "toSet", "next", "hasNext"); + + private static final Set<String> PREDICATES = Set.of( + "eq", "neq", "gt", "gte", "lt", "lte", "inside", "outside", "between", "within", "without", + "and", "or", "not", "startingWith", "notStartingWith", "endingWith", "notEndingWith", + "containing", "notContaining", "regex", "notRegex"); + + private static final Set<String> CONSTANTS = Set.of( + "true", "false", "null", "asc", "desc", "shuffle", "local", "global", "keys", "values", + "first", "last", "all", "mixed", "T.id", "T.label", "T.key", "T.value", Review Comment: Important: `CONSTANTS` lists the statically imported `desc`, `keys`, `values` and `local` tokens, but not the `T` tokens `label`, `id`, `key` and `value`. Only their `T.`-qualified forms are listed. Gremlin-Groovy imports `T.*` statically, the same way it imports `Order.*` and `Column.*`, so `g.V().groupCount().by(label)` is the standard TinkerPop way to ask "how many vertices of each label", and it is read-only. I compiled this class from the PR head and ran it on these queries: - rejected: `g.V().groupCount().by(label)`, `g.E().groupCount().by(label)`, `g.V().group().by(label).by(__.count())`, `g.V().project('id','name').by(id).by('name')` - accepted: `g.V().groupCount().by(T.label)`, `g.V().order().by('age', desc).limit(5)` The system prompt in `Text2GqlService` does not tell the model to qualify these tokens. When the model returns the usual form, `normalize` turns the rejection into a 502 `text2gql.invalid-output` ("Try rephrasing the question"). A user who then types the same query into the editor gets `text2gql.read-only`, so a common question about the graph cannot be answered through this tab. Please add `"label", "id", "key", "value"` to `CONSTANTS`. These are plain enum constants and run no code. Then add `by(label)` and `by(id)` cases to `ReadOnlyGremlinTest.acceptsReadTraversalsAndNestedPredicates`. If you would rather keep only the qualified forms, the prompt should say "use T.label and T.id, never bare label or id", but that still leaves edited queries rejected. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
