Search in sources :

Example 1 with SqlLibrary

use of org.apache.calcite.sql.fun.SqlLibrary in project calcite by apache.

the class StandardConvertletTable method convertSubstring.

/**
 * Converts a SUBSTRING expression.
 *
 * <p>Called automatically via reflection.
 */
public RexNode convertSubstring(SqlRexContext cx, SqlSubstringFunction op, SqlCall call) {
    final SqlLibrary library = cx.getValidator().config().conformance().semantics();
    final SqlBasicCall basicCall = (SqlBasicCall) call;
    switch(library) {
        case BIG_QUERY:
            return toRex(cx, basicCall, SqlLibraryOperators.SUBSTR_BIG_QUERY);
        case MYSQL:
            return toRex(cx, basicCall, SqlLibraryOperators.SUBSTR_MYSQL);
        case ORACLE:
            return toRex(cx, basicCall, SqlLibraryOperators.SUBSTR_ORACLE);
        case POSTGRESQL:
        default:
            return convertFunction(cx, op, call);
    }
}
Also used : SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlLibrary(org.apache.calcite.sql.fun.SqlLibrary)

Example 2 with SqlLibrary

use of org.apache.calcite.sql.fun.SqlLibrary in project calcite by apache.

the class DocumentationTest method testAllFunctionsAreDocumented.

/**
 * Tests that every function in {@link SqlStdOperatorTable} is documented in
 * reference.md.
 */
@Test
void testAllFunctionsAreDocumented() throws IOException {
    final FileFixture f = new FileFixture();
    final Map<String, PatternOp> map = new TreeMap<>();
    addOperators(map, "", SqlStdOperatorTable.instance().getOperatorList());
    for (SqlLibrary library : SqlLibrary.values()) {
        switch(library) {
            case STANDARD:
            case SPATIAL:
                continue;
        }
        addOperators(map, "\\| [^|]*" + library.abbrev + "[^|]* ", SqlLibraryOperatorTableFactory.INSTANCE.getOperatorTable(EnumSet.of(library)).getOperatorList());
    }
    final Set<String> regexSeen = new HashSet<>();
    try (LineNumberReader r = new LineNumberReader(Util.reader(f.inFile))) {
        for (; ; ) {
            final String line = r.readLine();
            if (line == null) {
                break;
            }
            for (Map.Entry<String, PatternOp> entry : map.entrySet()) {
                if (entry.getValue().pattern.matcher(line).matches()) {
                    // function is documented
                    regexSeen.add(entry.getKey());
                }
            }
        }
    }
    final Set<String> regexNotSeen = new TreeSet<>(map.keySet());
    regexNotSeen.removeAll(regexSeen);
    assertThat("some functions are not documented: " + map.entrySet().stream().filter(e -> regexNotSeen.contains(e.getKey())).map(e -> e.getValue().opName + "(" + e.getKey() + ")").collect(Collectors.joining(", ")), regexNotSeen.isEmpty(), is(true));
}
Also used : SqlAbstractParserImpl(org.apache.calcite.sql.parser.SqlAbstractParserImpl) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.fail(org.junit.jupiter.api.Assertions.fail) DiffTestCase(org.apache.calcite.test.DiffTestCase) TreeSet(java.util.TreeSet) SqlParserTest(org.apache.calcite.sql.parser.SqlParserTest) SqlOverlapsOperator(org.apache.calcite.sql.fun.SqlOverlapsOperator) HashSet(java.util.HashSet) Map(java.util.Map) SqlLibrary(org.apache.calcite.sql.fun.SqlLibrary) SqlLibraryOperatorTableFactory(org.apache.calcite.sql.fun.SqlLibraryOperatorTableFactory) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SqlOperator(org.apache.calcite.sql.SqlOperator) EnumSet(java.util.EnumSet) PrintWriter(java.io.PrintWriter) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader) Sources(org.apache.calcite.util.Sources) Collectors(java.util.stream.Collectors) File(java.io.File) Test(org.junit.jupiter.api.Test) List(java.util.List) SqlFunction(org.apache.calcite.sql.SqlFunction) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) TreeMap(java.util.TreeMap) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) Util(org.apache.calcite.util.Util) SqlSpecialOperator(org.apache.calcite.sql.SqlSpecialOperator) SqlLibrary(org.apache.calcite.sql.fun.SqlLibrary) TreeMap(java.util.TreeMap) LineNumberReader(java.io.LineNumberReader) TreeSet(java.util.TreeSet) Map(java.util.Map) TreeMap(java.util.TreeMap) HashSet(java.util.HashSet) SqlParserTest(org.apache.calcite.sql.parser.SqlParserTest) Test(org.junit.jupiter.api.Test)

Example 3 with SqlLibrary

use of org.apache.calcite.sql.fun.SqlLibrary in project calcite by apache.

the class CalciteConnectionConfigImpl method fun.

@Override
@PolyNull
public <T> T fun(Class<T> operatorTableClass, @PolyNull T defaultOperatorTable) {
    final String fun = CalciteConnectionProperty.FUN.wrap(properties).getString();
    if (fun == null || fun.equals("") || fun.equals("standard")) {
        return defaultOperatorTable;
    }
    final List<SqlLibrary> libraryList = SqlLibrary.parse(fun);
    final SqlOperatorTable operatorTable = SqlLibraryOperatorTableFactory.INSTANCE.getOperatorTable(ConsList.of(SqlLibrary.STANDARD, libraryList));
    return operatorTableClass.cast(operatorTable);
}
Also used : SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) SqlLibrary(org.apache.calcite.sql.fun.SqlLibrary) PolyNull(org.checkerframework.checker.nullness.qual.PolyNull)

Aggregations

SqlLibrary (org.apache.calcite.sql.fun.SqlLibrary)3 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 LineNumberReader (java.io.LineNumberReader)1 PrintWriter (java.io.PrintWriter)1 EnumSet (java.util.EnumSet)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)1 SqlFunction (org.apache.calcite.sql.SqlFunction)1 SqlOperator (org.apache.calcite.sql.SqlOperator)1 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)1