|
4 | 4 | from packaging.version import Version
|
5 | 5 | import pkg_resources
|
6 | 6 | import sqlalchemy as sa
|
7 |
| -from sqlalchemy import inspect |
| 7 | +from sqlalchemy import inspect, exc as sa_exc |
8 | 8 | from sqlalchemy.dialects.postgresql.base import (
|
9 | 9 | PGCompiler, PGDDLCompiler, PGIdentifierPreparer, PGTypeCompiler,
|
10 | 10 | PGExecutionContext, PGDialect
|
@@ -560,7 +560,7 @@ class RedshiftDialectMixin(DefaultDialect):
|
560 | 560 | max_identifier_length = 127
|
561 | 561 | # explicitly disables statement cache to disable warnings in logs
|
562 | 562 | # ref: https://docs.sqlalchemy.org/en/14/core/connections.html#caching-for-third-party-dialects # noqa
|
563 |
| - supports_statement_cache = False |
| 563 | + supports_statement_cache = True |
564 | 564 |
|
565 | 565 | statement_compiler = RedshiftCompiler
|
566 | 566 | ddl_compiler = RedshiftDDLCompiler
|
@@ -1053,16 +1053,49 @@ class RedshiftDialect_redshift_connector(RedshiftDialectMixin, PGDialect):
|
1053 | 1053 |
|
1054 | 1054 | class RedshiftCompiler_redshift_connector(RedshiftCompiler, PGCompiler):
|
1055 | 1055 | def limit_clause(self, select, **kw):
|
1056 |
| - text = "" |
1057 |
| - if select._limit_clause is not None: |
1058 |
| - # an integer value for limit is retrieved |
1059 |
| - text += " \n LIMIT " + str(select._limit) |
1060 |
| - if select._offset_clause is not None: |
1061 |
| - if select._limit_clause is None: |
1062 |
| - text += "\n LIMIT ALL" |
1063 |
| - # an integer value for offset is retrieved |
1064 |
| - text += " OFFSET " + str(select._offset) |
1065 |
| - return text |
| 1056 | + if sa_version >= Version('1.4.0'): |
| 1057 | + text = "" |
| 1058 | + |
| 1059 | + limit_clause = select._limit_clause |
| 1060 | + offset_clause = select._offset_clause |
| 1061 | + |
| 1062 | + if select._simple_int_clause(limit_clause): |
| 1063 | + text += " \n LIMIT %s" % ( |
| 1064 | + self.process( |
| 1065 | + limit_clause.render_literal_execute(), |
| 1066 | + **kw |
| 1067 | + ) |
| 1068 | + ) |
| 1069 | + elif limit_clause is not None: |
| 1070 | + raise sa_exc.CompileError( |
| 1071 | + "dialect 'redshift-dialect' can only \ |
| 1072 | + render simple integers for LIMIT" |
| 1073 | + ) |
| 1074 | + if select._simple_int_clause(offset_clause): |
| 1075 | + text += " \n OFFSET %s" % ( |
| 1076 | + self.process( |
| 1077 | + offset_clause.render_literal_execute(), |
| 1078 | + **kw |
| 1079 | + ) |
| 1080 | + ) |
| 1081 | + elif offset_clause is not None: |
| 1082 | + raise sa_exc.CompileError( |
| 1083 | + "dialect 'redshift-dialect' can only \ |
| 1084 | + render simple integers for OFFSET" |
| 1085 | + ) |
| 1086 | + |
| 1087 | + return text |
| 1088 | + else: |
| 1089 | + text = "" |
| 1090 | + if select._limit_clause is not None: |
| 1091 | + # an integer value for limit is retrieved |
| 1092 | + text += " \n LIMIT " + str(select._limit) |
| 1093 | + if select._offset_clause is not None: |
| 1094 | + if select._limit_clause is None: |
| 1095 | + text += "\n LIMIT ALL" |
| 1096 | + # an integer value for offset is retrieved |
| 1097 | + text += " OFFSET " + str(select._offset) |
| 1098 | + return text |
1066 | 1099 |
|
1067 | 1100 | def visit_mod_binary(self, binary, operator, **kw):
|
1068 | 1101 | return (
|
|
0 commit comments