Documentation
¶
Overview ¶
Package logchefql provides parsing and SQL generation for the LogchefQL query language. LogchefQL is a simple query language for filtering logs, designed to be user-friendly while translating to efficient ClickHouse SQL queries.
Index ¶
- Constants
- func BuildFullQuery(params QueryBuildParams) (string, error)
- func TranslateToSQLConditions(query string, schema *Schema) string
- type ASTNode
- type BoolOperator
- type ColumnInfo
- type ExpressionNode
- type FilterCondition
- type GroupNode
- type LogicalNode
- type NestedField
- type Operator
- type ParseError
- type ParseResult
- type Parser
- type Position
- type QueryBuildParams
- type QueryNode
- type SQLGenerator
- type Schema
- type SelectField
- type Token
- type TokenType
- type TokenizeResult
- type TranslateResult
- type ValidateResult
Constants ¶
const ( ErrUnterminatedString = "UNTERMINATED_STRING" ErrUnexpectedEnd = "UNEXPECTED_END" ErrUnexpectedToken = "UNEXPECTED_TOKEN" ErrExpectedOperator = "EXPECTED_OPERATOR" ErrExpectedValue = "EXPECTED_VALUE" ErrExpectedClosingParen = "EXPECTED_CLOSING_PAREN" ErrUnknownOperator = "UNKNOWN_OPERATOR" ErrUnknownBooleanOperator = "UNKNOWN_BOOLEAN_OPERATOR" ErrInvalidTokenType = "INVALID_TOKEN_TYPE" ErrMissingBooleanOperator = "MISSING_BOOLEAN_OPERATOR" )
Error codes for parse errors
Variables ¶
This section is empty.
Functions ¶
func BuildFullQuery ¶
func BuildFullQuery(params QueryBuildParams) (string, error)
BuildFullQuery builds a complete SQL query from LogchefQL with time range and other parameters. This is used when executing the query against ClickHouse.
func TranslateToSQLConditions ¶
TranslateToSQLConditions is a convenience function that returns just the SQL string. Returns empty string on error.
Types ¶
type ASTNode ¶
type ASTNode interface {
// contains filtered or unexported methods
}
ASTNode is the interface for all AST node types
type BoolOperator ¶
type BoolOperator string
BoolOperator represents boolean operators for combining conditions
const ( BoolAnd BoolOperator = "AND" BoolOr BoolOperator = "OR" )
func ParseBoolOperator ¶
func ParseBoolOperator(s string) (BoolOperator, bool)
ParseBoolOperator converts a string to a BoolOperator, returning ok=false if invalid
type ColumnInfo ¶
ColumnInfo represents column metadata from the schema
type ExpressionNode ¶
type ExpressionNode struct {
Key interface{} `json:"key"` // string or NestedField
Operator Operator `json:"operator"`
Value interface{} `json:"value"` // string, number, bool, or nil
Quoted bool `json:"quoted,omitempty"`
}
ExpressionNode represents a single comparison expression (e.g., field="value")
type FilterCondition ¶
type FilterCondition struct {
Field string `json:"field"`
Operator string `json:"operator"`
Value string `json:"value"`
IsRegex bool `json:"is_regex"`
}
FilterCondition represents a single filter condition extracted from the query This is used for the field sidebar feature
func GetConditionsFromQuery ¶
func GetConditionsFromQuery(query string) []FilterCondition
GetConditionsFromQuery extracts filter conditions from a LogchefQL query. This is useful for the field sidebar feature.
type GroupNode ¶
type GroupNode struct {
Children []ASTNode `json:"children"`
}
GroupNode represents a parenthesized group of expressions
type LogicalNode ¶
type LogicalNode struct {
Operator BoolOperator `json:"operator"`
Children []ASTNode `json:"children"`
}
LogicalNode represents a logical combination of nodes (AND/OR)
type NestedField ¶
NestedField represents a field with nested path access (e.g., log.level or attributes["key"])
type Operator ¶
type Operator string
Operator represents comparison operators in LogchefQL
func ParseOperator ¶
ParseOperator converts a string to an Operator, returning ok=false if invalid
type ParseError ¶
type ParseError struct {
Code string `json:"code"`
Message string `json:"message"`
Position *Position `json:"position,omitempty"`
}
ParseError represents an error encountered during parsing
func DetectMissingBooleanOperators ¶
func DetectMissingBooleanOperators(tokens []Token) *ParseError
DetectMissingBooleanOperators checks for patterns like key=value key=value without and/or
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type ParseResult ¶
type ParseResult struct {
AST ASTNode
Errors []ParseError
}
ParseResult represents the result of parsing
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser converts tokens into an AST
type QueryBuildParams ¶
type QueryBuildParams struct {
LogchefQL string // The LogchefQL query string
Schema *Schema // Optional schema for type-aware SQL generation
TableName string // Fully qualified table name (database.table)
TimestampField string // Name of the timestamp column
StartTime string // Start time in format "2006-01-02 15:04:05"
EndTime string // End time in format "2006-01-02 15:04:05"
Timezone string // Timezone for time conversion
Limit int // Result limit
SelectFields string // Optional SELECT clause (defaults to *)
}
QueryBuildParams contains parameters for building a full SQL query
type QueryNode ¶
type QueryNode struct {
Where ASTNode `json:"where,omitempty"`
Select []SelectField `json:"select,omitempty"`
}
QueryNode represents the top-level query with optional WHERE and SELECT
type SQLGenerator ¶
type SQLGenerator struct {
// contains filtered or unexported fields
}
SQLGenerator converts an AST into ClickHouse SQL
func NewSQLGenerator ¶
func NewSQLGenerator(schema *Schema) *SQLGenerator
NewSQLGenerator creates a new SQL generator with optional schema
func (*SQLGenerator) Generate ¶
func (g *SQLGenerator) Generate(node ASTNode) string
Generate converts an AST node to SQL WHERE clause conditions
func (*SQLGenerator) GenerateSelectClause ¶
func (g *SQLGenerator) GenerateSelectClause(selectFields []SelectField, defaultTimestampField string) string
GenerateSelectClause generates the SELECT clause from select fields
type Schema ¶
type Schema struct {
Columns []ColumnInfo `json:"columns"`
}
Schema represents table schema information for type-aware SQL generation
type SelectField ¶
type SelectField struct {
Field interface{} `json:"field"` // string or NestedField
Alias string `json:"alias,omitempty"`
}
SelectField represents a field selection with optional alias
type Token ¶
type Token struct {
Type TokenType `json:"type"`
Value string `json:"value"`
Position Position `json:"position"`
Quoted bool `json:"quoted,omitempty"`
Incomplete bool `json:"incomplete,omitempty"`
}
Token represents a lexical token from the tokenizer
type TokenizeResult ¶
type TokenizeResult struct {
Tokens []Token
Errors []ParseError
}
TokenizeResult represents the result of tokenization
func Tokenize ¶
func Tokenize(input string) TokenizeResult
Tokenize converts a LogchefQL query string into a sequence of tokens
type TranslateResult ¶
type TranslateResult struct {
SQL string `json:"sql"` // WHERE clause conditions only
SelectClause string `json:"select_clause,omitempty"` // Custom SELECT clause if pipe operator used
Valid bool `json:"valid"`
Error *ParseError `json:"error,omitempty"`
Conditions []FilterCondition `json:"conditions"`
FieldsUsed []string `json:"fields_used"`
}
TranslateResult represents the result of translating a LogchefQL query
func Translate ¶
func Translate(query string, schema *Schema) *TranslateResult
Translate parses a LogchefQL query and returns the SQL translation with metadata. This is the main entry point for query translation.
type ValidateResult ¶
type ValidateResult struct {
Valid bool `json:"valid"`
Error *ParseError `json:"error,omitempty"`
}
ValidateResult represents the result of validating a LogchefQL query
func Validate ¶
func Validate(query string) *ValidateResult
Validate checks if a LogchefQL query is syntactically valid.
func ValidateWithDetails ¶
func ValidateWithDetails(query string) *ValidateResult
ValidateWithDetails is an alias for Validate that returns detailed error information.