Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Columnstore: Adding tests and error handling for non TEXT columns
  • Loading branch information
fetzerms committed May 29, 2023
commit b70f90c2dbed25db67f7114ad4c6619e651edcfb
6 changes: 6 additions & 0 deletions src/crate/client/sqlalchemy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql.base import PGCompiler
from sqlalchemy.sql import compiler
from sqlalchemy.types import String
from .types import MutableDict, _Craty, Geopoint, Geoshape
from .sa_version import SA_VERSION, SA_1_4

Expand Down Expand Up @@ -129,6 +130,11 @@ def get_column_specification(self, column, **kwargs):
colspec += " INDEX OFF"

if column.dialect_options['crate'].get('columnstore') is False:
if not isinstance(column.type, (String, )):
raise sa.exc.CompileError(
"Controlling the columnstore is only allowed for STRING columns"
)

colspec += " STORAGE WITH (columnstore = false)"

return colspec
Expand Down
37 changes: 36 additions & 1 deletion src/crate/client/sqlalchemy/tests/create_table_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# software solely pursuant to the terms of the relevant commercial agreement.

import sqlalchemy as sa

try:
from sqlalchemy.orm import declarative_base
except ImportError:
Expand All @@ -31,7 +32,6 @@
from unittest import TestCase
from unittest.mock import patch, MagicMock


fake_cursor = MagicMock(name='fake_cursor')
FakeCursor = MagicMock(name='FakeCursor', spec=Cursor)
FakeCursor.return_value = fake_cursor
Expand Down Expand Up @@ -77,6 +77,7 @@ class DummyTable(self.Base):
__tablename__ = 'dummy'
pk = sa.Column(sa.String, primary_key=True)
obj_col = sa.Column(Object)

self.Base.metadata.create_all(bind=self.engine)
fake_cursor.execute.assert_called_with(
('\nCREATE TABLE dummy (\n\tpk STRING NOT NULL, \n\tobj_col OBJECT, '
Expand All @@ -91,6 +92,7 @@ class DummyTable(self.Base):
}
pk = sa.Column(sa.String, primary_key=True)
p = sa.Column(sa.String)

self.Base.metadata.create_all(bind=self.engine)
fake_cursor.execute.assert_called_with(
('\nCREATE TABLE t (\n\t'
Expand All @@ -105,6 +107,7 @@ class DummyTable(self.Base):
__tablename__ = 't'
ts = sa.Column(sa.BigInteger, primary_key=True)
p = sa.Column(sa.BigInteger, sa.Computed("date_trunc('day', ts)"))

self.Base.metadata.create_all(bind=self.engine)
fake_cursor.execute.assert_called_with(
('\nCREATE TABLE t (\n\t'
Expand All @@ -119,6 +122,7 @@ class DummyTable(self.Base):
__tablename__ = 't'
ts = sa.Column(sa.BigInteger, primary_key=True)
p = sa.Column(sa.BigInteger, sa.Computed("date_trunc('day', ts)", persisted=False))

with self.assertRaises(sa.exc.CompileError):
self.Base.metadata.create_all(bind=self.engine)

Expand All @@ -131,6 +135,7 @@ class DummyTable(self.Base):
}
pk = sa.Column(sa.String, primary_key=True)
p = sa.Column(sa.String)

self.Base.metadata.create_all(bind=self.engine)
fake_cursor.execute.assert_called_with(
('\nCREATE TABLE t (\n\t'
Expand Down Expand Up @@ -166,6 +171,7 @@ class DummyTable(self.Base):
}
pk = sa.Column(sa.String, primary_key=True)
p = sa.Column(sa.String, primary_key=True)

self.Base.metadata.create_all(bind=self.engine)
fake_cursor.execute.assert_called_with(
('\nCREATE TABLE t (\n\t'
Expand Down Expand Up @@ -207,6 +213,7 @@ def test_column_pk_nullable(self):
class DummyTable(self.Base):
__tablename__ = 't'
pk = sa.Column(sa.String, primary_key=True, nullable=True)

with self.assertRaises(sa.exc.CompileError):
self.Base.metadata.create_all(bind=self.engine)

Expand All @@ -230,5 +237,33 @@ class DummyTable(self.Base):
__tablename__ = 't'
pk = sa.Column(sa.String, primary_key=True)
a = sa.Column(Geopoint, crate_index=False)

with self.assertRaises(sa.exc.CompileError):
self.Base.metadata.create_all(bind=self.engine)

def test_text_column_without_columnstore(self):
class DummyTable(self.Base):

Check notice

Code scanning / CodeQL

Unused local variable

Variable DummyTable is not used.
Copy link
Member

@amotl amotl May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We know this admonition by CodeQL on those occasions. They can be dismissed as false positive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah... I was already wondering if there is a way to make CodeQL ignore it. I'm happy with them being dismissed tho.

Copy link
Member

@amotl amotl May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, dismissing them is the maximum we can do here. But unfortunately, the corresponding admonition items will never be collapsed. Instead, they will be displayed "expanded" here, into eternity, even if we would resolve this conversation about it.

Unfortunately, there is also no other way to mitigate warnings on such spots, or to acknowledge them upfront, for example, by placing corresponding annotations into the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi again. CodeQL admonition items will be made collapsible, it's so sweet. Thank you, @anaarmas.

-- github/codeql-action#1411 (comment)

__tablename__ = 't'
pk = sa.Column(sa.String, primary_key=True)
a = sa.Column(sa.String, crate_columnstore=False)
b = sa.Column(sa.String, crate_columnstore=True)
c = sa.Column(sa.String)

self.Base.metadata.create_all(bind=self.engine)

fake_cursor.execute.assert_called_with(
('\nCREATE TABLE t (\n\t'
'pk STRING NOT NULL, \n\t'
'a STRING STORAGE WITH (columnstore = false), \n\t'
'b STRING, \n\t'
'c STRING, \n\t'
'PRIMARY KEY (pk)\n)\n\n'), ())

def test_non_text_column_without_columnstore(self):
class DummyTable(self.Base):

Check notice

Code scanning / CodeQL

Unused local variable

Variable DummyTable is not used.
__tablename__ = 't'
pk = sa.Column(sa.String, primary_key=True)
a = sa.Column(sa.Integer, crate_columnstore=False)

with self.assertRaises(sa.exc.CompileError):
self.Base.metadata.create_all(bind=self.engine)