• Stars
    star
    101
  • Rank 326,017 (Top 7 %)
  • Language
    Go
  • License
    Other
  • Created about 9 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Query engine.

Introduction

This README describes the source code and implementation of the N1QL query engine and components.

Goals

The goals of this implementation are:

  • Language completeness

  • GA code base

  • Source code aesthetics

    • Design, object orientation
    • Data structures, algorithms
    • Modularity, readability

Features

This N1QL implementation provides the following features:

  • Read

    • SELECT
    • EXPLAIN
  • DDL

    • CREATE / DROP INDEX
    • CREATE PRIMARY INDEX
  • DML

    • UPDATE
    • DELETE
    • INSERT
    • UPSERT
    • MERGE

    The ACID semantics of the DML statements have not yet been decided or implemented. Nor has the underlying support in Couchbase Server. At this time, only the DML syntax and query engine processing have been provided.

Deployment architecture

The query engine is a multi-threaded server that runs on a single node. When deployed on a cluster, multiple instances are deployed on separate nodes. This is only for load-balancing and availability. In particular, the query engine does not perform distributed query processing, and separate instances do not communicate or interact.

In production, users will have the option of colocating query engines on KV and index nodes, or deploying query engines on dedicated query nodes. Because the query engine is highly data-parallel, we have a goal of achieving good speedup on dedicated query nodes with high numbers of cores.

The remainder of this document refers to a single instance of the query engine. At this time, load balancing, availability, and liveness are external concerns that will be handled later by complementary components.

Processing sequence

  • Parse: Text to algebra. In future, we could also add JSON to algebra (e.g. if we add something like JSONiq or the Mongo query API).

  • Prepare: Algebra to plan. This includes index selection.

  • Execute: Plan to results. When we add prepared statements, this phase can be invoked directly on a prepared statement.

Packages

Value

The value package implements JSON and non-JSON values, including delayed parsing. This implementation has measured a 2.5x speedup over dparval.

Primitive JSON values (boolean, number, string, null) are implemented as golang primitives and incur no memory or garbage-collection overhead.

This package also provides collation, sorting, and sets (de-duplication) over Values.

  • Value: Base interface.

  • AnnotatedValue: Can carry attachments and metadata.

  • CorrelatedValue: Refers and escalates to a parent Value. Used to implement subqueries and name scoping.

  • ParsedValue: Delayed evaluation of parsed values, including non-JSON values.

  • MissingValue: Explicit representation of MISSING values. These are useful for internal processing, and can be skipped during final projection of results.

  • BooleanValue, NumberValue, StringValue, NullValue, ArrayValue, ObjectValue: JSON values.

Errors

The errors package provides a dictionary of error codes and messages. When fully implemented, the error codes will mirror SQL, and the error messages will be localizable.

All user-visible errors and warnings should come from this package.

Expression

The expression package defines the interfaces for all expressions, and provides the implementation of scalar expressions.

This package is usable by both query and indexing (for computed indexes).

Expressions are evaluated within a context; this package provides a default context that can be used by indexing. The context includes a statement-level timestamp.

Expressions also provide support for query planning and processing; this includes equivalence testing, constant folding, etc.

The following types of scalar expressions are included:

  • arithmetic operators
  • CASE
  • Collection expressions (ANY / EVERY / ARRAY / FIRST)
  • Comparison operators (including IS operators)
  • String concat
  • Constants (including literals)
  • Functions
  • Identifiers
  • Navigation (fields, array indexing, array slicing)

Algebra

The algebra package defines the full algebra and AST (abstract syntax tree) for all N1QL statements (using the expression package for scalar expressions).

It includes aggregate functions, subquery expressions, parameter expressions, bucket references, and all the N1QL statements and clauses.

Aggregate functions

  • ARRAY_AGG(expr)

  • ARRAY_AGG(DISTINCT expr)

  • AVG(expr)

  • AVG(DISTINCT expr)

  • COUNT(*)

  • COUNT(expr)

  • COUNT(DISTINCT expr)

  • MAX(expr)

  • MIN(expr)

  • SUM(expr)

  • SUM(DISTINCT expr)

Plan

The plan package implements executable representations of queries. This includes both SELECTs and DML statements.

When we implement prepared statements, they will be represented as plans and stored as JSON documents or in-memory plan objects.

Plans are built from algebras using a visitor pattern. A separate planner / optimizer will be implemented for index selection.

Plans include the following operators:

  • Scans

    • PrimaryScan: Scans a primary index.

    • IndexScan: Scans a secondary index.

    • KeyScan: Does not perform a scan. Directly treats the provided keys as a scan.

    • ValueScan: Used for the VALUES clause of INSERT and UPSERT statements. Treats the provided values as the result of a scan.

    • DummyScan: Used for SELECTs with no FROM clause. Provides a single empty object as the result of a scan.

    • CountScan: Used for SELECT COUNT(*) FROM bucket-name. Treats the bucket size as the result of a scan, without actually performing a full scan of the bucket.

    • IntersectScan: A container that scans its child scanners and intersects the results. Used for scanning multiple secondary indexes concurrently for a single query.

  • Fetch

  • Joins

    • Join

    • Nest

    • Unnest

  • Filter

  • Group: To enable data-parallelism, grouping is divided into three phases. The first two phases can each be executed in a data-parallel fashion, and the final phase merges the results.

    • InitialGroup: Initial phase.

    • IntermediateGroup: Cumulate intermediate results. This phase can be chained.

    • FinalGroup: Compute final aggregate results.

  • Other SELECT operators

    • Project

    • Distinct

    • Order

    • Offset

    • Limit

    • Let

    • UnionAll: Combine the results of two queries. For UNION, we perform UNION ALL followed by DISTINCT.

  • Framework operators

    • Collect: Collect results into an array. Used for subqueries.

    • Discard: Discard results.

    • Stream: Stream results out. Used for returning results.

    • Parallel: A container that executes multiple copies of its child operator in parallel. Used for all data-parallelism.

    • Sequence: A container that chains its children into a sequence. Used for all execution pipelining.

  • DML operators

    • SendDelete

    • SendInsert

    • Set: Used for UPDATE.

    • Unset: Used for UPDATE.

    • Clone: Used for UPDATE. Clones data values so that UPDATEs read original values and mutate a clone.

    • SendUpdate

    • Merge

Execution

The execution package implements query execution. The objects in this package mirror those in the plan package, except that these are the running instances.

Golang channels are used extensively to implement concurrency and signaling.

Subquery execution

The Context object supports subquery execution. It performs planning, execution, and collection of subquery results. It also performs plan and result caching for uncorrelated subqueries.

Datastore

The datastore package defines the interface to the underlying database server.

Some key differences from the previous datastore API (previously catalog API):

  • DML support

  • Use of channels for error handling and stop signaling

  • Generalized index interface that supports any combination of hash and range indexing

Parser

This package will contain the parser and lexer.

Server

This package will contain the main engine executable and listener.

Clustering

This package defines the interface to the underlying cluster management system.

It provides a common abstraction for cluster management, including configuration of and the lifecycle of a cluster.

Accounting

This package will contain the interface to workload tracking and monitoring. Accounting data can cover metrics, statistics, event and potentially log data.

It provides a common abstraction for recording accounting data and services over accounting data.

Shell

This package will contain the client command-line shell.

Sort

This package provides a parallel sort. It was copied from the Golang source and basic parallelism was added, but it has not been fine-tuned.

cbq

This package provides a client library that will be used by the command-line shell to encapsulate cluster-awareness and other connectivity concerns.

The library will implement the standard golang database APIs at database/sql and database/sql/driver.

The library will connect using the Query REST API and the Query Clustering API.

Data parallelism

The query engine is designed to be highly data-parallel. By data-parallel, we mean that individual stages of the execution pipeline are parallelized over their input data. This is in addition to the parallelism achieved by giving each stage its own goroutine.

Below, N1QL statement execution pipelines are listed, along with the data-parallelization and serialization points.

SELECT

  1. Scan
  2. Parallelize
  3. Fetch
  4. Join / Nest / Unnest
  5. Let (Common subexpressions)
  6. Where (Filter)
  7. GroupBy: Initial
  8. GroupBy: Intermediate
  9. Serialize
  10. GroupBy: Final
  11. Parallelize
  12. Letting (common aggregate subexpressions)
  13. Having (aggregate filtering)
  14. Serialize
  15. Order By (Sort)
  16. Parallelize
  17. Select (Projection)
  18. Serialize
  19. Distinct (De-duplication)
  20. Offset (Skipping)
  21. Limit

INSERT

  1. Scan
  2. Parallelize
  3. SendInsert
  4. Returning (Projection)

DELETE

  1. Scan
  2. Parallelize
  3. Fetch
  4. Let (Common subexpressions)
  5. Where (Filter)
  6. Serialize
  7. Limit
  8. Parallelize
  9. SendDelete
  10. Returning (Projection)

UPDATE

  1. Scan
  2. Parallelize
  3. Fetch
  4. Let (Common subexpressions)
  5. Where (Filter)
  6. Serialize
  7. Limit
  8. Parallelize
  9. Clone
  10. Set / Unset
  11. SendUpdate
  12. Returning (Projection)

Build

The N1QL query engine build is part of the couchbase cluster build.

More Repositories

1

couchbase-lite-ios

Lightweight, embedded, syncable NoSQL database engine for iOS and MacOS apps.
Objective-C
1,603
star
2

forestdb

A Fast Key-Value Storage Engine Based on Hierarchical B+-Tree Trie
C++
1,259
star
3

couchbase-lite-android

Lightweight, embedded, syncable NoSQL database engine for Android.
Java
1,173
star
4

moss

moss - a simple, fast, ordered, persistable, key-val storage library for golang
Go
942
star
5

geocouch

GeoCouch, a spatial index for CouchDB
Erlang
513
star
6

vellum

A Go library implementing an FST (finite state transducer)
Go
496
star
7

couchnode

Couchbase Node.js Client Library (Official)
C++
462
star
8

sync_gateway

Manages access and synchronization between Couchbase Lite and Couchbase Server
Go
447
star
9

couchbase-lite-net

A lightweight, document-oriented (NoSQL), syncable database engine for .NET
C#
432
star
10

go-slab

slab allocator in go
Go
364
star
11

gocb

The Couchbase Go SDK
Go
356
star
12

go-couchbase

Couchbase client in Go
Go
318
star
13

fleece

A super-fast, compact, JSON-equivalent binary data format
C++
309
star
14

couchbase-net-client

The official Couchbase SDK for .NET Core and Full Frameworks
C#
271
star
15

couchbase-lite-core

Cross-platform C++ core library for Couchbase Lite
C++
255
star
16

couchbase-java-client

The official Java client for Couchbase Server
Java
252
star
17

couchbase-python-client

Couchbase Python Client Library (Official)
Python
239
star
18

couchbase-elasticsearch-connector

The Official Couchbase Elasticsearch Connector
Java
176
star
19

libcouchbase

The couchbase client for C.
C
173
star
20

docker

Dockerfiles and configuration scripts for the Docker Hub Official Couchbase images
Dockerfile
139
star
21

cbft

Couchbase Full Text server
Go
136
star
22

couchdb

CouchDB
Erlang
129
star
23

kv_engine

Couchbase Key-Value Engine
C++
124
star
24

couchbase-spark-connector

The Official Couchbase Spark Connector
Scala
119
star
25

couchbase-lite-C

C language bindings for the Couchbase Lite embedded NoSQL database engine
C++
110
star
26

couchbase-ruby-client

Couchbase Ruby Client Library (Official)
Ruby
107
star
27

nitro

A high performance in-memory index storage engine
Go
106
star
28

kafka-connect-couchbase

Kafka Connect connector for Couchbase Server
Java
70
star
29

couchbase-kafka-connector

Legacy Couchbase to Kafka connector, superseded by Kafka Connect based.
Java
69
star
30

couchbase-lite-java-core

Couchbase Lite Java core library
Java
68
star
31

CouchbaseMock

A Java mock for Couchbase
Java
62
star
32

couchbase-ruby-model

The Active Model implementation for Couchbase Server built on couchbase-ruby-client
Ruby
61
star
33

memcached

Memcached work planned for contribution back to memcached/memcached
C++
56
star
34

couchstore

couchbase storage file library
C
54
star
35

couchbase-lite-java

Portable java version of Couchbase Lite
Java
52
star
36

java-dcp-client

Couchbase Java DCP Client
Java
47
star
37

ns_server

The Membase Server Superdupervisor.
JavaScript
44
star
38

couchbase-lite-java-ce-root

The root workspace for the Community Editions of the Java language family of products (Java Desktop, Java WebService, and Android)
Shell
44
star
39

couchbase-jvm-clients

The Couchbase Monorepo for JVM Clients: Java, Scala, io-core…
Java
43
star
40

kubernetes

Deprecated. Please use the Couchbase Autonomous Operator
Shell
43
star
41

couchbase-cli

Command Line tools for Administering a Couchbase Cluster
Python
39
star
42

moxi

a memcached proxy with energy and pep
C
39
star
43

indexing

Couchbase Indexes
Go
37
star
44

tlm

top level makefile
CMake
35
star
45

docs-site

The Antora playbook project, contributing documentation, and home page for the new Couchbase Docs site.
JavaScript
34
star
46

couchbase-jvm-core

The JVM core for Couchbase SDKs.
Java
32
star
47

go_n1ql

N1QL Driver for Go lang's database/sql package
Go
32
star
48

docs-cb4

Documentation for Couchbase Server 4.x and 5.x GA releases
HTML
28
star
49

ep-engine

Eventually Persistent Couchbase Data Layer.
C++
28
star
50

couchbase-exporter

Couchbase Prometheus Exporter
Go
27
star
51

eventing

Couchbase Eventing Engine
Go
26
star
52

couchbase-lite-android-liteserv

An HTTP (ReST) interface to the Couchbase-Lite database running on the device/emulator
Java
24
star
53

cbgt

The cbgt project provides a generic golang library that manages partitions or data shards across a cluster of servers.
Go
22
star
54

gocbcore

The IO component of gocb
Go
21
star
55

testrunner

The TestRunner (Extracted from carlin).
Python
21
star
56

perfrunner

Performance TAF for Couchbase Server
Python
20
star
57

couchbase-lite-java-native

This is a shared native SQLite library used for Couchbase Lite Android/Java.
C++
20
star
58

couchbase-examples

Ruby
19
star
59

chronicle

Erlang
19
star
60

service-broker

An Open Service Broker Based Kubernetes Templating Engine
Go
17
star
61

build

jenkins scripts for executing builds, cgi scripts for status and reporting
Shell
17
star
62

docs-server

The Couchbase Server documentation source files (in AsciiDoc) used in the Couchbase Docs site.
HTML
17
star
63

phosphor

High performance event tracing
C++
16
star
64

goxdcr

Go
15
star
65

subjson

High performance JSON manipulation library
C++
13
star
66

gperftools

C++
12
star
67

platform

Small library providing a platform layer
C++
12
star
68

couchbase-lite-java-listener

Embedded web server to expose Couchbase Lite REST API on an http socket
Java
12
star
69

docs-ui-old

Produces the UI bundle used by the Couchbase documentation site.
CSS
12
star
70

couchbase-fluent-bit

Fast and Lightweight Log processor and forwarder. Based on upstream Fluent Bit, this includes some additional Couchbase specific configuration and support - https://github.com/fluent/fluent-bit
Go
12
star
71

cbmonitor

cbmonitor
Python
11
star
72

couchbase-lite-java-javascript

Javascript view engine for Couchbase Lite Android
Java
10
star
73

gometa

Go
10
star
74

query-ui

The Couchbase query workbench UI for SQL++ / N1QL.
JavaScript
10
star
75

sg-bucket

Sync Gateway Bucket interface and common code used by all Sync Gateway bucket implementations.
Go
10
star
76

docs-couchbase-lite

Documentation for Couchbase Lite
Java
9
star
77

couchbase-lite-android-ce

The community edition of couchbase lite for android
9
star
78

clog

Couchbase logging for go.
Go
9
star
79

godbc

Golang database connectivity API. This API is more flexible and extensible than golang's built-in database/sql package, because like JDBC, the API uses interfaces instead of concrete types. This allows it to be extended to handle both SQL and NoSQL / JSON data sources.
Go
9
star
80

cbbootstrap

REST API to help bootstrap Couchbase Server clusters
Go
8
star
81

build-infra

Various programs and scripts used by the Build & Release team not directly related to specific software build processes
Dockerfile
7
star
82

gocbmgr

A library for the making Couchbase REST API calls in golang
Go
7
star
83

sigar

System Information Gatherer And Reporter
C++
7
star
84

Android-EmptyApp

The android empty app.
Java
7
star
85

product-metadata

Various configuration files describing products we build
Jinja
6
star
86

go-blip

Go language implementation of BLIP-over-WebSocket protocol
Go
6
star
87

docs-sdk-go

The Go SDK documentation source files used in the new Couchbase Docs site.
Go
5
star
88

build-manifests

Internal build manifests for all products.
5
star
89

tools-common

Go
5
star
90

docs-sdk-java

The Java SDK documentation source files used in the Couchbase Docs site.
Java
5
star
91

couchbase-lite-java-common

Common code for the Java language family of products (Java Desktop, Java WebService, and Android)
Java
5
star
92

couchbase-hadoop-plugin

A Couchbase to Hadoop (Sqoop) plugin for importing and exporting data
Java
5
star
93

gocb-opentelemetry

Go
4
star
94

rhmap

robinhood hashmap in golang
Go
4
star
95

n1k1

n1k1, pronounced "nicky", is a prototype execution compiler and engine for N1QL query plans
Go
4
star
96

spring

Simple Couchbase CRUD-workload generator based on pylibcouchbase
Python
4
star
97

product-texts

Repository for product-specific documents (e.g. READMEs, license files, etc.)
Rich Text Format
3
star
98

cbauth

Go
3
star
99

docs-sdk-scala

The Scala SDK documentation source files to be used in the new Couchbase Docs site. https://docs.couchbase.com
Scala
3
star
100

java-couchbase-encryption

Crypto extensions for Couchbase Java Client
Java
3
star