|
| 1 | +/* |
| 2 | + * Copyright (C) 2017-2019 Dremio Corporation. This file is confidential and private property. |
| 3 | + */ |
| 4 | +package com.dremio.exec.store.jdbc.dialect; |
| 5 | + |
| 6 | +import com.dremio.exec.store.jdbc.dialect.arp.ArpYaml; |
| 7 | +import org.apache.calcite.sql.SqlNode; |
| 8 | +import org.apache.calcite.sql.SqlCall; |
| 9 | +import org.apache.calcite.sql.SqlSelect; |
| 10 | +import org.apache.calcite.sql.SqlSelectOperator; |
| 11 | +import org.apache.calcite.sql.SqlSelectKeyword; |
| 12 | +import org.apache.calcite.sql.SqlLiteral; |
| 13 | +import org.apache.calcite.sql.SqlNodeList; |
| 14 | +import org.apache.calcite.sql.SqlWriter; |
| 15 | +import org.apache.calcite.sql.parser.SqlParserPos; |
| 16 | +import com.dremio.exec.store.jdbc.dialect.arp.ArpDialect; |
| 17 | + |
| 18 | +/* |
| 19 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 20 | + * contributor license agreements. See the NOTICE file distributed with |
| 21 | + * this work for additional information regarding copyright ownership. |
| 22 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 23 | + * (the "License"); you may not use this file except in compliance with |
| 24 | + * the License. You may obtain a copy of the License at |
| 25 | + * |
| 26 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 27 | + * |
| 28 | + * Unless required by applicable law or agreed to in writing, software |
| 29 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 30 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 31 | + * See the License for the specific language governing permissions and |
| 32 | + * limitations under the License. |
| 33 | + */ |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * A <code>SqlDialect</code> implementation for the Sybase database. |
| 38 | + */ |
| 39 | +public class SybaseDialect extends ArpDialect { |
| 40 | + |
| 41 | + /** Creates a SybaseSqlDialect. */ |
| 42 | + public SybaseDialect(ArpYaml yaml) { |
| 43 | + super(yaml); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean hasImplicitTableAlias() { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void unparseCall(final SqlWriter writer, final SqlCall call, final int leftPrec, final int rightPrec) { |
| 53 | + // Transform SqlSelect nodes that have a fetch node to be SqlSelect nodes with a TOP and no fetch. |
| 54 | + if (call instanceof SqlSelect) { |
| 55 | + final SqlSelect select = (SqlSelect) call; |
| 56 | + |
| 57 | + // Transform SqlSelect nodes that have a fetch node without offset to be |
| 58 | + // SqlSelect nodes with a TOP and no fetch. |
| 59 | + if (null != select.getFetch() |
| 60 | + && (null == select.getOffset() || 0 == ((SqlLiteral) select.getOffset()).getValueAs(Long.class))) { |
| 61 | + final SqlNodeList keywords = new SqlNodeList(SqlParserPos.ZERO); |
| 62 | + |
| 63 | + // Add the DISTINCT or ALL keywords if the original Select had either. (Only can have one of these). |
| 64 | + // These must go before TOP. |
| 65 | + if (null != select.getModifierNode(SqlSelectKeyword.DISTINCT)) { |
| 66 | + keywords.add(select.getModifierNode(SqlSelectKeyword.DISTINCT)); |
| 67 | + } else if (null != select.getModifierNode(SqlSelectKeyword.ALL)) { |
| 68 | + keywords.add(select.getModifierNode(SqlSelectKeyword.ALL)); |
| 69 | + } |
| 70 | + |
| 71 | + // Inject the TOP <literal> nodes. |
| 72 | + keywords.add(SqlSelectExtraKeyword.TOP.symbol(SqlParserPos.ZERO)); |
| 73 | + keywords.add(select.getFetch()); |
| 74 | + |
| 75 | + // Unparse a version of this select with TOP injected and the FETCH removed. |
| 76 | + final SqlSelect modifiedSelect = SqlSelectOperator.INSTANCE.createCall( |
| 77 | + keywords, |
| 78 | + select.getSelectList(), |
| 79 | + select.getFrom(), |
| 80 | + select.getWhere(), |
| 81 | + select.getGroup(), |
| 82 | + select.getHaving(), |
| 83 | + select.getWindowList(), |
| 84 | + select.getOrderList(), |
| 85 | + null, |
| 86 | + null, |
| 87 | + SqlParserPos.ZERO); |
| 88 | + |
| 89 | + super.unparseCall(writer, modifiedSelect, leftPrec, rightPrec); |
| 90 | + } else { |
| 91 | + super.unparseCall(writer, call, leftPrec, rightPrec); |
| 92 | + } |
| 93 | + } else { |
| 94 | + // Fall through to the base class implementation. |
| 95 | + super.unparseCall(writer, call, leftPrec, rightPrec); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// End SybaseDialect.java |
0 commit comments