-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
122 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import Sequence | ||
|
||
from clickhouse_connect.datatypes.base import ClickHouseType | ||
from clickhouse_connect.driver.insert import InsertContext | ||
from clickhouse_connect.driver.query import QueryContext | ||
from clickhouse_connect.driver.types import ByteSource | ||
|
||
POINT_DATA_TYPE: ClickHouseType | ||
RING_DATA_TYPE: ClickHouseType | ||
POLYGON_DATA_TYPE: ClickHouseType | ||
MULTI_POLYGON_DATA_TYPE: ClickHouseType | ||
|
||
|
||
class Point(ClickHouseType): | ||
def write_column(self, column: Sequence, dest: bytearray, ctx: InsertContext): | ||
return POINT_DATA_TYPE.write_column(column, dest, ctx) | ||
|
||
def read_column_data(self, source: ByteSource, num_rows: int, ctx: QueryContext) -> Sequence: | ||
return POINT_DATA_TYPE.read_column_data(source, num_rows, ctx) | ||
|
||
|
||
class Ring(ClickHouseType): | ||
def write_column(self, column: Sequence, dest: bytearray, ctx: InsertContext): | ||
return RING_DATA_TYPE.write_column(column, dest, ctx) | ||
|
||
def read_column_data(self, source: ByteSource, num_rows: int, ctx: QueryContext) -> Sequence: | ||
return RING_DATA_TYPE.read_column_data(source, num_rows, ctx) | ||
|
||
|
||
class Polygon(ClickHouseType): | ||
def write_column(self, column: Sequence, dest: bytearray, ctx: InsertContext): | ||
return POLYGON_DATA_TYPE.write_column(column, dest, ctx) | ||
|
||
def read_column_data(self, source: ByteSource, num_rows: int, ctx: QueryContext) -> Sequence: | ||
return POLYGON_DATA_TYPE.read_column_data(source, num_rows, ctx) | ||
|
||
|
||
class MultiPolygon(ClickHouseType): | ||
def write_column(self, column: Sequence, dest: bytearray, ctx: InsertContext): | ||
return MULTI_POLYGON_DATA_TYPE.write_column(column, dest, ctx) | ||
|
||
def read_column_data(self, source: ByteSource, num_rows: int, ctx: QueryContext) -> Sequence: | ||
return MULTI_POLYGON_DATA_TYPE.read_column_data(source, num_rows, ctx) | ||
|
||
|
||
class LineString(Ring): | ||
pass | ||
|
||
|
||
class MultiLineString(Polygon): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
from clickhouse_connect.datatypes import registry, dynamic | ||
from clickhouse_connect.datatypes import registry, dynamic, geometric | ||
|
||
dynamic.SHARED_DATA_TYPE = registry.get_from_name('Array(String, String)') | ||
dynamic.STRING_DATA_TYPE = registry.get_from_name('String') | ||
|
||
point = 'Tuple(Float64, Float64)' | ||
ring = f'Array({point})' | ||
polygon = f'Array({ring})' | ||
multi_polygon = f'Array({polygon})' | ||
|
||
geometric.POINT_DATA_TYPE = registry.get_from_name(point) | ||
geometric.RING_DATA_TYPE = registry.get_from_name(ring) | ||
geometric.POLYGON_DATA_TYPE = registry.get_from_name(polygon) | ||
geometric.MULTI_POLYGON_DATA_TYPE = registry.get_from_name(multi_polygon) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Callable | ||
|
||
from clickhouse_connect.driver import Client | ||
|
||
|
||
def test_point_column(test_client: Client, table_context: Callable): | ||
with table_context('point_column_test', ['key Int32', 'point Point']): | ||
data = [[1, (3.55, 3.55)], [2, (4.55, 4.55)]] | ||
test_client.insert('point_column_test', data) | ||
|
||
query_result = test_client.query('SELECT * FROM point_column_test ORDER BY key').result_rows | ||
assert len(query_result) == 2 | ||
assert query_result[0] == (1, (3.55, 3.55)) | ||
assert query_result[1] == (2, (4.55, 4.55)) | ||
|
||
|
||
def test_ring_column(test_client: Client, table_context: Callable): | ||
with table_context('ring_column_test', ['key Int32', 'ring Ring']): | ||
data = [[1, [(5.522, 58.472),(3.55, 3.55)]], [2, [(4.55, 4.55)]]] | ||
test_client.insert('ring_column_test', data) | ||
|
||
query_result = test_client.query('SELECT * FROM ring_column_test ORDER BY key').result_rows | ||
assert len(query_result) == 2 | ||
assert query_result[0] == (1, [(5.522, 58.472),(3.55, 3.55)]) | ||
assert query_result[1] == (2, [(4.55, 4.55)]) | ||
|
||
|
||
def test_polygon_column(test_client: Client, table_context: Callable): | ||
with table_context('polygon_column_test', ['key Int32', 'polygon Polygon']): | ||
res = test_client.query("SELECT readWKTPolygon('POLYGON ((-64.8 32.3, -65.5 18.3, -80.3 25.2, -64.8 32.3))') as polygon") | ||
pg = res.first_row[0] | ||
test_client.insert('polygon_column_test', [(1, pg), (4, pg)]) | ||
query_result = test_client.query('SELECT key, polygon FROM polygon_column_test WHERE key = 4') | ||
assert query_result.first_row[1] == pg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters