dql

package
v25.0.0-custom-qt-impr... Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2025 License: Apache-2.0 Imports: 12 Imported by: 2

README

Comparing the old and new methods, we find that using slice makes the parsing 20% faster on average than using channels. Also, using slices allows the parser to backtrack and peek the tokens which couldn't be done using channels as each token can only be consumed once.

Name                              unit    Old     New     Improvement
----------------------------------------------------------------------
Benchmark_Filters-4               ns/op   14007   9634    31 %
Benchmark_Geq-4                   ns/op   11701   8602    26 %
Benchmark_Date-4                  ns/op   11687   8630    26 %
Benchmark_directors-4             ns/op   18663   14201   23 %
Benchmark_Filters_parallel-4      ns/op   6486    5015    22 %
Benchmark_Movies-4                ns/op   16097   12807   20 %
Benchmark_directors_parallel-4    ns/op   8766    6966    20 %
Benchmark_Mutation-4              ns/op   5167    4155    19 %
Benchmark_Movies_parallel-4       ns/op   7537    6151    18 %
Benchmark_Date_parallel-4         ns/op   5462    4515    17 %
Benchmark_Geq_parallel-4          ns/op   5390    4485    16 %
Benchmark_Mutation_parallel-4     ns/op   2326    2161    07 %
Benchmark_Mutation1000-4          ns/op   549428  512851  06 %
Benchmark_Mutation1000_parallel-4 ns/op   261785  254911  02 %

Documentation

Overview

Package dql is responsible for lexing and parsing a GraphQL query/mutation.

Index

Constants

View Source
const (
	AnyVar   = 0
	UidVar   = 1
	ValueVar = 2
	ListVar  = 3
)

Variables

This section is empty.

Functions

func IsInequalityFn

func IsInequalityFn(name string) bool

func ParseDQL

func ParseDQL(dqlQuery string) (*api.Request, error)

func ParseMutation

func ParseMutation(mutation string) (req *api.Request, err error)

ParseMutation parses a block into a mutation. Returns an object with a mutation or an upsert block with mutation, otherwise returns nil with an error.

func ParseUid

func ParseUid(xid string) (uint64, error)

ParseUid parses the given string into an UID. This method returns with an error if the string cannot be parsed or the parsed UID is zero.

func TypeValFrom

func TypeValFrom(val *api.Value) types.Val

Types

type Arg

type Arg struct {
	Value      string
	IsValueVar bool // If argument is val(a), e.g. eq(name, val(a))
	IsDQLVar   bool
}

Arg stores an argument to a function.

type FacetOrder

type FacetOrder struct {
	Key  string
	Desc bool // true if ordering should be decending by this facet.
}

FacetOrder stores ordering for single facet key.

type FilterTree

type FilterTree struct {
	Op    string
	Child []*FilterTree
	Func  *Function
}

FilterTree is the result of parsing the filter directive. Either you can have `Op and Children` on non-leaf nodes Or Func at leaf nodes.

type Function

type Function struct {
	Attr       string
	Lang       string // language of the attribute value
	Name       string // Specifies the name of the function.
	Args       []Arg  // Contains the arguments of the function.
	UID        []uint64
	NeedsVar   []VarContext // If the function requires some variable
	IsCount    bool         // gt(count(friends),0)
	IsValueVar bool         // eq(val(s), 5)
	IsLenVar   bool         // eq(len(s), 5)
}

Function holds the information about dql functions.

func (*Function) IsAggregator

func (f *Function) IsAggregator() bool

IsAggregator returns true if the function name is an aggregation function.

func (*Function) IsPasswordVerifier

func (f *Function) IsPasswordVerifier() bool

IsPasswordVerifier returns true if the function name is "checkpwd".

type GraphQuery

type GraphQuery struct {
	UID        []uint64
	Attr       string
	Langs      []string
	Alias      string
	IsCount    bool
	IsInternal bool
	IsGroupby  bool
	Var        string
	NeedsVar   []VarContext
	Func       *Function
	Expand     string // Which variable to expand with.

	Args map[string]string
	// Query can have multiple sort parameters.
	Order            []*pb.Order
	Children         []*GraphQuery
	Filter           *FilterTree
	MathExp          *MathTree
	Normalize        bool
	Recurse          bool
	RecurseArgs      RecurseArgs
	ShortestPathArgs ShortestPathArgs
	Cascade          []string
	IgnoreReflex     bool
	Facets           *pb.FacetParams
	FacetsFilter     *FilterTree
	GroupbyAttrs     []GroupByAttr
	FacetVar         map[string]string
	FacetsOrder      []*FacetOrder

	// Used for ACL enabled queries to curtail results to only accessible params
	AllowedPreds []string

	// True for blocks that don't have a starting function and hence no starting nodes. They are
	// used to aggregate and get variables defined in another block.
	IsEmpty bool
	// contains filtered or unexported fields
}

GraphQuery stores the parsed Query in a tree format. This gets converted to pb.y used query.SubGraph before processing the query.

func (*GraphQuery) DebugPrint

func (gq *GraphQuery) DebugPrint(prefix string)

DebugPrint is useful for debugging.

type GroupByAttr

type GroupByAttr struct {
	Attr  string
	Alias string
	Langs []string
}

GroupByAttr stores the arguments needed to process the @groupby directive.

type MathTree

type MathTree struct {
	Fn    string
	Var   string
	Const types.Val // This will always be parsed as a float value
	Val   *types.ShardedMap
	Child []*MathTree
}

MathTree represents math operations in tree form for evaluation.

type Mutation

type Mutation struct {
	Cond         string
	Set          []*api.NQuad
	Del          []*api.NQuad
	AllowedPreds []string

	Metadata *pb.Metadata
}

Mutation stores the strings corresponding to set and delete operations.

type NQuad

type NQuad struct {
	*api.NQuad
}

NQuad is an alias for the NQuad type in the API protobuf library.

func (NQuad) CreateUidEdge

func (nq NQuad) CreateUidEdge(subjectUid uint64, objectUid uint64) *pb.DirectedEdge

CreateUidEdge returns a Directed edge connecting the given subject and object UIDs.

func (NQuad) CreateValueEdge

func (nq NQuad) CreateValueEdge(subjectUid uint64) (*pb.DirectedEdge, error)

CreateValueEdge returns a DirectedEdge with the given subject. The predicate, language, and facet values are derived from the NQuad.

func (NQuad) ToDeletePredEdge

func (nq NQuad) ToDeletePredEdge() (*pb.DirectedEdge, error)

ToDeletePredEdge takes an NQuad of the form '* p *' and returns the equivalent directed edge. Returns an error if the NQuad does not have the expected form.

func (NQuad) ToEdgeUsing

func (nq NQuad) ToEdgeUsing(newToUid map[string]uint64) (*pb.DirectedEdge, error)

ToEdgeUsing determines the UIDs for the provided XIDs and populates the xidToUid map.

type RecurseArgs

type RecurseArgs struct {
	Depth     uint64
	AllowLoop bool
	// contains filtered or unexported fields
}

RecurseArgs stores the arguments needed to process the @recurse directive.

type Request

type Request struct {
	Str       string
	Variables map[string]string
}

Request stores the query text and the variable mapping.

type Result

type Result struct {
	Query     []*GraphQuery
	QueryVars []*Vars
	Schema    *pb.SchemaRequest
}

Result struct contains the Query list, its corresponding variable use list and the mutation block.

func Parse

func Parse(r Request) (Result, error)

Parse initializes and runs the lexer. It also constructs the GraphQuery subgraph from the lexed items.

func ParseWithNeedVars

func ParseWithNeedVars(r Request, needVars []string) (res Result, rerr error)

ParseWithNeedVars performs parsing of a query with given needVars.

The needVars parameter is passed in the case of upsert block. For example, when parsing the query block inside -

upsert {
  query {
    me(func: eq(email, "someone@gmail.com"), first: 1) {
      v as uid
    }
  }

  mutation {
    set {
      uid(v) <name> "Some One" .
      uid(v) <email> "someone@gmail.com" .
    }
  }
}

The variable name v needs to be passed through the needVars parameter. Otherwise, an error is reported complaining that the variable v is defined but not used in the query block.

type ShortestPathArgs

type ShortestPathArgs struct {
	// From, To can have a uid or a uid function as the argument.
	// 1. from: 0x01
	// 2. from: uid(0x01)
	// 3. from: uid(p) // a variable
	From *Function
	To   *Function
}

ShortestPathArgs stores the arguments needed to process the shortest path query.

type VarContext

type VarContext struct {
	Name string
	Typ  int //  1 for UID vars, 2 for value vars
}

VarContext stores information about the vars needed to complete a query.

type Vars

type Vars struct {
	Defines []string
	Needs   []string
}

Vars struct contains the list of variables defined and used by a query block.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL