Skip to content

Commit

Permalink
Initial work on adding operator map
Browse files Browse the repository at this point in the history
  • Loading branch information
tanclary committed Jan 5, 2024
1 parent 3c19347 commit c3b487b
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import org.apache.calcite.avatica.util.Casing;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlLibraryOperators;
import org.apache.calcite.sql.fun.SqlOperatorMap;
import org.apache.calcite.sql.parser.SqlParserPos;

/**
Expand All @@ -44,19 +45,12 @@ public SnowflakeSqlDialect(Context context) {
final int rightPrec) {
switch (call.getKind()) {
case CHAR_LENGTH:
SqlCall lengthCall = SqlLibraryOperators.LENGTH
.createCall(SqlParserPos.ZERO, call.getOperandList());
super.unparseCall(writer, lengthCall, leftPrec, rightPrec);
break;
case ENDS_WITH:
SqlCall endsWithCall = SqlLibraryOperators.ENDSWITH
.createCall(SqlParserPos.ZERO, call.getOperandList());
super.unparseCall(writer, endsWithCall, leftPrec, rightPrec);
break;
case STARTS_WITH:
SqlCall startsWithCall = SqlLibraryOperators.STARTSWITH
.createCall(SqlParserPos.ZERO, call.getOperandList());
super.unparseCall(writer, startsWithCall, leftPrec, rightPrec);
final SqlOperator snowflakeOp = SqlOperatorMap.getMatchingOperator(call.getOperator());
assert snowflakeOp != null;
final SqlCall snowflakeCall = snowflakeOp.createCall(SqlParserPos.ZERO, call.getOperandList());
super.unparseCall(writer, snowflakeCall, leftPrec, rightPrec);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.fun;

import org.apache.calcite.sql.SqlFunction;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/** Contains alias data for parameterized tests. */
public class SqlFunctionAlias {
final SqlFunction function;
final List<SqlLibrary> libraries;

private SqlFunctionAlias(SqlFunction function, List<SqlLibrary> libraries) {
this.function = function;
this.libraries = libraries;
}

static SqlFunctionAlias of(SqlFunction function) {
Field field =
lookupField(function.getName(), SqlStdOperatorTable.class,
SqlLibraryOperators.class);
List<SqlLibrary> libraries = new ArrayList<SqlLibrary>();
LibraryOperator libraryOperator = field.getAnnotation(LibraryOperator.class);
if (libraryOperator == null) {
libraries.add(SqlLibrary.STANDARD);
} else {
libraries.addAll(Arrays.asList(libraryOperator.libraries()));
}
return new SqlFunctionAlias(function, libraries);
}

@SuppressWarnings("rawtypes")
public static Field lookupField(String name, Class... classes) {
for (Class aClass : classes) {
try {
return aClass.getField(name);
} catch (NoSuchFieldException e) {
// ignore, and try the next class
}
}
throw new AssertionError("field " + name
+ " not found in classes" + Arrays.toString(classes));
}

// Used for test naming while running tests in IDE
@Override public String toString() {
return "function=" + function
+ ", libraries=" + libraries;
}
}
48 changes: 48 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/fun/SqlOperatorMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.fun;

import org.apache.calcite.linq4j.function.*;
import org.apache.calcite.sql.SqlOperator;

import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;

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

import static org.apache.calcite.sql.fun.SqlStdOperatorTable.*;

/** Map an operator to a list of dialect-specific operators it should be unparsed as. */
public class SqlOperatorMap {
public static final BiMap<SqlOperator, SqlOperator> SQL_OPERATOR_MAP =
ImmutableBiMap.<SqlOperator, SqlOperator>builder()
.put(SqlLibraryOperators.STARTSWITH, SqlLibraryOperators.STARTS_WITH)
.put(SqlLibraryOperators.ENDSWITH, SqlLibraryOperators.ENDSWITH)
.put(SqlLibraryOperators.LENGTH, CHAR_LENGTH)
.build();


public static @Nullable SqlOperator getMatchingOperator(SqlOperator operator) {
if (SQL_OPERATOR_MAP.containsKey(operator)) {
return SQL_OPERATOR_MAP.get(operator);
} else if (SQL_OPERATOR_MAP.containsValue(operator)) {
return SQL_OPERATOR_MAP.inverse().get(operator);
} else {
return null;
}
}
}

0 comments on commit c3b487b

Please sign in to comment.