Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/calcite #3

Merged
merged 14 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ MModelInstance trainModelInstance(

void dropModelInstance(String name) throws CatalogException;

Collection<MModelInstance> getModelInstances(String modelName) throws CatalogException;
Collection<MModelInstance> getModelInstances() throws CatalogException;

boolean modelInstanceExists(String name) throws CatalogException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,12 @@ public void dropModelInstance(String name) throws CatalogException {
}

@Override
public Collection<MModelInstance> getModelInstances(String modelName) throws CatalogException {
public Collection<MModelInstance> getModelInstances() throws CatalogException {
try {
Query query = pm.newQuery(MModelInstance.class);
query.setFilter("model.name == modelName");
query.declareParameters("String modelName");

return (List<MModelInstance>) query.execute(modelName);
return (List<MModelInstance>) query.execute();
} catch (RuntimeException e) {
throw new CatalogException("failed to get model '" + modelName + "' instances", e);
throw new CatalogException("failed to get model instances", e);
}
}

Expand All @@ -152,7 +149,9 @@ public boolean modelInstanceExists(String name) throws CatalogException {
public MModelInstance getModelInstance(String name) throws CatalogException {
try {
Query query = pm.newQuery(MModelInstance.class);
query.setFilter("name == name");
if (name != null) {
query.setFilter("name == name");
}
query.declareParameters("String name");
query.setUnique(true);

Expand Down
3 changes: 3 additions & 0 deletions traindb-common/src/main/scripts/traindb-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ fi

TRAINDB_OPTS="$JAVA_HEAP_MAX"

# uncomment if you want to attach a debugger
#TRAINDB_OPTS="$TRAINDB_OPTS -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"

lines=$("$JAVA" -version 2>&1 | tr '\r' '\n')
ver=$(echo $lines | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q')
if [[ $ver = "1."* ]]; then
Expand Down
2 changes: 1 addition & 1 deletion traindb-core/src/main/antlr4/traindb/sql/TrainDBSql.g4
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ showStmt

showTargets
: K_MODELS # ShowModels
| K_MODEL modelName K_INSTANCES # ShowModelInstances
| K_MODEL K_INSTANCES # ShowModelInstances
| K_SYNOPSES # ShowSynopses
;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed 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
*
* https://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 traindb.adapter.jdbc;

import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.plan.Convention;
import org.apache.calcite.plan.RelOptPlanner;
import org.apache.calcite.plan.RelOptRule;
import org.apache.calcite.rel.rules.CoreRules;
import org.apache.calcite.sql.SqlDialect;

/**
* Calling convention for relational operations that occur in a JDBC
* database.
*
* <p>The convention is a slight misnomer. The operations occur in whatever
* data-flow architecture the database uses internally. Nevertheless, the result
* pops out in JDBC.</p>
*
* <p>This is the only convention, thus far, that is not a singleton. Each
* instance contains a JDBC schema (and therefore a data source). If Calcite is
* working with two different databases, it would even make sense to convert
* from "JDBC#A" convention to "JDBC#B", even though we don't do it currently.
* (That would involve asking database B to open a database link to database
* A.)</p>
*
* <p>As a result, converter rules from and to this convention need to be
* instantiated, at the start of planning, for each JDBC database in play.</p>
*/
public class JdbcConvention extends Convention.Impl {
/**
* Cost of a JDBC node versus implementing an equivalent node in a "typical"
* calling convention.
*/
public static final double COST_MULTIPLIER = 0.8d;

public final SqlDialect dialect;
public final Expression expression;

public JdbcConvention(SqlDialect dialect, Expression expression,
String name) {
super("JDBC." + name, JdbcRel.class);
this.dialect = dialect;
this.expression = expression;
}

public static JdbcConvention of(SqlDialect dialect, Expression expression,
String name) {
return new JdbcConvention(dialect, expression, name);
}

@Override
public void register(RelOptPlanner planner) {
for (RelOptRule rule : JdbcRules.rules(this)) {
planner.addRule(rule);
}
planner.addRule(CoreRules.FILTER_SET_OP_TRANSPOSE);
planner.addRule(CoreRules.PROJECT_REMOVE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed 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
*
* https://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 traindb.adapter.jdbc;

import com.google.common.collect.ImmutableList;
import org.apache.calcite.adapter.java.JavaTypeFactory;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.util.Util;

/**
* State for generating a SQL statement.
*/
public class JdbcImplementor extends RelToSqlConverter {
public JdbcImplementor(SqlDialect dialect, JavaTypeFactory typeFactory) {
super(dialect);
Util.discard(typeFactory);
}

// CHECKSTYLE: IGNORE 1

/**
* @see #dispatch
*/
@SuppressWarnings("MissingSummary")
public Result visit(JdbcTableScan scan) {
return result(scan.jdbcTable.tableName(),
ImmutableList.of(Clause.FROM), scan, null);
}

public Result implement(RelNode node) {
return dispatch(node);
}
}
24 changes: 24 additions & 0 deletions traindb-core/src/main/java/traindb/adapter/jdbc/JdbcRel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed 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
*
* https://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 traindb.adapter.jdbc;

import org.apache.calcite.rel.RelNode;

/**
* Relational expression that uses JDBC calling convention.
*/
public interface JdbcRel extends RelNode {
SqlImplementor.Result implement(JdbcImplementor implementor);
}
Loading