@@ -171,7 +171,7 @@ def array_agg_fn_not_implemented(
171
171
raise NotImplementedError ('ARRAY_AGG is not implemented.' )
172
172
173
173
174
- def array_index_fn_googlesql (array : str , zero_based_idx : int ):
174
+ def array_index_safe_offset_fn (array : str , zero_based_idx : int ):
175
175
return f'{ array } [SAFE_OFFSET({ zero_based_idx } )]'
176
176
177
177
@@ -371,6 +371,7 @@ def duplicate_data_n_times_not_implemented(n, alias: Optional[str] = None):
371
371
'MariaDB' : drop_temp_table_if_exists_then_create_temp_table ,
372
372
'Oracle' : create_temp_table_fn_not_implemented ,
373
373
'SQL Server' : 'SELECT * INTO #{alias} FROM ({query});' .format ,
374
+ 'Calcite' : 'CREATE OR REPLACE TEMPORARY TABLE {alias} AS {query};' .format ,
374
375
}
375
376
SUPPORT_FULL_JOIN_OPTIONS = {
376
377
'Default' : True ,
@@ -391,6 +392,7 @@ def duplicate_data_n_times_not_implemented(n, alias: Optional[str] = None):
391
392
'Default' : lambda columns : ', ' .join (columns .aliases ),
392
393
'SQL Server' : lambda columns : ', ' .join (columns .expressions ),
393
394
'Trino' : lambda columns : ', ' .join (map (str , range (1 , len (columns ) + 1 ))),
395
+ 'Calcite' : lambda columns : ', ' .join (columns .expressions ),
394
396
}
395
397
SAFE_DIVIDE_OPTIONS = {
396
398
'Default' : safe_divide_fn_default ,
@@ -405,6 +407,7 @@ def duplicate_data_n_times_not_implemented(n, alias: Optional[str] = None):
405
407
'Oracle' : 'DBMS_RANDOM.VALUE' .format ,
406
408
'SQL Server' : sql_server_rand_fn_not_implemented ,
407
409
'SQLite' : '0.5 - RANDOM() / CAST(-9223372036854775808 AS REAL) / 2' .format ,
410
+ 'Calcite' : 'RAND()' .format ,
408
411
}
409
412
# Manually evalueated run_only_once_in_with_clause for each dialect.
410
413
NEED_TEMP_TABLE_OPTIONS = {
@@ -415,6 +418,7 @@ def duplicate_data_n_times_not_implemented(n, alias: Optional[str] = None):
415
418
'SQL Server' : True ,
416
419
'Trino' : True ,
417
420
'SQLite' : False ,
421
+ 'Calcite' : True ,
418
422
}
419
423
CEIL_OPTIONS = {
420
424
'Default' : 'CEIL({})' .format ,
@@ -426,6 +430,7 @@ def duplicate_data_n_times_not_implemented(n, alias: Optional[str] = None):
426
430
'PostgreSQL' : percentile_cont_fn ,
427
431
'Oracle' : percentile_cont_fn ,
428
432
'Trino' : approx_percentile_fn ,
433
+ 'Calcite' : percentile_cont_fn ,
429
434
}
430
435
ARRAY_AGG_OPTIONS = {
431
436
'Default' : array_agg_fn_not_implemented ,
@@ -436,14 +441,16 @@ def duplicate_data_n_times_not_implemented(n, alias: Optional[str] = None):
436
441
# JSON_ARRAYAGG has been added in SQL Server 2025. Will update later.
437
442
'SQL Server' : array_agg_fn_not_implemented ,
438
443
'Trino' : array_agg_fn_no_use_filter_no_limit ,
444
+ 'Calcite' : array_agg_fn_no_use_filter_no_limit ,
439
445
}
440
446
ARRAY_INDEX_OPTIONS = {
441
447
'Default' : array_index_fn_not_implemented ,
442
- 'GoogleSQL' : array_index_fn_googlesql ,
448
+ 'GoogleSQL' : array_index_safe_offset_fn ,
443
449
'PostgreSQL' : array_subscript_fn ,
444
450
'MariaDB' : json_extract_fn ,
445
451
'Oracle' : json_value_fn ,
446
452
'Trino' : element_at_index_fn ,
453
+ 'Calcite' : array_index_safe_offset_fn ,
447
454
}
448
455
NTH_OPTIONS = {
449
456
'Default' : nth_fn_default ,
@@ -463,6 +470,7 @@ def duplicate_data_n_times_not_implemented(n, alias: Optional[str] = None):
463
470
'Oracle' : 'TO_CHAR({})' .format ,
464
471
'SQL Server' : 'CAST({} AS VARCHAR(MAX))' .format ,
465
472
'Trino' : 'CAST({} AS VARCHAR)' .format ,
473
+ 'Calcite' : 'CAST({} AS VARCHAR)' .format ,
466
474
}
467
475
UNIFORM_MAPPING_OPTIONS = {
468
476
'Default' : uniform_mapping_fn_not_implemented ,
@@ -486,12 +494,14 @@ def duplicate_data_n_times_not_implemented(n, alias: Optional[str] = None):
486
494
'MariaDB' : unnest_json_array_fn ,
487
495
'Oracle' : unnest_json_array_fn ,
488
496
'Trino' : unnest_array_with_ordinality_fn ,
497
+ 'Calcite' : unnest_array_with_ordinality_fn ,
489
498
}
490
499
UNNEST_ARRAY_LITERAL_OPTIONS = {
491
500
'Default' : unnest_array_literal_fn_not_implemented ,
492
501
'GoogleSQL' : unnest_array_literal_fn_googlesql ,
493
502
'PostgreSQL' : unnest_array_literal_fn_postgresql ,
494
503
'Trino' : unnest_array_literal_fn_postgresql ,
504
+ 'Calcite' : unnest_array_literal_fn_postgresql ,
495
505
}
496
506
GENERATE_ARRAY_OPTIONS = {
497
507
'Default' : generate_array_fn_not_implemented ,
@@ -513,11 +523,13 @@ def duplicate_data_n_times_not_implemented(n, alias: Optional[str] = None):
513
523
}
514
524
515
525
516
- def set_dialect (dialect : str ):
526
+ def set_dialect (dialect : Optional [ str ] ):
517
527
"""Sets the dialect of the SQL query."""
518
528
# You can manually override the options below. You can manually test it in
519
529
# https://colab.research.google.com/drive/1y3UigzEby1anMM3-vXocBx7V8LVblIAp?usp=sharing.
520
530
global DIALECT , NEED_TEMP_TABLE , CREATE_TEMP_TABLE_FN , SUPPORT_FULL_JOIN , ROW_NUMBER_REQUIRE_ORDER_BY , GROUP_BY_FN , RAND_FN , CEIL_FN , SAFE_DIVIDE_FN , QUANTILE_FN , ARRAY_AGG_FN , ARRAY_INDEX_FN , NTH_VALUE_FN , COUNTIF_FN , STRING_CAST_FN , FLOAT_CAST_FN , UNIFORM_MAPPING_FN , UNNEST_ARRAY_FN , UNNEST_ARRAY_LITERAL_FN , GENERATE_ARRAY_FN , DUPLICATE_DATA_N_TIMES_FN
531
+ if not dialect :
532
+ return
521
533
DIALECT = dialect
522
534
NEED_TEMP_TABLE = _get_dialect_option (NEED_TEMP_TABLE_OPTIONS )
523
535
CREATE_TEMP_TABLE_FN = _get_dialect_option (CREATE_TEMP_TABLE_OPTIONS )
0 commit comments