cube2222 / octosql

OctoSQL is a query tool that allows you to join, analyse and transform data from multiple databases and file formats using SQL.
Mozilla Public License 2.0
4.75k stars 202 forks source link

"Error: couldn't materialize physical plan" with mysql (mariadb) #280

Closed aredridel closed 2 years ago

aredridel commented 2 years ago
:; octosql 'select * from magento.eav_attribute limit 10'
Usage:
  octosql <query> [flags]
  octosql [command]

Examples:
octosql "SELECT * FROM myfile.json"
octosql "SELECT * FROM mydir/myfile.csv"
octosql "SELECT * FROM plugins.plugins"

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  plugin      

Flags:
      --describe         Describe query output schema.
      --explain int      Describe query output schema.
  -h, --help             help for octosql
      --optimize         Whether OctoSQL should optimize the query. (default true)
      --output string    Output format to use. Available options are live_table, batch_table, csv, json and stream_native. (default "live_table")
      --profile string   Enable profiling of the given type: cpu, memory, trace.
  -v, --version          version for octosql

Use "octosql [command] --help" for more information about a command.

Error: couldn't materialize physical plan: couldn't materialize plugin datasource: rpc error: code = Unknown desc = couldn't materialize datasource: couldn't prepare statement 'SELECT , attribute_code, , attribute_model, , backend_model, , backend_type, , backend_table, , frontend_model, , frontend_input, , frontend_label, , frontend_class, , source_model, , default_value, , note FROM eav_attribute WHERE (TRUE)': Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' attribute_code, , attribute_model, , backend_model, , backend_type, , backen...' at line 1

Any thoughts? Looks like it's generating invalid SQL there

cube2222 commented 2 years ago

Hey @aredridel! Thanks a lot for the bug report. That looks weird and indeed like bad SQL generation.

There are a few things that would help me greatly debug this:

aredridel commented 2 years ago

Whoops! Query was in the original post but I screwed up the markdown code fence. It's there now.

octosql-explain-1700003076

The schema:

CREATE TABLE `eav_attribute` (
  `attribute_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Attribute Id',
  `entity_type_id` smallint(5) unsigned NOT NULL DEFAULT 0 COMMENT 'Entity Type Id',
  `attribute_code` varchar(255) NOT NULL COMMENT 'Attribute Code',
  `attribute_model` varchar(255) DEFAULT NULL COMMENT 'Attribute Model',
  `backend_model` varchar(255) DEFAULT NULL COMMENT 'Backend Model',
  `backend_type` varchar(8) NOT NULL DEFAULT 'static' COMMENT 'Backend Type',
  `backend_table` varchar(255) DEFAULT NULL COMMENT 'Backend Table',
  `frontend_model` varchar(255) DEFAULT NULL COMMENT 'Frontend Model',
  `frontend_input` varchar(50) DEFAULT NULL COMMENT 'Frontend Input',
  `frontend_label` varchar(255) DEFAULT NULL COMMENT 'Frontend Label',
  `frontend_class` varchar(255) DEFAULT NULL COMMENT 'Frontend Class',
  `source_model` varchar(255) DEFAULT NULL COMMENT 'Source Model',
  `is_required` smallint(5) unsigned NOT NULL DEFAULT 0 COMMENT 'Defines Is Required',
  `is_user_defined` smallint(5) unsigned NOT NULL DEFAULT 0 COMMENT 'Defines Is User Defined',
  `default_value` text DEFAULT NULL COMMENT 'Default Value',
  `is_unique` smallint(5) unsigned NOT NULL DEFAULT 0 COMMENT 'Defines Is Unique',
  `note` varchar(255) DEFAULT NULL COMMENT 'Note',
  PRIMARY KEY (`attribute_id`),
  UNIQUE KEY `UNQ_EAV_ATTRIBUTE_ENTITY_TYPE_ID_ATTRIBUTE_CODE` (`entity_type_id`,`attribute_code`),
  KEY `IDX_EAV_ATTRIBUTE_ENTITY_TYPE_ID` (`entity_type_id`),
  CONSTRAINT `FK_EAV_ATTRIBUTE_ENTITY_TYPE_ID_EAV_ENTITY_TYPE_ENTITY_TYPE_ID` FOREIGN KEY (`entity_type_id`) REFERENCES `eav_entity_type` (`entity_type_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=509 DEFAULT CHARSET=utf8mb3 COMMENT='Eav Attribute'
aredridel commented 2 years ago

It does still happen with --optimize=false

:; octosql --version 
octosql version 0.7.2
aredridel commented 2 years ago

Additionally, showing the help when an error happens is weird and confused me a bit

cube2222 commented 2 years ago

Thanks for all the details! I'll try to reproduce this as soon as I have some time.

aredridel commented 2 years ago

Awesome! Thank you for a really neat tool! I'm excited to use it.

cube2222 commented 2 years ago

I, unfortunately, wasn't able to reproduce this (the table works just fine for me).

Which version of MariaDB are you using? And which operating system?

I've also released octosql v0.7.3 and the mysql plugin 0.3.0, it includes support for smallints and verbose logging, you can install it by running octosql plugin install mysql@0.3.0

Running queries with the environment variable OCTOSQL_MYSQL_VERBOSE=1 will result in verbose logs that will be printed to ~/.octosql/logs.txt. It'd be great if you could provide the contents of those logs for your query.

cube2222 commented 2 years ago

Ok, I found the bug.

If I'm not mistaken, then you have multiple copies of this table in various schemas on your database instance. Looks like that broke during schema inference and looking up fields. Same bug is in the Postgres plugin actually, so thanks for that!

I've just released version 0.4.0 of the MySQL plugin which should work.

aredridel commented 2 years ago

Oh delightful! I'll test on Monday!

aredridel commented 2 years ago

And indeed I do have the same table in multiple schemas.

cube2222 commented 2 years ago

This is now also fixed in Postgres, so closing this issue. Feel free to reopen if needed.