You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2020/08/24 04:12:13 UTC

[GitHub] [incubator-doris] HappenLee opened a new pull request #4430: [ODBC SCAN NODE] 3/4 Add ODBC_TABLE and ODBC_SCAN NODE in FE.

HappenLee opened a new pull request #4430:
URL: https://github.com/apache/incubator-doris/pull/4430


   we can create odbc_table use SQL like
   
   ```
   CREATE EXTERNAL TABLE `baseall_oracle` (
     `k1` decimal(9, 3) NOT NULL COMMENT "",
     `k2` char(10) NOT NULL COMMENT "",
     `k3` datetime NOT NULL COMMENT "",
     `k5` varchar(20) NOT NULL COMMENT "",
     `k6` double NOT NULL COMMENT ""
   ) ENGINE=ODBC
   PROPERTIES (
   "host" = "192.168.0.1",
   "port" = "8086",
   "user" = "happenlee",
   "password" = "doris",
   "database" = "doris",
   "table" = "baseall",
   "driver" = "Oracle 19 ODBC driver",
   "type" = "oracle"
   );
   ```
   
   Now we only support Oracle and MySQL Database and this feature default turned off by conf enable_odbc_table.
   
   #4376  issue
   ## Proposed changes
   
   Describe the big picture of your changes here to communicate to the maintainers why we should accept this pull request. If it fixes a bug or resolves a feature request, be sure to link to that issue.
   
   ## Types of changes
   
   What types of changes does your code introduce to Doris?
   _Put an `x` in the boxes that apply_
   
   - [] Bugfix (non-breaking change which fixes an issue)
   - [x] New feature (non-breaking change which adds functionality)
   - [] Breaking change (fix or feature that would cause existing functionality to not work as expected)
   - [] Documentation Update (if none of the other choices apply)
   - [] Code refactor (Modify the code structure, format the code, etc...)
   
   ## Checklist
   
   _Put an `x` in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code._
   
   - [x] I have create an issue on (Fix #ISSUE), and have described the bug/feature there in detail
   - [x] Compiling and unit tests pass locally with my changes
   - [x] I have added tests that prove my fix is effective or that my feature works
   - [x] If this change need a document change, I have updated the document
   - [x] Any dependent changes have been merged
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] morningman commented on a change in pull request #4430: [ODBC SCAN NODE] 3/4 Add ODBC_TABLE and ODBC_SCAN NODE in FE.

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #4430:
URL: https://github.com/apache/incubator-doris/pull/4430#discussion_r480748372



##########
File path: fe/fe-core/src/main/java/org/apache/doris/load/ExportJob.java
##########
@@ -284,6 +286,8 @@ private ScanNode genScanNode() throws UserException {
                 scanNode.init(analyzer);
                 ((OlapScanNode) scanNode).selectBestRollupByRollupSelector(analyzer);
                 break;
+            case ODBC:
+                scanNode = new OdbcScanNode(new PlanNodeId(0), exportTupleDesc, (OdbcTable) this.exportTable);

Review comment:
       missing break;




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] morningman merged pull request #4430: [ODBC SCAN NODE] 3/4 Add ODBC_TABLE and ODBC_SCAN NODE in FE.

Posted by GitBox <gi...@apache.org>.
morningman merged pull request #4430:
URL: https://github.com/apache/incubator-doris/pull/4430


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] kangkaisen commented on a change in pull request #4430: [ODBC SCAN NODE] 3/4 Add ODBC_TABLE and ODBC_SCAN NODE in FE.

Posted by GitBox <gi...@apache.org>.
kangkaisen commented on a change in pull request #4430:
URL: https://github.com/apache/incubator-doris/pull/4430#discussion_r476402115



##########
File path: fe/fe-core/src/main/java/org/apache/doris/catalog/OdbcTable.java
##########
@@ -0,0 +1,296 @@
+// 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.doris.catalog;
+
+import com.google.common.collect.Maps;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.common.io.Text;
+import org.apache.doris.thrift.TOdbcTable;
+import org.apache.doris.thrift.TTableDescriptor;
+import org.apache.doris.thrift.TTableType;
+import org.apache.doris.thrift.TOdbcTableType;
+
+import com.google.common.base.Strings;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.zip.Adler32;
+
+public class OdbcTable extends Table {
+    private static final Logger LOG = LogManager.getLogger(OlapTable.class);
+
+    private static final String ODBC_HOST = "host";
+    private static final String ODBC_PORT = "port";
+    private static final String ODBC_USER = "user";
+    private static final String ODBC_PASSWORD = "password";
+    private static final String ODBC_DATABASE = "database";
+    private static final String ODBC_TABLE = "table";
+    private static final String ODBC_DRIVER = "driver";
+    private static final String ODBC_TYPE = "type";
+
+    private static Map<String, TOdbcTableType> TABLE_TYPE_MAP;
+    static {
+        Map<String, TOdbcTableType> tempMap = new HashMap<>();
+        tempMap.put("oracle", TOdbcTableType.ORACLE);
+        // we will support mysql driver in the future after we slove the core problem of
+        // driver and static library
+        //tempMap.put("mysql", TOdbcTableType.MYSQL);
+        TABLE_TYPE_MAP = Collections.unmodifiableMap(tempMap);
+    }
+
+    private String host;
+    private String port;
+    private String userName;
+    private String passwd;
+    private String odbcDatabaseName;
+    private String odbcTableName;
+    private String driver;
+    private String odbcTableTypeName;
+
+    public OdbcTable() {
+        super(TableType.ODBC);
+    }
+
+    public OdbcTable(long id, String name, List<Column> schema, Map<String, String> properties)
+            throws DdlException {
+        super(id, name, TableType.ODBC, schema);
+        validate(properties);
+    }
+
+    private void validate(Map<String, String> properties) throws DdlException {
+        if (properties == null) {
+            throw new DdlException("Please set properties of odbc table, "
+                    + "they are: host, port, user, password, database and table");
+        }
+
+        // Set up
+        host = properties.get(ODBC_HOST);
+        if (Strings.isNullOrEmpty(host)) {
+            throw new DdlException("Host of Odbc table is null. "
+                    + "Please add properties('host'='xxx.xxx.xxx.xxx') when create table");
+        }
+
+        port = properties.get(ODBC_PORT);
+        if (Strings.isNullOrEmpty(port)) {
+            // Maybe null pointer or number convert
+            throw new DdlException("Port of Odbc table is null. "
+                    + "Please add properties('port'='3306') when create table");
+        } else {
+            try {
+                Integer.valueOf(port);
+            } catch (Exception e) {
+                throw new DdlException("Port of Odbc table must be a number."
+                        + "Please add properties('port'='3306') when create table");
+
+            }
+        }
+
+        userName = properties.get(ODBC_USER);
+        if (Strings.isNullOrEmpty(userName)) {
+            throw new DdlException("User of Odbc table is null. "
+                    + "Please add properties('user'='root') when create table");
+        }
+
+        passwd = properties.get(ODBC_PASSWORD);
+        if (passwd == null) {
+            throw new DdlException("Password of Odbc table is null. "
+                    + "Please add properties('password'='xxxx') when create table");
+        }
+
+        odbcDatabaseName = properties.get(ODBC_DATABASE);
+        if (Strings.isNullOrEmpty(odbcDatabaseName)) {
+            throw new DdlException("Database of Odbc table is null. "
+                    + "Please add properties('database'='xxxx') when create table");
+        }
+
+        odbcTableName = properties.get(ODBC_TABLE);
+        if (Strings.isNullOrEmpty(odbcTableName)) {
+            throw new DdlException("Database of Odbc table is null. "
+                    + "Please add properties('table'='xxxx') when create table");
+        }
+
+        driver = properties.get(ODBC_DRIVER);
+        if (Strings.isNullOrEmpty(driver)) {
+            throw new DdlException("Driver of Odbc table is null. "
+                    + "Please add properties('diver'='xxxx') when create table");
+        }
+
+        String tableType = properties.get(ODBC_TYPE);
+        if (Strings.isNullOrEmpty(tableType)) {
+            throw new DdlException("Type of Odbc table is null. "
+                    + "Please add properties('type'='xxxx') when create table");
+        } else {
+            odbcTableTypeName = tableType.toLowerCase();
+            if (!TABLE_TYPE_MAP.containsKey(odbcTableTypeName)) {
+                throw new DdlException("Invaild Odbc table type:" + tableType
+                + " Now Odbc table type only support:" + supportTableType());
+            }
+        }
+    }
+
+    public String getHost() {
+        return host;
+    }
+
+    public String getPort() {
+        return port;
+    }
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public String getPasswd() {
+        return passwd;
+    }
+
+    public String getOdbcDatabaseName() {
+        return odbcDatabaseName;
+    }
+
+    public String getOdbcTableName() {
+        return odbcTableName;
+    }
+
+    public String getOdbcDriver() {
+        return driver;
+    }
+
+    public String getOdbcTableTypeName() {
+        return odbcTableTypeName;
+    }
+
+    public TOdbcTableType getOdbcTableType() {
+        return TABLE_TYPE_MAP.get(odbcTableTypeName);
+    }
+
+    public TTableDescriptor toThrift() {
+        TOdbcTable tOdbcTable =
+                new TOdbcTable();
+
+        tOdbcTable.host = host;
+        tOdbcTable.port = port;
+        tOdbcTable.user = userName;
+        tOdbcTable.passwd = passwd;
+        tOdbcTable.db = odbcDatabaseName;
+        tOdbcTable.table = odbcTableName;
+        tOdbcTable.driver = driver;
+        tOdbcTable.type = getOdbcTableType();
+
+        TTableDescriptor tTableDescriptor = new TTableDescriptor(getId(), TTableType.ODBC_TABLE,
+                fullSchema.size(), 0, getName(), "");
+        tTableDescriptor.setOdbcTable(tOdbcTable);
+        return tTableDescriptor;
+    }
+
+    @Override
+    public int getSignature(int signatureVersion) {
+        Adler32 adler32 = new Adler32();
+        adler32.update(signatureVersion);
+        String charsetName = "UTF-8";
+
+        try {
+            // name
+            adler32.update(name.getBytes(charsetName));
+            // type
+            adler32.update(type.name().getBytes(charsetName));
+            // host
+            adler32.update(host.getBytes(charsetName));
+            // port
+            adler32.update(port.getBytes(charsetName));
+            // username
+            adler32.update(userName.getBytes(charsetName));
+            // passwd
+            adler32.update(passwd.getBytes(charsetName));
+            // odbc db
+            adler32.update(odbcDatabaseName.getBytes(charsetName));
+            // odbc table
+            adler32.update(odbcTableName.getBytes(charsetName));
+            // odbc driver
+            adler32.update(driver.getBytes(charsetName));
+            // odbc type
+            adler32.update(odbcTableTypeName.getBytes(charsetName));
+        } catch (UnsupportedEncodingException e) {
+            LOG.error("encoding error", e);
+            return -1;
+        }
+
+        return Math.abs((int) adler32.getValue());
+    }
+
+    @Override
+    public void write(DataOutput out) throws IOException {
+        super.write(out);
+
+        Map<String, String> serializeMap = Maps.newHashMap();

Review comment:
       Would better Serde by json




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] HappenLee commented on a change in pull request #4430: [ODBC SCAN NODE] 3/4 Add ODBC_TABLE and ODBC_SCAN NODE in FE.

Posted by GitBox <gi...@apache.org>.
HappenLee commented on a change in pull request #4430:
URL: https://github.com/apache/incubator-doris/pull/4430#discussion_r477104059



##########
File path: fe/fe-core/src/main/java/org/apache/doris/common/Config.java
##########
@@ -1146,6 +1146,12 @@
     @ConfField(mutable = true, masterOnly = true)
     public static boolean enable_spark_load = false;
 
+    /**
+     * enable use odbc table
+     */
+    @ConfField(mutable = true, masterOnly = true)
+    public static boolean enable_odbc_table = false;

Review comment:
       Sorry, I forgot that. I will add it




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] kangkaisen commented on a change in pull request #4430: [ODBC SCAN NODE] 3/4 Add ODBC_TABLE and ODBC_SCAN NODE in FE.

Posted by GitBox <gi...@apache.org>.
kangkaisen commented on a change in pull request #4430:
URL: https://github.com/apache/incubator-doris/pull/4430#discussion_r476403844



##########
File path: fe/fe-core/src/main/java/org/apache/doris/common/Config.java
##########
@@ -1146,6 +1146,12 @@
     @ConfField(mutable = true, masterOnly = true)
     public static boolean enable_spark_load = false;
 
+    /**
+     * enable use odbc table
+     */
+    @ConfField(mutable = true, masterOnly = true)
+    public static boolean enable_odbc_table = false;

Review comment:
       Add this config to FE config doc.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [incubator-doris] kangkaisen commented on a change in pull request #4430: [ODBC SCAN NODE] 3/4 Add ODBC_TABLE and ODBC_SCAN NODE in FE.

Posted by GitBox <gi...@apache.org>.
kangkaisen commented on a change in pull request #4430:
URL: https://github.com/apache/incubator-doris/pull/4430#discussion_r476402777



##########
File path: fe/fe-core/src/main/java/org/apache/doris/planner/OdbcScanNode.java
##########
@@ -0,0 +1,200 @@
+// 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.doris.planner;
+
+import com.google.common.base.CharMatcher;
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.ExprSubstitutionMap;
+import org.apache.doris.analysis.SlotDescriptor;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.OdbcTable;
+import org.apache.doris.common.UserException;
+import org.apache.doris.thrift.TExplainLevel;
+import org.apache.doris.thrift.TOdbcScanNode;
+import org.apache.doris.thrift.TOdbcTableType;
+import org.apache.doris.thrift.TPlanNode;
+import org.apache.doris.thrift.TPlanNodeType;
+import org.apache.doris.thrift.TScanRangeLocations;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.Lists;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Full scan of an ODBC table.
+ */
+public class OdbcScanNode extends ScanNode {
+    private static final Logger LOG = LogManager.getLogger(OdbcScanNode.class);
+
+    private static String mysqlProperName(String name) {
+        return "`" + name + "`";
+    }
+
+    private static String databaseProperName(TOdbcTableType tableType, String name) {
+        switch (tableType) {
+            case MYSQL:
+                return mysqlProperName(name);
+        }
+
+        return name;
+    }
+
+    // now we do not support push down filter with char not in ASCII
+    private static boolean isASCIIString(String filter) {
+        return CharMatcher.ascii().matchesAllOf(filter);
+    }
+
+    private final List<String> columns = new ArrayList<String>();
+    private final List<String> filters = new ArrayList<String>();
+    private String tblName;
+    private String driver;
+    private TOdbcTableType odbcType;
+
+    /**
+     * Constructs node to scan given data files of table 'tbl'.
+     */
+    public OdbcScanNode(PlanNodeId id, TupleDescriptor desc, OdbcTable tbl) {
+        super(id, desc, "SCAN ODBC");
+        driver = tbl.getOdbcDriver();
+        odbcType = tbl.getOdbcTableType();
+        tblName = databaseProperName(odbcType, tbl.getOdbcTableName());
+    }
+
+    @Override
+    protected String debugString() {
+        MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(this);
+        return helper.addValue(super.debugString()).toString();
+    }
+
+    @Override
+    public void finalize(Analyzer analyzer) throws UserException {
+        // Convert predicates to Odbc columns and filters.
+        createOdbcColumns(analyzer);
+        createOdbcFilters(analyzer);
+        computeStats(analyzer);
+    }
+
+    @Override
+    protected String getNodeExplainString(String prefix, TExplainLevel detailLevel) {
+        StringBuilder output = new StringBuilder();
+        output.append(prefix).append("TABLE: ").append(tblName).append("\n");
+        output.append(prefix).append("Query: ").append(getOdbcQueryStr()).append("\n");
+        return output.toString();
+    }
+
+    private String getOdbcQueryStr() {
+        StringBuilder sql = new StringBuilder("SELECT ");
+        sql.append(Joiner.on(", ").join(columns));
+        sql.append(" FROM ").append(tblName);
+
+        if (!filters.isEmpty()) {
+            sql.append(" WHERE (");
+            sql.append(Joiner.on(") AND (").join(filters));
+            sql.append(")");
+        }
+        return sql.toString();
+    }
+
+    private void createOdbcColumns(Analyzer analyzer) {
+        for (SlotDescriptor slot : desc.getSlots()) {
+            if (!slot.isMaterialized()) {
+                continue;
+            }
+            Column col = slot.getColumn();
+            columns.add(databaseProperName(odbcType, col.getName()));
+        }
+        // this happens when count(*)
+        if (0 == columns.size()) {
+            columns.add("*");
+        }
+    }
+
+    // We convert predicates of the form <slotref> op <constant> to Odbc filters
+    private void createOdbcFilters(Analyzer analyzer) {
+        if (conjuncts.isEmpty()) {
+            return;
+
+        }
+        List<SlotRef> slotRefs = Lists.newArrayList();
+        Expr.collectList(conjuncts, SlotRef.class, slotRefs);
+        ExprSubstitutionMap sMap = new ExprSubstitutionMap();
+        for (SlotRef slotRef : slotRefs) {
+            SlotRef tmpRef = (SlotRef) slotRef.clone();
+            tmpRef.setTblName(null);
+            tmpRef.setLabel(databaseProperName(odbcType, tmpRef.getColumnName()));
+            sMap.put(slotRef, tmpRef);
+        }
+        ArrayList<Expr> odbcConjuncts = Expr.cloneList(conjuncts, sMap);
+        for (Expr p : odbcConjuncts) {
+            String filter = p.toMySql();
+            if (isASCIIString(filter)) {
+                filters.add(filter);
+                conjuncts.remove(p);
+            }
+        }
+    }
+
+    @Override
+    protected void toThrift(TPlanNode msg) {
+        msg.node_type = TPlanNodeType.ODBC_SCAN_NODE;
+
+        TOdbcScanNode odbcScanNode = new TOdbcScanNode();
+        odbcScanNode.tuple_id = desc.getId().asInt();
+        odbcScanNode.table_name = tblName;
+        odbcScanNode.driver = driver;
+        odbcScanNode.type = odbcType;
+        odbcScanNode.columns = columns;
+        odbcScanNode.filters = filters;
+
+        msg.odbc_scan_node = odbcScanNode;
+    }
+
+    /**
+     * We query Odbc Meta to get request's data location
+     * extra result info will pass to backend ScanNode
+     */
+    @Override
+    public List<TScanRangeLocations> getScanRangeLocations(long maxScanRangeLength) {
+        return null;
+    }
+
+    @Override
+    public int getNumInstances() {
+        return 1;
+    }
+
+    @Override
+    public void computeStats(Analyzer analyzer) {
+        super.computeStats(analyzer);
+        // even if current node scan has no data,at least on backend will be assigned when the fragment actually execute
+        numNodes = numNodes <= 0 ? 1 : numNodes;
+        // this is just to avoid mysql scan node's cardinality being -1. So that we can calculate the join cost

Review comment:
       ```suggestion
           // this is just to avoid odbc scan node's cardinality being -1. So that we can calculate the join cost
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org