/
test_client.py
850 lines (732 loc) · 24.1 KB
/
test_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
import os
import platform
import re
import numpy as np
import pandas as pd
import pandas.testing as tm
import pytest
import sqlalchemy as sa
from pytest import mark, param
import ibis
import ibis.common.exceptions as com
import ibis.expr.datatypes as dt
import ibis.expr.operations as ops
import ibis.expr.types as ir
from ibis.util import guid
@pytest.fixture
def new_schema():
return ibis.schema([('a', 'string'), ('b', 'bool'), ('c', 'int32')])
def _create_temp_table_with_schema(con, temp_table_name, schema, data=None):
con.drop_table(temp_table_name, force=True)
con.create_table(temp_table_name, schema=schema)
temporary = con.table(temp_table_name)
assert temporary.execute().empty
if data is not None and isinstance(data, pd.DataFrame):
con.load_data(temp_table_name, data, if_exists="append")
result = temporary.execute()
assert len(result) == len(data.index)
tm.assert_frame_equal(result, data)
return temporary
@pytest.mark.notimpl(["snowflake"])
def test_load_data_sqlalchemy(alchemy_backend, alchemy_con, alchemy_temp_table):
sch = ibis.schema(
[
('first_name', 'string'),
('last_name', 'string'),
('department_name', 'string'),
('salary', 'float64'),
]
)
df = pd.DataFrame(
{
'first_name': ['A', 'B', 'C'],
'last_name': ['D', 'E', 'F'],
'department_name': ['AA', 'BB', 'CC'],
'salary': [100.0, 200.0, 300.0],
}
)
alchemy_con.create_table(alchemy_temp_table, schema=sch)
alchemy_con.load_data(alchemy_temp_table, df, if_exists='append')
result = alchemy_con.table(alchemy_temp_table).execute()
alchemy_backend.assert_frame_equal(df, result)
@mark.parametrize(
('expr_fn', 'expected'),
[
(lambda t: t.string_col, [('string_col', dt.String)]),
(
lambda t: t[t.string_col, t.bigint_col],
[('string_col', dt.String), ('bigint_col', dt.Int64)],
),
],
)
@mark.notimpl(["datafusion", "polars"])
def test_query_schema(ddl_backend, ddl_con, expr_fn, expected):
expr = expr_fn(ddl_backend.functional_alltypes)
# we might need a public API for it
ast = ddl_con.compiler.to_ast(expr, ddl_backend.make_context())
schema = ddl_con.ast_schema(ast)
# clickhouse columns has been defined as non-nullable
# whereas other backends don't support non-nullable columns yet
expected = ibis.schema(
[(name, dtype(nullable=schema[name].nullable)) for name, dtype in expected]
)
assert schema.equals(expected)
@pytest.mark.notimpl(["datafusion", "snowflake", "polars"])
@pytest.mark.notyet(["sqlite"])
@pytest.mark.never(
["dask", "pandas"],
reason="dask and pandas do not support SQL",
)
def test_sql(con):
# execute the expression using SQL query
expr = con.sql("SELECT * FROM functional_alltypes LIMIT 10")
result = expr.execute()
assert len(result) == 10
@mark.notimpl(["clickhouse", "datafusion", "polars"])
def test_create_table_from_schema(con, new_schema, temp_table):
con.create_table(temp_table, schema=new_schema)
t = con.table(temp_table)
for k, i_type in t.schema().items():
assert new_schema[k] == i_type
@mark.notimpl(
[
"clickhouse",
"dask",
"datafusion",
"duckdb",
"mysql",
"pandas",
"postgres",
"sqlite",
"snowflake",
"polars",
]
)
def test_rename_table(con, temp_table, new_schema):
temp_table_original = f'{temp_table}_original'
con.create_table(temp_table_original, schema=new_schema)
try:
t = con.table(temp_table_original)
t.rename(temp_table)
assert con.table(temp_table) is not None
assert temp_table in con.list_tables()
finally:
con.drop_table(temp_table_original, force=True)
con.drop_table(temp_table, force=True)
@mark.notimpl(["clickhouse", "datafusion", "polars"])
@mark.never(["impala", "pyspark"], reason="No non-nullable datatypes")
def test_nullable_input_output(con, temp_table):
sch = ibis.schema(
[
('foo', 'int64'),
('bar', ibis.expr.datatypes.int64(nullable=False)),
('baz', 'boolean'),
]
)
con.create_table(temp_table, schema=sch)
t = con.table(temp_table)
assert t.schema().types[0].nullable
assert not t.schema().types[1].nullable
assert t.schema().types[2].nullable
@mark.notimpl(
[
"clickhouse",
"datafusion",
"duckdb",
"mysql",
"postgres",
"sqlite",
"snowflake",
"polars",
]
)
@mark.notyet(["pyspark"])
def test_create_drop_view(ddl_con, temp_view):
# setup
table_name = 'functional_alltypes'
expr = ddl_con.table(table_name).limit(1)
# create a new view
ddl_con.create_view(temp_view, expr)
# check if the view was created
assert temp_view in ddl_con.list_tables()
t_expr = ddl_con.table(table_name)
v_expr = ddl_con.table(temp_view)
# check if the view and the table has the same fields
assert set(t_expr.schema().names) == set(v_expr.schema().names)
@mark.notimpl(["postgres", "mysql", "clickhouse", "datafusion", "polars"])
def test_separate_database(ddl_con, alternate_current_database, current_data_db):
# using alternate_current_database switches "con" current
# database to a temporary one until a test is over
tmp_db = ddl_con.database(alternate_current_database)
# verifying we can open another db which isn't equal to current
db = ddl_con.database(current_data_db)
assert db.name == current_data_db
assert tmp_db.name == alternate_current_database
def _skip_snowflake(con, reason="snowflake can't drop tables"):
if con.name == "snowflake":
pytest.skip(reason)
@pytest.fixture
def employee_empty_temp_table(alchemy_con, test_employee_schema):
_skip_snowflake(alchemy_con)
temp_table_name = f"temp_to_table_{guid()[:6]}"
_create_temp_table_with_schema(
alchemy_con,
temp_table_name,
test_employee_schema,
)
try:
yield temp_table_name
finally:
alchemy_con.drop_table(temp_table_name)
@pytest.fixture
def employee_data_1_temp_table(
alchemy_con,
test_employee_schema,
test_employee_data_1,
):
_skip_snowflake(alchemy_con)
temp_table_name = f"temp_to_table_{guid()[:6]}"
_create_temp_table_with_schema(
alchemy_con,
temp_table_name,
test_employee_schema,
data=test_employee_data_1,
)
try:
yield temp_table_name
finally:
alchemy_con.drop_table(temp_table_name)
@pytest.fixture
def employee_data_2_temp_table(
alchemy_con,
test_employee_schema,
test_employee_data_2,
):
_skip_snowflake(alchemy_con)
temp_table_name = f"temp_to_table_{guid()[:6]}"
_create_temp_table_with_schema(
alchemy_con,
temp_table_name,
test_employee_schema,
data=test_employee_data_2,
)
try:
yield temp_table_name
finally:
alchemy_con.drop_table(temp_table_name)
def test_insert_no_overwrite_from_dataframe(
alchemy_con,
test_employee_data_2,
employee_empty_temp_table,
):
temporary = alchemy_con.table(employee_empty_temp_table)
alchemy_con.insert(
employee_empty_temp_table,
obj=test_employee_data_2,
overwrite=False,
)
result = temporary.execute()
assert len(result) == 3
tm.assert_frame_equal(result, test_employee_data_2)
def test_insert_overwrite_from_dataframe(
alchemy_con,
employee_data_1_temp_table,
test_employee_data_2,
):
temporary = alchemy_con.table(employee_data_1_temp_table)
alchemy_con.insert(
employee_data_1_temp_table,
obj=test_employee_data_2,
overwrite=True,
)
result = temporary.execute()
assert len(result) == 3
tm.assert_frame_equal(result, test_employee_data_2)
def test_insert_no_overwite_from_expr(
alchemy_con,
employee_empty_temp_table,
employee_data_2_temp_table,
):
temporary = alchemy_con.table(employee_empty_temp_table)
from_table = alchemy_con.table(employee_data_2_temp_table)
alchemy_con.insert(
employee_empty_temp_table,
obj=from_table,
overwrite=False,
)
result = temporary.execute()
assert len(result) == 3
tm.assert_frame_equal(result, from_table.execute())
def test_insert_overwrite_from_expr(
alchemy_con,
employee_data_1_temp_table,
employee_data_2_temp_table,
):
temporary = alchemy_con.table(employee_data_1_temp_table)
from_table = alchemy_con.table(employee_data_2_temp_table)
alchemy_con.insert(
employee_data_1_temp_table,
obj=from_table,
overwrite=True,
)
result = temporary.execute()
assert len(result) == 3
tm.assert_frame_equal(result, from_table.execute())
def test_insert_overwrite_from_list(
alchemy_con,
employee_data_1_temp_table,
):
def _emp(a, b, c, d):
return dict(first_name=a, last_name=b, department_name=c, salary=d)
alchemy_con.insert(
employee_data_1_temp_table,
[
_emp('Adam', 'Smith', 'Accounting', 50000.0),
_emp('Mohammed', 'Ali', 'Boxing', 150000),
_emp('María', 'Gonzalez', 'Engineering', 100000.0),
],
overwrite=True,
)
assert len(alchemy_con.table(employee_data_1_temp_table).execute()) == 3
def test_insert_from_memtable(alchemy_con):
df = pd.DataFrame({"x": range(3)})
table_name = "memtable_test"
alchemy_con.insert(table_name, ibis.memtable(df))
alchemy_con.insert(table_name, ibis.memtable(df))
try:
table = alchemy_con.tables[table_name]
assert len(table.execute()) == 6
assert alchemy_con.tables[table_name].schema() == ibis.schema({"x": "int64"})
finally:
alchemy_con.raw_sql(f"DROP TABLE IF EXISTS {table_name}")
assert table_name not in alchemy_con.list_tables()
def test_list_databases(alchemy_con):
# Every backend has its own databases
TEST_DATABASES = {
'sqlite': ['main'],
'postgres': ['postgres', 'ibis_testing'],
'mysql': ['ibis_testing', 'information_schema'],
'duckdb': ['information_schema', 'main', 'temp'],
'snowflake': ['IBIS_TESTING'],
}
assert alchemy_con.list_databases() == TEST_DATABASES[alchemy_con.name]
@pytest.mark.never(
["postgres", "mysql", "snowflake"],
reason="postgres and mysql do not support in-memory tables",
raises=(sa.exc.OperationalError, TypeError),
)
def test_in_memory(alchemy_backend):
con = getattr(ibis, alchemy_backend.name()).connect(":memory:")
table_name = f"t{guid()[:6]}"
con.raw_sql(f"CREATE TABLE {table_name} (x int)")
try:
assert table_name in con.list_tables()
finally:
con.raw_sql(f"DROP TABLE IF EXISTS {table_name}")
assert table_name not in con.list_tables()
@pytest.mark.parametrize(
"coltype",
[dt.uint8, dt.uint16, dt.uint32, dt.uint64],
ids=["uint8", "uint16", "uint32", "uint64"],
)
@pytest.mark.notyet(
["postgres", "mysql", "sqlite"],
raises=TypeError,
reason="postgres, mysql and sqlite do not support unsigned integer types",
)
def test_unsigned_integer_type(alchemy_con, coltype):
tname = f"t{guid()[:6]}"
alchemy_con.create_table(tname, schema=ibis.schema(dict(a=coltype)), force=True)
try:
assert tname in alchemy_con.list_tables()
finally:
alchemy_con.drop_table(tname, force=True)
@pytest.mark.backend
@pytest.mark.parametrize(
"url",
[
param(
"clickhouse://default@localhost:9000/ibis_testing",
marks=mark.clickhouse,
id="clickhouse",
),
param(
"dask://",
marks=[mark.dask, mark.xfail(raises=NotImplementedError)],
id="dask",
),
param(
"datafusion://",
marks=[mark.datafusion, mark.xfail(raises=NotImplementedError)],
id="datafusion",
),
param(
"impala://localhost:21050/ibis_testing",
marks=mark.impala,
id="impala",
),
param(
"mysql://ibis:ibis@localhost:3306/ibis_testing",
marks=mark.mysql,
id="mysql",
),
param(
"pandas://",
marks=[mark.pandas, mark.xfail(raises=NotImplementedError)],
id="pandas",
),
param(
"postgres://postgres:postgres@localhost:5432/ibis_testing",
marks=mark.postgres,
id="postgres",
),
param(
"postgresql://postgres:postgres@localhost:5432/ibis_testing",
marks=mark.postgres,
id="postgresql",
),
param(
"pyspark://?spark.app.name=test-pyspark",
marks=mark.pyspark,
id="pyspark",
),
param(
"pyspark://my-warehouse-dir?spark.app.name=test-pyspark",
marks=mark.pyspark,
id="pyspark_with_warehouse",
),
param(
"pyspark://my-warehouse-dir",
marks=mark.pyspark,
id="pyspark_with_warehouse_no_params",
),
],
)
def test_connect_url(url):
con = ibis.connect(url)
one = ibis.literal(1)
assert con.execute(one) == 1
not_windows = pytest.mark.skipif(
condition=platform.system() == "Windows",
reason=(
"windows prevents two connections to the same duckdb file even in "
"the same process"
),
)
@pytest.fixture(params=["duckdb", "sqlite"])
def tmp_db(request, tmp_path):
api = request.param
mod = pytest.importorskip(api)
db = tmp_path / "test.db"
mod.connect(str(db)).execute("CREATE TABLE tmp_t AS SELECT 1 AS a").fetchall()
return db
@pytest.mark.duckdb
@pytest.mark.parametrize(
"url",
[
param(lambda p: p, id="no-scheme-duckdb-ext"),
param(lambda p: f"duckdb://{p}", id="absolute-path"),
param(
lambda p: f"duckdb://{os.path.relpath(p)}",
marks=[
not_windows
], # hard to test in CI since tmpdir & cwd are on different drives
id="relative-path",
),
param(lambda p: "duckdb://", id="in-memory-empty"),
param(lambda p: "duckdb://:memory:", id="in-memory-explicit"),
param(
lambda p: f"duckdb://{p}?read_only=1",
id="duckdb_read_write_int",
),
param(
lambda p: f"duckdb://{p}?read_only=False",
id="duckdb_read_write_upper",
),
param(
lambda p: f"duckdb://{p}?read_only=false",
id="duckdb_read_write_lower",
),
],
)
def test_connect_duckdb(url, tmp_path):
duckdb = pytest.importorskip("duckdb")
path = os.path.abspath(tmp_path / "test.duckdb")
with duckdb.connect(path):
pass
con = ibis.connect(url(path))
one = ibis.literal(1)
assert con.execute(one) == 1
@pytest.mark.sqlite
@pytest.mark.parametrize(
"url, ext",
[
param(lambda p: p, "sqlite", id="no-scheme-sqlite-ext"),
param(lambda p: p, "db", id="no-scheme-db-ext"),
param(lambda p: f"sqlite://{p}", "db", id="absolute-path"),
param(
lambda p: f"sqlite://{os.path.relpath(p)}",
"db",
marks=[
not_windows
], # hard to test in CI since tmpdir & cwd are on different drives
id="relative-path",
),
param(lambda p: "sqlite://", "db", id="in-memory-empty"),
param(lambda p: "sqlite://:memory:", "db", id="in-memory-explicit"),
],
)
def test_connect_sqlite(url, ext, tmp_path):
import sqlite3
path = os.path.abspath(tmp_path / f"test.{ext}")
with sqlite3.connect(path):
pass
con = ibis.connect(url(path))
one = ibis.literal(1)
assert con.execute(one) == 1
@pytest.mark.duckdb
@pytest.mark.parametrize(
"out_method, extension",
[
("to_csv", "csv"),
("to_parquet", "parquet"),
],
)
def test_connect_local_file(out_method, extension, test_employee_data_1, tmp_path):
getattr(test_employee_data_1, out_method)(tmp_path / f"out.{extension}")
t = ibis.connect(tmp_path / f"out.{extension}")
assert isinstance(t, ir.Table)
assert not t.head().execute().empty
@not_windows
def test_invalid_connect():
pytest.importorskip("duckdb")
url = "?".join(
[
"duckdb://ci/ibis-testing-data/ibis_testing.ddb",
"read_only=invalid_value",
]
)
with pytest.raises(ValueError):
ibis.connect(url)
@pytest.mark.never(
[
"clickhouse",
"dask",
"datafusion",
"impala",
"mysql",
"pandas",
"postgres",
"pyspark",
"snowflake",
"polars",
],
reason="backend isn't file-based",
)
def test_deprecated_path_argument(backend, tmp_path):
with pytest.warns(UserWarning, match="The `path` argument is deprecated"):
getattr(ibis, backend.name()).connect(path=str(tmp_path / "test.db"))
@pytest.mark.parametrize(
("expr", "expected"),
[
param(
ibis.memtable([(1, 2.0, "3")], columns=list("abc")),
pd.DataFrame([(1, 2.0, "3")], columns=list("abc")),
id="simple",
),
param(
ibis.memtable([(1, 2.0, "3")]),
pd.DataFrame([(1, 2.0, "3")], columns=["col0", "col1", "col2"]),
id="simple_auto_named",
),
param(
ibis.memtable(
[(1, 2.0, "3")],
schema=ibis.schema(dict(a="int8", b="float32", c="string")),
),
pd.DataFrame([(1, 2.0, "3")], columns=list("abc")).astype(
{"a": "int8", "b": "float32"}
),
id="simple_schema",
),
param(
ibis.memtable(
pd.DataFrame({"a": [1], "b": [2.0], "c": ["3"]}).astype(
{"a": "int8", "b": "float32"}
)
),
pd.DataFrame([(1, 2.0, "3")], columns=list("abc")).astype(
{"a": "int8", "b": "float32"}
),
id="dataframe",
),
param(
ibis.memtable([dict(a=1), dict(a=2)]),
pd.DataFrame({"a": [1, 2]}),
id="list_of_dicts",
),
],
)
@pytest.mark.notyet(
["mysql", "sqlite"],
reason="SQLAlchemy generates incorrect code for `VALUES` projections.",
raises=(sa.exc.ProgrammingError, sa.exc.OperationalError),
)
@pytest.mark.notimpl(["dask", "datafusion", "pandas"])
def test_in_memory_table(backend, con, expr, expected):
result = con.execute(expr)
backend.assert_frame_equal(result, expected)
@pytest.mark.notyet(
["mysql", "sqlite"],
reason="SQLAlchemy generates incorrect code for `VALUES` projections.",
raises=(sa.exc.ProgrammingError, sa.exc.OperationalError),
)
@pytest.mark.notimpl(["dask", "datafusion", "pandas"])
def test_filter_memory_table(backend, con):
t = ibis.memtable([(1, 2), (3, 4), (5, 6)], columns=["x", "y"])
expr = t.filter(t.x > 1)
expected = pd.DataFrame({"x": [3, 5], "y": [4, 6]})
result = con.execute(expr)
backend.assert_frame_equal(result, expected)
@pytest.mark.notyet(
["mysql", "sqlite"],
reason="SQLAlchemy generates incorrect code for `VALUES` projections.",
raises=(sa.exc.ProgrammingError, sa.exc.OperationalError),
)
@pytest.mark.notimpl(["dask", "datafusion", "pandas"])
def test_agg_memory_table(con):
t = ibis.memtable([(1, 2), (3, 4), (5, 6)], columns=["x", "y"])
expr = t.x.count()
result = con.execute(expr)
assert result == 3
@pytest.mark.parametrize(
"t",
[
param(
ibis.memtable([("a", 1.0)], columns=["a", "b"]),
id="python",
),
param(
ibis.memtable(pd.DataFrame([("a", 1.0)], columns=["a", "b"])),
id="pandas-memtable",
),
param(
pd.DataFrame([("a", 1.0)], columns=["a", "b"]),
id="pandas",
),
],
)
@pytest.mark.notimpl(["clickhouse", "dask", "datafusion", "pandas", "polars"])
def test_create_from_in_memory_table(backend, con, t):
if backend.name() == "snowflake":
pytest.skip("snowflake is unreliable here")
tmp_name = f"t{guid()[:6]}"
con.create_table(tmp_name, t)
try:
assert tmp_name in con.list_tables()
finally:
con.drop_table(tmp_name)
assert tmp_name not in con.list_tables()
def test_default_backend_no_duckdb(backend):
# backend is used to ensure that this test runs in CI in the setting
# where only the dependencies for a a given backend are installed
# if duckdb is available then this test won't fail and so we skip it
try:
import duckdb # noqa: F401
pytest.skip("duckdb is installed; it will be used as the default backend")
except ImportError:
pass
df = pd.DataFrame({'a': [1, 2, 3]})
t = ibis.memtable(df)
expr = t.a.sum()
# run this twice to ensure that we hit the optimizations in
# `_default_backend`
for _ in range(2):
with pytest.raises(
com.IbisError,
match="Expression depends on no backends",
):
expr.execute()
@pytest.mark.duckdb
def test_default_backend():
pytest.importorskip("duckdb")
df = pd.DataFrame({'a': [1, 2, 3]})
t = ibis.memtable(df)
expr = t.a.sum()
# run this twice to ensure that we hit the optimizations in
# `_default_backend`
for _ in range(2):
assert expr.execute() == df.a.sum()
sql = ibis.to_sql(expr)
rx = """\
SELECT
SUM\\((\\w+)\\.a\\) AS sum
FROM \\w+ AS \\1"""
assert re.match(rx, sql) is not None
@pytest.mark.parametrize("dtype", [None, "f8"])
def test_dunder_array_table(alltypes, df, dtype):
expr = alltypes.group_by("string_col").int_col.sum().order_by("string_col")
result = np.asarray(expr, dtype=dtype)
expected = np.asarray(expr.execute(), dtype=dtype)
np.testing.assert_array_equal(result, expected)
@pytest.mark.parametrize("dtype", [None, "f8"])
def test_dunder_array_column(alltypes, df, dtype):
expr = (
alltypes.group_by("string_col")
.agg(int_col=lambda _: _.int_col.sum())
.order_by("string_col")
.int_col
)
result = np.asarray(expr, dtype=dtype)
expected = np.asarray(expr.execute(), dtype=dtype)
np.testing.assert_array_equal(result, expected)
@pytest.mark.parametrize("interactive", [True, False])
def test_repr(alltypes, interactive):
expr = alltypes.select("id", "int_col")
val = str(alltypes.limit(5).id.execute().iloc[0])
old = ibis.options.interactive
ibis.options.interactive = interactive
try:
s = repr(expr)
# no control characters
assert all(c.isprintable() or c in "\n\r\t" for c in s)
assert "id" in s
if interactive:
assert val in s
else:
assert val not in s
finally:
ibis.options.interactive = old
@pytest.mark.parametrize("expr_type", ["table", "column"])
@pytest.mark.parametrize("interactive", [True, False])
def test_repr_mimebundle(alltypes, interactive, expr_type):
if expr_type == "column":
expr = alltypes.id
else:
expr = alltypes.select("id", "int_col")
val = str(alltypes.limit(5).id.execute().iloc[0])
old = ibis.options.interactive
ibis.options.interactive = interactive
try:
reprs = expr._repr_mimebundle_(include=["text/plain", "text/html"], exclude=[])
for format in ["text/plain", "text/html"]:
assert "id" in reprs[format]
if interactive:
assert val in reprs[format]
else:
assert val not in reprs[format]
finally:
ibis.options.interactive = old
@pytest.mark.never(
["postgres", "mysql"],
reason="These backends explicitly do support Geo operations",
)
def test_has_operation_no_geo(con):
"""Previously some backends mistakenly reported Geo operations as
supported.
Since most backends don't support Geo operations, we test that
they're excluded here, skipping the few backends that explicitly do
support them.
"""
for op in [ops.GeoDistance, ops.GeoAsText, ops.GeoUnaryUnion]:
assert not con.has_operation(op)