infi.clickhouse_orm/docs/class_reference.md

1078 lines
25 KiB
Markdown
Raw Normal View History

2017-05-05 15:31:08 +03:00
Class Reference
===============
infi.clickhouse_orm.database
----------------------------
### Database
2017-09-10 17:17:04 +03:00
Database instances connect to a specific ClickHouse database for running queries,
2017-05-05 15:31:08 +03:00
inserting data and other operations.
2019-06-25 07:46:37 +03:00
#### Database(db_name, db_url="http://localhost:8123/", username=None, password=None, readonly=False, autocreate=True, timeout=60, verify_ssl_cert=True, log_statements=False)
2017-05-05 15:31:08 +03:00
Initializes a database instance. Unless it's readonly, the database will be
created on the ClickHouse server if it does not already exist.
- `db_name`: name of the database to connect to.
- `db_url`: URL of the ClickHouse server.
- `username`: optional connection credentials.
- `password`: optional connection credentials.
- `readonly`: use a read-only connection.
2018-12-14 09:34:40 +03:00
- `autocreate`: automatically create the database if it does not exist (unless in readonly mode).
2018-12-14 09:20:43 +03:00
- `timeout`: the connection timeout in seconds.
- `verify_ssl_cert`: whether to verify the server's certificate when connecting via HTTPS.
2019-06-25 07:46:37 +03:00
- `log_statements`: when True, all database statements are logged.
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
#### add_setting(name, value)
2018-10-14 09:51:04 +03:00
Adds a database setting that will be sent with every request.
2018-10-13 23:19:43 +03:00
For example, `db.add_setting("max_execution_time", 10)` will
limit query execution time to 10 seconds.
The name must be string, and the value is converted to string in case
it isn't. To remove a setting, pass `None` as the value.
2017-05-05 15:31:08 +03:00
#### count(model_class, conditions=None)
Counts the number of records in the model's table.
- `model_class`: the model to count.
- `conditions`: optional SQL conditions (contents of the WHERE clause).
#### create_database()
Creates the database on the ClickHouse server if it does not already exist.
#### create_table(model_class)
Creates a table for the given model class, if it does not exist already.
2018-08-21 16:01:10 +03:00
#### does_table_exist(model_class)
Checks whether a table for the given model class already exists.
Note that this only checks for existence of a table with the expected name.
2017-05-05 15:31:08 +03:00
#### drop_database()
Deletes the database on the ClickHouse server.
#### drop_table(model_class)
Drops the database table of the given model class, if it exists.
#### insert(model_instances, batch_size=1000)
Insert records into the database.
- `model_instances`: any iterable containing instances of a single model class.
- `batch_size`: number of records to send per chunk (use a lower number if your records are very large).
#### migrate(migrations_package_name, up_to=9999)
Executes schema migrations.
2017-09-10 17:17:04 +03:00
- `migrations_package_name` - fully qualified name of the Python package
2017-05-05 15:31:08 +03:00
containing the migrations.
- `up_to` - number of the last migration to apply.
#### paginate(model_class, order_by, page_num=1, page_size=100, conditions=None, settings=None)
Selects records and returns a single page of model instances.
- `model_class`: the model class matching the query's table,
or `None` for getting back instances of an ad-hoc model.
- `order_by`: columns to use for sorting the query (contents of the ORDER BY clause).
- `page_num`: the page number (1-based), or -1 to get the last page.
- `page_size`: number of records to return per page.
- `conditions`: optional SQL conditions (contents of the WHERE clause).
- `settings`: query settings to send as HTTP GET parameters
2017-09-10 17:17:04 +03:00
The result is a namedtuple containing `objects` (list), `number_of_objects`,
2017-05-05 15:31:08 +03:00
`pages_total`, `number` (of the current page), and `page_size`.
#### raw(query, settings=None, stream=False)
Performs a query and returns its output as text.
- `query`: the SQL query to execute.
- `settings`: query settings to send as HTTP GET parameters
- `stream`: if true, the HTTP response from ClickHouse will be streamed.
#### select(query, model_class=None, settings=None)
Performs a query and returns a generator of model instances.
- `query`: the SQL query to execute.
- `model_class`: the model class matching the query's table,
or `None` for getting back instances of an ad-hoc model.
- `settings`: query settings to send as HTTP GET parameters
### DatabaseException
Extends Exception
Raised when a database operation fails.
infi.clickhouse_orm.models
--------------------------
### Model
A base class for ORM models. Each model class represent a ClickHouse table. For example:
2017-09-10 17:17:04 +03:00
2017-05-05 15:31:08 +03:00
class CPUStats(Model):
timestamp = DateTimeField()
cpu_id = UInt16Field()
cpu_percent = Float32Field()
engine = Memory()
#### Model(**kwargs)
Creates a model instance, using keyword arguments as field values.
Since values are immediately converted to their Pythonic type,
invalid values will cause a `ValueError` to be raised.
Unrecognized field names will cause an `AttributeError`.
#### Model.create_table_sql(db)
2017-05-05 15:31:08 +03:00
Returns the SQL command for creating a table for this model.
#### Model.drop_table_sql(db)
2017-05-05 15:31:08 +03:00
Returns the SQL command for deleting this model's table.
2018-04-21 12:10:30 +03:00
#### Model.fields(writable=False)
Returns an `OrderedDict` of the model's fields (from name to `Field` instance).
If `writable` is true, only writable fields are included.
Callers should not modify the dictionary.
2018-06-10 14:30:40 +03:00
#### Model.from_tsv(line, field_names, timezone_in_use=UTC, database=None)
2017-05-05 15:31:08 +03:00
Create a model instance from a tab-separated line. The line may or may not include a newline.
The `field_names` list must match the fields defined in the model, but does not have to include all of them.
- `line`: the TSV-formatted data.
- `field_names`: names of the model fields in the data.
- `timezone_in_use`: the timezone to use when parsing dates and datetimes.
- `database`: if given, sets the database that this instance belongs to.
#### get_database()
2017-09-10 17:17:04 +03:00
Gets the `Database` that this model instance belongs to.
2017-05-05 15:31:08 +03:00
Returns `None` unless the instance was read from the database or written to it.
#### get_field(name)
Gets a `Field` instance given its name, or `None` if not found.
2018-06-10 14:30:40 +03:00
#### Model.is_read_only()
Returns true if the model is marked as read only.
#### Model.is_system_model()
Returns true if the model represents a system table.
2017-05-05 15:31:08 +03:00
#### Model.objects_in(database)
Returns a `QuerySet` for selecting instances of this model class.
#### set_database(db)
2017-09-10 17:17:04 +03:00
Sets the `Database` that this model instance belongs to.
2017-05-05 15:31:08 +03:00
This is done automatically when the instance is read from the database or written to it.
#### Model.table_name()
Returns the model's database table name. By default this is the
class name converted to lowercase. Override this if you want to use
a different table name.
#### to_dict(include_readonly=True, field_names=None)
Returns the instance's column values as a dict.
- `include_readonly`: if false, returns only fields that can be inserted into database.
- `field_names`: an iterable of field names to return (optional)
#### to_tsv(include_readonly=True)
Returns the instance's column values as a tab-separated line. A newline is not included.
- `include_readonly`: if false, returns only fields that can be inserted into database.
### BufferModel
Extends Model
#### BufferModel(**kwargs)
Creates a model instance, using keyword arguments as field values.
Since values are immediately converted to their Pythonic type,
invalid values will cause a `ValueError` to be raised.
Unrecognized field names will cause an `AttributeError`.
#### BufferModel.create_table_sql(db)
2017-05-05 15:31:08 +03:00
Returns the SQL command for creating a table for this model.
#### BufferModel.drop_table_sql(db)
2017-05-05 15:31:08 +03:00
Returns the SQL command for deleting this model's table.
2018-04-21 12:10:30 +03:00
#### BufferModel.fields(writable=False)
Returns an `OrderedDict` of the model's fields (from name to `Field` instance).
If `writable` is true, only writable fields are included.
Callers should not modify the dictionary.
2018-06-10 14:30:40 +03:00
#### BufferModel.from_tsv(line, field_names, timezone_in_use=UTC, database=None)
2017-05-05 15:31:08 +03:00
Create a model instance from a tab-separated line. The line may or may not include a newline.
The `field_names` list must match the fields defined in the model, but does not have to include all of them.
- `line`: the TSV-formatted data.
- `field_names`: names of the model fields in the data.
- `timezone_in_use`: the timezone to use when parsing dates and datetimes.
- `database`: if given, sets the database that this instance belongs to.
#### get_database()
2017-09-10 17:17:04 +03:00
Gets the `Database` that this model instance belongs to.
2017-05-05 15:31:08 +03:00
Returns `None` unless the instance was read from the database or written to it.
#### get_field(name)
Gets a `Field` instance given its name, or `None` if not found.
2018-06-10 14:30:40 +03:00
#### BufferModel.is_read_only()
Returns true if the model is marked as read only.
#### BufferModel.is_system_model()
Returns true if the model represents a system table.
2017-05-05 15:31:08 +03:00
#### BufferModel.objects_in(database)
Returns a `QuerySet` for selecting instances of this model class.
#### set_database(db)
2017-09-10 17:17:04 +03:00
Sets the `Database` that this model instance belongs to.
2017-05-05 15:31:08 +03:00
This is done automatically when the instance is read from the database or written to it.
#### BufferModel.table_name()
Returns the model's database table name. By default this is the
class name converted to lowercase. Override this if you want to use
a different table name.
#### to_dict(include_readonly=True, field_names=None)
Returns the instance's column values as a dict.
- `include_readonly`: if false, returns only fields that can be inserted into database.
- `field_names`: an iterable of field names to return (optional)
#### to_tsv(include_readonly=True)
Returns the instance's column values as a tab-separated line. A newline is not included.
- `include_readonly`: if false, returns only fields that can be inserted into database.
2018-04-21 12:10:30 +03:00
### DistributedModel
Extends Model
Model for Distributed engine
#### DistributedModel(**kwargs)
Creates a model instance, using keyword arguments as field values.
Since values are immediately converted to their Pythonic type,
invalid values will cause a `ValueError` to be raised.
Unrecognized field names will cause an `AttributeError`.
2018-04-21 13:48:00 +03:00
#### DistributedModel.create_table_sql(db)
2018-04-21 12:10:30 +03:00
2018-04-21 13:48:00 +03:00
#### DistributedModel.drop_table_sql(db)
2018-04-21 12:10:30 +03:00
Returns the SQL command for deleting this model's table.
#### DistributedModel.fields(writable=False)
Returns an `OrderedDict` of the model's fields (from name to `Field` instance).
If `writable` is true, only writable fields are included.
Callers should not modify the dictionary.
#### DistributedModel.fix_engine_table()
Remember: Distributed table does not store any data, just provides distributed access to it.
So if we define a model with engine that has no defined table for data storage
(see FooDistributed below), that table cannot be successfully created.
This routine can automatically fix engine's storage table by finding the first
non-distributed model among your model's superclasses.
>>> class Foo(Model):
... id = UInt8Field(1)
...
>>> class FooDistributed(Foo, DistributedModel):
... engine = Distributed('my_cluster')
...
>>> FooDistributed.engine.table
None
>>> FooDistributed.fix_engine()
>>> FooDistributed.engine.table
<class '__main__.Foo'>
However if you prefer more explicit way of doing things,
you can always mention the Foo model twice without bothering with any fixes:
>>> class FooDistributedVerbose(Foo, DistributedModel):
... engine = Distributed('my_cluster', Foo)
>>> FooDistributedVerbose.engine.table
<class '__main__.Foo'>
See tests.test_engines:DistributedTestCase for more examples
2018-06-10 14:30:40 +03:00
#### DistributedModel.from_tsv(line, field_names, timezone_in_use=UTC, database=None)
2018-04-21 12:10:30 +03:00
Create a model instance from a tab-separated line. The line may or may not include a newline.
The `field_names` list must match the fields defined in the model, but does not have to include all of them.
- `line`: the TSV-formatted data.
- `field_names`: names of the model fields in the data.
- `timezone_in_use`: the timezone to use when parsing dates and datetimes.
- `database`: if given, sets the database that this instance belongs to.
#### get_database()
Gets the `Database` that this model instance belongs to.
Returns `None` unless the instance was read from the database or written to it.
#### get_field(name)
Gets a `Field` instance given its name, or `None` if not found.
2018-06-10 14:30:40 +03:00
#### DistributedModel.is_read_only()
Returns true if the model is marked as read only.
#### DistributedModel.is_system_model()
Returns true if the model represents a system table.
2018-04-21 12:10:30 +03:00
#### DistributedModel.objects_in(database)
Returns a `QuerySet` for selecting instances of this model class.
#### set_database(db)
#### DistributedModel.table_name()
Returns the model's database table name. By default this is the
class name converted to lowercase. Override this if you want to use
a different table name.
#### to_dict(include_readonly=True, field_names=None)
Returns the instance's column values as a dict.
- `include_readonly`: if false, returns only fields that can be inserted into database.
- `field_names`: an iterable of field names to return (optional)
#### to_tsv(include_readonly=True)
Returns the instance's column values as a tab-separated line. A newline is not included.
- `include_readonly`: if false, returns only fields that can be inserted into database.
2017-05-05 15:31:08 +03:00
infi.clickhouse_orm.fields
--------------------------
2018-10-13 23:19:43 +03:00
### ArrayField
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends Field
2017-05-05 15:31:08 +03:00
2019-06-25 07:46:37 +03:00
#### ArrayField(inner_field, default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### BaseEnumField
2017-05-05 15:31:08 +03:00
Extends Field
2018-10-13 23:19:43 +03:00
Abstract base class for all enum-type fields.
2017-05-05 15:31:08 +03:00
2019-06-25 07:46:37 +03:00
#### BaseEnumField(enum_cls, default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### BaseFloatField
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends Field
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Abstract base class for all float-type fields.
2017-05-05 15:31:08 +03:00
2019-06-25 07:46:37 +03:00
#### BaseFloatField(default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
### BaseIntField
Extends Field
Abstract base class for all integer-type fields.
2019-06-25 07:46:37 +03:00
#### BaseIntField(default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### DateField
2017-05-05 15:31:08 +03:00
Extends Field
2019-06-25 07:46:37 +03:00
#### DateField(default=None, alias=None, materialized=None, readonly=None, codec=None)
2018-10-13 23:19:43 +03:00
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### DateTimeField
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends Field
2017-05-05 15:31:08 +03:00
2019-06-25 07:46:37 +03:00
#### DateTimeField(default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### Decimal128Field
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends DecimalField
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
#### Decimal128Field(scale, default=None, alias=None, materialized=None, readonly=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### Decimal32Field
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends DecimalField
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
#### Decimal32Field(scale, default=None, alias=None, materialized=None, readonly=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### Decimal64Field
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends DecimalField
#### Decimal64Field(scale, default=None, alias=None, materialized=None, readonly=None)
### DecimalField
2017-06-23 11:56:05 +03:00
Extends Field
2018-10-13 23:19:43 +03:00
Base class for all decimal fields. Can also be used directly.
2017-06-23 11:56:05 +03:00
2018-10-13 23:19:43 +03:00
#### DecimalField(precision, scale, default=None, alias=None, materialized=None, readonly=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### Enum16Field
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends BaseEnumField
2017-05-05 15:31:08 +03:00
2019-06-25 07:46:37 +03:00
#### Enum16Field(enum_cls, default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### Enum8Field
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends BaseEnumField
2017-05-05 15:31:08 +03:00
2019-06-25 07:46:37 +03:00
#### Enum8Field(enum_cls, default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### Field
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Abstract base class for all field types.
2017-05-05 15:31:08 +03:00
2019-06-25 07:46:37 +03:00
#### Field(default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### FixedStringField
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends StringField
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
#### FixedStringField(length, default=None, alias=None, materialized=None, readonly=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### Float32Field
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends BaseFloatField
2017-05-05 15:31:08 +03:00
2019-06-25 07:46:37 +03:00
#### Float32Field(default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### Float64Field
Extends BaseFloatField
2019-06-25 07:46:37 +03:00
#### Float64Field(default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
### Int16Field
Extends BaseIntField
2019-06-25 07:46:37 +03:00
#### Int16Field(default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
### Int32Field
Extends BaseIntField
2019-06-25 07:46:37 +03:00
#### Int32Field(default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
### Int64Field
Extends BaseIntField
2019-06-25 07:46:37 +03:00
#### Int64Field(default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### Int8Field
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends BaseIntField
2017-05-05 15:31:08 +03:00
2019-06-25 07:46:37 +03:00
#### Int8Field(default=None, alias=None, materialized=None, readonly=None, codec=None)
### LowCardinalityField
Extends Field
#### LowCardinalityField(inner_field, default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### NullableField
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends Field
2017-05-05 15:31:08 +03:00
2019-06-25 07:46:37 +03:00
#### NullableField(inner_field, default=None, alias=None, materialized=None, extra_null_values=None, codec=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### StringField
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends Field
2017-05-05 15:31:08 +03:00
2019-06-25 07:46:37 +03:00
#### StringField(default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
### UInt16Field
2017-05-05 15:31:08 +03:00
2018-10-13 23:19:43 +03:00
Extends BaseIntField
2017-05-05 15:31:08 +03:00
2019-06-25 07:46:37 +03:00
#### UInt16Field(default=None, alias=None, materialized=None, readonly=None, codec=None)
2018-10-13 23:19:43 +03:00
### UInt32Field
Extends BaseIntField
2019-06-25 07:46:37 +03:00
#### UInt32Field(default=None, alias=None, materialized=None, readonly=None, codec=None)
2018-10-13 23:19:43 +03:00
### UInt64Field
Extends BaseIntField
2019-06-25 07:46:37 +03:00
#### UInt64Field(default=None, alias=None, materialized=None, readonly=None, codec=None)
2018-10-13 23:19:43 +03:00
### UInt8Field
Extends BaseIntField
2019-06-25 07:46:37 +03:00
#### UInt8Field(default=None, alias=None, materialized=None, readonly=None, codec=None)
2017-05-05 15:31:08 +03:00
2019-06-13 05:19:16 +03:00
### UUIDField
Extends Field
2019-06-25 07:46:37 +03:00
#### UUIDField(default=None, alias=None, materialized=None, readonly=None, codec=None)
2019-06-13 05:19:16 +03:00
2017-05-05 15:31:08 +03:00
infi.clickhouse_orm.engines
---------------------------
### Engine
### TinyLog
Extends Engine
### Log
Extends Engine
### Memory
Extends Engine
### MergeTree
Extends Engine
2019-07-15 11:01:45 +03:00
#### MergeTree(date_col=None, order_by=(), sampling_expr=None, index_granularity=8192, replica_table_path=None, replica_name=None, partition_key=None, primary_key=None)
2017-05-05 15:31:08 +03:00
### Buffer
Extends Engine
Buffers the data to write in RAM, periodically flushing it to another table.
Must be used in conjuction with a `BufferModel`.
2018-04-21 15:23:00 +03:00
Read more [here](https://clickhouse.yandex/docs/en/table_engines/buffer/).
2017-05-05 15:31:08 +03:00
#### Buffer(main_model, num_layers=16, min_time=10, max_time=100, min_rows=10000, max_rows=1000000, min_bytes=10000000, max_bytes=100000000)
2017-09-13 12:15:44 +03:00
### Merge
Extends Engine
The Merge engine (not to be confused with MergeTree) does not store data itself,
but allows reading from any number of other tables simultaneously.
Writing to a table is not supported
https://clickhouse.yandex/docs/en/single/index.html#document-table_engines/merge
#### Merge(table_regex)
2018-04-21 12:10:30 +03:00
### Distributed
Extends Engine
The Distributed engine by itself does not store data,
but allows distributed query processing on multiple servers.
Reading is automatically parallelized.
During a read, the table indexes on remote servers are used, if there are any.
See full documentation here
https://clickhouse.yandex/docs/en/table_engines/distributed.html
2018-04-21 13:48:00 +03:00
#### Distributed(cluster, table=None, sharding_key=None)
2018-04-21 12:10:30 +03:00
:param cluster: what cluster to access data from
:param table: underlying table that actually stores data.
If you are not specifying any table here, ensure that it can be inferred
from your model's superclass (see models.DistributedModel.fix_engine_table)
:param sharding_key: how to distribute data among shards when inserting
straightly into Distributed table, optional
2017-05-05 15:31:08 +03:00
### CollapsingMergeTree
Extends MergeTree
2019-07-15 11:01:45 +03:00
#### CollapsingMergeTree(date_col=None, order_by=(), sign_col="sign", sampling_expr=None, index_granularity=8192, replica_table_path=None, replica_name=None, partition_key=None, primary_key=None)
2017-05-05 15:31:08 +03:00
### SummingMergeTree
Extends MergeTree
2019-07-15 11:01:45 +03:00
#### SummingMergeTree(date_col=None, order_by=(), summing_cols=None, sampling_expr=None, index_granularity=8192, replica_table_path=None, replica_name=None, partition_key=None, primary_key=None)
2017-05-05 15:31:08 +03:00
### ReplacingMergeTree
Extends MergeTree
2019-07-15 11:01:45 +03:00
#### ReplacingMergeTree(date_col=None, order_by=(), ver_col=None, sampling_expr=None, index_granularity=8192, replica_table_path=None, replica_name=None, partition_key=None, primary_key=None)
2017-05-05 15:31:08 +03:00
infi.clickhouse_orm.query
-------------------------
### QuerySet
2017-06-24 12:28:42 +03:00
A queryset is an object that represents a database query using a specific `Model`.
It is lazy, meaning that it does not hit the database until you iterate over its
2017-05-05 15:31:08 +03:00
matching rows (model instances).
#### QuerySet(model_cls, database)
Initializer. It is possible to create a queryset like this, but the standard
way is to use `MyModel.objects_in(database)`.
#### aggregate(*args, **kwargs)
Returns an `AggregateQuerySet` over this query, with `args` serving as
grouping fields and `kwargs` serving as calculated fields. At least one
calculated field is required. For example:
```
Event.objects_in(database).filter(date__gt='2017-08-01').aggregate('event_type', count='count()')
```
is equivalent to:
```
SELECT event_type, count() AS count FROM event
WHERE data > '2017-08-01'
GROUP BY event_type
```
2017-05-05 15:31:08 +03:00
#### as_sql()
Returns the whole query as a SQL string.
2019-02-27 09:42:09 +03:00
#### conditions_as_sql(prewhere=False)
2017-05-05 15:31:08 +03:00
2018-12-19 08:10:03 +03:00
Returns the contents of the query's `WHERE` or `PREWHERE` clause as a string.
2017-05-05 15:31:08 +03:00
#### count()
Returns the number of matching model instances.
2017-09-10 17:17:04 +03:00
#### distinct()
Adds a DISTINCT clause to the query, meaning that any duplicate rows
in the results will be omitted.
2019-02-27 09:42:09 +03:00
#### exclude(*q, **kwargs)
2017-05-05 15:31:08 +03:00
Returns a copy of this queryset that excludes all rows matching the conditions.
2019-02-27 09:42:09 +03:00
Pass `prewhere=True` to apply the conditions as PREWHERE instead of WHERE.
2017-05-05 15:31:08 +03:00
2019-02-27 09:42:09 +03:00
#### filter(*q, **kwargs)
2017-05-05 15:31:08 +03:00
Returns a copy of this queryset that includes only rows matching the conditions.
2019-02-27 09:42:09 +03:00
Pass `prewhere=True` to apply the conditions as PREWHERE instead of WHERE.
2017-05-05 15:31:08 +03:00
2018-12-14 09:34:40 +03:00
#### final()
Adds a FINAL modifier to table, meaning data will be collapsed to final version.
Can be used with `CollapsingMergeTree` engine only.
#### limit_by(offset_limit, *fields)
Adds a LIMIT BY clause to the query.
- `offset_limit`: either an integer specifying the limit, or a tuple of integers (offset, limit).
- `fields`: the field names to use in the clause.
2017-05-05 15:31:08 +03:00
#### only(*field_names)
Returns a copy of this queryset limited to the specified field names.
2017-05-05 15:31:08 +03:00
Useful when there are large fields that are not needed,
or for creating a subquery to use with an IN operator.
#### order_by(*field_names)
Returns a copy of this queryset with the ordering changed.
#### order_by_as_sql()
Returns the contents of the query's `ORDER BY` clause as a string.
#### paginate(page_num=1, page_size=100)
Returns a single page of model instances that match the queryset.
Note that `order_by` should be used first, to ensure a correct
partitioning of records into pages.
- `page_num`: the page number (1-based), or -1 to get the last page.
- `page_size`: number of records to return per page.
The result is a namedtuple containing `objects` (list), `number_of_objects`,
`pages_total`, `number` (of the current page), and `page_size`.
2019-02-27 09:42:09 +03:00
#### select_fields_as_sql()
2019-02-27 09:58:41 +03:00
Returns the selected fields or expressions as a SQL string.
### AggregateQuerySet
Extends QuerySet
A queryset used for aggregation.
#### AggregateQuerySet(base_qs, grouping_fields, calculated_fields)
Initializer. Normally you should not call this but rather use `QuerySet.aggregate()`.
The grouping fields should be a list/tuple of field names from the model. For example:
```
('event_type', 'event_subtype')
```
The calculated fields should be a mapping from name to a ClickHouse aggregation function. For example:
```
{'weekday': 'toDayOfWeek(event_date)', 'number_of_events': 'count()'}
```
At least one calculated field is required.
#### aggregate(*args, **kwargs)
This method is not supported on `AggregateQuerySet`.
#### as_sql()
Returns the whole query as a SQL string.
2019-02-27 09:42:09 +03:00
#### conditions_as_sql(prewhere=False)
2018-12-19 08:10:03 +03:00
Returns the contents of the query's `WHERE` or `PREWHERE` clause as a string.
#### count()
Returns the number of rows after aggregation.
2017-09-10 17:17:04 +03:00
#### distinct()
Adds a DISTINCT clause to the query, meaning that any duplicate rows
in the results will be omitted.
2019-02-27 09:42:09 +03:00
#### exclude(*q, **kwargs)
Returns a copy of this queryset that excludes all rows matching the conditions.
2019-02-27 09:42:09 +03:00
Pass `prewhere=True` to apply the conditions as PREWHERE instead of WHERE.
2019-02-27 09:42:09 +03:00
#### filter(*q, **kwargs)
Returns a copy of this queryset that includes only rows matching the conditions.
2019-02-27 09:42:09 +03:00
Pass `prewhere=True` to apply the conditions as PREWHERE instead of WHERE.
2018-12-14 09:34:40 +03:00
#### final()
Adds a FINAL modifier to table, meaning data will be collapsed to final version.
Can be used with `CollapsingMergeTree` engine only.
#### group_by(*args)
This method lets you specify the grouping fields explicitly. The `args` must
be names of grouping fields or calculated fields that this queryset was
created with.
#### limit_by(offset_limit, *fields)
Adds a LIMIT BY clause to the query.
- `offset_limit`: either an integer specifying the limit, or a tuple of integers (offset, limit).
- `fields`: the field names to use in the clause.
#### only(*field_names)
This method is not supported on `AggregateQuerySet`.
#### order_by(*field_names)
Returns a copy of this queryset with the ordering changed.
2017-05-05 15:31:08 +03:00
#### order_by_as_sql()
Returns the contents of the query's `ORDER BY` clause as a string.
#### paginate(page_num=1, page_size=100)
Returns a single page of model instances that match the queryset.
Note that `order_by` should be used first, to ensure a correct
partitioning of records into pages.
- `page_num`: the page number (1-based), or -1 to get the last page.
- `page_size`: number of records to return per page.
The result is a namedtuple containing `objects` (list), `number_of_objects`,
`pages_total`, `number` (of the current page), and `page_size`.
2019-02-27 09:42:09 +03:00
#### select_fields_as_sql()
2019-02-27 09:58:41 +03:00
Returns the selected fields or expressions as a SQL string.
#### with_totals()
Adds WITH TOTALS modifier ot GROUP BY, making query return extra row
with aggregate function calculated across all the rows. More information:
https://clickhouse.yandex/docs/en/query_language/select/#with-totals-modifier