parser

package
v0.0.0-...-6719cd2 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2019 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package parser implements a parser combinator for the akutan query language. After parsing a query string into a query object, it is passed to the planner for a cost based evaluation of possible execution paths and finally the executor where the retrieval and join operations on the best plan are performed.

For documentation on the query language specifics; see the root README.md.

https://en.wikipedia.org/wiki/Parser_combinator

Index

Constants

View Source
const (
	// QuerySparql indicates that the query is in the sparql(ish) subset format.
	QuerySparql = "sparql"
	// QueryFactPattern indicates that the query is a bare set of fact patterns to match.
	QueryFactPattern = "facts"
)
View Source
const (
	// InsertFormatTSV takes one fact per line, consisting of three or four
	// whitespace-separated columns. It's far more forgiving about whitespace
	// than normal tab-separated values files. Blank lines and lines consisting
	// of # comments are allowed.
	//
	// <dont_care> may be used as a dummy fact ID.
	//
	// Variables like ?var capture a fact ID and must be used as subject or
	// object in a subsequent fact.
	InsertFormatTSV = "tsv"
)

These are possible values for ParseInsert formats.

Variables

View Source
var (
	// NoUnit indicates no units were specified for the literal.
	NoUnit = Unit{}

	// NoLanguage indicates no language was specified for the string.
	NoLanguage = Language{}
)

Functions

func MustRewrite

func MustRewrite(ctx context.Context, lookup lookups.PO, idx uint64, in *Query)

MustRewrite uses a set of rewrite rules in order to rewrite the query definition and panics if an error occurs. It simplifies variable initialization.

func Rewrite

func Rewrite(ctx context.Context, lookup lookups.PO, idx uint64, in *Query) error

Rewrite uses a set of rewrite rules in order to rewrite the query definition. Currently we replace external ids with object ids with a lookup po request.

Types

type AggregateExpr

type AggregateExpr struct {
	Function AggregateFunction

	// Of is the target to be aggregated.
	Of aggregateTarget // *Variable, Wildcard [Later Expression]
}

AggregateExpr describes a single aggregate expression.

func (*AggregateExpr) String

func (a *AggregateExpr) String() string

type AggregateFunction

type AggregateFunction int

AggregateFunction describes the different types of aggregate functions. Only COUNT is currently supported.

const (
	// AggCount counts rows (or solutions in SPARQL terms)
	AggCount AggregateFunction = iota + 1
)

func (AggregateFunction) String

func (f AggregateFunction) String() string

type BoundExpression

type BoundExpression struct {
	Expr Expression
	As   *Variable
}

BoundExpression represents an expression whose result is bound to a variable. e.g (COUNT(?v) AS ?c) is a BoundExpression, COUNT(?v) is the expression and its bound to the ?c Variable.

func (*BoundExpression) String

func (b *BoundExpression) String() string

type Distinct

type Distinct struct {
}

Distinct defines select clause DISTINCT keyword.

func (Distinct) String

func (d Distinct) String() string

type Entity

type Entity struct {
	ID    uint64
	Value string
}

Entity type.

func (*Entity) String

func (e *Entity) String() string

type Expression

type Expression interface {
	String() string
	// contains filtered or unexported methods
}

Expression is a marker interface for types that are expressions. For example COUNT(?v) is an expression. (AggregateExpr is our only Expression type)

type Insert

type Insert struct {
	// The possible types for each Quad are restricted significantly.
	//
	// Each ID is either Nil or Variable.
	//
	// Each Subject is either LiteralID, QName, Entity, or Variable.
	//
	// Each Predicate is either LiteralID, QName, or Entity.
	//
	// Each Object is either LiteralID, QName, Entity, Variable, or a primitive
	// literal type (LiteralBool, LiteralInt, LiteralFloat, LiteralString,
	// LiteralTime).
	//
	// For QName, Entity, unit, or language values, only the string form is
	// given.
	//
	// All returned Quads will have a MatchSpecificity of MatchRequired.
	Facts []*Quad
}

Insert is the result of ParseInsert.

func ParseInsert

func ParseInsert(format string, input string) (*Insert, error)

ParseInsert parses 'input' for inserting facts into the graph. The given format identifies the syntax of the input; the possible values are defined as InsertFormat* constants.

func (*Insert) String

func (insert *Insert) String() string

type Language

type Language struct {
	ID    uint64
	Value string
}

Language type.

func (*Language) String

func (l *Language) String() string

type LimitOffset

type LimitOffset struct {
	// Limit and Offset are nil if not explicitly specified in the query.
	Limit  *uint64
	Offset *uint64
}

LimitOffset contains paging related values

func (*LimitOffset) String

func (o *LimitOffset) String() string

type Literal

type Literal interface {
	Term
	// Literal returns the api.KGObject representation of a parsed literal value.
	Literal() *api.KGObject
}

Literal represents comparable object values.

func ParseLiteral

func ParseLiteral(in string) (Literal, error)

ParseLiteral parses an object literal value.

type LiteralBool

type LiteralBool struct {
	Unit  Unit
	Value bool
}

LiteralBool type.

func (*LiteralBool) Literal

func (l *LiteralBool) Literal() *api.KGObject

Literal returns a boolean api object.

func (*LiteralBool) String

func (l *LiteralBool) String() string

type LiteralFloat

type LiteralFloat struct {
	Unit  Unit
	Value float64
}

LiteralFloat type.

func (*LiteralFloat) Literal

func (l *LiteralFloat) Literal() *api.KGObject

Literal returns a floating point api object.

func (*LiteralFloat) String

func (l *LiteralFloat) String() string

type LiteralID

type LiteralID struct {
	Value uint64
	// A human-readable string that might be more evocative than the opaque
	// Value. This string includes angle brackets (like "<foo>") when rewritten
	// from an Entity.
	Hint string
}

LiteralID type.

func (*LiteralID) Literal

func (l *LiteralID) Literal() *api.KGObject

Literal returns a kid api object.

func (*LiteralID) String

func (l *LiteralID) String() string

type LiteralInt

type LiteralInt struct {
	Unit  Unit
	Value int64
}

LiteralInt type.

func (*LiteralInt) Literal

func (l *LiteralInt) Literal() *api.KGObject

Literal returns an integer api object.

func (*LiteralInt) String

func (l *LiteralInt) String() string

type LiteralSet

type LiteralSet struct {
	Values []Term
}

LiteralSet represents a set of literal values.

func (*LiteralSet) String

func (l *LiteralSet) String() string

type LiteralString

type LiteralString struct {
	Language Language
	Value    string
}

LiteralString object value type.

func (*LiteralString) Literal

func (l *LiteralString) Literal() *api.KGObject

Literal returns a string api object.

func (*LiteralString) String

func (l *LiteralString) String() string

type LiteralTime

type LiteralTime struct {
	Unit      Unit
	Value     time.Time
	Precision api.Precision
}

LiteralTime type.

func (*LiteralTime) Literal

func (l *LiteralTime) Literal() *api.KGObject

Literal returns a time api object.

func (*LiteralTime) String

func (l *LiteralTime) String() string

type MatchSpecificity

type MatchSpecificity int

MatchSpecificity describes requirements for how a query line fits with the overall query

const (
	// MatchRequired indicates that the queryline is required to have a matching
	// fact for there to be a result
	MatchRequired MatchSpecificity = 0
	// MatchOptional indicates that the queryline is optional, if it exists it's
	// returned, if it doesn't it does not prevent an otherwise complete result
	// being generated.
	MatchOptional MatchSpecificity = 1
)

func (MatchSpecificity) String

func (m MatchSpecificity) String() string

type Nil

type Nil struct{}

Nil type.

func (*Nil) String

func (n *Nil) String() string

type Operator

type Operator struct {
	Value rpc.Operator
}

Operator type.

func (*Operator) String

func (o *Operator) String() string

type OrderCondition

type OrderCondition struct {
	Direction SortDirection
	On        *Variable
}

OrderCondition describes a single expression in the order by clause. SPARQL supports ordering by expressions, however we only support ordering by variables.

func (*OrderCondition) String

func (o *OrderCondition) String() string

type ParseError

type ParseError struct {
	// query, literal, etc.
	ParseType string
	// The input string to the parser which resulted in this error.
	Input string
	// Offset is the byte offset into 'Input' at which the error ocurred.
	Offset int
	// Line is the line number in 'Input' at which the error ocurred.
	Line int
	// Column is the column (in runes) into the indicated Line that the error
	// ocurred. Line & Column represent the same point in 'Input' as 'Offset'.
	Column int
	// The specific parser error that ocurred.
	Details string
}

ParseError captures more detailed information about a parsing error, and where it occurred.

func (*ParseError) Error

func (p *ParseError) Error() string

type QName

type QName struct {
	ID    uint64
	Value string
}

QName type. eg. rdf:type is a QName (qualified name)

func (*QName) String

func (q *QName) String() string

type Quad

type Quad struct {
	// ID is set to &Nil{} when not present.
	// TODO: should it be nil instead?
	ID          Term
	Subject     Term
	Predicate   Term
	Object      Term
	Specificity MatchSpecificity
}

Quad query statement.

func (*Quad) String

func (q *Quad) String() string

type Query

type Query struct {
	Type   QueryType
	Select SelectClause
	// DatasetClause not supported
	Where     WhereClause
	Modifiers SolutionModifier
}

Query contains the definition of a single query.

func MustParse

func MustParse(format, in string) *Query

MustParse parses the akutan query language and panics if an error occurs. It simplifies variable initialization. This is primarily meant for writing unit tests.

func Parse

func Parse(format, in string) (*Query, error)

Parse parses a query of the specified format and builds a query definition. format can be either QuerySparl or QueryFactPattern

func (*Query) String

func (q *Query) String() string

type QueryType

type QueryType int

QueryType defines the different type of queries. See https://www.w3.org/TR/sparql11-query/#QueryForms

const (
	// LegacyPatternQuery indicates the query should use the legacy akutan query
	// where a fact pattern is specified, and sets of facts matching the pattern
	// are returned. This is what is now in the Where clause of a Select query.
	LegacyPatternQuery QueryType = iota + 1
	// SelectQuery indicates the query should match patterns from the where
	// clause and return results as a tabular set of values. The result values
	// are either based on variables from the where clause, or on an aggregate
	// function.
	SelectQuery
	// AskQuery indicates the query should check if there exists at least one
	// set of facts that match the WHERE clause. If so it returns true, false
	// otherwise
	AskQuery
)

func (QueryType) String

func (q QueryType) String() string

type Reduced

type Reduced struct {
}

Reduced defines select clause REDUCED keyword.

func (Reduced) String

func (d Reduced) String() string

type SelectClause

type SelectClause struct {
	Keyword selectClauseKeyword // Distinct / Reduced
	Items   []selectClauseItem  // *Variable, *BoundExpression, Wildcard
}

SelectClause details the requested results projection for the query. A Select * query would result in a single Wildcard instance in the Items slice.

func (SelectClause) String

func (s SelectClause) String() string

type SolutionModifier

type SolutionModifier struct {
	// GroupClause not supported
	// HavingClause not supported
	OrderBy []OrderCondition
	Paging  LimitOffset
}

SolutionModifier describes the different ways in which the solutions (aka rows) produced by the where clause can be manipulated, such as sorting.

func (*SolutionModifier) String

func (s *SolutionModifier) String() string

type SortDirection

type SortDirection int

SortDirection is the direction that a sort should be in.

const (
	// SortAsc indicate an ascending sort, i.e. smaller values appear before
	// larger values.
	SortAsc SortDirection = 1
	// SortDesc indicate a descending sort, i.e. larger values appear
	// before smaller values.
	SortDesc SortDirection = 2
)

func (SortDirection) String

func (d SortDirection) String() string

type Term

type Term interface {

	// Return a parseable string representation of the Term. It is used for
	// debugging to build a parseable string of the query. It is also used to
	// distinguish Terms when building the literal set.
	String() string
	// contains filtered or unexported methods
}

A Term is a reference to an entity, an operator, a variable, Nil, a primitive literal, or a set of these. Sets of sets are not allowed. Each field in a Quad is a Term.

func ParseTerm

func ParseTerm(in string) (Term, error)

ParseTerm parses a single term.

type Unit

type Unit struct {
	ID    uint64
	Value string
}

Unit type.

func (*Unit) String

func (u *Unit) String() string

type Variable

type Variable struct {
	Name string
}

Variable name type.

func (*Variable) String

func (v *Variable) String() string

type WhereClause

type WhereClause []*Quad

WhereClause type.

func (WhereClause) String

func (q WhereClause) String() string

type Wildcard

type Wildcard struct {
}

Wildcard represents the * identifier used in the SELECT *... and SELECT (COUNT(*)...) expressions.

func (Wildcard) String

func (w Wildcard) String() string

Jump to

Keyboard shortcuts

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