Skip to content

Instantly share code, notes, and snippets.

@chaudum
Last active May 3, 2018 06:54
Show Gist options
  • Save chaudum/77fc358b53afe22450a59b0d782d1381 to your computer and use it in GitHub Desktop.
Save chaudum/77fc358b53afe22450a59b0d782d1381 to your computer and use it in GitHub Desktop.
c_bool => True
c_bool_arr => [True, False]
c_byte => 127
c_byte_arr => [-128, 127]
c_double => 0.3333333333333333
c_double_arr => [0.5, 0.3333333333333333, 0.25]
c_float => 0.5
c_float_arr => [0.5, 0.33333334, 0.25]
c_geo_point => [9.74379, 47.4124]
c_geo_shape => {'coordinates': [[[16.979667, 48.123497], [16.903754, 47.714866], [16.340584, 47.712902], [16.534268, 47.496171], [16.202298, 46.852386], [16.011664, 46.683611], [15.137092, 46.658703], [14.632472, 46.431817], [13.806475, 46.509306], [12.376485, 46.767559], [12.153088, 47.115393], [11.164828, 46.941579], [11.048556, 46.751359], [10.442701, 46.893546], [9.932448, 46.920728], [9.47997, 47.10281], [9.632932, 47.347601], [9.594226, 47.525058], [9.896068, 47.580197], [10.402084, 47.302488], [10.544504, 47.566399], [11.426414, 47.523766], [12.141357, 47.703083], [12.62076, 47.672388], [12.932627, 47.467646], [13.025851, 47.637584], [12.884103, 48.289146], [13.243357, 48.416115], [13.595946, 48.877172], [14.338898, 48.555305], [14.901447, 48.964402], [15.253416, 49.039074], [16.029647, 48.733899], [16.499283, 48.785808], [16.960288, 48.596982], [16.879983, 48.470013], [16.979667, 48.123497]]], 'type': 'Polygon'}
c_int => 2147483647
c_int_arr => [-2147483648, 2147483647]
c_long => 9223372036854775807
c_long_arr => [-9223372036854775808, 9223372036854775807]
c_obj => {'c_long': -9223372036854775808, 'c_float': 0.5, 'c_str': 'foo', 'c_double': 0.3333333333333333, 'c_short': -32768, 'c_byte': -128, 'c_int': -2147483648, 'c_bool': False}
c_obj_arr => [{'a': 1}, {'a': 2}]
c_short => 32767
c_short_arr => [-32768, 32767]
c_str => lorem ipsum
c_str_arr => ['foo', 'bar', 'foobar']
#!/usr/bin/env python3
import sys
from crate.client import connect
def drop_table(conn):
cur = conn.cursor()
cur.execute('''
DROP TABLE IF EXISTS t1;
''')
def create_table(conn):
cur = conn.cursor()
cur.execute('''
CREATE TABLE t1 (
c_bool BOOLEAN,
c_byte BYTE,
c_short SHORT,
c_int INTEGER,
c_long LONG,
c_float FLOAT,
c_double DOUBLE,
c_str STRING,
c_obj OBJECT(strict) AS (
c_bool BOOLEAN,
c_byte BYTE,
c_short SHORT,
c_int INTEGER,
c_long LONG,
c_float FLOAT,
c_double DOUBLE,
c_str STRING
),
c_bool_arr ARRAY(BOOLEAN),
c_byte_arr ARRAY(BYTE),
c_short_arr ARRAY(SHORT),
c_int_arr ARRAY(INTEGER),
c_long_arr ARRAY(LONG),
c_float_arr ARRAY(FLOAT),
c_double_arr ARRAY(DOUBLE),
c_str_arr ARRAY(STRING),
c_obj_arr ARRAY(OBJECT),
c_geo_point GEO_POINT,
c_geo_shape GEO_SHAPE
)
''')
def insert_data(conn):
args = (
True,
2 ** 7 - 1, 2 ** 15 - 1,
2 ** 31 -1, 2 ** 63 - 1,
1 / 2, 1 / 3,
'lorem ipsum',
{
'c_bool': False,
'c_byte': (-2 ** 7),
'c_short': (-2 ** 15),
'c_int': (-2 ** 31),
'c_long': (-2 ** 63),
'c_float': (1 / 2),
'c_double': (1 / 3),
'c_str': 'foo'
},
[True, False],
[-2 ** 7, 2 ** 7 - 1], [-2 ** 15, 2 ** 15 - 1],
[-2 ** 31, 2 ** 31 - 1], [-2 ** 63, 2 ** 63 - 1],
[1 / 2, 1 / 3, 1 / 4], [1 / 2, 1 / 3, 1 / 4],
['foo', 'bar', 'foobar'],
[{'a': 1}, {'a': 2}],
[9.74379, 47.4124], {"type":"Polygon","coordinates":[[[16.979667,48.123497],[16.903754,47.714866],[16.340584,47.712902],[16.534268,47.496171],[16.202298,46.852386],[16.011664,46.683611],[15.137092,46.658703],[14.632472,46.431817],[13.806475,46.509306],[12.376485,46.767559],[12.153088,47.115393],[11.164828,46.941579],[11.048556,46.751359],[10.442701,46.893546],[9.932448,46.920728],[9.47997,47.10281],[9.632932,47.347601],[9.594226,47.525058],[9.896068,47.580197],[10.402084,47.302488],[10.544504,47.566399],[11.426414,47.523766],[12.141357,47.703083],[12.62076,47.672388],[12.932627,47.467646],[13.025851,47.637584],[12.884103,48.289146],[13.243357,48.416115],[13.595946,48.877172],[14.338898,48.555305],[14.901447,48.964402],[15.253416,49.039074],[16.029647,48.733899],[16.499283,48.785808],[16.960288,48.596982],[16.879983,48.470013],[16.979667,48.123497]]]},
)
cur = conn.cursor()
cur.execute('''
INSERT INTO t1 (
c_bool,
c_byte, c_short,
c_int, c_long,
c_float, c_double,
c_str,
c_obj,
c_bool_arr,
c_byte_arr, c_short_arr,
c_int_arr, c_long_arr,
c_float_arr, c_double_arr,
c_str_arr,
c_obj_arr,
c_geo_point, c_geo_shape
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', args)
cur.execute('''
REFRESH TABLE t1
''')
def select_data(conn):
cur = conn.cursor()
cur.execute('''
SELECT * FROM t1
''')
cols = [col[0] for col in cur.description]
for idx, col in enumerate(cur.fetchone()):
print(cols[idx], '=>', col)
def main():
with connect('localhost:4200', schema='test') as conn:
drop_table(conn)
create_table(conn)
insert_data(conn)
select_data(conn)
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment