Skip to content

Commit 2f276b1

Browse files
authored
Merge pull request #86 from keboola/mj-teradata-platform
TER-18 Table preview filters
2 parents b322ffe + fed859b commit 2f276b1

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

src/Connection/Teradata/TeradataPlatform.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Doctrine\DBAL\Platforms\AbstractPlatform;
88
use Exception;
9+
use LogicException;
10+
use RuntimeException;
911

1012
class TeradataPlatform extends AbstractPlatform
1113
{
@@ -95,4 +97,52 @@ public function getCurrentDatabaseExpression(): string
9597
throw new Exception('method is not implemented yet');
9698
// TODO: Implement getCurrentDatabaseExpression() method.
9799
}
100+
101+
/**
102+
* {@inheritDoc}
103+
*/
104+
protected function doModifyLimitQuery($query, $limit, $offset): string
105+
{
106+
if (($limit === null || $limit === 0) && $offset <= 0) {
107+
return $query;
108+
}
109+
110+
if (preg_match('/^\s*SELECT\s+DISTINCT/im', $query) > 0) {
111+
/** @codingStandardsIgnoreStart */
112+
/*
113+
You cannot specify the TOP n operator in any of these SQL statements or statement components:
114+
- Correlated subquery
115+
- Subquery in a search condition
116+
- CREATE JOIN INDEX
117+
- CREATE HASH INDEX
118+
- Seed statement or recursive statement in a CREATE RECURSIVE VIEW statement or WITH RECURSIVE statement modifier
119+
- Subselects of set operations.
120+
You cannot specify these options in a SELECT statement that specifies the TOP n operator:
121+
- DISTINCT option
122+
- QUALIFY clause
123+
- SAMPLE clause
124+
- WITH clause
125+
- This restriction refers to the WITH clause you can specify for summary lines and breaks. See WITH Clause. The nonrecursive WITH statement modifier that can precede the SELECT keyword can be included in statements that also specify the TOP n operator. See WITH Modifier.
126+
- ORDER BY clause where the sort expression is an ordered analytical function.
127+
See https://www.docs.teradata.com/r/Teradata-VantageTM-SQL-Data-Manipulation-Language/July-2021/SELECT-Statements/Select-List-Syntax/TOP-Clause/Usage-Notes/Rules-and-Restrictions-for-the-TOP-n-Operator
128+
*/
129+
/** @codingStandardsIgnoreEnd */
130+
// skip limit
131+
} else {
132+
$query = preg_replace(
133+
'/^(\s*SELECT\b)/im',
134+
sprintf('$1 TOP %s', $limit),
135+
$query
136+
);
137+
if ($query === null) {
138+
throw new RuntimeException('Adding LIMIT to SQL retunrs error.');
139+
}
140+
}
141+
142+
if ($offset > 0) {
143+
throw new LogicException('Support for OFFSET not implemented yet.');
144+
}
145+
146+
return $query;
147+
}
98148
}

0 commit comments

Comments
 (0)