Skip to content

Commit

Permalink
Merge pull request #38 from Functor10/master
Browse files Browse the repository at this point in the history
Add functions control and Elasticsearch related features.
  • Loading branch information
Functor10 committed Feb 16, 2019
2 parents 14520b4 + 72536a1 commit a257be1
Show file tree
Hide file tree
Showing 51 changed files with 1,431 additions and 667 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.GROUPING;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.GROUPING_ID;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.GROUP_ID;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.IF;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.INITCAP;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.IS_A_SET;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.IS_EMPTY;
Expand All @@ -149,6 +150,7 @@
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LAG;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LAST_VALUE;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LEAD;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LENGTH;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LESS_THAN;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LESS_THAN_OR_EQUAL;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LIKE;
Expand Down Expand Up @@ -189,9 +191,12 @@
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.RAND;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.RAND_INTEGER;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.RANK;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.REGEXP_EXTRACT;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.REGEXP_REPLACE;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.REGR_COUNT;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.REINTERPRET;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.REPLACE;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.REVERSE;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.ROUND;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.ROW;
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.ROW_NUMBER;
Expand Down Expand Up @@ -235,7 +240,16 @@ public class RexImpTable {
private final Map<SqlAggFunction, Supplier<? extends WinAggImplementor>> winAggMap =
new HashMap<>();

//Updated by qsql-team
RexImpTable() {
//qsql start
defineMethod(LENGTH, BuiltInMethod.LENGTH.method, NullPolicy.STRICT);
defineMethod(REVERSE, BuiltInMethod.REVERSE.method, NullPolicy.STRICT);
defineMethod(REGEXP_EXTRACT, BuiltInMethod.REGEXP_EXTRACT.method, NullPolicy.STRICT);
defineMethod(REGEXP_REPLACE, BuiltInMethod.REGEXP_REPLACE.method, NullPolicy.STRICT);
defineMethod(IF, BuiltInMethod.IF.method, NullPolicy.ANY);
//qsql end

defineMethod(ROW, BuiltInMethod.ARRAY.method, NullPolicy.ANY);
defineMethod(UPPER, BuiltInMethod.UPPER.method, NullPolicy.STRICT);
defineMethod(LOWER, BuiltInMethod.LOWER.method, NullPolicy.STRICT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,14 @@ public Result visit(Filter e) {
}
}

//Updated by
/** @see #dispatch */
public Result visit(Project e) {
Result x = visitChild(0, e.getInput());
parseCorrelTable(e, x);
if (isStar(e.getChildExps(), e.getInput().getRowType(), e.getRowType())) {
return x;
}
// if (isStar(e.getChildExps(), e.getInput().getRowType(), e.getRowType())) {
// return x;
// }
final Builder builder =
x.builder(e, Clause.SELECT);
final List<SqlNode> selectList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class RexSqlStandardConvertletTable
extends RexSqlReflectiveConvertletTable {
//~ Constructors -----------------------------------------------------------

//Updated by qsql-team
public RexSqlStandardConvertletTable() {
super();

Expand Down Expand Up @@ -95,6 +96,16 @@ public RexSqlStandardConvertletTable() {
registerEquivOp(SqlStdOperatorTable.MINUS_DATE);
registerEquivOp(SqlStdOperatorTable.EXTRACT);

//qsql start
registerEquivOp(SqlStdOperatorTable.LENGTH);
registerEquivOp(SqlStdOperatorTable.REVERSE);
registerEquivOp(SqlStdOperatorTable.REGEXP_EXTRACT);
registerEquivOp(SqlStdOperatorTable.REGEXP_REPLACE);
registerEquivOp(SqlStdOperatorTable.IF);
registerEquivOp(SqlStdOperatorTable.LTRIM);
registerEquivOp(SqlStdOperatorTable.RTRIM);
//qsql end

registerEquivOp(SqlStdOperatorTable.SUBSTRING);
registerEquivOp(SqlStdOperatorTable.CONVERT);
registerEquivOp(SqlStdOperatorTable.TRANSLATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.calcite.runtime;

import java.util.regex.Matcher;
import org.apache.calcite.DataContext;
import org.apache.calcite.avatica.util.ByteString;
import org.apache.calcite.avatica.util.DateTimeUtils;
Expand Down Expand Up @@ -107,6 +108,33 @@ public Enumerator<Object[]> enumerator() {
private SqlFunctions() {
}

//Updated by qsql-team
//qsql-start
public static int length(String str) {
return str.length();
}

public static String reverse(String str) {
return new StringBuilder(str).reverse().toString();
}

public static String regexpExtract(String column, String regexp, int group) {
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(column);
return matcher.group(group);
}

public static String regexpReplace(String column, String regexp, String replaced) {
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(column);
return matcher.replaceAll(replaced);
}

public static Object ifOperation(Boolean condition, Object args0, Object args1) {
return condition ? args0 : args1;
}
//qsql-end

/** SQL SUBSTRING(string FROM ... FOR ...) function. */
public static String substring(String c, int s, int l) {
int lc = c.length();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.apache.calcite.sql.fun;

import java.util.List;
import org.apache.calcite.sql.SqlCallBinding;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperandCountRange;
import org.apache.calcite.sql.SqlOperatorBinding;
import org.apache.calcite.sql.type.InferTypes;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlOperandCountRanges;
import org.apache.calcite.sql.validate.SqlMonotonicity;

public class SqlIfFunction extends SqlFunction {
public SqlIfFunction() {
super("IF",
SqlKind.OTHER_FUNCTION,
ReturnTypes.ARG1_NULLABLE,
InferTypes.RETURN_TYPE,
null,
SqlFunctionCategory.STRING);
}

@Override
public boolean checkOperandTypes(
SqlCallBinding callBinding,
boolean throwOnFailure
) {
final List<SqlNode> operands = callBinding.operands();
int n = operands.size();
assert (3 == n);
//TODO add type checker
return OperandTypes.BOOLEAN.checkSingleOperandType(
callBinding, operands.get(0), 0, throwOnFailure);
}

@Override
public SqlOperandCountRange getOperandCountRange() {
return SqlOperandCountRanges.between(3, 3);
}

@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
return SqlMonotonicity.INCREASING;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.apache.calcite.sql.fun;

import java.util.List;
import org.apache.calcite.sql.SqlCallBinding;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperandCountRange;
import org.apache.calcite.sql.SqlOperatorBinding;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlOperandCountRanges;
import org.apache.calcite.sql.validate.SqlMonotonicity;

//Updated by qsql-team
public class SqlRegexpExtractFunction extends SqlFunction {
public SqlRegexpExtractFunction() {
super(
"REGEXP_EXTRACT",
SqlKind.OTHER_FUNCTION,
ReturnTypes.ARG0_NULLABLE_VARYING,
null,
null,
SqlFunctionCategory.STRING);
}

@Override
public boolean checkOperandTypes(
SqlCallBinding callBinding,
boolean throwOnFailure
) {
final List<SqlNode> operands = callBinding.operands();
int n = operands.size();
assert (3 == n);

return OperandTypes.STRING
.checkSingleOperandType(callBinding, operands.get(0), 0, throwOnFailure)
&& OperandTypes.STRING
.checkSingleOperandType(callBinding, operands.get(1), 0, throwOnFailure)
&& OperandTypes.NUMERIC
.checkSingleOperandType(callBinding, operands.get(2), 0, throwOnFailure);
}

public SqlOperandCountRange getOperandCountRange() {
return SqlOperandCountRanges.between(3, 3);
}

@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
return SqlMonotonicity.INCREASING;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.apache.calcite.sql.fun;

import java.util.List;
import org.apache.calcite.sql.SqlCallBinding;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperandCountRange;
import org.apache.calcite.sql.SqlOperatorBinding;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlOperandCountRanges;
import org.apache.calcite.sql.validate.SqlMonotonicity;

//Updated by qsql-team
public class SqlRegexpReplaceFunction extends SqlFunction {
public SqlRegexpReplaceFunction() {
super(
"REGEXP_REPLACE",
SqlKind.OTHER_FUNCTION,
ReturnTypes.ARG0_NULLABLE_VARYING,
null,
null,
SqlFunctionCategory.STRING);
}

@Override
public boolean checkOperandTypes(
SqlCallBinding callBinding,
boolean throwOnFailure
) {
final List<SqlNode> operands = callBinding.operands();
int n = operands.size();
assert (3 == n);

return OperandTypes.STRING
.checkSingleOperandType(callBinding, operands.get(0), 0, throwOnFailure)
&& OperandTypes.STRING
.checkSingleOperandType(callBinding, operands.get(1), 0, throwOnFailure)
&& OperandTypes.STRING
.checkSingleOperandType(callBinding, operands.get(2), 0, throwOnFailure);
}

@Override
public SqlOperandCountRange getOperandCountRange() {
return SqlOperandCountRanges.between(3, 3);
}

@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
return SqlMonotonicity.INCREASING;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlOperandCountRanges;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.type.SqlTypeTransforms;
import org.apache.calcite.sql.util.ReflectiveSqlOperatorTable;
import org.apache.calcite.sql.validate.SqlConformance;
import org.apache.calcite.sql.validate.SqlModality;
Expand Down Expand Up @@ -1270,6 +1271,44 @@ public SqlOperandCountRange getOperandCountRange() {
// FUNCTIONS
//-------------------------------------------------------------

//Updated by qsql-team
public static final SqlFunction LENGTH =
new SqlFunction(
"LENGTH",
SqlKind.OTHER_FUNCTION,
ReturnTypes.INTEGER_NULLABLE,
null,
OperandTypes.CHARACTER,
SqlFunctionCategory.NUMERIC);

public static final SqlFunction REVERSE =
new SqlFunction(
"REVERSE",
SqlKind.OTHER_FUNCTION,
ReturnTypes.ARG0_NULLABLE_VARYING,
null,
OperandTypes.CHARACTER,
SqlFunctionCategory.NUMERIC);

public static final SqlFunction REGEXP_EXTRACT = new SqlRegexpExtractFunction();

public static final SqlFunction REGEXP_REPLACE = new SqlRegexpReplaceFunction();

public static final SqlFunction IF = new SqlIfFunction();

/** The "LTRIM(string)" function. */
public static final SqlFunction LTRIM =
new SqlFunction("LTRIM", SqlKind.LTRIM,
ReturnTypes.cascade(ReturnTypes.ARG0, SqlTypeTransforms.TO_NULLABLE,
SqlTypeTransforms.TO_VARYING), null,
OperandTypes.STRING, SqlFunctionCategory.STRING);

/** The "RTRIM(string)" function. */
public static final SqlFunction RTRIM =
new SqlFunction("RTRIM", SqlKind.RTRIM,
ReturnTypes.cascade(ReturnTypes.ARG0, SqlTypeTransforms.TO_NULLABLE,
SqlTypeTransforms.TO_VARYING), null,
OperandTypes.STRING, SqlFunctionCategory.STRING);
/**
* The character substring function: <code>SUBSTRING(string FROM start [FOR
* length])</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ private boolean hasFractionalPart(BigDecimal bd) {
public static final SqlSingleOperandTypeChecker STRING_SAME_SAME_INTEGER =
OperandTypes.and(STRING_STRING_INTEGER, SAME_SAME_INTEGER);

//Updated by qsql-team
public static final SqlSingleOperandTypeChecker BOOLEAN_SAME_SAME =
OperandTypes.and(BOOLEAN, SAME_SAME);

public static final SqlSingleOperandTypeChecker ANY =
family(SqlTypeFamily.ANY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ public List<SqlOperator> getOperatorList() {
return ImmutableList.copyOf(operators.values());
}

//Updated by qsql-team
/** Key for looking up operators. The name is stored in upper-case because we
* store case-insensitively, even in a case-sensitive session. */
private static class Key extends Pair<String, SqlSyntax> {
Key(String name, SqlSyntax syntax) {
public static class Key extends Pair<String, SqlSyntax> {
public Key(String name, SqlSyntax syntax) {
super(name.toUpperCase(Locale.ROOT), normalize(syntax));
}

Expand All @@ -160,6 +161,11 @@ private static SqlSyntax normalize(SqlSyntax syntax) {
}
}
}

//Updated by qsql-team
public Multimap<Key, SqlOperator> getOperators() {
return operators;
}
}

// End ReflectiveSqlOperatorTable.java
10 changes: 10 additions & 0 deletions analysis/src/main/java/org/apache/calcite/util/BuiltInMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,17 @@
/**
* Built-in methods.
*/
//Updated by qsql-team
public enum BuiltInMethod {
//qsql start
LENGTH(SqlFunctions.class, "length", String.class),
REVERSE(SqlFunctions.class, "reverse", String.class),
REGEXP_EXTRACT(SqlFunctions.class, "regexpExtract", String.class, String.class, int.class),
REGEXP_REPLACE(SqlFunctions.class, "regexpReplace", String.class, String.class, String.class),
IF(SqlFunctions.class, "ifOperation", Boolean.class, Object.class, Object.class),

//qsql end

QUERYABLE_SELECT(Queryable.class, "select", FunctionExpression.class),
QUERYABLE_AS_ENUMERABLE(Queryable.class, "asEnumerable"),
QUERYABLE_TABLE_AS_QUERYABLE(QueryableTable.class, "asQueryable",
Expand Down
Loading

0 comments on commit a257be1

Please sign in to comment.