4
4
from typing import NamedTuple
5
5
6
6
import pytest
7
+ import sqlglot as sg
8
+ import sqlglot .expressions as sge
7
9
8
10
import ibis .expr .datatypes as dt
9
11
import ibis .expr .schema as sch
@@ -440,8 +442,6 @@ def test_null_fields():
440
442
441
443
442
444
def test_to_sqlglot_column_defs ():
443
- import sqlglot .expressions as sge
444
-
445
445
schema = sch .schema ({"a" : "int64" , "b" : "string" , "c" : "!string" })
446
446
columns = schema .to_sqlglot_column_defs ("duckdb" )
447
447
@@ -463,9 +463,6 @@ def test_to_sqlglot_column_defs_empty_schema():
463
463
464
464
465
465
def test_to_sqlglot_column_defs_create_table_integration ():
466
- import sqlglot as sg
467
- import sqlglot .expressions as sge
468
-
469
466
schema = sch .schema ({"id" : "!int64" , "name" : "string" })
470
467
columns = schema .to_sqlglot_column_defs ("duckdb" )
471
468
@@ -478,3 +475,130 @@ def test_to_sqlglot_column_defs_create_table_integration():
478
475
sql = create_stmt .sql (dialect = "duckdb" )
479
476
expected = 'CREATE TABLE "test_table" ("id" BIGINT NOT NULL, "name" TEXT)'
480
477
assert sql == expected
478
+
479
+
480
+ def test_schema_from_sqlglot ():
481
+ columns = [
482
+ sge .ColumnDef (
483
+ this = sg .to_identifier ("bigint_col" , quoted = True ),
484
+ kind = sge .DataType (this = sge .DataType .Type .BIGINT ),
485
+ ),
486
+ sge .ColumnDef (
487
+ this = sg .to_identifier ("int_col" , quoted = True ),
488
+ kind = sge .DataType (this = sge .DataType .Type .INT ),
489
+ ),
490
+ sge .ColumnDef (
491
+ this = sg .to_identifier ("smallint_col" , quoted = True ),
492
+ kind = sge .DataType (this = sge .DataType .Type .SMALLINT ),
493
+ ),
494
+ sge .ColumnDef (
495
+ this = sg .to_identifier ("tinyint_col" , quoted = True ),
496
+ kind = sge .DataType (this = sge .DataType .Type .TINYINT ),
497
+ ),
498
+ sge .ColumnDef (
499
+ this = sg .to_identifier ("double_col" , quoted = True ),
500
+ kind = sge .DataType (this = sge .DataType .Type .DOUBLE ),
501
+ ),
502
+ sge .ColumnDef (
503
+ this = sg .to_identifier ("float_col" , quoted = True ),
504
+ kind = sge .DataType (this = sge .DataType .Type .FLOAT ),
505
+ ),
506
+ sge .ColumnDef (
507
+ this = sg .to_identifier ("varchar_col" , quoted = True ),
508
+ kind = sge .DataType (this = sge .DataType .Type .VARCHAR ),
509
+ ),
510
+ sge .ColumnDef (
511
+ this = sg .to_identifier ("text_col" , quoted = True ),
512
+ kind = sge .DataType (this = sge .DataType .Type .TEXT ),
513
+ ),
514
+ sge .ColumnDef (
515
+ this = sg .to_identifier ("boolean_col" , quoted = True ),
516
+ kind = sge .DataType (this = sge .DataType .Type .BOOLEAN ),
517
+ ),
518
+ sge .ColumnDef (
519
+ this = sg .to_identifier ("date_col" , quoted = True ),
520
+ kind = sge .DataType (this = sge .DataType .Type .DATE ),
521
+ ),
522
+ sge .ColumnDef (
523
+ this = sg .to_identifier ("timestamp_col" , quoted = True ),
524
+ kind = sge .DataType (this = sge .DataType .Type .DATETIME ),
525
+ ),
526
+ sge .ColumnDef (
527
+ this = sg .to_identifier ("time_col" , quoted = True ),
528
+ kind = sge .DataType (this = sge .DataType .Type .TIME ),
529
+ ),
530
+ sge .ColumnDef (
531
+ this = sg .to_identifier ("binary_col" , quoted = True ),
532
+ kind = sge .DataType (this = sge .DataType .Type .BINARY ),
533
+ ),
534
+ sge .ColumnDef (
535
+ this = sg .to_identifier ("uuid_col" , quoted = True ),
536
+ kind = sge .DataType (this = sge .DataType .Type .UUID ),
537
+ ),
538
+ sge .ColumnDef (
539
+ this = sg .to_identifier ("json_col" , quoted = True ),
540
+ kind = sge .DataType (this = sge .DataType .Type .JSON ),
541
+ ),
542
+ sge .ColumnDef (
543
+ this = sg .to_identifier ("decimal_col" , quoted = True ),
544
+ kind = sge .DataType (
545
+ this = sge .DataType .Type .DECIMAL ,
546
+ expressions = [
547
+ sge .DataTypeParam (this = sge .Literal .number (10 )),
548
+ sge .DataTypeParam (this = sge .Literal .number (2 )),
549
+ ],
550
+ ),
551
+ ),
552
+ sge .ColumnDef (
553
+ this = sg .to_identifier ("not_null_col" , quoted = True ),
554
+ kind = sge .DataType (this = sge .DataType .Type .VARCHAR ),
555
+ constraints = [sge .ColumnConstraint (kind = sge .NotNullColumnConstraint ())],
556
+ ),
557
+ sge .ColumnDef (
558
+ this = sg .to_identifier ("array_col" , quoted = True ),
559
+ kind = sge .DataType (
560
+ this = sge .DataType .Type .ARRAY ,
561
+ expressions = [sge .DataType (this = sge .DataType .Type .VARCHAR )],
562
+ nested = True ,
563
+ ),
564
+ ),
565
+ sge .ColumnDef (
566
+ this = sg .to_identifier ("map_col" , quoted = True ),
567
+ kind = sge .DataType (
568
+ this = sge .DataType .Type .MAP ,
569
+ expressions = [
570
+ sge .DataType (this = sge .DataType .Type .VARCHAR ),
571
+ sge .DataType (this = sge .DataType .Type .INT ),
572
+ ],
573
+ nested = True ,
574
+ ),
575
+ ),
576
+ ]
577
+
578
+ sqlglot_schema = sge .Schema (expressions = columns )
579
+ ibis_schema = sch .Schema .from_sqlglot (sqlglot_schema )
580
+ expected = sch .Schema (
581
+ {
582
+ "bigint_col" : dt .int64 ,
583
+ "int_col" : dt .int32 ,
584
+ "smallint_col" : dt .int16 ,
585
+ "tinyint_col" : dt .int8 ,
586
+ "double_col" : dt .float64 ,
587
+ "float_col" : dt .float32 ,
588
+ "varchar_col" : dt .string ,
589
+ "text_col" : dt .string ,
590
+ "boolean_col" : dt .boolean ,
591
+ "date_col" : dt .date ,
592
+ "timestamp_col" : dt .timestamp ,
593
+ "time_col" : dt .time ,
594
+ "binary_col" : dt .binary ,
595
+ "uuid_col" : dt .uuid ,
596
+ "json_col" : dt .json ,
597
+ "decimal_col" : dt .Decimal (10 , 2 ),
598
+ "not_null_col" : dt .String (nullable = False ),
599
+ "array_col" : dt .Array (dt .string ),
600
+ "map_col" : dt .Map (dt .string , dt .int32 ),
601
+ }
602
+ )
603
+
604
+ assert ibis_schema == expected
0 commit comments