Skip to content

Commit

Permalink
KE-11534 support kylin dialect
Browse files Browse the repository at this point in the history
  • Loading branch information
pfzhan committed Feb 28, 2024
1 parent 56874fc commit 276ad39
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 5 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ public static DatabaseProduct getProduct(
return DatabaseProduct.INTERBASE;
case "LUCIDDB":
return DatabaseProduct.LUCIDDB;
case "KYLIN":
return DatabaseProduct.KYLIN;
case "ORACLE":
return DatabaseProduct.ORACLE;
case "PHOENIX":
Expand Down Expand Up @@ -1323,6 +1325,7 @@ public enum DatabaseProduct {
INFORMIX("Informix", null, NullCollation.HIGH),
INGRES("Ingres", null, NullCollation.HIGH),
JETHRO("JethroData", "\"", NullCollation.LOW),
KYLIN("Apache KYLIN", "\"", NullCollation.HIGH),
LUCIDDB("LucidDB", "\"", NullCollation.HIGH),
INTERBASE("Interbase", null, NullCollation.HIGH),
PHOENIX("Phoenix", "\"", NullCollation.HIGH),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public SqlIdentifier skipLast(int n) {
SqlWriter writer,
int leftPrec,
int rightPrec) {
SqlUtil.unparseSqlIdentifierSyntax(writer, this, false);
SqlUtil.unparseSqlIdentifierSyntax(writer, this, null, false);
}

@Override public void validate(SqlValidator validator, SqlValidatorScope scope) {
Expand Down
12 changes: 8 additions & 4 deletions core/src/main/java/org/apache/calcite/sql/SqlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public static void unparseFunctionSyntax(SqlOperator operator,
if (id == null) {
writer.keyword(operator.getName());
} else {
unparseSqlIdentifierSyntax(writer, id, true);
unparseSqlIdentifierSyntax(writer, id, operator, true);
}
} else {
writer.print(operator.getName());
Expand Down Expand Up @@ -371,12 +371,16 @@ public static void unparseFunctionSyntax(SqlOperator operator,
public static void unparseSqlIdentifierSyntax(
SqlWriter writer,
SqlIdentifier identifier,
SqlOperator sqlIdentifierOp,
boolean asFunctionID) {
final boolean isUnquotedSimple = identifier.isSimple()
&& !identifier.getParserPosition().isQuoted();
final SqlOperator operator = isUnquotedSimple
? SqlValidatorUtil.lookupSqlFunctionByID(SqlStdOperatorTable.instance(), identifier, null)
: null;
SqlOperator operator = sqlIdentifierOp;
if (operator == null) {
operator = isUnquotedSimple
? SqlValidatorUtil.lookupSqlFunctionByID(SqlStdOperatorTable.instance(), identifier, null)
: null;
}
boolean unparsedAsFunc = false;
final SqlWriter.Frame frame =
writer.startList(SqlWriter.FrameTypeEnum.IDENTIFIER);
Expand Down
132 changes: 132 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/dialect/KylinSqlDialect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.sql.dialect;

import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.SqlAlienSystemTypeNameSpec;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlDataTypeSpec;
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSyntax;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.fun.SqlSubstringFunction;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.BasicSqlType;
import org.apache.calcite.util.RelToSqlConverterUtil;

import org.checkerframework.checker.nullness.qual.Nullable;

/**
* A <code>SqlDialect</code> implementation for the Apache Hive database.
*/
public class KylinSqlDialect extends SqlDialect {
public static final SqlDialect.Context DEFAULT_CONTEXT = SqlDialect.EMPTY_CONTEXT
.withDatabaseProduct(DatabaseProduct.KYLIN)
.withIdentifierQuoteString("\"");

public static final SqlDialect DEFAULT = new CalciteSqlDialect(DEFAULT_CONTEXT);

/**
* Creates a HiveSqlDialect.
*/
public KylinSqlDialect(Context context) {
super(context);
}

@Override public void unparseOffsetFetch(SqlWriter writer, @Nullable SqlNode offset,
@Nullable SqlNode fetch) {
unparseFetchUsingLimit(writer, offset, fetch);
}

@Override public void unparseCall(final SqlWriter writer, final SqlCall call,
final int leftPrec, final int rightPrec) {
switch (call.getKind()) {
case POSITION:
final SqlWriter.Frame frame = writer.startFunCall("INSTR");
writer.sep(",");
call.operand(1).unparse(writer, leftPrec, rightPrec);
writer.sep(",");
call.operand(0).unparse(writer, leftPrec, rightPrec);
if (3 == call.operandCount()) {
throw new RuntimeException("3rd operand Not Supported for Function INSTR in Hive");
}
writer.endFunCall(frame);
break;
case MOD:
SqlOperator op = SqlStdOperatorTable.PERCENT_REMAINDER;
SqlSyntax.BINARY.unparse(writer, op, call, leftPrec, rightPrec);
break;
case TRIM:
RelToSqlConverterUtil.unparseHiveTrim(writer, call, leftPrec, rightPrec);
break;
case OTHER_FUNCTION:
if (call.getOperator() instanceof SqlSubstringFunction) {
final SqlWriter.Frame funCallFrame = writer.startFunCall(call.getOperator().getName());
call.operand(0).unparse(writer, leftPrec, rightPrec);
writer.sep(",", true);
call.operand(1).unparse(writer, leftPrec, rightPrec);
if (3 == call.operandCount()) {
writer.sep(",", true);
call.operand(2).unparse(writer, leftPrec, rightPrec);
}
writer.endFunCall(funCallFrame);
} else {
super.unparseCall(writer, call, leftPrec, rightPrec);
}
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
}

@Override public boolean supportsCharSet() {
return false;
}

@Override public boolean supportsGroupByWithRollup() {
return true;
}

@Override public boolean supportsGroupByWithCube() {
return true;
}

@Override public boolean supportsApproxCountDistinct() {
return true;
}

@Override public boolean supportsNestedAggregations() {
return false;
}

@Override public @Nullable SqlNode getCastSpec(final RelDataType type) {
if (type instanceof BasicSqlType) {
switch (type.getSqlTypeName()) {
case INTEGER:
SqlAlienSystemTypeNameSpec typeNameSpec = new SqlAlienSystemTypeNameSpec(
"INT", type.getSqlTypeName(), SqlParserPos.ZERO);
return new SqlDataTypeSpec(typeNameSpec, SqlParserPos.ZERO);
default:
break;
}
}
return super.getCastSpec(type);
}
}

0 comments on commit 276ad39

Please sign in to comment.