Esempio n. 1
0
 def setUp(self):
     self.data = [[1, 2, 3, None, "m"], [2, 3, 1, 4, "f"], [None, None, None, None, None], [7, None, 3, None, "f"]]
     self.conn, self.table_name = self.create_sql_table(self.data)
     table = SqlTable(self.conn, self.table_name, inspect_values=True)
     variables = table.domain.variables
     new_table = table.copy()
     new_table.domain = domain.Domain(variables[:-1], variables[-1:])
     self.table = new_table
Esempio n. 2
0
    def test_discrete_varchar(self):
        table = np.array(['M', 'F', 'M', 'F', 'M', 'F']).reshape(-1, 1)
        conn, table_name = self.create_sql_table(table, ['varchar(1)'])

        sql_table = SqlTable(conn, table_name, inspect_values=False)
        self.assertFirstMetaIsInstance(sql_table, StringVariable)

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertFirstAttrIsInstance(sql_table, DiscreteVariable)
Esempio n. 3
0
    def test_boolean(self):
        table = np.array(['F', 'T', 0, 1, 'False', 'True']).reshape(-1, 1)
        conn, table_name = self.create_sql_table(table, ['boolean'])

        sql_table = SqlTable(conn, table_name, inspect_values=False)
        self.assertFirstAttrIsInstance(sql_table, DiscreteVariable)

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertFirstAttrIsInstance(sql_table, DiscreteVariable)
Esempio n. 4
0
    def test_discrete_smallint(self):
        table = np.arange(6).reshape((-1, 1))
        conn, table_name = self.create_sql_table(table, ['smallint'])

        sql_table = SqlTable(conn, table_name, inspect_values=False)
        self.assertFirstAttrIsInstance(sql_table, ContinuousVariable)

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertFirstAttrIsInstance(sql_table, DiscreteVariable)
Esempio n. 5
0
    def test_double_precision(self):
        table = np.arange(25).reshape((-1, 1))
        conn, table_name = self.create_sql_table(table, ['double precision'])

        sql_table = SqlTable(conn, table_name, inspect_values=False)
        self.assertFirstAttrIsInstance(sql_table, ContinuousVariable)

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertFirstAttrIsInstance(sql_table, ContinuousVariable)
Esempio n. 6
0
    def test_meta_varchar(self):
        table = np.array(list('ABCDEFGHIJKLMNOPQRSTUVW')).reshape(-1, 1)
        conn, table_name = self.create_sql_table(table, ['varchar(1)'])

        sql_table = SqlTable(conn, table_name, inspect_values=False)
        self.assertFirstMetaIsInstance(sql_table, StringVariable)

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertFirstMetaIsInstance(sql_table, StringVariable)
Esempio n. 7
0
    def test_date(self):
        table = np.array(['2014-04-12', '2014-04-13', '2014-04-14',
                          '2014-04-15', '2014-04-16']).reshape(-1, 1)
        conn, table_name = self.create_sql_table(table, ['date'])

        sql_table = SqlTable(conn, table_name, inspect_values=False)
        self.assertFirstMetaIsInstance(sql_table, StringVariable)

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertFirstMetaIsInstance(sql_table, StringVariable)
Esempio n. 8
0
    def test_contingencies(self):
        iris = SqlTable(self.conn, self.iris, inspect_values=True)
        iris.domain = Domain(iris.domain[:2] + (EqualWidth()(iris, iris.domain['sepal width']),),
                             iris.domain['iris'])

        conts = get_contingencies(iris)
        self.assertEqual(len(conts), 3)
        self.assertIsInstance(conts[0], Continuous)
        self.assertIsInstance(conts[1], Continuous)
        self.assertIsInstance(conts[2], Discrete)
Esempio n. 9
0
    def test_bigserial(self):
        table = np.arange(25).reshape((-1, 1))
        conn, table_name = self.create_sql_table(table, ['bigserial'])

        sql_table = SqlTable(conn, table_name, inspect_values=False)
        self.assertFirstAttrIsInstance(sql_table, ContinuousVariable)

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertFirstAttrIsInstance(sql_table, ContinuousVariable)
        self.drop_sql_table(table_name)
Esempio n. 10
0
    def test_contingencies(self):
        iris = SqlTable(self.conn, self.iris, inspect_values=True)
        iris.domain = Domain(iris.domain[:2] + (EqualWidth()(iris, iris.domain['sepal width']),),
                             iris.domain['iris'])

        conts = get_contingencies(iris)
        self.assertEqual(len(conts), 3)
        self.assertIsInstance(conts[0], Continuous)
        self.assertIsInstance(conts[1], Continuous)
        self.assertIsInstance(conts[2], Discrete)
Esempio n. 11
0
 def test_type_hints(self):
     table = SqlTable(self.conn, self.iris, inspect_values=True)
     self.assertEqual(len(table.domain), 5)
     self.assertEqual(len(table.domain.metas), 0)
     table = SqlTable(self.conn,
                      self.iris,
                      inspect_values=True,
                      type_hints=Domain([], [],
                                        metas=[StringVariable("iris")]))
     self.assertEqual(len(table.domain), 4)
     self.assertEqual(len(table.domain.metas), 1)
Esempio n. 12
0
    def test_time_timetz(self):
        table = np.array([
            '17:39:51+0200', '11:51:48.46+01', '05:20:21.4921',
            '21:47:06-0600', '04:47:35.8+0330'
        ]).reshape(-1, 1)
        conn, table_name = self.create_sql_table(table, ['timetz'])

        sql_table = SqlTable(conn, table_name, inspect_values=False)
        self.assertFirstAttrIsInstance(sql_table, TimeVariable)

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertFirstAttrIsInstance(sql_table, TimeVariable)
Esempio n. 13
0
    def test_time_date(self):
        table = np.array([
            "2014-04-12", "2014-04-13", "2014-04-14", "2014-04-15",
            "2014-04-16"
        ]).reshape(-1, 1)
        conn, table_name = self.create_sql_table(table, ["date"])

        sql_table = SqlTable(conn, table_name, inspect_values=False)
        self.assertFirstAttrIsInstance(sql_table, TimeVariable)

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertFirstAttrIsInstance(sql_table, TimeVariable)
Esempio n. 14
0
    def test_time_time(self):
        table = np.array([
            "17:39:51", "11:51:48.46", "05:20:21.492149", "21:47:06",
            "04:47:35.8"
        ]).reshape(-1, 1)
        conn, table_name = self.create_sql_table(table, ["time"])

        sql_table = SqlTable(conn, table_name, inspect_values=False)
        self.assertFirstAttrIsInstance(sql_table, TimeVariable)

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertFirstAttrIsInstance(sql_table, TimeVariable)
Esempio n. 15
0
    def test_time_timestamptz(self):
        table = np.array([
            '2014-07-15 17:39:51.348149+0200', '2008-10-05 11:51:48.468149+02',
            '2008-11-03 05:20:21.492149+01', '2015-01-02 21:47:06.228149+0100',
            '2016-04-16 04:47:35.892149+0330'
        ]).reshape(-1, 1)
        conn, table_name = self.create_sql_table(table, ['timestamptz'])

        sql_table = SqlTable(conn, table_name, inspect_values=False)
        self.assertFirstAttrIsInstance(sql_table, TimeVariable)

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertFirstAttrIsInstance(sql_table, TimeVariable)
Esempio n. 16
0
 def setUp(self):
     self.data = [
         [1, 2, 3, None, 'm'],
         [2, 3, 1, 4, 'f'],
         [None, None, None, None, None],
         [7, None, 3, None, 'f'],
     ]
     self.table_uri = self.create_sql_table(self.data)
     table = SqlTable(self.table_uri, guess_values=True)
     variables = table.domain.variables
     new_table = table.copy()
     new_table.domain = domain.Domain(variables[:-1], variables[-1:])
     self.table = new_table
Esempio n. 17
0
    def test_query_subset_of_attributes(self):
        table = SqlTable(self.conn, self.iris)
        attributes = [
            self._mock_attribute("sepal length"),
            self._mock_attribute("sepal width"),
            self._mock_attribute("double width", '2 * "sepal width"')
        ]
        results = list(table._query(attributes))

        self.assertSequenceEqual(results[:5],
                                 [(5.1, 3.5, 7.0), (4.9, 3.0, 6.0),
                                  (4.7, 3.2, 6.4), (4.6, 3.1, 6.2),
                                  (5.0, 3.6, 7.2)])
Esempio n. 18
0
 def setUpDB(self):
     self.data = [
         [1, 2, 3, None, 'm'],
         [2, 3, 1, 4, 'f'],
         [None, None, None, None, None],
         [7, None, 3, None, 'f'],
     ]
     self.conn, self.table_name = self.create_sql_table(self.data)
     table = SqlTable(self.conn, self.table_name, inspect_values=True)
     variables = table.domain.variables
     new_table = table.copy()
     new_table.domain = domain.Domain(variables[:-1], variables[-1:])
     self.table = new_table
Esempio n. 19
0
 def setUp(self):
     self.data = [
         [1, 2, 3, None, 'm'],
         [2, 3, 1, 4, 'f'],
         [None, None, None, None, None],
         [7, None, 3, None, 'f'],
     ]
     self.table_uri = self.create_sql_table(self.data)
     table = SqlTable(self.table_uri)
     variables = table.domain.variables
     new_table = table.copy()
     new_table.domain = domain.Domain(variables[:-1], variables[-1:])
     self.table = new_table
Esempio n. 20
0
    def test_XY_large(self):
        from Orange.data.sql.table import AUTO_DL_LIMIT as DLL

        mat = np.random.randint(0, 2, (DLL + 100, 3))
        conn, table_name = self.create_sql_table(mat)
        sql_table = SqlTable(
            conn,
            table_name,
            type_hints=Domain([],
                              DiscreteVariable(name="col2",
                                               values=["0", "1", "2"])),
        )
        self.assertRaises(ValueError, lambda: sql_table.X)
        self.assertRaises(ValueError, lambda: sql_table.Y)
        with self.assertRaises(ValueError):
            sql_table.download_data(DLL + 10)
        # Download partial data
        sql_table.download_data(DLL + 10, partial=True)
        assert_almost_equal(sql_table.X, mat[:DLL + 10, :2])
        assert_almost_equal(sql_table.Y.flatten()[:DLL + 10], mat[:DLL + 10,
                                                                  2])
        # Download all data
        sql_table.download_data()
        assert_almost_equal(sql_table.X, mat[:, :2])
        assert_almost_equal(sql_table.Y.flatten(), mat[:, 2])
Esempio n. 21
0
    def test_discrete_bigger_char(self):
        """Test if the discrete values are the same for bigger char fields"""
        table = np.array(['M', 'F', 'M', 'F', 'M', 'F']).reshape(-1, 1)
        conn, table_name = self.create_sql_table(table, ['char(10)'])

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertSequenceEqual(sql_table.domain[0].values, ['F', 'M'])
Esempio n. 22
0
    def test_inputs_check_sql(self):
        """Test if check_sql_input is called when data is sent to a widget."""
        d = Table()
        self.send_signal(self.widget.Inputs.data, d)
        self.assertIs(self.widget.pop_called_with(), d)

        a_table = object()
        with patch("Orange.widgets.utils.sql.Table",
                   MagicMock(return_value=a_table)) as table_mock:
            d = SqlTable(None, None, MagicMock())

            d.approx_len = MagicMock(return_value=AUTO_DL_LIMIT - 1)
            self.send_signal(self.widget.Inputs.data, d)
            table_mock.assert_called_once_with(d)
            self.assertIs(self.widget.pop_called_with(), a_table)
            table_mock.reset_mock()

            d.approx_len = MagicMock(return_value=AUTO_DL_LIMIT + 1)
            self.send_signal(self.widget.Inputs.data, d)
            table_mock.assert_not_called()
            self.assertIs(self.widget.pop_called_with(), None)
            self.assertTrue(self.widget.Error.download_sql_data.is_shown())
            table_mock.reset_mock()

            self.send_signal(self.widget.Inputs.data, None)
            table_mock.assert_not_called()
            self.assertIs(self.widget.pop_called_with(), None)
            self.assertFalse(self.widget.Error.download_sql_data.is_shown())
Esempio n. 23
0
    def sql_table_from_data(self, data, guess_values=True):
        assert Psycopg2Backend.connection_pool is not None

        table_name = self._create_sql_table(data)
        yield SqlTable(connection_params(), table_name,
                       inspect_values=guess_values)
        self.drop_sql_table(table_name)
Esempio n. 24
0
 def test_NaiveBayes(self):
     table = SqlTable(
         connection_params(),
         "iris",
         type_hints=Domain(
             [],
             DiscreteVariable("iris",
                              values=[
                                  "Iris-setosa", "Iris-virginica",
                                  "Iris-versicolor"
                              ]),
         ),
     )
     table = preprocess.Discretize(table)
     bayes = nb.NaiveBayesLearner()
     clf = bayes(table)
     # Single instance prediction
     self.assertEqual(clf(table[0]), table[0].get_class())
     # Table prediction
     pred = clf(table)
     actual = array([ins.get_class() for ins in table])
     ca = pred == actual
     ca = ca.sum() / len(ca)
     self.assertGreater(ca, 0.95)
     self.assertLess(ca, 1.0)
Esempio n. 25
0
    def test_class_var_type_hints(self):
        iris = SqlTable(self.conn,
                        self.iris,
                        type_hints=Domain([], self.IRIS_VARIABLE))

        self.assertEqual(len(iris.domain.class_vars), 1)
        self.assertEqual(iris.domain.class_vars[0].name, 'iris')
Esempio n. 26
0
    def test_metas_type_hints(self):
        iris = SqlTable(self.conn,
                        self.iris,
                        type_hints=Domain([], [], metas=[self.IRIS_VARIABLE]))

        self.assertEqual(len(iris.domain.metas), 1)
        self.assertEqual(iris.domain.metas[0].name, 'iris')
Esempio n. 27
0
    def open_table(self):
        if self.tablecombo.currentIndex() <= 0:
            return

        if self.tablecombo.currentIndex() < self.tablecombo.count() - 1:
            self.table = self.tablecombo.currentText()
        else:
            self.table = self.sqltext.toPlainText()

        table = SqlTable(dict(host=self.host,
                              port=self.port,
                              database=self.database,
                              user=self.username,
                              password=self.password),
                         self.table,
                         inspect_values=False)
        sample = False
        if table.approx_len() > LARGE_TABLE and self.guess_values:
            confirm = QMessageBox(self)
            confirm.setIcon(QMessageBox.Warning)
            confirm.setText("Attribute discovery might take "
                            "a long time on large tables.\n"
                            "Do you want to auto discover attributes?")
            confirm.addButton("Yes", QMessageBox.YesRole)
            no_button = confirm.addButton("No", QMessageBox.NoRole)
            sample_button = confirm.addButton("Yes, on a sample",
                                              QMessageBox.YesRole)
            confirm.exec()
            if confirm.clickedButton() == no_button:
                self.guess_values = False
            elif confirm.clickedButton() == sample_button:
                sample = True

        self.information(1)
        if self.guess_values:
            QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
            if sample:
                s = table.sample_time(1)
                domain = s.get_domain(guess_values=True)
                self.information(
                    1, "Domain was generated from a sample of the table.")
            else:
                domain = table.get_domain(guess_values=True)
            QApplication.restoreOverrideCursor()
            table.domain = domain

        self.send("Data", table)
Esempio n. 28
0
    def test_other(self):
        table = np.array([
            'bcd4d9c0-361e-bad4-7ceb-0d171cdec981',
            '544b7ddc-d861-0201-81c8-9f7ad0bbf531',
            'b35a10f7-7901-f313-ec16-5ad9778040a6',
            'b267c4be-4a26-60b5-e664-737a90a40e93'
        ]).reshape(-1, 1)
        conn, table_name = self.create_sql_table(table, ['uuid'])

        sql_table = SqlTable(conn, table_name, inspect_values=False)
        self.assertFirstMetaIsInstance(sql_table, StringVariable)

        sql_table = SqlTable(conn, table_name, inspect_values=True)
        self.assertFirstMetaIsInstance(sql_table, StringVariable)

        filters = filter.Values([filter.FilterString(-1, 0, 'foo')])
        self.assertEqual(len(filters(sql_table)), 0)
Esempio n. 29
0
 def test_XY_small(self):
     mat = np.random.randint(0, 2, (20, 3))
     conn, table_name = self.create_sql_table(mat)
     sql_table = SqlTable(conn, table_name,
                          type_hints=Domain([], DiscreteVariable(
                              name='col2', values=['0', '1', '2'])))
     assert_almost_equal(sql_table.X, mat[:, :2])
     assert_almost_equal(sql_table.Y.flatten(), mat[:, 2])
Esempio n. 30
0
 def execute_sql(self):
     self.sql = self.sqltext.toPlainText()
     table = SqlTable.from_sql(host=self.host,
                               database=self.database,
                               user=self.username,
                               password=self.password,
                               sql=self.sql)
     self.send("Data", table)
Esempio n. 31
0
 def setUpDB(self):
     self.data = [
         [1, 2, 3, 'a', 'm'],
         [2, None, 1, 'a', 'f'],
         [None, 3, 1, 'b', None],
         [2, 2, 3, 'b', 'f'],
     ]
     conn, self.table_name = self.create_sql_table(self.data)
     self.table = SqlTable(conn, self.table_name, inspect_values=True)
Esempio n. 32
0
    def test_distributions(self):
        iris = SqlTable(self.conn, self.iris, inspect_values=True)

        dists = get_distributions(iris)
        self.assertEqual(len(dists), 5)
        dist = dists[0]
        self.assertAlmostEqual(dist.min(), 4.3)
        self.assertAlmostEqual(dist.max(), 7.9)
        self.assertAlmostEqual(dist.mean(), 5.8, 1)
Esempio n. 33
0
 def execute_sql(self):
     self.sql = self.sqltext.toPlainText()
     table = SqlTable.from_sql(
         host=self.host,
         database=self.database,
         user=self.username,
         password=self.password,
         sql=self.sql)
     self.send("Data", table)
Esempio n. 34
0
 def setUp(self):
     self.data = [
         [1, 2, 3, "a", "m"],
         [2, None, 1, "a", "f"],
         [None, 3, 1, "b", None],
         [2, 2, 3, "b", "f"],
     ]
     self.conn, self.table_name = self.create_sql_table(self.data)
     self.table = SqlTable(self.conn, self.table_name, inspect_values=True)
Esempio n. 35
0
 def setUp(self):
     self.data = [
         [1, 2, 3, None, "m"],
         [2, 3, 1, 4, "f"],
         [None, None, None, None, None],
         [7, None, 3, None, "f"],
     ]
     conn, self.table_name = self.create_sql_table(self.data)
     self.table = SqlTable(conn, self.table_name, inspect_values=True)
Esempio n. 36
0
    def test_query_subset_of_attributes(self):
        table = SqlTable(self.conn, self.iris)
        attributes = [
            self._mock_attribute("sepal length"),
            self._mock_attribute("sepal width"),
            self._mock_attribute("double width", '2 * "sepal width"')
        ]
        results = list(table._query(
            attributes
        ))

        self.assertSequenceEqual(
            results[:5],
            [(5.1, 3.5, 7.0),
             (4.9, 3.0, 6.0),
             (4.7, 3.2, 6.4),
             (4.6, 3.1, 6.2),
             (5.0, 3.6, 7.2)]
        )
Esempio n. 37
0
    def test_recovers_connection_after_sql_error(self):
        import psycopg2

        conn, table_name = self.create_sql_table(
            np.arange(25).reshape((-1, 1)))
        sql_table = SqlTable(conn, table_name)

        try:
            broken_query = "SELECT 1/%s FROM %s" % (
                sql_table.domain.attributes[0].to_sql(), sql_table.table_name)
            with sql_table._execute_sql_query(broken_query) as cur:
                cur.fetchall()
        except psycopg2.DataError:
            pass

        working_query = "SELECT %s FROM %s" % (
            sql_table.domain.attributes[0].to_sql(), sql_table.table_name)
        with sql_table._execute_sql_query(working_query) as cur:
            cur.fetchall()
Esempio n. 38
0
 def test_XY_large(self):
     from Orange.data.sql.table import AUTO_DL_LIMIT as DLL
     mat = np.random.randint(0, 2, (DLL + 100, 3))
     conn, table_name = self.create_sql_table(mat)
     sql_table = SqlTable(conn, table_name,
                          type_hints=Domain([], DiscreteVariable(
                              name='col2', values=['0', '1', '2'])))
     self.assertRaises(ValueError, lambda: sql_table.X)
     self.assertRaises(ValueError, lambda: sql_table.Y)
     with self.assertRaises(ValueError):
         sql_table.download_data(DLL + 10)
     # Download partial data
     sql_table.download_data(DLL + 10, partial=True)
     assert_almost_equal(sql_table.X, mat[:DLL + 10, :2])
     assert_almost_equal(sql_table.Y.flatten()[:DLL + 10], mat[:DLL + 10, 2])
     # Download all data
     sql_table.download_data()
     assert_almost_equal(sql_table.X, mat[:, :2])
     assert_almost_equal(sql_table.Y.flatten(), mat[:, 2])
Esempio n. 39
0
    def test_query_subset_of_rows(self):
        table = SqlTable(self.conn, self.iris)
        all_results = list(table._query())

        results = list(table._query(rows=range(10)))
        self.assertEqual(len(results), 10)
        self.assertSequenceEqual(results, all_results[:10])

        results = list(table._query(rows=range(10)))
        self.assertEqual(len(results), 10)
        self.assertSequenceEqual(results, all_results[:10])

        results = list(table._query(rows=slice(None, 10)))
        self.assertEqual(len(results), 10)
        self.assertSequenceEqual(results, all_results[:10])

        results = list(table._query(rows=slice(10, None)))
        self.assertEqual(len(results), 140)
        self.assertSequenceEqual(results, all_results[10:])
Esempio n. 40
0
def connection_params():
    return SqlTable.parse_uri(get_dburi())
Esempio n. 41
0
    def get_table(self):
        curIdx = self.tablecombo.currentIndex()
        if curIdx <= 0:
            if self.database_desc:
                self.database_desc["Table"] = "(None)"
            self.data_desc_table = None
            return

        if self.tablecombo.itemText(curIdx) != "Custom SQL":
            self.table = self.tables[self.tablecombo.currentIndex()]
            self.database_desc["Table"] = self.table
            if "Query" in self.database_desc:
                del self.database_desc["Query"]
            what = self.table
        else:
            what = self.sql = self.sqltext.toPlainText()
            self.table = "Custom SQL"
            if self.materialize:
                import psycopg2  # pylint: disable=import-error
                if not self.materialize_table_name:
                    self.Error.connection(
                        "Specify a table name to materialize the query")
                    return
                try:
                    with self.backend.execute_sql_query("DROP TABLE IF EXISTS " +
                                                        self.materialize_table_name):
                        pass
                    with self.backend.execute_sql_query("CREATE TABLE " +
                                                        self.materialize_table_name +
                                                        " AS " + self.sql):
                        pass
                    with self.backend.execute_sql_query("ANALYZE " + self.materialize_table_name):
                        pass
                except (psycopg2.ProgrammingError, BackendError) as ex:
                    self.Error.connection(str(ex))
                    return

        try:
            table = SqlTable(dict(host=self.host,
                                  port=self.port,
                                  database=self.database,
                                  user=self.username,
                                  password=self.password),
                             what,
                             backend=type(self.backend),
                             inspect_values=False)
        except BackendError as ex:
            self.Error.connection(str(ex))
            return

        self.Error.connection.clear()

        sample = False

        if table.approx_len() > LARGE_TABLE and self.guess_values:
            confirm = QMessageBox(self)
            confirm.setIcon(QMessageBox.Warning)
            confirm.setText("Attribute discovery might take "
                            "a long time on large tables.\n"
                            "Do you want to auto discover attributes?")
            confirm.addButton("Yes", QMessageBox.YesRole)
            no_button = confirm.addButton("No", QMessageBox.NoRole)
            if is_postgres(self.backend):
                sample_button = confirm.addButton("Yes, on a sample",
                                                  QMessageBox.YesRole)
            confirm.exec()
            if confirm.clickedButton() == no_button:
                self.guess_values = False
            elif is_postgres(self.backend) and \
                    confirm.clickedButton() == sample_button:
                sample = True

        self.Information.clear()
        if self.guess_values:
            QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
            if sample:
                s = table.sample_time(1)
                domain = s.get_domain(inspect_values=True)
                self.Information.data_sampled()
            else:
                domain = table.get_domain(inspect_values=True)
            QApplication.restoreOverrideCursor()
            table.domain = domain

        if self.download:
            if table.approx_len() > AUTO_DL_LIMIT:
                if is_postgres(self.backend):
                    confirm = QMessageBox(self)
                    confirm.setIcon(QMessageBox.Warning)
                    confirm.setText("Data appears to be big. Do you really "
                                    "want to download it to local memory?")

                    if table.approx_len() <= MAX_DL_LIMIT:
                        confirm.addButton("Yes", QMessageBox.YesRole)
                    no_button = confirm.addButton("No", QMessageBox.NoRole)
                    sample_button = confirm.addButton("Yes, a sample",
                                                      QMessageBox.YesRole)
                    confirm.exec()
                    if confirm.clickedButton() == no_button:
                        return
                    elif confirm.clickedButton() == sample_button:
                        table = table.sample_percentage(
                            AUTO_DL_LIMIT / table.approx_len() * 100)
                else:
                    if table.approx_len() > MAX_DL_LIMIT:
                        QMessageBox.warning(
                            self, 'Warning', "Data is too big to download.\n")
                        return
                    else:
                        confirm = QMessageBox.question(
                            self, 'Question',
                            "Data appears to be big. Do you really "
                            "want to download it to local memory?",
                            QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
                        if confirm == QMessageBox.No:
                            return

            table.download_data(MAX_DL_LIMIT)
            table = Table(table)

        return table
Esempio n. 42
0
    def get_table(self):
        if self.tablecombo.currentIndex() <= 0:
            if self.database_desc:
                self.database_desc["Table"] = "(None)"
            self.data_desc_table = None
            return

        if self.tablecombo.currentIndex() < self.tablecombo.count() - 1:
            self.table = self.tablecombo.currentText()
            self.database_desc["Table"] = self.table
            if "Query" in self.database_desc:
                del self.database_desc["Query"]
        else:
            self.sql = self.table = self.sqltext.toPlainText()
            if self.materialize:
                if not self.materialize_table_name:
                    self.Error.connection(
                        "Specify a table name to materialize the query")
                    return
                try:
                    cur = self._connection.cursor()
                    cur.execute("DROP TABLE IF EXISTS " + self.materialize_table_name)
                    cur.execute("CREATE TABLE " + self.materialize_table_name + " AS " + self.table)
                    cur.execute("ANALYZE " + self.materialize_table_name)
                    self.table = self.materialize_table_name
                except psycopg2.ProgrammingError as ex:
                    self.Error.connection(str(ex))
                    return
                finally:
                    self._connection.commit()

        try:
            table = SqlTable(dict(host=self.host,
                                  port=self.port,
                                  database=self.database,
                                  user=self.username,
                                  password=self.password),
                             self.table,
                             inspect_values=False)
        except psycopg2.ProgrammingError as ex:
            self.Error.connection(str(ex))
            return

        self.Error.connection.clear()


        sample = False
        if table.approx_len() > LARGE_TABLE and self.guess_values:
            confirm = QMessageBox(self)
            confirm.setIcon(QMessageBox.Warning)
            confirm.setText("Attribute discovery might take "
                            "a long time on large tables.\n"
                            "Do you want to auto discover attributes?")
            confirm.addButton("Yes", QMessageBox.YesRole)
            no_button = confirm.addButton("No", QMessageBox.NoRole)
            sample_button = confirm.addButton("Yes, on a sample",
                                              QMessageBox.YesRole)
            confirm.exec()
            if confirm.clickedButton() == no_button:
                self.guess_values = False
            elif confirm.clickedButton() == sample_button:
                sample = True

        self.Information.clear()
        if self.guess_values:
            QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
            if sample:
                s = table.sample_time(1)
                domain = s.get_domain(guess_values=True)
                self.Information.data_sampled()
            else:
                domain = table.get_domain(guess_values=True)
            QApplication.restoreOverrideCursor()
            table.domain = domain

        if self.download:
            if table.approx_len() > MAX_DL_LIMIT:
                QMessageBox.warning(
                    self, 'Warning', "Data is too big to download.\n"
                    "Consider using the Data Sampler widget to download "
                    "a sample instead.")
                self.download = False
            elif table.approx_len() > AUTO_DL_LIMIT:
                confirm = QMessageBox.question(
                    self, 'Question', "Data appears to be big. Do you really "
                                      "want to download it to local memory?",
                    QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
                if confirm == QMessageBox.No:
                    self.download = False
        if self.download:
            table.download_data(MAX_DL_LIMIT)
            table = Table(table)

        return table
Esempio n. 43
0
    def open_table(self):
        if self.tablecombo.currentIndex() <= 0:
            return

        if self.tablecombo.currentIndex() < self.tablecombo.count() - 1:
            self.table = self.tablecombo.currentText()
        else:
            self.table = self.sqltext.toPlainText()

        table = SqlTable(dict(host=self.host,
                              port=self.port,
                              database=self.database,
                              user=self.username,
                              password=self.password),
                         self.table,
                         inspect_values=False)
        sample = False
        if table.approx_len() > LARGE_TABLE and self.guess_values:
            confirm = QMessageBox(self)
            confirm.setIcon(QMessageBox.Warning)
            confirm.setText("Attribute discovery might take "
                            "a long time on large tables.\n"
                            "Do you want to auto discover attributes?")
            confirm.addButton("Yes", QMessageBox.YesRole)
            no_button = confirm.addButton("No", QMessageBox.NoRole)
            sample_button = confirm.addButton("Yes, on a sample",
                                              QMessageBox.YesRole)
            confirm.exec()
            if confirm.clickedButton() == no_button:
                self.guess_values = False
            elif confirm.clickedButton() == sample_button:
                sample = True

        self.information(1)
        if self.guess_values:
            QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
            if sample:
                s = table.sample_time(1)
                domain = s.get_domain(guess_values=True)
                self.information(
                    1, "Domain was generated from a sample of the table.")
            else:
                domain = table.get_domain(guess_values=True)
            QApplication.restoreOverrideCursor()
            table.domain = domain

        if self.download:
            if table.approx_len() > MAX_DL_LIMIT:
                QMessageBox.warning(
                    self, 'Warning', "Data is too big to download.\n"
                    "Consider using the Data Sampler widget to download "
                    "a sample instead.")
                self.download = False
            elif table.approx_len() > AUTO_DL_LIMIT:
                confirm = QMessageBox.question(
                    self, 'Question', "Data appears to be big. Do you really "
                                      "want to download it to local memory?",
                    QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
                if confirm == QMessageBox.No:
                    self.download = False
        if self.download:
            table.download_data(MAX_DL_LIMIT)
            table = Table(table)

        self.send("Data", table)