Skip to content

Commit a257be1

Browse files
authored
Merge pull request #38 from Functor10/master
Add functions control and Elasticsearch related features.
2 parents 14520b4 + 72536a1 commit a257be1

File tree

51 files changed

+1431
-667
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1431
-667
lines changed

analysis/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.GROUPING;
135135
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.GROUPING_ID;
136136
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.GROUP_ID;
137+
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.IF;
137138
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.INITCAP;
138139
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.IS_A_SET;
139140
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.IS_EMPTY;
@@ -149,6 +150,7 @@
149150
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LAG;
150151
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LAST_VALUE;
151152
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LEAD;
153+
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LENGTH;
152154
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LESS_THAN;
153155
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LESS_THAN_OR_EQUAL;
154156
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LIKE;
@@ -189,9 +191,12 @@
189191
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.RAND;
190192
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.RAND_INTEGER;
191193
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.RANK;
194+
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.REGEXP_EXTRACT;
195+
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.REGEXP_REPLACE;
192196
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.REGR_COUNT;
193197
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.REINTERPRET;
194198
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.REPLACE;
199+
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.REVERSE;
195200
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.ROUND;
196201
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.ROW;
197202
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.ROW_NUMBER;
@@ -235,7 +240,16 @@ public class RexImpTable {
235240
private final Map<SqlAggFunction, Supplier<? extends WinAggImplementor>> winAggMap =
236241
new HashMap<>();
237242

243+
//Updated by qsql-team
238244
RexImpTable() {
245+
//qsql start
246+
defineMethod(LENGTH, BuiltInMethod.LENGTH.method, NullPolicy.STRICT);
247+
defineMethod(REVERSE, BuiltInMethod.REVERSE.method, NullPolicy.STRICT);
248+
defineMethod(REGEXP_EXTRACT, BuiltInMethod.REGEXP_EXTRACT.method, NullPolicy.STRICT);
249+
defineMethod(REGEXP_REPLACE, BuiltInMethod.REGEXP_REPLACE.method, NullPolicy.STRICT);
250+
defineMethod(IF, BuiltInMethod.IF.method, NullPolicy.ANY);
251+
//qsql end
252+
239253
defineMethod(ROW, BuiltInMethod.ARRAY.method, NullPolicy.ANY);
240254
defineMethod(UPPER, BuiltInMethod.UPPER.method, NullPolicy.STRICT);
241255
defineMethod(LOWER, BuiltInMethod.LOWER.method, NullPolicy.STRICT);

analysis/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,14 @@ public Result visit(Filter e) {
172172
}
173173
}
174174

175+
//Updated by
175176
/** @see #dispatch */
176177
public Result visit(Project e) {
177178
Result x = visitChild(0, e.getInput());
178179
parseCorrelTable(e, x);
179-
if (isStar(e.getChildExps(), e.getInput().getRowType(), e.getRowType())) {
180-
return x;
181-
}
180+
// if (isStar(e.getChildExps(), e.getInput().getRowType(), e.getRowType())) {
181+
// return x;
182+
// }
182183
final Builder builder =
183184
x.builder(e, Clause.SELECT);
184185
final List<SqlNode> selectList = new ArrayList<>();

analysis/src/main/java/org/apache/calcite/rex/RexSqlStandardConvertletTable.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class RexSqlStandardConvertletTable
3939
extends RexSqlReflectiveConvertletTable {
4040
//~ Constructors -----------------------------------------------------------
4141

42+
//Updated by qsql-team
4243
public RexSqlStandardConvertletTable() {
4344
super();
4445

@@ -95,6 +96,16 @@ public RexSqlStandardConvertletTable() {
9596
registerEquivOp(SqlStdOperatorTable.MINUS_DATE);
9697
registerEquivOp(SqlStdOperatorTable.EXTRACT);
9798

99+
//qsql start
100+
registerEquivOp(SqlStdOperatorTable.LENGTH);
101+
registerEquivOp(SqlStdOperatorTable.REVERSE);
102+
registerEquivOp(SqlStdOperatorTable.REGEXP_EXTRACT);
103+
registerEquivOp(SqlStdOperatorTable.REGEXP_REPLACE);
104+
registerEquivOp(SqlStdOperatorTable.IF);
105+
registerEquivOp(SqlStdOperatorTable.LTRIM);
106+
registerEquivOp(SqlStdOperatorTable.RTRIM);
107+
//qsql end
108+
98109
registerEquivOp(SqlStdOperatorTable.SUBSTRING);
99110
registerEquivOp(SqlStdOperatorTable.CONVERT);
100111
registerEquivOp(SqlStdOperatorTable.TRANSLATE);

analysis/src/main/java/org/apache/calcite/runtime/SqlFunctions.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.calcite.runtime;
1818

19+
import java.util.regex.Matcher;
1920
import org.apache.calcite.DataContext;
2021
import org.apache.calcite.avatica.util.ByteString;
2122
import org.apache.calcite.avatica.util.DateTimeUtils;
@@ -107,6 +108,33 @@ public Enumerator<Object[]> enumerator() {
107108
private SqlFunctions() {
108109
}
109110

111+
//Updated by qsql-team
112+
//qsql-start
113+
public static int length(String str) {
114+
return str.length();
115+
}
116+
117+
public static String reverse(String str) {
118+
return new StringBuilder(str).reverse().toString();
119+
}
120+
121+
public static String regexpExtract(String column, String regexp, int group) {
122+
Pattern pattern = Pattern.compile(regexp);
123+
Matcher matcher = pattern.matcher(column);
124+
return matcher.group(group);
125+
}
126+
127+
public static String regexpReplace(String column, String regexp, String replaced) {
128+
Pattern pattern = Pattern.compile(regexp);
129+
Matcher matcher = pattern.matcher(column);
130+
return matcher.replaceAll(replaced);
131+
}
132+
133+
public static Object ifOperation(Boolean condition, Object args0, Object args1) {
134+
return condition ? args0 : args1;
135+
}
136+
//qsql-end
137+
110138
/** SQL SUBSTRING(string FROM ... FOR ...) function. */
111139
public static String substring(String c, int s, int l) {
112140
int lc = c.length();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.apache.calcite.sql.fun;
2+
3+
import java.util.List;
4+
import org.apache.calcite.sql.SqlCallBinding;
5+
import org.apache.calcite.sql.SqlFunction;
6+
import org.apache.calcite.sql.SqlFunctionCategory;
7+
import org.apache.calcite.sql.SqlKind;
8+
import org.apache.calcite.sql.SqlNode;
9+
import org.apache.calcite.sql.SqlOperandCountRange;
10+
import org.apache.calcite.sql.SqlOperatorBinding;
11+
import org.apache.calcite.sql.type.InferTypes;
12+
import org.apache.calcite.sql.type.OperandTypes;
13+
import org.apache.calcite.sql.type.ReturnTypes;
14+
import org.apache.calcite.sql.type.SqlOperandCountRanges;
15+
import org.apache.calcite.sql.validate.SqlMonotonicity;
16+
17+
public class SqlIfFunction extends SqlFunction {
18+
public SqlIfFunction() {
19+
super("IF",
20+
SqlKind.OTHER_FUNCTION,
21+
ReturnTypes.ARG1_NULLABLE,
22+
InferTypes.RETURN_TYPE,
23+
null,
24+
SqlFunctionCategory.STRING);
25+
}
26+
27+
@Override
28+
public boolean checkOperandTypes(
29+
SqlCallBinding callBinding,
30+
boolean throwOnFailure
31+
) {
32+
final List<SqlNode> operands = callBinding.operands();
33+
int n = operands.size();
34+
assert (3 == n);
35+
//TODO add type checker
36+
return OperandTypes.BOOLEAN.checkSingleOperandType(
37+
callBinding, operands.get(0), 0, throwOnFailure);
38+
}
39+
40+
@Override
41+
public SqlOperandCountRange getOperandCountRange() {
42+
return SqlOperandCountRanges.between(3, 3);
43+
}
44+
45+
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
46+
return SqlMonotonicity.INCREASING;
47+
}
48+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.apache.calcite.sql.fun;
2+
3+
import java.util.List;
4+
import org.apache.calcite.sql.SqlCallBinding;
5+
import org.apache.calcite.sql.SqlFunction;
6+
import org.apache.calcite.sql.SqlFunctionCategory;
7+
import org.apache.calcite.sql.SqlKind;
8+
import org.apache.calcite.sql.SqlNode;
9+
import org.apache.calcite.sql.SqlOperandCountRange;
10+
import org.apache.calcite.sql.SqlOperatorBinding;
11+
import org.apache.calcite.sql.type.OperandTypes;
12+
import org.apache.calcite.sql.type.ReturnTypes;
13+
import org.apache.calcite.sql.type.SqlOperandCountRanges;
14+
import org.apache.calcite.sql.validate.SqlMonotonicity;
15+
16+
//Updated by qsql-team
17+
public class SqlRegexpExtractFunction extends SqlFunction {
18+
public SqlRegexpExtractFunction() {
19+
super(
20+
"REGEXP_EXTRACT",
21+
SqlKind.OTHER_FUNCTION,
22+
ReturnTypes.ARG0_NULLABLE_VARYING,
23+
null,
24+
null,
25+
SqlFunctionCategory.STRING);
26+
}
27+
28+
@Override
29+
public boolean checkOperandTypes(
30+
SqlCallBinding callBinding,
31+
boolean throwOnFailure
32+
) {
33+
final List<SqlNode> operands = callBinding.operands();
34+
int n = operands.size();
35+
assert (3 == n);
36+
37+
return OperandTypes.STRING
38+
.checkSingleOperandType(callBinding, operands.get(0), 0, throwOnFailure)
39+
&& OperandTypes.STRING
40+
.checkSingleOperandType(callBinding, operands.get(1), 0, throwOnFailure)
41+
&& OperandTypes.NUMERIC
42+
.checkSingleOperandType(callBinding, operands.get(2), 0, throwOnFailure);
43+
}
44+
45+
public SqlOperandCountRange getOperandCountRange() {
46+
return SqlOperandCountRanges.between(3, 3);
47+
}
48+
49+
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
50+
return SqlMonotonicity.INCREASING;
51+
}
52+
53+
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.apache.calcite.sql.fun;
2+
3+
import java.util.List;
4+
import org.apache.calcite.sql.SqlCallBinding;
5+
import org.apache.calcite.sql.SqlFunction;
6+
import org.apache.calcite.sql.SqlFunctionCategory;
7+
import org.apache.calcite.sql.SqlKind;
8+
import org.apache.calcite.sql.SqlNode;
9+
import org.apache.calcite.sql.SqlOperandCountRange;
10+
import org.apache.calcite.sql.SqlOperatorBinding;
11+
import org.apache.calcite.sql.type.OperandTypes;
12+
import org.apache.calcite.sql.type.ReturnTypes;
13+
import org.apache.calcite.sql.type.SqlOperandCountRanges;
14+
import org.apache.calcite.sql.validate.SqlMonotonicity;
15+
16+
//Updated by qsql-team
17+
public class SqlRegexpReplaceFunction extends SqlFunction {
18+
public SqlRegexpReplaceFunction() {
19+
super(
20+
"REGEXP_REPLACE",
21+
SqlKind.OTHER_FUNCTION,
22+
ReturnTypes.ARG0_NULLABLE_VARYING,
23+
null,
24+
null,
25+
SqlFunctionCategory.STRING);
26+
}
27+
28+
@Override
29+
public boolean checkOperandTypes(
30+
SqlCallBinding callBinding,
31+
boolean throwOnFailure
32+
) {
33+
final List<SqlNode> operands = callBinding.operands();
34+
int n = operands.size();
35+
assert (3 == n);
36+
37+
return OperandTypes.STRING
38+
.checkSingleOperandType(callBinding, operands.get(0), 0, throwOnFailure)
39+
&& OperandTypes.STRING
40+
.checkSingleOperandType(callBinding, operands.get(1), 0, throwOnFailure)
41+
&& OperandTypes.STRING
42+
.checkSingleOperandType(callBinding, operands.get(2), 0, throwOnFailure);
43+
}
44+
45+
@Override
46+
public SqlOperandCountRange getOperandCountRange() {
47+
return SqlOperandCountRanges.between(3, 3);
48+
}
49+
50+
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
51+
return SqlMonotonicity.INCREASING;
52+
}
53+
}

analysis/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.apache.calcite.sql.type.ReturnTypes;
5353
import org.apache.calcite.sql.type.SqlOperandCountRanges;
5454
import org.apache.calcite.sql.type.SqlTypeName;
55+
import org.apache.calcite.sql.type.SqlTypeTransforms;
5556
import org.apache.calcite.sql.util.ReflectiveSqlOperatorTable;
5657
import org.apache.calcite.sql.validate.SqlConformance;
5758
import org.apache.calcite.sql.validate.SqlModality;
@@ -1270,6 +1271,44 @@ public SqlOperandCountRange getOperandCountRange() {
12701271
// FUNCTIONS
12711272
//-------------------------------------------------------------
12721273

1274+
//Updated by qsql-team
1275+
public static final SqlFunction LENGTH =
1276+
new SqlFunction(
1277+
"LENGTH",
1278+
SqlKind.OTHER_FUNCTION,
1279+
ReturnTypes.INTEGER_NULLABLE,
1280+
null,
1281+
OperandTypes.CHARACTER,
1282+
SqlFunctionCategory.NUMERIC);
1283+
1284+
public static final SqlFunction REVERSE =
1285+
new SqlFunction(
1286+
"REVERSE",
1287+
SqlKind.OTHER_FUNCTION,
1288+
ReturnTypes.ARG0_NULLABLE_VARYING,
1289+
null,
1290+
OperandTypes.CHARACTER,
1291+
SqlFunctionCategory.NUMERIC);
1292+
1293+
public static final SqlFunction REGEXP_EXTRACT = new SqlRegexpExtractFunction();
1294+
1295+
public static final SqlFunction REGEXP_REPLACE = new SqlRegexpReplaceFunction();
1296+
1297+
public static final SqlFunction IF = new SqlIfFunction();
1298+
1299+
/** The "LTRIM(string)" function. */
1300+
public static final SqlFunction LTRIM =
1301+
new SqlFunction("LTRIM", SqlKind.LTRIM,
1302+
ReturnTypes.cascade(ReturnTypes.ARG0, SqlTypeTransforms.TO_NULLABLE,
1303+
SqlTypeTransforms.TO_VARYING), null,
1304+
OperandTypes.STRING, SqlFunctionCategory.STRING);
1305+
1306+
/** The "RTRIM(string)" function. */
1307+
public static final SqlFunction RTRIM =
1308+
new SqlFunction("RTRIM", SqlKind.RTRIM,
1309+
ReturnTypes.cascade(ReturnTypes.ARG0, SqlTypeTransforms.TO_NULLABLE,
1310+
SqlTypeTransforms.TO_VARYING), null,
1311+
OperandTypes.STRING, SqlFunctionCategory.STRING);
12731312
/**
12741313
* The character substring function: <code>SUBSTRING(string FROM start [FOR
12751314
* length])</code>.

analysis/src/main/java/org/apache/calcite/sql/type/OperandTypes.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ private boolean hasFractionalPart(BigDecimal bd) {
410410
public static final SqlSingleOperandTypeChecker STRING_SAME_SAME_INTEGER =
411411
OperandTypes.and(STRING_STRING_INTEGER, SAME_SAME_INTEGER);
412412

413+
//Updated by qsql-team
414+
public static final SqlSingleOperandTypeChecker BOOLEAN_SAME_SAME =
415+
OperandTypes.and(BOOLEAN, SAME_SAME);
416+
413417
public static final SqlSingleOperandTypeChecker ANY =
414418
family(SqlTypeFamily.ANY);
415419

analysis/src/main/java/org/apache/calcite/sql/util/ReflectiveSqlOperatorTable.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ public List<SqlOperator> getOperatorList() {
142142
return ImmutableList.copyOf(operators.values());
143143
}
144144

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

@@ -160,6 +161,11 @@ private static SqlSyntax normalize(SqlSyntax syntax) {
160161
}
161162
}
162163
}
164+
165+
//Updated by qsql-team
166+
public Multimap<Key, SqlOperator> getOperators() {
167+
return operators;
168+
}
163169
}
164170

165171
// End ReflectiveSqlOperatorTable.java

0 commit comments

Comments
 (0)