forked from apache/calcite
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
122 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
core/src/main/java/org/apache/calcite/sql/fun/SqlFunctionAlias.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
core/src/main/java/org/apache/calcite/sql/fun/SqlOperatorMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |